Java RMI
Java RMI
Java RMI
Introduction to RMI
Warning
This course is very volatile. Software is upgraded every semester. You should not assume that an example in the presentation is complete, or that it will work in the present environment. It is best to get your code examples from the Sun Java tutorials and modify them to work. Use the lectures to understand the general principles.
Java
Java is a general purpose Object-Oriented language Java provides a number of features that support development of GUI applications and client/server applications over local and wide area networks (RMI and JDBC).
2
Java Client Client code 7 Remote Interface 3
1
Java Server Server 5 Implementation Remote Interface
4
Network
Server registers its interface in the RMI registry Client queries registry for remote interface Client implementation invokes interface object The interfaces inherited classes marshal invocation parameters to network packets The Server implements the interfaces methods The Server returns its result to the network The Client is issued the result
The type needs to be casted to the servers type as shown here (Server)
Need to supply a command line directive to indicate the security policy file.
java -Djava.security.policy=policy Server java -Djava.security.policy=policy Client
5.
6. 7. 8.
Three representative eye images are used to denote trusted users of the system Eye images are encoded by a radial signature algorithm The signatures derived from the images are stored in an array within the Server for comparison Validation requests are either accept or denied based on the comparison results of step 4
We choose to balance the compute and communications overhead by processing the eye image at the Client and only send its signature to the Server instead of the whole image.
Client (2)
package cs633rmi;
import import import import import import java.io.*; javax.imageio.*; java.awt.image.*; java.rmi.registry.LocateRegistry; java.rmi.registry.Registry; cs633rmi.Validation;
Client (3)
public static void main(String args[]) { if (System.getSecurityManager() == null) { System.setSecurityManager(new SecurityManager()); } try { Registry registry = LocateRegistry.getRegistry(args[0]); Validation v = (Validation) registry.lookup("Validator");
Client (4)
Image img = new Image(); img.Init();
BufferedImage eye = new BufferedImage(100, 100, BufferedImage.TYPE_BYTE_GRAY); eye = ImageIO.read(new File(args[1])); // Derive this eye's signature. int [] signature = new int[img.NumSamples()]; signature = img.DeriveEyeSignature(eye);
Client (5)
// Pass this signature to the remote server for authentication.
// Perform the RMI call here Boolean b = v.Validate(signature); if (b) System.out.println(Authenticated."); else System.out.println(Denied access."); }
Client (6)
catch (IOException e) { System.out.println(e); } catch (Exception e) { System.out.println(e); } } }
Eyes
eye1
eye2
eye3
eyeX
Eyes 1, 2, 3 are trusted at the server. Eye X is unknown to the server. Another eye, called eye2withnoise.jpg is eye2 with noise to emulate a real system.
Demo Discussion
The screenshots on the previous slides show three test cases
Eye 2 shows a perfect match (1.0) at index 1 (the second eye). Eye 2 with noise shows that eye 2 in the server is still a 96% match, thus allowing it access. Unknown shows no correlation above 95%, thus, it is denied access.
RMI Security
One of the most common problems encountered with RMI is a failure due to security constraints. The security policy can be set by constructing a SecurityManager object and calling the setSecurityManager method of the System class. The SecurityManager class has a large number of methods whose name begins with check. For example, checkConnect (String host, int port).
Security Continued
If the current security policy does not allow connection to this host and port, then the call throws an exception. This usually causes the program to terminate with a message such as: java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:1099 connect,resolve)
Comparison Continued
Since Java-RMI is tightly coupled with The Java Language, Java-RMI can work with true sub-classes. Because of this, parameters passed during method calls between machines can be true Java Objects. This is impossible in DCOM or CORBA at present.
Comparison Continued
If a process in an RMI system receives an object of a class that it has never seen before, it can request that its class information be sent over the network. Over and above all this, Java-RMI supports Distributed Garbage Collection that ties into the local Garbage Collectors in each JVM.
Garbage Collection
The Java Virtual Machine has an automatic garbage collector that will reclaim the memory from any object that has been discarded by the running program. Garbage collection is difficult if the server and client run on different machines. The RMI subsystem implements a reference counting-based distributed garbage collection (DGC) algorithm to provide automatic memory management facilities for remote server objects.
Advantages of RMI
Object-Oriented Safe and Secure Easy to Write/Easy to Use Connects to Existing/Legacy Systems (JNI) Write Once, Run Anywhere Distributed Garbage Collection Parallel Computing
RMI Limitations
RMI is not full featured middleware No mechanism for object description No server events Java only, not language independent Firewalls are not supported Naming service is not persistent No load balancing
Summary
Java RMI is a distributed object model for the Java platform. RMI extends the Java object model beyond a single virtual machine address space. RMI uses object serialization to convert object graphs to byte streams for transport.
Trouble Shooting
Default port for RMI RegistryImpl is 1099. If it is already in use, you will get a
java.net.SocketException: Address already in use.
Do not try to use the loopback address, 127.0.0.1 improperly. It does not work if you have a network card installed on your computer. RMI is subject to Connection Exceptions that are idiosyncratic and difficult to overcome. This is one of the most common topics in RMI discussions.
Problems Encountered
A java.rmi.NotBoundException was overcome by bypassing the start command and starting the server directly. This is not recommended, as the program will not exit normally. NOTE: A probable cause is another version of Java on the system that is referenced by the start command. Several java.rmi.UnmarshallException were caused by classes not in the class path. Some students just had to add the current directory: set classpath=.%classpath%
ClassNotFoundException
Exception: java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: Caused by: java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: Caused by: java.lang.ClassNotFoundException: ChatServerImpl_Stub
Root cause:
Did not specify the codebase property as one of the VM arguments
Solution:
Pass codebase property to java interpreter java Djava.rmi.server.codebase=http://web.njit.edu/~gblank/rmi/b in/ChatServerImpl
NoClassDefFoundError
Exception: Exception in thread "main" java.lang.NoClassDefFoundError: com/is/chat/ChatServerImpl Root cause: Failed to run the client from the deployment directory /afs/cad/u/g/b/gblank/public_html/rmi/bin Solution: Change directory
Solution: Start rmi registry and register the server class before running the client
Hints
When you have a problem, you may have to kill all your processes and restart everything. You must start the registry (Windows command) start rmiregistrystart All remote interfaces must extend java.rmi.RemoteInterface, and all remote objects must extend java.rmi.UnicastRemoteObject. Security management and implementing class policies are complex and difficult in RMI. The client policy file permits connection to a specific referenced port number.
Lessons Learned
You can get a trace of the server activity using
java -Djava.rmi.server.logCalls=true <YourServer>
Properties beginning java.rmi.* are part of published RMI spec and docs, while sun.rmi.* indicates things that may vary. You can find the host name of a caller of a remote method with
java.rmi.server.RemoteServer.getClientHost
On a server with multiple host names, you need to force a fully qualified host name with
java.rmi.server.useLocalHostname=true
Environment Conflicts
If you may have multiple versions of Java on your system, try java -version to see which version your environment defaults to. Remember that different versions seldom work together, and that your Windows autoexec.bat file may set some environment values that you might not think about.
References
Andrew Tanenbaum and Martin van Steen, Distributed Systems, Prentice Hall, 2002 http://java.sun.com/j2se/1.4.2/docs/guide/rmi/getstart.d oc.html http://developer.java.sun.com/developer/onlineTraining /rmi/RMI.html http://java.sun.com/j2se/1.4/docs/relnotes/features.htm l http://java.sun.com/marketing/collateral/javarmi.html
References (2)
http://java.sun.com/javase/6/docs/technotes/guides/rmi/relnotes. html http://www.javacoffeebreak.com/articles/javarmi/javarmi.html Sun Microsystems, http://java.sun.com/developer/codesamples/rmi.html Vaclav Matyas Jr., Zdenek Riha, Biometric Authentication Systems, Faculty of Informatics, Masaryk University, November 2000.
References (3)
Eye images
http://buytaert.net/cache/images-miscellaneous-2006eye-500x500.jpg http://www.inkycircus.com/photos/uncategorized/2007/ 04/25/eye.jpg http://van-der-walt.blogspot.com/2007/09/how-eyeworks.html http://michelemiller.blogs.com/marketing to women/eye.bmp