Software Product Line Engineering

Download Report

Transcript Software Product Line Engineering

Distributed Objects in Java
Object-Oriented Software
Development Using Java
by Xiaoping Jia
(Chapter 12)
Harvey Siy
Object-Oriented Software Development Using Java by Jia
Slide 1
Objectives


Provide a very brief overview of distributed
computing using Java.
Examine different technologies supporting
distributed systems.
Harvey Siy
Object-Oriented Software Development Using Java by Jia
Slide 2
Outline




Socket-based Communication
Remote Method Invocation
Java Database Connectivity
Common Object Request Broker
Harvey Siy
Object-Oriented Software Development Using Java by Jia
Slide 3
Distributed systems




Distributed systems consists of components that
reside and execute on different hosts.
They do not physically share any storage space.
Communication exchange take place over
network connections.
The environment can be heterogenous (different
programming languages, operating systems,
processor types).
Harvey Siy
Object-Oriented Software Development Using Java by Jia
Slide 4
Socket-based communication

Sockets – the endpoints of two-way
communications between two distributed
components
•
•

Address – a unique identifier for a machine on a
network
•
•

Source and destination address
Source and destination port
IP address
Hostname
Port – a numeric identifier for a particular
connection on a given machine
Harvey Siy
Object-Oriented Software Development Using Java by Jia
Slide 5
Sockets in Java

Server socket class: ServerSocket
•
•

Wait for requests from clients.
After a request is received, a client socket is generated.
Client socket class: Socket
•
•
An endpoint for communication between two apps/applets.
Obtained by
• Contacting a server
• Generated by the server socket



