Does anyone tell me the difference between the following two programs
There are two programs which use activation mechanism of RMI to instantiate remote objects.
The interface is
import java.rmi.Remote;
import java.rmi.RemoteException;
interface Product // shared by client and server
extends Remote
{
String getDescription() throws RemoteException;
}
The remote object is
import java.rmi.MarshalledObject;
import java.rmi.RemoteException;
import java.rmi.activation.Activatable;
import java.rmi.activation.ActivationID;
import java.rmi.server.*;
/**
This is the implementation class for the remote product
objects.
*/
public class ProductImpl extends UnicastRemoteObject implements Product {
private static final long serialVersionUID = 6613571299445532659L;
public ProductImpl(ActivationID id , MarshalledObject object)throws RemoteException{
//Activatable.exportObject(this , id , 0);
System.out.println("marshalledobject");
}
public ProductImpl(String n) throws RemoteException {
name = n;
System.out.println("no marshalledobject");
}
public String getDescription() throws RemoteException {
return "I am a " + name + ". Buy me!";
}
private String name;
}
ProductActivator class is
import java.io.File;
import java.rmi.MarshalledObject;
import java.rmi.RMISecurityManager;
import java.rmi.activation.Activatable;
import java.rmi.activation.ActivationDesc;
import java.rmi.activation.ActivationGroup;
import java.rmi.activation.ActivationGroupDesc;
import java.rmi.activation.ActivationGroupID;
import java.util.Properties;
/**
This server program activates two remote objects and
registers them with the naming service.
*/
public class ProductActivator {
public static void main(String args[]) {
try {
System.out.println("Constructing activation descriptors...");
Properties props = new Properties();
// use the server.policy file in the current directory
//props.put("java.security.policy", new File("server.policy").getCanonicalPath());
System.setSecurityManager(new RMISecurityManager());
ActivationGroupDesc group = new ActivationGroupDesc(props, null);
ActivationGroupID id = ActivationGroup.getSystem().registerGroup(
group);
MarshalledObject p1param = new MarshalledObject("Blackwell Toaster");
MarshalledObject p2param = new MarshalledObject("ZapXpress Microwave Oven");
String classDir = ".";
// turn the class directory into a file URL
// for this demo we assume that the classes are in the current dir
// we use toURI so that spaces and other special characters in file names are escaped
String classURL = new File(classDir).getCanonicalFile().toURI().toString();
//String classURL=System.getProperty("java.rmi.server.codebase");
ActivationDesc desc1 = new ActivationDesc(id, "ProductImpl",classURL, p1param);
ActivationDesc desc2 = new ActivationDesc(id, "ProductImpl",classURL, p2param);
Product p1 = (Product) Activatable.register(desc1);
Product p2 = (Product) Activatable.register(desc2);
System.out.println("Binding activable implementations to registry...");
} catch (Exception e) {
e.printStackTrace();
}
}
}
NewProductActivator class is
import java.io.IOException;
import java.rmi.MarshalledObject;
import java.rmi.Remote;
import java.rmi.activation.ActivationDesc;
import java.rmi.activation.ActivationException;
import java.rmi.activation.ActivationGroup;
import java.rmi.activation.ActivationGroupDesc;
import java.rmi.activation.ActivationGroupID;
import java.rmi.activation.ActivationID;
import java.rmi.activation.ActivationGroupDesc.CommandEnvironment;
import java.util.Properties;
public class NewProductActivator {
public static void main(String args[]) throws ActivationException,
IOException {
ActivationGroupID gid = null;
ActivationID aid = null;
Object proxy = null;
Properties properties = new Properties();
properties.put("java.security.policy", System
.getProperty("java.security.policy"));
properties.put("java.rmi.server.codebase", System
.getProperty("java.rmi.server.codebase"));
String[] options = new String[] { "-cp",
System.getProperty("java.class.path") };
CommandEnvironment cmdToExecute = new CommandEnvironment(null, options);
gid = ActivationGroup.getSystem().registerGroup(
new ActivationGroupDesc(properties, cmdToExecute));
MarshalledObject params = new MarshalledObject(null);
ActivationDesc desc = new ActivationDesc(gid, "ProductImpl", null,
params, true);
/* Register the desired service with the activation system */
aid = ActivationGroup.getSystem().registerObject(desc);
Remote serverStub = aid.activate(false);
}
}
The methods executed and the classes created in the two main() are the same except the last methods invoked.
Does anyone know the difference that the two programs do?
Thank you.

