Can anybody help me to understand this RMI registry service code?

RMI is so difficult to me. please let me know following code what it means.

If this class is instantiated with 50000 port number,

Does RMI Registry Service get started with 50000 port number in place of rmiregistry commands?

And I don't have to execute rmiregistry command?

thanks in advance.

import java.rmi.AccessException;

import java.rmi.AlreadyBoundException;

import java.rmi.NotBoundException;

import java.rmi.Remote;

import java.rmi.RemoteException;

import java.rmi.registry.Registry;

import java.rmi.server.ObjID;

import java.rmi.server.RemoteServer;

import java.util.Enumeration;

import java.util.Hashtable;

import sun.rmi.server.UnicastServerRef;

import sun.rmi.transport.LiveRef;

/**

* Registry implementation.

*

*

* @version 1.0

*/

publicclass RegistryImplextends RemoteServerimplements Registry{

privatestaticfinallong serialVersionUID = -7162736590129110751;

private Hashtable bindings;

privatestatic ObjID id =new ObjID(0);

/**

*

* @param i port number

* @throws RemoteException

*/

public RegistryImpl(int i)throws RemoteException{

bindings =new Hashtable(101);

LiveRef liveref =new LiveRef(id, i);

setup(new UnicastServerRef(liveref));

}

privatevoid setup(UnicastServerRef unicastserverref)throws RemoteException{

super.ref = unicastserverref;

unicastserverref.exportObject(this, null,true);

}

/**

* Lookup a remote object.

*

* @param name Name of the remote object.

* @return Remote object of the specified name.

*/

public Remote lookup(String name)throws RemoteException, NotBoundException{

//Logger.debug("NamingService", "lookup: " + name);

Remote obj;

synchronized(bindings){

obj = (Remote)bindings.get(name);

if(obj ==null)

thrownew NotBoundException(name);

}

return obj;

}

/**

* Binds the specified name to a remote object.

*

* @param name A URL-formatted name for the remote object.

* @param obj A reference for the remote object (usually a stub).

*/

publicvoid bind(String name, Remote obj)throws RemoteException, AlreadyBoundException, AccessException{

//Logger.debug("NamingService", "bind: " + name);

checkAccess("Registry.bind");

synchronized(bindings){

if(bindings.containsKey(name))

thrownew AlreadyBoundException(name);

bindings.put(name, obj);

}

}

/**

* Destroys the binding for the specified name that is associated with a remote object.

*

* @param name A URL-formatted name for the remote object.

*/

publicvoid unbind(String name)throws RemoteException, NotBoundException, AccessException{

//Logger.debug("NamingService", "unbind: " + name);

checkAccess("Registry.unbind");

synchronized(bindings){

if(!bindings.containsKey(name))

thrownew NotBoundException(name);

bindings.remove(name);

}

}

/**

* Rebinds the specified name to a new remote object. Any existing binding

* for the name is replaced.

*

* @param name A URL-formatted name for the remote object.

* @param obj A reference for the remote object (usually a stub).

*/

publicvoid rebind(String name, Remote obj)throws RemoteException, AccessException{

//Logger.debug("NamingService", "rebind: " + name);

checkAccess("Registry.rebind");

bindings.put(name, obj);

}

/**

* Returns an array of the names bound in the registry.

*

* @return An array of the names bound in the registry.

*/

public String[] list()throws RemoteException{

String[] names;

synchronized(bindings){

int i = bindings.size();

names =new String[i];

Enumeration e = bindings.keys();

while(--i >= 0)

names[i] = (String)e.nextElement();

}

return names;

}

/**

* TO-DO

*/

publicstaticvoid checkAccess(String s)throws AccessException{

// check if s is in the list of agents.

if(true)

;

else

thrownew AccessException("");

}

publicstatic ObjID getID(){

return id;

}

}

[8256 byte] By [yunjonghaa] at [2007-10-2 16:28:45]
# 1

You don't have any need to understand this code. The complication is due to the registry needing a fixed objectID.

You just need to know the methods of java.rmi.registry.LocateRegistry class. This allows you to start an RMI Registry inside your JVM and give it any TCP port number you like.

ejpa at 2007-7-13 17:30:03 > top of Java-index,Core,Core APIs...
# 2
Thanks for your comment.I get to know that the code does the same thing with rmiregistry command.
yunjonghaa at 2007-7-13 17:30:03 > top of Java-index,Core,Core APIs...