Introduction
 

What is RMI

The Distributed Calculator: 
A Simple Example Scaling Up: Enterprise Concerns

RMI Future

The Calculator as an Applet  -- 

        Practical Experiences in RMI Programming

(Dave Purdie)




RMI Programming for Applets

While there are many outstanding books and tutorials availble on the topic of RMI, there are very few concrete examples of using RMI with an applet interface.  I found this to be an odd omission in the literature at hand. As I now suspect the reason for this is because the applet application injects a level of indirection into the Naming and Lookup Mechanism (the server must talk to the remote methods as well as to the applet code) and adds a level of complexity to Security issues by attempting to load an applet with remote references into a browser.
 
 

RMI Programming for Applets - A Practical Example

I began preparing for the Applet version by attempting to compile and run several example programs - the RMIAuctionServer at http://developer.java.sun.com/developer/onlineTraining/Programming/JDCBook/rmi.html,
the HelloWorld example at http://www.depaul.edu/~elliott/420/java/rmi/ and the Arith Server at http://www.javacats.com/US/articles/Qusay/RMI.html among others. Only the HelloWorld example included any code for an applet version.

I was able to quickly get each of these running as a command line application with little difficulty. The HelloWorld example was the only one which included any sample applet code, but the simple HelloWorld applet refused to load into either the browser or the appletviewer.  Worse yet, the results were inconsistent between them - the appletviewer yeilded errors to the effect of "applet not initialized" while the browser would simply hang at "loading C:..."  Not even the Java console yeilded an error on the browser.

Undaunted, I set about converting YanPing's application code into an applet version  extending the Naming, Lookup and Rebind methods to throw specific exceptions and adding them as the first methods called in the init() method of the calclientApplet.java.  I extended the exceptions of the calculatorServerImpl as well and added System.out messages in order to track the startup sequence. After recompiling and meddling with the code for quite a while, I was able to coax the entire thing into starting in separate memory spaces and was able to get the applet to load into the browser as well as the appletviewer. 

The function method servers were started in separate memory spaces and no exceptions were thrown when loading the applet. This proves that server and all clients were entered into the registry.  Calling a remote method, however produced an addServer NotBound exception from the applet:

C:\$user\DS420\BP\calc\temp> java  calculatorServerImpl
calcServerImpl.main: creating registry
calcServerImpl.main: creating server
new calculatorServer.constr
calcServerImpl.main: binding server
calcServerImpl.main: done

C:\$user\DS420\BP\calc\temp>java   addServerImpl
Initializing addserver
addServer bound in registry on c3482633.sch.advantis.com
Ready to serve remote method invocations.

C:\$user\DS420\BP\calc>appletviewer calculator.html
Catch NotBoundException error.
NotBoundExceptionaddServer
java.rmi.NotBoundException: addServer

So it appears that in this version,  both the calculatorServerImpl and functionServerImpls start without throwing exceptions, i.e. all are bound into the registry. The exception was thrown by the applet but not until the method was called. It is unclear at this point as to why this would occur.  These problems grew more complex as I continued.
 

Comparison to CORBA

I moved on to try the CORBA assignment with the idea of going back to the calculator applet.  Since I had been using Java 1.1.7b,  I downloaded Java 1.2 and was able to run the CORBA examples with few problems. In these simple examples, CORBA is very similar to RMI - CORBA requires the use of the idltojava compiler to generate stubs and skeletons while RMI uses rmic; both CORBA and RMI define a marshaling protocol which Java thankfully leaves in the background; CORBA  uses tnamserv for Naming and Lookup services while RMI uses rmiregistry. Both require similar application code to find remote methods and invoke them.

The big difference seems to be in RMI's use of the registry as a Security mechanism and the more stringent security requirements of Java 1.2 in general.
 

RMI Security

After installing Java 1.2, the same calclientApplet.java and serverImpls refuse to run at all:

C:\$user\DS420\BP\calc\temp>java -classpath . calculatorServerImpl 
new calculatorServer.constr
calcServerImpl.main: binding server
calcServerImpl.main: (exception): access denied (java.net.SocketPermission 127.0
.0.1:2005 connect,resolve)
java.security.AccessControlException: access denied (java.net.SocketPermission 1
27.0.0.1:2005 connect,resolve)
        at java.security.AccessControlContext.checkPermission(Compiled Code)...

Apparently Java2 requires a java.policy file in order to grant security permissions [http://developer.java.sun.com/developer/onlineTraining/Programming/JDCBook/rmi.html]
and the above stackTrace indicates what permissions are missing.

The security issues involved in using RMI with applets appear to be that the applet running in the browser's VM must invoke methods outside the VM, which are generally not allowed in applet programming.  Since the remote methods are not resident on the server from which the applet was downloaded,  it is hard to imagine how a local browser VM might be coaxed into referencing a method located on a 3rd party.  I suspect that objects referenced by an applet must also extend Serialized. Some more investigation is needed.
 

Conclusions

At this point, although my attmepts to call RMI from a browser have been futile, some interesting points have arisen. Java security issues are clearly demonstrated. RMI programming in general appears to yeild a number of benefits:
 

  • implementation of command-line applications is fairly straightforward
  • RMI handles many difficult protocol issues in the background (such as opening socket connections and referring to its own naming and lookup services)
  • future versions promise to improve facets such as remote activation and remote reference counting for more efficient garbage collection.
  • the separation of the RMI interface and transport mechanisms from the main application can be useful for rapid prototyping and faster time-to-market than non-Java implementations.
  • RMI (as opposed to CORBA and DCOM) allows objects to be passed by reference as well as by value, thereby enabling objects to be copied to a remote environment. In this manner RMI can be used to implement remote agents and aglets (relocatable helper objects).
  • RMI preserves all other advantages found in Java programming.


While there are many drawbacks, RMI must be considered among the many competing  implementation methodologies as a viable platform choice for implementing distributed designs.

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Late Update (and a small victory):

finally was able to get calclientApplet to run in appletviewer by changing appletview-properties-class access to UNRESTRICTED.

The Applet still does not run in the browser, however.  It appears that Netscape at least might accept some sort of Certificate attached to the applet...  this will require still further investigation.

The applet code can be found here.