Communication is handled by input/output streams.
Socket provides an input and an output stream.
Uses java.net.* package
Harvey Siy
Object-Oriented Software Development Using Java by Jia
Slide 6
Server socket example
try {
ServerSocket s = new ServerSocket(8008);
while (true) {
Socket incoming = s.accept();
// Handle a client
}
} catch (IOException e) {
// handle exception: fail to create a socket
}
Harvey Siy
Object-Oriented Software Development Using Java by Jia
Slide 7
Client socket example
try {
Socket s = new Socket(host,8008);
PrintWriter out = new PrintWriter(
new OutputStreamWriter(s.getOutputStream()));
BufferedReader in = new BufferedReader(
new InputStreamReader(s.getInputStream()));
// send and receive data …
in.close();
out.close();
s.close();
} catch (IOException e) {
// handle exception: connect failed
}
Harvey Siy
Object-Oriented Software Development Using Java by Jia
Slide 8
A Simple Echo Server
import java.io.*;
import java.net.*;
public class EchoServer {
public static void main(String[] args) {
try {
ServerSocket s = new ServerSocket(8008);
while (true) {
Socket incoming = s.accept();
BufferedReader in …
PrintWriter out …
out.println("Hello! ....");
out.println("Enter BYE to exit.");
out.flush();
Harvey Siy
Object-Oriented Software Development Using Java by Jia
Slide 9
A Simple Echo Server (cont'd)
while (true) {
String str = in.readLine();
if (str == null) {
break; // client closed connection
} else {
out.println("Echo: " + str);
out.flush();
if (str.trim().equals("BYE"))
break;
}
}
incoming.close();
}
} catch (Exception e) {}
}
}
Harvey Siy
Object-Oriented Software Development Using Java by Jia
Slide 10
A Simple Echo Client
import java.io.*;
import java.net.*;
public class EchoClient {
public static void main(String[] args) {
try {
String host;
if (args.length > 0) {
host = args[0];
} else {
host = "localhost";
}
Socket socket = new Socket(host, 8008);
Harvey Siy
Object-Oriented Software Development Using Java by Jia
Slide 11
A Simple Echo Client (con’t)
BufferedReader in …
PrintWriter out …
// send data to the server
for (int i = 1; i <= 10; i++) {
System.out.println("Sending: line " + i);
out.println("line " + i);
out.flush();
}
out.println("BYE");
out.flush();
Harvey Siy
Object-Oriented Software Development Using Java by Jia
Slide 12
A Simple Echo Client (con’t)
// receive data from the server
while (true) {
String str = in.readLine();
if (str == null) {
break;
} else {
System.out.println(str);
}
}
} catch (Exception e) {}
}
}
Harvey Siy
Object-Oriented Software Development Using Java by Jia
Slide 13
Outline




Socket-based Communication
Remote Method Invocation
Java Database Connectivity
Common Object Request Broker
Harvey Siy
Object-Oriented Software Development Using Java by Jia
Slide 14
Remote Method Invocation (RMI)


RMI simplifies the programming model of
distributed applications – no explicit connections
Uses java.rmi.* package.
Client
Client wants to
call server.m();
actual call is
stub.m()
Server
Skeleton calls
3
server.m()
6 Stub returns
result to Client
1
Stub
JVM
Stub serializes the args and
sends request to Skeleton
2
Server executes
4 m() and returns to
Skeleton
Skeleton
JVM
5
Skeleton sends results back to Stub
Harvey Siy
Object-Oriented Software Development Using Java by Jia
Slide 15
Using RMI

Define an interface for the remote object, which
becomes the contract interface between server
and clients.
public interface Contract extends remote {
public void aService(…) throws RemoteException;
// other services …
}
Harvey Siy
Object-Oriented Software Development Using Java by Jia
Slide 16
Step 2


Define a service implementation class that
implements the Contract interface.
The implementation class must extend
UnicastRemoteObject.
public class ServiceProvider extends
UnicastRemoteObject, implements Contract {
public void aService(…) throws RemoteException {
…
}
// other services
}
Harvey Siy
Object-Oriented Software Development Using Java by Jia
Slide 17
Step 3

Create an instance of the server and register that
server to an RMI registry.
Contract server = new ServiceProvider(…);
Naming.rebind(name, server);
Harvey Siy
Object-Oriented Software Development Using Java by Jia
Slide 18
Step 4


Generate stub and skeleton classes using RMI
compiler.
Generates:
• ServiceProvider_Skel
• ServiceProvider_Stub

Stub class also implements Contract.
Harvey Siy
Object-Oriented Software Development Using Java by Jia
Slide 19
Step 5

Develop a client that uses the service.
Remote remoteObj = Naming.lookup(name);
Contract serverObj = (Contract) remoteObj;
// …
serverObj.aService(…); // remote method invocation
// …


remoteObj is actually an instance of the stub
class
name is of the form rmi://host:port/name
Harvey Siy
Object-Oriented Software Development Using Java by Jia
Slide 20
Class diagram
Proxy
pattern
Harvey Siy
Object-Oriented Software Development Using Java by Jia
Slide 21
Proxy pattern
Subject
Request()
Proxy
Request()


realSubject
RealSubject
Request()
Interface inheritance is used to specify the
interface shared by Proxy and RealSubject.
Delegation is used to catch and forward any
accesses to the RealSubject (if desired)
Harvey Siy
Object-Oriented Software Development Using Java by Jia
Slide 22
RMI server example
// Contract interface
public interface Hello extends java.rmi.Remote {
String sayHello() throws RemoteException;
}
// Server implementation
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
public class HelloImpl extends UnicastRemoteObject
implements Hello {
private String name;
public HelloImpl(String s) throws RemoteException {
super();
name = s;
}
public String sayHello() throws RemoteException {
return “Hello world”;
}
Harvey Siy
Object-Oriented Software Development Using Java by Jia
Slide 23
RMI server example (cont’d)
}
// main creates an instance of HelloImpl and binds it to
// a name “HelloServer” in the RMI registry
public static void main(String args[]) {
System.setSecurityManager(new RMISecurityManager());
try {
HelloImpl obj = new HelloImpl(“HelloServer”);
Naming.rebind(“HelloServer”, obj);
} catch (Exception e) {
e.printStackTrace();
}
}
Harvey Siy
Object-Oriented Software Development Using Java by Jia
Slide 24
RMI client example
import java.awt.*;
import java.rmi.*;
public class HelloApplet extends Applet {
String message=“”;
public void init() {
try {
Hello obj =
(Hello) Naming.lookup(“rmi://localhost/HelloServer”);
message = obj.sayHello();
} catch (Exception e) {
e.printStackTrace();
}
}
public void paint(Graphics g) {
g.drawString(message, 25, 50);
}
}
Harvey Siy
Object-Oriented Software Development Using Java by Jia
Slide 25
Outline




Socket-based Communication
Remote Method Invocation
Java Database Connectivity
Common Object Request Broker
Harvey Siy
Object-Oriented Software Development Using Java by Jia
Slide 26
Java Database Connectivity (JDBC)



JDBC allows Java applications to interface with
databases.
Uses java.sql.* package
Four types:
1. JDBC-ODBC bridge
•
Provides JDBC access via existing ODBC drivers
2. Native-API
•
Converts JDBC calls to DBMS calls
3. JDBC-Net protocol
•
Translates JDBC calls into DBMS independent protocol
4. Native protocol
•
Harvey Siy
Converts JDBC calls to the protocol used by DBMS directly
Object-Oriented Software Development Using Java by Jia
Slide 27
Establishing a connection with JDBC

Load the JDBC class driver
Class.forName(JDBCDrivername);
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);

Connect to database
DriverManager.getConnection(url, “ID”, “password”);
• url:
• “jdbc:subprotocol:subname” (local)
• “jdbc:subprotocol://host[:port]/subname” (remote)
•
•
•
Harvey Siy
subprotocol – specifies driver connection mechanism
subname – name of database
Returns Connection object
Object-Oriented Software Development Using Java by Jia
Slide 28
Some methods

Connection class:
•
•
•

createStatement() – new Statement object
commit() – commits database changes
close() – releases the database and all resources
associated with this Connection
Statement class:
•
•
•
Harvey Siy
executeQuery(sqlstring) – returns resultSet object
executeUpdate(sqlstring) – INSERT, UPDATE,
DELETE SQL statements
close() – releases all resources associated with this
Statement
Object-Oriented Software Development Using Java by Jia
Slide 29
Outline




Socket-based Communication
Remote Method Invocation
Java Database Connectivity
Common Object Request Broker
Harvey Siy
Object-Oriented Software Development Using Java by Jia
Slide 30
Common Object Request Broker
Architecture (CORBA)


CORBA is an open, distributed object-computing
infrastructure
Enables Java applications to talk to components
written in other languages
Client
1
Server
6
Stub
3
2
ORB
4
Skeleton
ORB
5
Harvey Siy
Object-Oriented Software Development Using Java by Jia
Slide 31
Interface Definition Language


IDL is a programming-language-independent
language for specifying the contract interface of a
CORBA server
IDL compiler
•
•
Harvey Siy
Translates interfaces written in IDL to various
programming languages
Generates stubs and skeletons
Object-Oriented Software Development Using Java by Jia
Slide 32
Object Request Broker


ORB defines the mechanism and interfaces that
enable objects to make requests and receive
responses in a distributed environment
Infrastructure for platform-independent
communication
Harvey Siy
Object-Oriented Software Development Using Java by Jia
Slide 33
Using CORBA




Client requests service
ORB locates a server that can accept the service
ORB sends arguments, invokes the method on
the server returns results to client
Client interacts with stub and does not need to
know where the actual server object is located
Harvey Siy
Object-Oriented Software Development Using Java by Jia
Slide 34
Summary




Presented an overview of distributed computing
using Java.
Java programs communicate with other
machines through sockets or RMI.
JDBC drivers enable Java programs to
communicate with databases.
CORBA enables Java programs to communicate
with other components written in other
languages.
Harvey Siy
Object-Oriented Software Development Using Java by Jia
Slide 35