Advanced Remote Method Invocations - PowerPoint PPT Presentation

1 / 40
About This Presentation
Title:

Advanced Remote Method Invocations

Description:

The Java RMI API has a rich collection of features. ... A java security policy file needs to be set on the server host and also on the client host. ... – PowerPoint PPT presentation

Number of Views:99
Avg rating:3.0/5.0
Slides: 41
Provided by: ise2
Category:

less

Transcript and Presenter's Notes

Title: Advanced Remote Method Invocations


1
Advanced Remote Method Invocations

2
RMI Advanced topics
  • The Java RMI API has a rich collection of
    features.
  • We will look at some of RMIs more interesting
    advanced features, namely
  • stub downloading
  • security manager
  • client callback.
  • Although these features are not inherent to the
    distributed object paradigm, they are helpful
    mechanisms and can be useful to application
    developers.

3
The Java RMI Architecture
4
Java RMI Client Server Interaction
5
RMI Stub Downloading
  • RMI is designed to allow stubs to be made
    available to the client dynamically (in HW_3).
    Doing so allows changes to be made in the remote
    methods without affecting the client program.
  • The stub can be filed with an web server and be
    downloaded using HTTP/FTP.
  • Security measures are needed in both the client
    side and the server side
  • A java security policy file needs to be set on
    the server host and also on the client host.
  • A Java Security Manager should be instantiated in
    both the client and server programs.

6
Stub downloading
  • If the stub will be downloaded from a remote
    server, transfer the stub class to the
    appropriate directory that HTTP server can reach,
    e.g., www.csc.calpoly.edu/mliu/www, and make
    sure that the RIGHT access permission to the file
    is set.
  • When activating the server, specify command option

java -D java.rmi.server.codebase
\ -D java.rmi.server.hostname
\ -D java.security.policyto java.policy file
7
The java.policy file
  • The RMI security manager does not permit network
    access. Exceptions can be made via the
    specification in a java.policy file.
  • grant
  • // permits socket access to all common TCP
    ports, including the default
  • // RMI registry port (1099) need for both
    the client and the server.
  • permission java.net.SocketPermission
    "1024-65535", "connect,accept,resolve"
  • // permits socket access to port 80, the
    default HTTP port needed
  • // by client to contact an HTTP server for
    stub downloading
  • permission java.net.SocketPermission "80",
    "connect"
  • grant // Allow everything hw_3
  • permission java.security.AllPermission

8
The java.policy file - 2
  • This file can be filed in the same directory as
    the server class file.
  • When activating the client, a java.policy file
    also should be specified
  • java -D java.rmi.server.useCodebaseOnlytrue
  • -D java.rmi.server.codebase
    http//hostname80/stub_dir/
  • -D java.security.manager -D
    java.security.policyjava.policy
  • SomeClient
  • -D propertyvalue -Djava.security.policys
    omeURL SomeApp
  • where someURL is a URL specifying the location
    of a policy file
  • java.rmi.server.codebase this property specifies
    the locations from which classes that are
    published by this JVM.
  • java.rmi.server.useCodebaseOnly If this value is
    true, automatic loading of classes is prohibited
    except from the local CLASSPATH and from the
    java.rmi.server.codebase property set on this
    JVM.
  • Default security policy file java_jre_home/lib/s
    ecurity/java.policy
  • permission java.net.SocketPermission
    "localhost1024-", "listen"

9
The java.policy file - 3
  • The "-D java.security.manager" argument ensures
    that the default security manager is installed,
    and thus the application is subject to policy
    checks.
  • Default security manager is not required if the
    application installs a security manager.
  • If you use java -Djava.security.manager D
    java.security.policysomeURL SomeApp, then just
    the specified policy file will be used all the
    ones indicated in the security properties file
    will be ignored.

Ref http//java.sun.com/j2se/1.4.2/docs/guide/sec
urity/PolicyFiles.html
10
File Placements
11
RMI Security Manager
  • Since RMI involves access to/from a
    remote/foreign host, and possibly object
    downloading, it is important for both the server
    and the client to protect its system from
    malicious access.
  • The RMISecurityManager--a Java class, can be
    instantiated in both the client and the server
    for limiting access privileges.
  • RMI's class loader will not download any classes
    from remote locations if no security manager has
    been set.
  • RMISecurityManager does not apply to applets,
    which run under the protection of their browser's
    security manager.
  • You can instantiate/write your own security
    manager, if so desired.
  • try
  • System.setSecurityManager(new
  • RMISecurityManager( ))
  • catch

12
Sample Code for Stub Downloading
  • The possible ways--accept, connect, listen, and
    resolve, to connect to a host in SocketPermission
    java class.
  • The "listen" action is only meaningful when used
    with "localhost".
  • The "resolve" action is implied when any of the
    other actions are present. The action "resolve"
    refers to host/ip name service lookups.
  • p1 new SocketPermission(ise.gmu.edu7777",
    "connect, accept") allows that code to connect
    to port 7777 on ise.gmu.edu, and to accept
    connections on that port.
  • p2 new SocketPermission("localhost1024-",
    "accept, connect, listen") allows that code to
    accept connections on, connect to, or listen on
    any port between 1024 and 65535 on the local
    host.

Ref http//java.sun.com/j2se/1.4.2/docs/api/java/
net/SocketPermission.html
13
Algorithm for building an RMI Application
  • Server side
  • Open a directory for all the files to be
    generated for this application.
  • Specify the remote-server interface, and compile
    it to generate the interface class file.
  • Build the remote server class by implementing the
    interface, and compile it using javac.
  • Use rmic to process the server class to generate
    a stub.class file and a skelton.class file rmic
    SomeServerImpl
  • If stub downloading is desired, copy the stub
    file to an appropriate directory on the HTTP
    host.
  • Activate the RMIRegistry, if it has not already
    been activated.
  • Set up a java.policy file.
  • Activate the server, specifying (i) the codebase
    if stub downloading is desired, (ii) the server
    host name, and (iii) the security policy file.
  •  

