Java Reference
In-Depth Information
Next, we have the server main() method. It first sets a security manager.
The security manager controls what the VM is allowed to do. A number of
default security managers are provided, and here we use one that is designed
specifically to give safe and reasonable defaults for RMI applications. You can,
of course, write your own security manager. Security managers use “policy
specifications” to alter their capabilities. For now, we will explain enough to
run a simple example. See Section 5.8.4.2 for more information on policies for
our example.
Remember that main() is static, so there is no instance of SessionImpl
yet, and thus also no instance of Session . We declare a variable of type
Session , and set it to a new instance of SessionImpl . (There is no need
to typecast here because SessionImpl implements Session , therefore
SessionImpl is, among other things, a Session .) We now have an instance
of the server class.
Next, the server must make itself available to the world. It does this by
registering itself with the RMI registry (see Section 5.8.3). This is done through
a static method of the java.rmi.Naming class, rebind() . Put simply, this
maps a remote object to a string name in the registry. When clients contact the
registry looking for a name then, if a remote object is mapped to that name,
the communication can take place (yes, we are simplifying at the moment).
The call to rebind() does not return. The server is up and running.
Finally, we have the implementation of our remote method, add() .
This looks like a lot of hassle to go through, and it is, but consider writing
an interface that offers, for example, methods like getDirContents() ,
chDir() , downloadFile() , uploadFile() . You've just written something
like an FTP server. No matter how many methods you add to your interface,
the complexity of the setup code does not increase. Maybe now it looks a little
more useful?
5.8.1.5
At this point, Example 5.12 should be fairly obvious. Our class has just a single
static method, main() . It, like our server side main() , sets up a security man-
ager. It then contacts a registry on the machine named penfold looking for an
instance of a remote interface named Session (again, lookup() is a static
method of the java.rmi.Naming class). We store that reference in a variable
of type Session called sess . We can then call the add() on sess . We'll show
the server and client running shortly.
Writing the Client Class
Search WWH ::




Custom Search