jini.core.lookup.ServiceItem;
import java.io.IOException;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
import net.jini.lookup.JoinManager;
import net.jini.lookup.ServiceIDListener;
public class AddingService {
public AddingService() throws IOException {
setUpSecurityManager();
setUpJoinManager();
}
private void setUpSecurityManager(){
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
}
private void setUpJoinManager(){
try{
JoinManager joinManager =
new JoinManager(new AdderProxy(), null,
(ServiceIDListener)null, null, null);
} catch (IOException e) {}
}
public static void main(String args[]) {
try {
new AddingService();
Thread.sleep(5000);
} catch (IOException e) {
e.printStackTrace();
498
Chapter 20: Why Dream of Jini?
} catch (InterruptedException e){
e.printStackTrace();
}
}
}
Similarly, here's SubtractingService:
package ProxyCalculator;
import net.jini.core.lookup.ServiceItem;
import java.io.IOException;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
import net.jini.lookup.JoinManager;
import net.jini.lookup.ServiceIDListener;
public class SubtractingService {
public SubtractingService() throws IOException {
setUpSecurityManager();
setUpJoinManager();
}
private void setUpSecurityManager(){
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
}
private void setUpJoinManager(){
try{
JoinManager joinManager =
new JoinManager(new SubtractorProxy(), null,
(ServiceIDListener)null, null, null);
} catch (IOException e) {}
}
public static void main(String args[]) {
try {
new SubtractingService();
Thread.sleep(5000);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e){
e.printStackTrace();
}
}
}
At this point you can skip down to the sections on compiling and running the application before getting the client up and running. You can run your adding and subtracting services and see that you can view them in the lookup browser. You won't be able to see much, but you can verify that they are there, and then you can see them go away.
499
Chapter 20: Why Dream of Jini?
The Jini Calculator client
You can separate the functionality that uses the Calculator from the parts of the client that register with the lookup service to find the Calculator, as you did on the service side. This separation made practical sense on the service side because you were delivering the proxy object to the client via the lookup service. On the client side more needs to be done, but it can all be done by a single object.
As in the case of the service, you've got to start by ensuring that a java.rmi.SecurityManager has been set up.
Before you perform the calculation, you have quite a bit of setup work to do. This shouldn't be surprising if you consider what you're trying to accomplish. You want to perform a calculation using an object that you don't have. Not only don't you have it, you don't really know where it is. Not only that, but you don't really know where anyone is who knows where the service might be.
As a first step, you set up a net.jini.lookup.ServiceDiscoveryManager as follows: private void setUpServiceDiscoveryManager() throws
RemoteException, IOException{
serviceDiscoveryManager =
new ServiceDiscoveryManager(null,null);
}
Later in the chapter, you'll expand on this basic setup to include caching. For now, as a next step, you're going to ask the ServiceDiscoveryManager that you've just constructed to find you the service you're looking for.
Before you do that, you must somehow describe the service you are looking for. You do this using a net.jini.core.lookup.ServiceTemplate, which you'll set up as follows: private void setUpServiceTemplate(){
Class[] types = new Class[] { Calculator.class };
serviceTemplate = new ServiceTemplate(null, types, null);
}