java-rmi lab

22
Java-RMI Lab

Upload: vandan

Post on 04-Jan-2017

248 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Java-RMI Lab

Java-RMI Lab

Page 2: Java-RMI Lab

Outline

• Let first builds a simple home-made framework

• This is useful to understand the main issues

• We see later how java-rmi works and how it solves the same issues

Page 3: Java-RMI Lab

Generic architecture

object A object BskeletonRequestproxy for B

Reply

CommunicationRemote Remote referenceCommunication

modulemodulereference module module

for B’s class& dispatcher

remoteclient server

servant

Local proxy: Remote methods appear as local Remote Reference Module: local-remote object mapDispatcher: Dispatch the request to a SkeletonSkeleton: Invoke the method on B; send back the reply

Page 4: Java-RMI Lab

Implementation

object Aproxy for Counter

Remote Communication

modulereference module

client

We’d like something like this…

Client Object

Receive input

Call method on the proxy

Page 5: Java-RMI Lab

Implementation

public int inc(int value){//marshal value

//prepare a request

//send the request to the Server//The address is in the//Remote Reference Module

//… on in a remote reference data structure

}

public int dec(int value){

}

object Aproxy for Counter

Remote Communication

modulereference module

client

Proxy…

Page 6: Java-RMI Lab

Implementation Communication module…

object Bskeleton

Communication Remote reference

modulemodule

for B’s class

& dispatcher

remote

server

servant

CommunicationModuleRegister an object

(uses Object Table)

Initialize

Receive request

Dispatch to the Skeleton

Page 7: Java-RMI Lab

Implementation Skeleton

object Bskeleton

Communication Remote reference

modulemodule

for B’s class

& dispatcher

remote

server

servant

Skeleton &Dispatcher

Initialize(bound to a Counter)

Receive request

Dispatch to the Object

Send the reply

Page 8: Java-RMI Lab

Implementation details

• See RMI-LAB on the web site

• Exercise: add a new object…

Page 9: Java-RMI Lab

Lesson learned

• Key (and borrowing) aspects• Managing communication • Managing remote reference • Implements support modules (proxy,

skeleton)

Page 10: Java-RMI Lab

Java-rmi solution

• Managing communication– Embedded into the JVM

• Managing remote reference – Rmiregistry, remote reference layer

• Implements support modules (proxy, skeleton) – Proxy: automatically generated from the code – Skeleton: no longer needed thanks to

‘reflection’ (see later)

Page 11: Java-RMI Lab
Page 12: Java-RMI Lab

Lab1: Remote counterimport java.rmi.*; //Import rmi API

public interface Counter extends Remote //makes Counter a remote interface{int inc(int i) throws RemoteException; //manage or throws this exception….int dec(int i) throws RemoteException;

}

Page 13: Java-RMI Lab

Step2: interface implementationimport java.rmi.*;

public class Counter_impl implements Counter{

private int counter;

public Counter_impl() throws RemoteException {counter = 0;}public int inc(int i) throws RemoteException {counter++;return counter;}public int dec(int i) throws RemoteException {counter--;return counter;}

}

Page 14: Java-RMI Lab

import java.rmi.RemoteException;import java.rmi.server.UnicastRemoteObject;import java.rmi.registry.LocateRegistry;import java.rmi.registry.Registry;public class Server{

public static void main(String args[]) throws RemoteException{

//Create a remote object..Counter c = new Counter_impl() ;

Counter stub = (Counter)UnicastRemoteObject.exportObject(c, 0);

//bind "counter" to the stubRegistry registry = LocateRegistry.getRegistry();registry.rebind("counter", stub);System.out.println("Counter bound");

}}

Export object = It can receive requests

Page 15: Java-RMI Lab
Page 16: Java-RMI Lab

Client

import java.rmi.RemoteException;import java.rmi.registry.LocateRegistry;import java.rmi.registry.Registry;public class Client{

public static void main(String args[]){try{

Registry registry = LocateRegistry.getRegistry(args[0]);Counter c = (Counter) registry.lookup("counter");int i = c.inc(10);System.out.println(i);

}catch (Exception e){e.printStackTrace();}}

}

Page 17: Java-RMI Lab

Running the example

• On the same machine via classpath– Run rmiregisrty: rmiregistry (make sure CLASSPATH is

unset)– Run the server: java -classpath . Server– Run the client: java -classpath . Client localhost

Page 18: Java-RMI Lab

Class downloading

• JAVA-RMI allows to download the definition of an object's class if the class is not defined in the receiver's Java virtual machine.

• Classes definitions are typically made network accessible through a web server

Page 19: Java-RMI Lab

Class downloading • Class downloading are

controlled by a Security manager

• Without a security manager installed, RMI will not download classes, other than from the local class path.

• This restriction ensures that the operations performed by downloaded code are subject to a security policy.

Page 20: Java-RMI Lab

Installing Security Manager…if (System.getSecurityManager() == null) { System.setSecurityManager(new SecurityManager());

}…

java -Djava.rmi.server.codebase="http://xxxx. " -Djava.security.policy=“java.policy”

codebase = Where classes are network accessible

policy file

grant { permission java.security.AllPermission; };

Page 21: Java-RMI Lab

LAB2

• Account example

Page 22: Java-RMI Lab

LAB3

• Shared whiteboard