1 / 94

George Blank University Lecturer

George Blank University Lecturer. Java RMI. Introduction to RMI. Java and the Web. Java’s popularity is due to its suitability for use on the World Wide Web (WWW). Several Web browsers have support for Java: Netscape Navigator Internet Explorer HotJava

dewayne
Download Presentation

George Blank University Lecturer

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. George Blank University Lecturer

  2. Java RMI Introduction to RMI

  3. Java and the Web • Java’s popularity is due to its suitability for use on the World Wide Web (WWW). • Several Web browsers have support for Java: • Netscape Navigator • Internet Explorer • HotJava • Java incorporates audio, video, and animation directly on the web page.

  4. Method Modifiers • Instance Methods • Class Methods • Abstract Methods • Native Methods • Final Methods

  5. Method Modifiers with Effect on Scoping • public: Method can be accessed by any class • private: methods accessed only by class methods • friendly: methods accessed by other methods in the same package • protected: methods accessed by subclass methods.

  6. Interfaces • A Java interface is similar to a class, except there is no data associated with the interface. • Example: public interface MyInterface { methods-with no implementation details final variables }

  7. Properties of Interfaces • The variables in an interface must be final. • The methods are only declarations. • A class that extends another class is guaranteed to support the contracts entered into by its superclass. • Interfaces are used to approximate multiple inheritances

  8. Java Native Interface (JNI) • The Java Native Interface (JNI) is part of the core JDK and provides a framework for interfacing to native code. The native code is not easily portable across different hardware platforms. So using native code takes way one of the major advantages of java. JNI was developed to bridge the gap. (JNI section by Rekha Telkar)

  9. When to use JNI • When the standard Java class library does not support the platform-dependent features needed by the application. • You need to use a library written in another language, and wish to make it accessible to Java code through the JNI. • You want to implement a small portion of time-critical code in a lower-level language such as assembly.

  10. JNI Capabilities • You can use native methods to: • Create, inspect, and update Java objects (including arrays and strings). • Call Java methods. • Catch and throw exceptions. • Load classes and obtain class information. • Perform runtime type checking.

  11. Enabling Java code with JNI • You can use the JNI with the Invocation API to enable an arbitrary native application to embed the Java VM. Programmers can make their existing applications Java-enabled without having to link with the VM source code.

  12. Remote Method Invocation • RMI provides the means to invoke methods remotely. • RMI allows for applications to communicate and execute across multiple systems on a network. • RMI is supported by the java.rmi, java.rmi.server, and java.rmi.registry • Enhanced security of Java 2 requires a security policy implementation.

  13. Parts in a RMI System • Interface definitions for remote services • Implementations of the remote services • Stub and Skeleton files • A server to host the remote services • An RMI Naming service that allows clients to find the remote services • A class file provider (an HTTP or FTP server)

  14. Java Client Java Server Client Method Called Method arguments results Client Stub Server Skeleton Network transport Network Transport Network RMI process Not needed In Java 2

  15. RMI Server, Client, and Registry • The server process registers the remote object X with the registry using the Naming.bind() method. • The client calls Naming.lookup(), which contacts the registry and obtains a stub object for X. • The client then uses the stub as if it is a local object.

  16. Stub Class • A stub for a remote object is the client-side proxy for the remote object. Such a stub implements all the interfaces that are supported by the remote object implementation. The client-side stub responsibilities are shown on the next slide.

  17. Stub Class Responsibilities • Initiating a call to the remote object (by calling the remote reference layer). • Marshaling arguments to a marshal stream (obtained from the remote reference layer). • Informing the remote reference layer that the call should be invoked. • Unmarshaling the return value or exception from a marshal stream. • Informing the remote reference layer that the call is complete.

  18. Skeleton Class • A skeleton for a remote object is a server-side entity that contains a method which dispatches calls to the actual remote object implementation. The skeleton is responsible for: • Unmarshaling arguments from the marshal stream. • Making the up-call to the actual remote object implementation. • Marshaling the return value of the call or an exception (if one occurred) onto the marshal stream.

  19. Remote Reference Layer • The remote reference layer deals with the lower level transport interface and is responsible for carrying out a specific remote reference protocol which is independent of the client stubs and server skeletons. The remote reference layer has two cooperating components: the client-side and the server-side components.

  20. Remote Reference Layer (2) • The client-side component contains information specific to the remote server (or servers, if the remote reference is to a replicated object) and communicates via the transport to the server-side component. During each method invocation, the client and server-side components perform the specific remote reference semantics.

  21. Remote Reference Layer (3) • For example, if a remote object is part of a replicated object, the client-side component can forward the invocation to each replica rather than just a single remote object. • In a corresponding manner, the server-side component implements the specific remote reference semantics prior to delivering a remote method invocation to the skeleton.

  22. Remote Reference Layer (4) • For example, the server side could handle ensuring atomic multiple delivery by communicating with other servers in the replica group.The remote reference layer transmits data to the transport layer via the abstraction of a stream-oriented connection. The transport takes care of the implementation details of connections. Although connections present a streams-based interface, a connectionless transport may be implemented beneath the abstraction

  23. RMI Registry • The Registry tracks the addresses of the remote objects exported by applications • It is the central management point for RMI • Does not actually invoke remote methods • Bind() links the object in the registry • Rebind() replaces object with a new one

  24. Parameter Passing • When a remote procedure is executed, the java.rmi runtime encodes the arguments and sends them over the network to a server that decodes them. • The server then invokes the method, encodes the results, and sends it back. • Finally, the client-side java.rmi runtime decodes the result.

  25. Parameter Marshalling • RMI stubs are responsible for packaging parameters used in a remote method in a block of bytes using the big-endian byte order. This is called parameter marshalling. A receiver object on the server must unmarshall the parameters or report errors.

  26. Building RMI Applications • Define remote interfaces • Create classes that implement the interfaces • Create stub and skeleton classes for the implementation classes. • Create Security Policy

  27. A Distributed Hello World Program Using RMI • It uses an applet to make a remote method call to the server from which it was downloaded to retrieve the message "Hello World!". • When the applet runs, “Hello World!” is displayed on the client browser.

  28. Steps Involved • Write The HTML and Java Source Files. • Compile and Deploy Class Files and HTML Files. • Start the Remote Object Registry, Server, and Applet

  29. Source Files • The Java remote interface. (Hello.java) • The Java remote object (server) which implements the remote interface. (HelloImpl.java) • The Java applet that remotely invokes the remote method, sayHello(). (HelloApplet.java) • The HTML code for the web page that references the applet. (hello.html)

  30. The Remote Interface • Must be public. • Extends the interface java.rmi.Remote. • Each method must declare java.rmi.RemoteException in its throws clause • A remote object passed as an argument or return value must be declared as the remote interface.

  31. Remote Interface package examples.hello; import java.rmi.Remote; import java.rmi.RemoteException; public interface Hello extends java.rmi.Remote { String sayHello() throws java.rmi.RemoteException; }

  32. The Implementation Class • Specify the remote interface(s) being implemented. • Define the constructor for the remote object. • Provide implementations for the methods that can be invoked remotely. • Create and install a security manager.

  33. The Implementation Class (Cont’d) • Create one or more instances of a remote object. • Register at least one of the remote objects with the RMI remote object registry, for bootstrapping purposes.

  34. Server Code (1) package examples.hello; import java.rmi.Naming; import java.rmi.RemoteException; import java.rmi.RMISecurityManager; import java.rmi.server.UnicastRemoteObject; public class HelloImpl extends UnicastRemoteObject implements Hello { public HelloImpl() throws RemoteException { super(); }

  35. Server Code (2) // Implementation of remote method public String sayHello() { return "Hello World!"; }

  36. Server Code (3) main begins public static void main(String args[]) { // Create and install a security manager if (System.getSecurityManager() == null) { System.setSecurityManager(new RMISecurityManager()); }

  37. Server Code (4) main ends try { HelloImpl obj = new HelloImpl(); // Bind this object instance to the name "HelloServer" Naming.rebind("//afsxx.njit.edu/HelloServer", obj); System.out.println("HelloServer bound in registry"); } catch (Exception e) { System.out.println("HelloImpl err: " + e.getMessage()); e.printStackTrace(); } } }

  38. Notes on Server Code • By extending remote class UnicastRemoteObject, the HelloImpl class can be used to create a remote object that: • Uses RMI's default sockets-based transport for communication • Runs all the time (In Java 2 SDK, the object can be activated (created) when a client requests it, using Remote Object Activation, rather than running all the time – extend java.rmi.activation.Activatable) • To bind to a different port, for example 2001, use: Naming.rebind("//afsxx.njit.edu:2001/HelloServer", obj);

  39. Notes on Server Code (contd) • Arguments to, or return values from, remote methods can be any data type for the Java platform, including objects, as long as those objects implement the interface java.io.Serializable. • By default, local objects are passed by copy, which means that all data members (or fields) of an object are copied, except those marked as static or transient • Remote objects are passed by reference. A reference to a remote object is actually a reference to a stub, which is a client-side proxy for the remote object.

  40. Notes on Server Code (contd) • Security manager guarantees that the classes that get loaded perform only allowed operations. • If no security manager is specified, no class loading, by RMI clients or servers, is allowed, aside from what can be found in the local CLASSPATH. • Client Applets use the security manager already installed in the client browser. • If the client were an application rather than an applet, Security manager would need to be installed. • A security manager is required in any JVM that needs to download code, and RMI clients need to download RMI stubs (as well as any other custom classes or interfaces needed to communicate with the RMI server).

  41. A Remote Service Applet package examples.hello; import java.applet.Applet; import java.awt.Graphics; import java.rmi.Naming; import java.rmi.RemoteException; public class HelloApplet extends Applet { String message = ""; // "obj" is the identifier that we'll use // to refer to the remote object that // implements the "Hello" interface Hello obj = null;

  42. Remote Applet Code (2) public void init() { try { Hello obj = (Hello)Naming.lookup("//" + getCodeBase().getHost() + "/HelloServer"); message = obj.sayHello(); } catch (Exception e) { System.out.println("Applet exception: " + e.getMessage()); e.printStackTrace(); } }

  43. Remote Applet Code (3) public void paint(Graphics g) { g.drawString(message, 25, 50); } }

  44. Notes on Client Applet • The applet gets a reference to the remote object implementation (advertised as "HelloServer") from the server host's rmiregistry. • The applet invokes the remote sayHello method on the server's remote object. • The applet invokes the paint method, causing the string "Hello World!" to be displayed in the drawing area of the applet.

  45. The Web Page (hello.html) <HTML> <title>Hello World</title> <center> <h1>Hello World</h1> </center> <p> <applet codebase="myclasses/" code="examples.hello.HelloApplet" width=500 height=120> </applet> </HTML>

  46. Compiling the Java Source Code • Create a directory myclasses in your public_html on AFS and compile the java code: • javac -d $HOME/public_html/myclasses Hello.java HelloImpl.java HelloApplet.java

  47. Generate Stubs and Skeletons rmic -d $HOME/public_html/myclasses examples.hello.HelloImpl The following files are created in the directory: $HOME/public_html/myclasses/examples/hello HelloImpl_Stub.class HelloImpl_Skel.class (before Java 2 only)

  48. Java 2 SDK Stubs • In the Java 2 SDK implementation of RMI, skeleton classes are obsolete. RMI uses reflection to make the connection to the remote service object. • If you will never need support for 1.1 clients, rmic can be run with the -v1.2 option • rmic –v1.2 -d $HOME/public_html/myclasses examples.hello.HelloImpl • This creates only HelloImpl_Stub.class

  49. Deploying Applet (1) • Move the HTML File (hello.html) to the Deployment Directory: $HOME/public_html/

  50. Sample Policy File for User (.java.policy) in $HOME grant { permission java.net.SocketPermission "*:1024-65535","connect,accept"; permission java.net.SocketPermission "*:80", "connect"; }; Replace * with name or IP of client to restrict access

More Related