CS 696 Emerging Technologies: Java Distributed Computing Spring Semester, 1999 Dynamically Downloading Classes |
||
---|---|---|
© 1999, All Rights Reserved, SDSU & Roger Whitney San Diego State University -- This page last updated 16-Mar-99 |
RMI, http and Downloading Class files
Classes can be automatically downloaded at runtime to an RMI client. Following conditions must be met for a class to be dynamically downloaded:
java -Djava.rmi.server.useCodebaseOnly=true -Djava.rmi.server.codebase=http://eli.sdsu.edu:8888/ whitney.rmi.examples.codebase.RogerClientThe URL must end in a "/" and must be to the directory containing the full package directory structure of the classes that the client will down load. See the examples later for more details.
Server Side java.rmi.server.codebase
Setting the java.rmi.server.codebase property on the server side is more involved. If you set the property on the server side correctly, then the property need not be set on the client side.
http Server for Examples
Any Web server can be used to download class files to a client. JavaSoft provides a small http server (ClassFileServer) written in java for downloading classes. The source code can be found at: " ftp://ftp.javasoft.com/pub/jdk1.1/rmi/class-server.zip". The examples will use ClassFileServer as the web server. Jini comes with a web server written in Java. Jrun ( http://www.jrun.com/) is another java web server you can download.
Example – Download Stub
The first example involves three classes and one interface:
Roger
Starting the Web Server
The command line arguments for ClassFileServer are:
java ClassFileServer <port> <classpath>where
java ClassFileServer 8888 /export/home/whitney/javaNote: The files ClassFileServer and from Sun are in the package examples.classServer. To simplify the presentation, I removed them from the package. Otherwise the command line would be: java examples.classServer.ClassFileServer 8888 /export/home/whitney/java
Client Side codebase
Server Side The source files on the server side are:
javac *.javaGenerate the stub class via rmic
rmic whitney.rmi.examples.codebase.GetRogerServer
rmiregistry &
java whitney.rmi.examples.codebase.GetRogerServer -label=ClientTest
Client The source files on the client side are:
javac *.java
java -Djava.rmi.server.useCodebaseOnly=true -Djava.rmi.server.codebase=http://eli.sdsu.edu:8888/ whitney.rmi.examples.codebase.RogerClient -host=eli.sdsu.edu -label=ClientTestThe stub class for the server will be downloaded to the client via http when the client needs the stub.
Server Side codebase
Server Side The source files on the server side are:
javac *.javaGenerate the stub class via rmic
rmic whitney.rmi.examples.codebase.GetRogerServer
rmiregistry &
java -Djava.rmi.server.codebase=http://eli.sdsu.edu:8888/ whitney.rmi.examples.codebase.GetRogerServer -label=ServerTest &
Client The source files on the client side are:
javac *.java
java whitney.rmi.examples.codebase.RogerClient -host=eli.sdsu.edu -label=ClientTestThe stub class for the server will be downloaded to the client via http when the client needs the stub.
Classes
Roger
package whitney.rmi.examples.codebase; import java.io.Serializable; public class Roger implements Serializable { private String data; public Roger( String message) { data = message; } public String toString() { return "Roger( " + data + ")"; } }GetRoger
package whitney.rmi.examples.codebase; import java.rmi.RemoteException; import java.rmi.Remote; public interface GetRoger extends Remote { public abstract Roger get() throws RemoteException; }
RogerClient
package whitney.rmi.examples.codebase; import java.io.IOException; import java.net.MalformedURLException; import java.rmi.*; import java.rmi.registry.Registry; import sdsu.util.ProgramProperties; public class RogerClient { public static void main(String args[]) throws Exception { System.setSecurityManager(new RMISecurityManager()); String server = getHelloHostAddress( args); GetRoger remote = (GetRoger) Naming.lookup( server ); Roger message = remote.get(); System.out.println( message.toString() ); } private static String getHelloHostAddress( String args[] ) throws IOException { ProgramProperties flags = new ProgramProperties( args ); String host = flags.getString( "host" ); int defaultPort = Registry.REGISTRY_PORT; int port = flags.getInt( "port", defaultPort ); String serverLabel = flags.getString( "label" ); return "rmi://" + host + ":" + port + "/" + serverLabel; } }
GetRogerServer
package whitney.rmi.examples.codebase; import java.io.IOException; import java.rmi.Naming; import java.rmi.registry.Registry; // for port number import java.rmi.RemoteException; import java.rmi.RMISecurityManager; import java.rmi.server.UnicastRemoteObject; import java.text.DateFormat; import java.util.Date; import sdsu.util.ProgramProperties; public class GetRogerServer extends UnicastRemoteObject implements GetRoger { public GetRogerServer() throws RemoteException {}; public Roger get() { return new Roger( date() ); } private String date() { Date today = new Date(); return DateFormat.getDateTimeInstance().format( today); } private static String getRegistration( String args[] ) throws IOException { ProgramProperties flags = new ProgramProperties( args ); int defaultPort = Registry.REGISTRY_PORT; int port = flags.getInt( "port", defaultPort ); String serverLabel = flags.getString( "label" ); return "rmi://" + ":" + port + "/" + serverLabel ; }
/** * The commandline is of the form: * java GetRogerServer -label=objectlabel -port=rmiregistryPort * rmiregistryPort is the port used by the rmiregistry and is optional * if the registry uses the standard port * objectlabel is the label that the server object is stored under in the * rmiregistry * (You need to add the package name to the class above) */ public static void main(String args[]) throws Exception { System.setSecurityManager(new RMISecurityManager()); String serverAddress = getRegistration( args ); GetRogerServer serverObject = new GetRogerServer(); Naming.rebind( serverAddress, serverObject); System.out.println("Server bound in registry"); } }
Copyright ©, All rights reserved.
1999 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.
Previous    visitors since 16-Mar-99    Next