14
Sample Code for Stub Downloading
public interface HelloInterface extends Remote
public String sayHello() throws
java.rmi.RemoteException // end of
HelloInterface interface public class HelloImpl
extends UnicastRemoteObject implements
HelloInterface public HelloImpl() throws
RemoteException super( )
public String sayHello() throws RemoteException
return "Hello, World!" // end
HelloImpl class
15
Sample Code for Stub Downloading
public class HelloServer public static void
main(String args) try
// System.setSecurityManager( new
RMISecurityManager())
startRegistry(RMIPortNum) HelloImpl
exportedObj new HelloImpl()
registryURL "rmi//cs1.cs.gmu.edu" portNum
"/hello" Naming.rebind(registryURL,
exportedObj) System.out.println("Hello
Server ready.") // end try catch
(Exception re) System.out.println("Exce
ption in HelloServer.main " re) // end
main
16
Sample Code for Stub Downloading
grant // Allows RMI clients to make socket
connections to the // public ports on any
host. // If you start the RMI registry on a
port in this range, you // will not incur a
resolve access violation. permission
java.net.SocketPermission "1024-65535",
"connect, accept, resolve" // Permits
socket access to port 80, the default HTTP port
- // needed by client to contact an HTTP
server for stub // downloading. permission
java.net.SocketPermission "80", "connect,
accept, resolve"
17
Sample Code for Stub Downloading
build (JAVAC) HelloInterface.java
(JAVAC) HelloServer.java (JAVAC)
HelloImpl.java rmic (RMIC)
HelloImpl runs (JAVA) -D
java.security.policyjava.policy
-D java.rmi.server.codebasehttp//server_URL
HelloServer
18
Algorithm for building an RMI Application
  • Client side
  • Open a directory for all the files to be
    generated for this application.
  • Implement the client program or applet, and
    compile it to generate the client class.
  • If stub downloading is not in effect, copy the
    server interface stub class file.
  • Set up a java.policy file.
  • Activate the client, specifying (i) the server
    host name, (ii) the security policy file, and
    (iii) the codebase if stub downloading is
    desired.

19
Client Code for Stub Downloading - 1
public class HelloClient public static void
main(String args) try
System.setSecurityManager(new RMISecurityManager()
) String registryURL "rmi//ise.gmu.edu"
portNum "/hello" // find the remote
object and cast it to an interface object
HelloInterface h (HelloInterface)Naming.lookup(r
egistryURL) // invoke the remote method
String message h.sayHello() // end
try catch (Exception e)
System.out.println("Exception in HelloClient "
e) // end catch //end main //end class
20
Client Code for Stub Downloading - 2
build (JAVAC) HelloClient.java
(JAVAC) HelloInterface.java runc
(JAVA) D java.rmi.server.useCodebaseOnlytrue
-D java.rmi.server.codebasehttp//URL_stub_
dir/ -D java.security.policyjava.policy
HelloClient
21
RMI Callbacks
22
Introduction
  • In the client server model, the server is
    passive the IPC is initiated by the client the
    server waits for the arrival of requests and
    provides responses.
  • Some applications require the server to initiate
    communication upon certain events. Examples
    applications are
  • monitoring
  • games
  • auctioning
  • voting/polling
  • chat-room
  • message/bulletin board
  • groupware

23
Polling vs. Callback
  • In the absence of callback, a client will have to
    poll a passive server repeatedly if it needs to
    be notified that an event has occurred at the
    server end.

24
Two-way communications
  • Some applications require that both sides may
    initiate IPC.
  • Using sockets, duplex communication can be
    achieved by using two sockets on either side.
  • With connection-oriented sockets, each side acts
    as both a client and a server.

Process 2
25
RMI Callbacks
  • A callback client registers itself with an RMI
    server.
  • The server makes a callback to each registered
    client upon the occurrence of a certain event.

26
Callback Client-Server Interactions
27
Callback application files
28
RMI Callback file placements
29
The Hello Application with Callback

30
RMI Callback Interface
  • The server provides a remote method (in server
    interface), which allows a client to register
    itself for callbacks.
  • A client remote interface for the callback is
    needed, in addition to the server-side interface.
  • The client remote interface specifies a method
    for accepting a callback from the server.
  • The client program is a subclass of RemoteObject,
    and implements the callback (client) remote
    interface, including the callback
    methodNotifyMe().
  • The client registers itself for callback in its
    main method, by passing an object reference to
    the client remote interface.
  • The server invokes the clients remote
    methodNotifyMe(), upon the occurrence of the
    anticipated event.

31
Algorithm for building an RMI Callback Application
  • Server side
  • Open a directory for all the files to be
    generated for this application.
  • Specify the remote-server interface, and compile
    it to generate the interface class file.
  • Build the remote server class by implementing the
    interface, and compile it using javac.
  • Use rmic to process the server class to generate
    a stub class file and a skeleton class file
    rmic ServerInterfaceImpl
  • If stub downloading is desired, copy the stub
    file to an appropriate directory on the HTTP
    host.
  • Activate the RMIRegistry, if it has not already
    been activated.
  • Set up a java.policy file.
  • Activate the server, specifying (i) the codebase
    if stub downloading is desired, (ii) the server
    host name, and (iii) the security policy file.
  • Obtain the CallbackClientInterface and its stub
    file. Use rmic CallbackClientInterfaceImpl to
    generate the stub file for the callback.
  •  

32
Remote Interface for Server
  • public interface CallbackServerInterface extends
    Remote
  • // remote method
  • public String sayHello() throws
    java.rmi.RemoteException
  • // method to be invoked by a client to add
    itself to the callback list
  • public void registerForCallback (
  • CallbackClientInterface CallbackObject)
  • throws java.rmi.RemoteException
  • public void unregisterForCallback(
  • CallbackClientInterface CallbackObject)
  • throws java.rmi.RemoteException

33
Client Remote Interface for Callback
  • // a remote interface specifying a callback
    method
  • public interface CallbackClientInterface extends
    java.rmi.Remote
  • // callback method to be called by the server
  • public void NotifyMe ( String message )
  • throws java.rmi.RemoteException

34
ServerInterfaceImpl with callback
  • public class CallbackServerInterfaceImpl extends
    UnicastRemoteObject implements
    CallbackServerInterface
  • public CallbackServerInterfaceImpl() throws
    RemoteException
  • super( ) clientList new Vector()
  • public String sayHello( ) throws
    java.rmi.RemoteException
  • return("hello")
  • public synchronized void registerForCallback(
    CallbackClientInterface callbackClientObject)
    throws java.rmi.RemoteException
  • if (!(clientList.contains(callbackClientObject)))
  • clientList.addElement(callbackClientObject)
  • doCallbacks()
  • private synchronized void doCallbacks( ) throws
    java.rmi.RemoteException
  • for (int i 0 i
  • CallbackClientInterface nextClient
    (CallbackClientInterface)clientList.elementAt(i)
  • String returnMsg nextClient.notifyMe("Num of
    clients" clientList.size())

35
ClientInterfaceImpl with callback
public class CallbackClientInterfaceImpl extends
UnicastRemoteObject implements CallbackClientInter
face public CallbackClientInterfaceImpl()
throws RemoteException super( )
public String notifyMe (String message)
String retMessage "Call back received "
message return retMessage
36
Algorithm for building an RMI Callback Application
  • Client side
  • Open a directory for all the files to be
    generated for this application.
  • Implement the client program or applet, and
    compile it to generate the client class.
  • If stub downloading is not in effect, copy the
    server interface stub class file by hand.
  • Implement the callback client interfaceclient
    interface impl class
  • using rmic to generate a stub class and a
    skeleton class for it for both client callback
    interface and server interface.
  • Set up a java.policy file.
  • Activate the client, specifying (i) the server
    host name, (ii) the security policy file, and
    (iii) the codebase if stub downloading is desired.

37
CallbackClient
  • public class CallbackClient
  • public static void main(String args)
  • try // stub downloading
  • System.setSecurityManager(new
    RMISecurityManager())
  • String registryURL "rmi//cs1.cs.gmu.edu
    " portNum "/callback"
  • CallbackServerInterface h (CallbackServerInterfa
    ce)Naming.lookup(registryURL)
  • CallbackClientInterface callbackObj new
    CallbackClientInterfaceImpl()
  • // register for callback
  • h.registerForCallback(callbackObj)
  • System.out.println (Registered for
    callback.")
  • h.unregisterForCallback(callbackObj)
  • catch (Exception e)
  • System.out.println ("Exception in
    CallbackClient " e) // end catch
  • // end of main()
  • //end class

38
Summary-1
  • Stub downloading allows a stub class to be
    downloaded to an object client at runtime,
    thereby allowing a remote objects implementation
    to be modified and its stub class regenerated
    without affecting the software on the client
    host.
  • A security manager oversees access restrictions
    specified in a Java security policy file, which
    can be a system-wide policy file, or a policy
    file applied to an individual application only.
  • For security protection, the use of security
    managers is recommended in all RMI applications,
    regardless of whether stub downloading is
    involved.

39
Summary-2
  • Client callback
  • Client callback is useful for an application
    where the clients desire to be notified by the
    server of the occurrence of some event.
  • Client callback allows an object server to make a
    remote method call to a client via a reference to
    a client remote interface.

40
Summary-3
  • Client callback
  • To provide client callback, the client-side
  • supplies a remote interface,
  • instantiates a callback interface object
  • passes a reference to the object to the server
    via a remote method call to the server.
  • The object server
  • collects these client references in a data
    structure.
  • invokes a callback method, defined in the client
    remote interface, to pass data to the client,
    when the awaited event occurs.
  • Two sets of stub-skeletons are needed one for
    the server remote interface, the other one for
    the client remote interface.
Write a Comment
User Comments (0)
About PowerShow.com