Book Home Java Servlet Programming Search this book

13.6. Acting as an RMI Client

In Chapter 10, "Applet-Servlet Communication", we saw how a servlet can act as an RMI server. Here we turn the tables and see a servlet acting as an RMI client. By taking the role of an RMI client, a servlet can leverage the services of other servers to accomplish its task, coordinate its efforts with other servers or servlets on those servers, and/or act as an proxy on behalf of applets that can't communicate with RMI servers themselves.

Example 13-8 shows DaytimeClientServlet, a servlet that gets the current time of day from the DaytimeServlet RMI server shown in Chapter 10, "Applet-Servlet Communication".

Example 13-8. A servlet as an RMI client

import java.io.*;
import java.rmi.*;
import java.rmi.registry.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class DaytimeClientServlet extends HttpServlet {

  DaytimeServer daytime;

  // Returns a reference to a DaytimeServer or null if there was a problem.
  protected DaytimeServer getDaytimeServer() {
    // Set the security manager if it hasn't been done already.
    // Provides protection from a malicious DaytimeServer stub.
    if (System.getSecurityManager() == null) {
      System.setSecurityManager(new RMISecurityManager());
    }

    try {
      Registry registry =
        LocateRegistry.getRegistry(getRegistryHost(), getRegistryPort());
      return (DaytimeServer)registry.lookup(getRegistryName());
    }
    catch (Exception e) {
      getServletContext().log(e, "Problem getting DaytimeServer reference");
      return null;
    }
  }

  private String getRegistryName() {
    String name = getInitParameter("registryName");
    return (name == null ? "DaytimeServlet" : name);
  }

  private String getRegistryHost() {
    // Return either the hostname given by "registryHost" or
    // if no name was given return null to imply localhost 
    return getInitParameter("registryHost");
  }

  private int getRegistryPort() {
    try { return Integer.parseInt(getInitParameter("registryPort")); }
    catch (NumberFormatException e) { return Registry.REGISTRY_PORT; }
  }

  public void doGet(HttpServletRequest req, HttpServletResponse res)
                               throws ServletException, IOException {
    res.setContentType("text/plain");
    PrintWriter out = res.getWriter();

    // Get a daytime object if we haven't before
    if (daytime == null) {
      daytime = getDaytimeServer();
      if (daytime == null) {
        // Couldn't get it, so report we're unavailable.
        throw new UnavailableException(this, "Could not locate daytime");
      }
    }

    // Get and print the current time on the (possibly remote) daytime host
    out.println(daytime.getDate().toString());
  }
}

This servlet should remind you of the applet you saw in Chapter 10, "Applet-Servlet Communication". Both servlets and applets perform the same basic steps to access an RMI server. They both locate a registry using a hostname and port number, then use that registry to look up a reference to the remote object. The only significant difference is that a servlet must first ensure it's running under the protection of a default security manager. Every RMI client has to have a security manager in place to protect itself from hostile remotely loaded stubs. An applet is guaranteed to run under an applet security manager, so this step isn't necessary. A servlet, however, can operate without a default security manager, so before acting as an RMI client it may need to assign one.



Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.