Wednesday, October 17, 2012

A Better Java RMI Example Than the Oracle Tutorial

A Beginner's Guide to RMI.

Except, unfortunately, it still references the RMIC stub compiler, which is no longer needed.

Even more unfortunately, the very basic code that they supply does not work.

UPDATE: It appears that since this beginner's guide to RMI was created, not only is RMIC stub compilation no longer required, but
if (System.getSecurityManager() == null) {
    System.setSecurityManager(new SecurityManager());
}

is now required in both the server and client.  At least on the client side, this is needed to be able to load objects from the server.

Part of what makes this so complicated is the magic collection of command line arguments required when you start the server and client.  As an example: it appears that when specifying the security policy, it is expecting a file name (not a URL that can be coerced into specifying a local file name) and it needs to be inside apostrophes, like this:
-Djava.security.policy='c:/tomcat5/webapps/rmi/server.policy'
However, the server codebase is a URL, so if you are running the server locally, you need something like this:
-Djava.rmi.server.codebase='file:///c:/tomcat5/webapps/rmi/computer.jar'
Note: three slashes after file:, not two, not one, not four.  But I still have not resolved this security policy question.  By the time I have figured all this out, I will be able to write a guide to these mysteries.

UPDATE 2: Except it appears that the policy file uses a different format:

grant codeBase "file:C:/tomcat5/webapps/rmi/-" {
    permission java.security.AllPermission;
};
Yes, "file:" would suggest a URL type of path, and thus file:///, but this suggests otherwise.  Why have a consistent format, when you have multiple formats?

UPDATE 3: At least the -Djava.security.policy parameter for Windows I now understand -- and it is shockingly simple:
-Djava.security.manager=c:\rmi\hello\server.policy
 The sever.policy file (at least the one that is probably too wide open from a security standpoint, but at least it works):

grant codeBase "file:///C:/rmi/hello/*" {
    permission java.security.AllPermission;
};
UPDATE 4: It turns out that the example I mentioned at the top of this posting is actually valid.  The only parts that were a problem were the parameters passed to Java.  The following items are required to start the HelloServerImpl:
java -Djava.security.manager -Djava.security.policy=c:\rmi\hello\server.policy HelloServerImpl
The server.policy that seems to work where both server and client are running on the same Windows box:

grant codeBase "file:///C:/rmi/hello/*" {
    permission java.security.AllPermission;
};

Some web pages indicate that a - is needed, not a *, but I do not think that is current.

To start the client was much simpler:
java HelloClient


No comments:

Post a Comment