Java Reference
In-Depth Information
* no server is running, a {@code null} will be returned.
* @param fromHost name of the host on which the
* {@link StatRecorder} is running
* @return a remote reference to the {@link StatRecorder},
* or {@code null} if there is no such server or if it cannot
* be contacted.
*/
private StatRecorder getRecorder(String fromHost) {
if (System.getSecurityManager() == null) {
System.setSecurityManager(new SecurityManager());
}
try {
Registry useRegistry = LocateRegistry.getRegistry(fromHost);
return ((StatRecorder) useRegistry.lookup("Recorder"));
} catch (Exception e) {
System.out.println("Unable to find StatRecorder");
e.printStackTrace();
return null;
}
}
}
The two public methods in this class allow us to find the roster for any team from a remote
server and send a BoxScore object to a remote server to report the results of a game. These
methods look just like regular Java code, in which we call methods and handle the exceptions
that might be thrown by those methods. That is the idea behind all remote procedure call sys-
tems—to make a remote call look as much as possible like a local call, while still making it
clear that they are different. We know that these calls aren't really local, since they might fail
because of network problems. That's why in our implementation of the StatReporterImpl
we have to handle the RemoteException that both methods are declared as possibly throw-
ing. Our handling of the exception is not all that sophisticated; we might have chosen to have
our getRecord() and reportGame() methods also throw RemoteException to their callers,
pushing the handling of the exception to a higher level in the code.
Binding to the StatRecorder server is handled by the private getRecorder() method. This
method takes a string as an argument, which should be the network name of the host on which
the StatRecorder has registered. Note that with the current default implementation of the
RMI registry, this will also be the host on which the StatRecorder server itself is running,
as only objects running on a particular host are allowed to add entries to the registry on that
host. The first thing this method does is make sure that there is a security manager running
in the JVM; because we need to download the proxy object for the StatRecorder server, a
Search WWH ::




Custom Search