Link to home
Start Free TrialLog in
Avatar of rameshaa
rameshaa

asked on

Dynamic Downloading using RMI

Hi,

 I have simple RMI client and server running between 2
machines. I want to download the classes dynamically to client machine.[which are coded in server.]


I couldn't able to download the classes using
java.rmi.server.codebase feature.

Here is the,

RMIServer code
==============

import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;

public
class RemoteServer extends UnicastRemoteObject implements RemoteInterface {

  static RemoteServer theServer = null;

  String name;

  public RemoteServer(String name) throws RemoteException {
    super();
    this.name = name;
  }

  public String message(String message) throws RemoteException {
    try {
          return "My Name is:"+name+",thanks message:"+message;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }

   public Foo getObject() {
      return new Foo();
   }

  public static void main (String args[]) {
    java.lang.System.out.println("Server started.");
    java.lang.System.setSecurityManager (new RMISecurityManager());

    String rmiHostName = "localhost";
    String rmiPortNum  = "1099";
    String serverName  = "SimpleServer";
    String url = "";

    if ( args.length == 2 ) {
      rmiHostName = args[0];
      rmiPortNum  = args[1];
      url = "//"+rmiHostName+":"+rmiPortNum+"/"+serverName;
    }
    else if ( args.length == 1 ) {
      rmiPortNum = args[0];
      url = "//"+rmiHostName+":"+rmiPortNum+"/"+serverName;
    }
    System.out.println("URL="+url);
    try{
      theServer = new RemoteServer (serverName);
      Naming.rebind(url,theServer);
    } catch (Exception e){
      java.lang.System.out.println("An Exception occured while creating server");
      e.printStackTrace();
    }
  }
}
--------------------------------------------------------

public interface RemoteInterface extends java.rmi.Remote {

  String message ( String message ) throws java.rmi.RemoteException;

  Foo getObject() throws java.rmi.RemoteException;

}
--------------------------------------------------------
public class Foo implements java.io.Serializable {

 public int myVar;

    public Foo() {
    }

    public Koo getKoo() {
      return new Koo();
    }
    public String toString() {
      return "I'm Foo";
    }
}

--------------------------------------------------------
class Koo {

    public Koo() { }

    public String toString() {
      return "I'm Koo";
    }
}
--------------------------------------------------------

import java.rmi.*;

public class RemoteClient {
   
    public static void main(String args[]){

      String rmiHostName = "localhost";
      String rmiPortNum  = "1099";
      String serverName  = "SimpleServer";
      String url = "rmi://"+rmiHostName+":"+rmiPortNum+"/"+serverName;

    if ( args.length == 2 ) {
      rmiHostName = args[0];
      rmiPortNum  = args[1];
      url = "rmi://"+rmiHostName+":"+rmiPortNum+"/"+serverName;
    }
    else if ( args.length == 1 ) {
      rmiPortNum = args[0];
      url = "rmi://"+rmiHostName+":"+rmiPortNum+"/"+serverName;
    }


    System.out.println("URL="+url);

    java.lang.System.setSecurityManager(new RMISecurityManager());

    try{
      RemoteInterface server = (RemoteInterface)Naming.lookup(url);
      //RemoteInterface server = (RemoteInterface)Naming.lookup("rmi://"+args[0]+"/SimpleServer");
      String message = server.message("Hello There");
      java.lang.System.out.println("The server says :\n" + message);
      Foo foo = server.getObject();
      Koo koo = foo.getKoo();
    } catch (Exception e){
      java.lang.System.out.println("Error while performing RMI");
      e.printStackTrace();
    }
  }
}
--------------------------------------------------------

The way I'm running code for server,

in my linux machine, file called run.sh

#!/bin/bash

# NOTE: classpath shouldn't be set for this shell.

rmiregistry &

java -Djava.rmi.server.codebase=http://linuxserver/~user/ -Djava.security.policy=/home/user/policy/my.policy -classpath ~/public_html/ RemoteServer linuxserver 1099
-----------------------------------------------------

my.policy looks like,

grant {
    permission java.net.SocketPermission "*:1024-",
        "connect,accept";
    permission java.lang.RuntimePermission "createSecurityManager";
    permission java.lang.RuntimePermission "setSecurityManager";
    permission java.util.PropertyPermission "*", "read, write";
};
---------------------------------------------------------

and I'm running the RemoteClient from Windows2K machine,
java -Djava.security.policy=file:/d:/test/dynamicrmi/java1.policy RemoteClient linuxserver 1099


and the java1.policy looks like,

grant {
    permission java.net.SocketPermission "*:1024-",
        "connect,accept";
    permission java.lang.RuntimePermission "createSecurityManager";
    permission java.lang.RuntimePermission "setSecurityManager";
    permission java.util.PropertyPermission "*", "read, write";
    permission java.net.SocketPermission "linuxserver", "connect,resolve";
};

---------------------------------------------------------

and in the client machine(Windows2K) I copied only
RemoteClient.class & RemoteInterface.class

when I tried to run, I got,
Exception in thread "main" java.lang.NoClassDefFoundError: Foo

then I copied Foo, then I got,
Exception in thread "main" java.lang.NoClassDefFoundError: Koo
---------------------------------------------------------
Exception in thread "main" java.lang.NoClassDefFoundError: Koo
        at java.lang.Class.getMethods0(Native Method)
        at java.lang.Class.getDeclaredMethods(Class.java:1039)
        at java.io.ObjectStreamClass.computeSerialVersionUID(ObjectStreamClass.j
ava:873)
        at java.io.ObjectStreamClass.access$200(ObjectStreamClass.java:46)
        at java.io.ObjectStreamClass$2.run(ObjectStreamClass.java:420)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.io.ObjectStreamClass.init(ObjectStreamClass.java:401)
        at java.io.ObjectStreamClass.lookupInternal(ObjectStreamClass.java:112)
        at java.io.ObjectStreamClass.setClass(ObjectStreamClass.java:566)
        at java.io.ObjectInputStream.inputClassDescriptor(ObjectInputStream.java
:936)
        at java.io.ObjectInputStream.readObject(ObjectInputStream.java:366)
        at java.io.ObjectInputStream.readObject(ObjectInputStream.java:236)
        at java.io.ObjectInputStream.inputObject(ObjectInputStream.java:1186)
        at java.io.ObjectInputStream.readObject(ObjectInputStream.java:386)
        at java.io.ObjectInputStream.readObject(ObjectInputStream.java:236)
        at sun.rmi.server.UnicastRef.unmarshalValue(UnicastRef.java:300)
        at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:134)
        at RemoteServer_Stub.getObject(Unknown Source)
        at RemoteClient.main(RemoteClient.java:34)


So the dynamic downloading only downloads "stub" files ??
or Am I doing wrong ?
How do i download the associated classes to clients ???

Is there any sample code explaing how to run ???
or what is the mistake I'm doing ...

Any help will be appreciated.

Thanks alot for reading this big email.
Ramesh//
ASKER CERTIFIED SOLUTION
Avatar of Moondancer
Moondancer

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial