A possible solution for the challenge program

Senario

Based on the program listed in Migrate an existing remote object section, modify the Migration class, make it migrate more than one remote objects. The following program uses HelloImpl object which is developed in the RMI level I section as second remote object. All related files are listed below:

//Hello.java

package tabletalking;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface Hello extends Remote {
    public String sayHello(String toWhom) throws RemoteException;
}

//HelloImpl.java

package tabletalking; 

import java.rmi.RemoteException;

public class HelloImpl implements Hello {
    public String sayHello(String toWhom) throws java.rmi.RemoteException {
        return "Hello, " + toWhom; 
    }
}

//TableTalk.java

package tabletalking;

import java.rmi.*;

public interface TableTalk extends Remote {
     public String getTopic() throws RemoteException;
}

//TableTalkImpl.java

package tabletalking;

import java.rmi.*;

public class TableTalkImpl implements TableTalk {
    private String[] questions = {"What is your favorite time of the day?",
                                  "What is your favorite fruit or vegetable?",
				  "Talk about something beautiful you saw this week.",
                                  "What I tile best about our family is ... ",
                                  "Tell about a mistake you have made recently.",
				  "Tell about a time when you felt happy.",
                                  "How do you react when someone crowds in line in front of you?",
                                  "Talk about a special gift that you remember.",
                                  "If your family received a gift of $5,000, how would you like your family to spend it?",
                                  "Tell about a family tradition that you enjoy." 
                                  };
    private int idx = 0;
    	 
    public String getTopic() throws RemoteException {
         idx = (int)(Math.random()*questions.length);
         return questions[idx];
    }
}

//Migration.java

package tabletalking; 

import java.rmi.*;
import java.rmi.activation.*;

public class Migration implements TableTalk, Hello{
    private TableTalkImpl tti = new TableTalkImpl();
private HelloImpl hello = new HelloImpl();

    public Migration(ActivationID id, MarshalledObject data) throws RemoteException { 
         Activatable.exportObject(this,id, 0);
    }
    public String getTopic() throws RemoteException {
         return tti.getTopic(); 
    }
    public String sayHello(String s) throws RemoteException {
         return hello.sayHello(s);
    }
}

//TableTalkSetup.java

package tabletalking; 

import java.rmi.*;
import java.rmi.activation.*;
import java.util.Properties;

public class TableTalkSetup {
 
    public static void main(String[] args) throws Exception {
	
	System.setSecurityManager(new RMISecurityManager());

	Properties props = new Properties(); 
	props.put("java.security.policy", "/myrmi/myrmi.policy"); 
	
	ActivationGroupDesc.CommandEnvironment ace = null; 
	ActivationGroupDesc exampleGroup = new ActivationGroupDesc(props, ace);
	
	ActivationGroupID agi = 
	   ActivationGroup.getSystem().registerGroup(exampleGroup);
	

	String location = "file:/myrmi/tabletalking/";
        String helloLoc = "file:/myrmi/";

	MarshalledObject data = null;

	ActivationDesc desc = new ActivationDesc 
	    (agi, "tabletalking.Migration", location, data);

        ActivationDesc desc2 = new ActivationDesc 
	    (agi, "tabletalking.Migration", helloLoc, data);
	
	TableTalk tt = (TableTalk)Activatable.register(desc);
	System.out.println("Got the stub for the TableTalkImpl");

        Hello hl = (Hello)Activatable.register(desc2);
	System.out.println("Got the stub for the HelloImpl");

	Naming.rebind("Topic", tt);
	System.out.println("Exported a table-talking object");

        Naming.rebind("Hello", hl);
	System.out.println("Exported a HelloImpl object");

	System.exit(0);
    } 
} 

//TableTalkClient.java

package tabletalking;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class TableTalkClient {
    private static TableTalk stub = null;
private static Hello hl = null;
    
    private TableTalkClient() {}

    public static void main(String[] args) {

	try {
	    Registry reg = LocateRegistry.getRegistry();
            stub = (TableTalk) reg.lookup("Topic");
            System.out.println("What is the topic for dinner table? \n" + stub.getTopic());	   
            hl = (Hello) reg.lookup("Hello");
            System.out.println("HelloImpl feed back \n" + hl.sayHello("Betty"));	    
	} catch (Exception e) {
	    System.err.println("Client exception thrown: " + e.toString());
	    e.printStackTrace();
	}
       
    }
}

//myrmi.policy

grant {
  Permission java.security.AllPermission;
}; 
 

To run this program, you need to use jdk 1.5.0 version or above. If you use jdk 1.2 - 1.4, you may need to use the command below to generate 2 stub classes for two remote objects before you start rmiregistry command.

rmic -v1.2 TableTalkImpl
rmic -v1.2 HelloImpl

When you execute the above code, you may have the following printout:

 

C:\myrmi>javac -d . TableTalk.java TableTalkImpl.java Migration.java TableTalkSe
tup.java TableTalkClient.java Hello.java HelloImpl.java

C:\myrmi>set classpath=

C:\myrmi>start rmiregistry

C:\myrmi>start rmid -J-Djava.security.policy=myrmi.policy

C:\myrmi>java -Djava.security.policy=myrmi.policy tabletalking.TableTalkSetup
Got the stub for the TableTalkImpl
Got the stub for the HelloImpl
Exported a table-talking object
Exported a HelloImpl object

C:\myrmi>java -Djava.security.policy=myrmi.policy tabletalking.TableTalkClient
What is the topic for dinner table?
What is your favorite fruit or vegetable?
HelloImpl feed back
Hello, Betty

C:\myrmi>

If you have a problem to compile or any comments, please feel free to contact javacamp.org