001: // Copyright (C) 1998-2001 by Jason Hunter <jhunter_AT_acm_DOT_org>.
002: // All rights reserved. Use of this class is limited.
003: // Please see the LICENSE for more information.
004:
005: package com.oreilly.servlet;
006:
007: import java.io.*;
008: import java.net.*;
009: import java.rmi.*;
010: import java.rmi.server.*;
011: import java.rmi.registry.*;
012: import java.util.*;
013: import javax.servlet.*;
014: import javax.servlet.http.*;
015:
016: /**
017: * A superclass for any HTTP servlet that wishes to act as an RMI server.
018: * RemoteHttpServlet begins listening for RMI calls in its
019: * <tt>init()</tt> method and stops listening in its <tt>destroy()</tt>
020: * method. To register itself it uses the registry on the local machine
021: * on the port determined by <tt>getRegistryPort()</tt>. It registers
022: * under the name determined by <tt>getRegistryName()</tt>.
023: *
024: * @see com.oreilly.servlet.RemoteDaemonHttpServlet
025: *
026: * @author <b>Jason Hunter</b>, Copyright © 1998
027: * @version 1.0, 98/09/18
028: */
029: public abstract class RemoteHttpServlet extends HttpServlet implements
030: Remote {
031: /**
032: * The registry for the servlet
033: */
034: protected Registry registry;
035:
036: /**
037: * Begins the servlet's RMI operations. Causes the servlet to export
038: * itself and then bind itself to the registry. Logs any errors.
039: * Subclasses that override this method must be sure to first call
040: * <tt>super.init(config)</tt>.
041: *
042: * @param config the servlet config
043: * @exception ServletException if a servlet exception occurs
044: */
045: public void init(ServletConfig config) throws ServletException {
046: super .init(config);
047: try {
048: UnicastRemoteObject.exportObject(this );
049: bind();
050: } catch (RemoteException e) {
051: log("Problem binding to RMI registry: " + e.getMessage());
052: }
053: }
054:
055: /**
056: * Halts the servlet's RMI operations. Causes the servlet to
057: * unbind itself from the registry. Logs any errors. Subclasses that
058: * override this method must be sure to first call <tt>super.destroy()</tt>.
059: */
060: public void destroy() {
061: unbind();
062: }
063:
064: /**
065: * Returns the name under which the servlet should be bound in the
066: * registry. By default the name is the servlet's class name. This
067: * can be overridden with the <tt>registryName</tt> init parameter.
068: *
069: * @return the name under which the servlet should be bound in the registry
070: */
071: protected String getRegistryName() {
072: // First name choice is the "registryName" init parameter
073: String name = getInitParameter("registryName");
074: if (name != null)
075: return name;
076:
077: // Fallback choice is the name of this class
078: return this .getClass().getName();
079: }
080:
081: /**
082: * Returns the port where the registry should be running. By default
083: * the port is the default registry port (1099). This can be
084: * overridden with the <tt>registryPort</tt> init parameter.
085: *
086: * @return the port for the registry
087: */
088: protected int getRegistryPort() {
089: // First port choice is the "registryPort" init parameter
090: try {
091: return Integer.parseInt(getInitParameter("registryPort"));
092: }
093:
094: // Fallback choice is the default registry port (1099)
095: catch (NumberFormatException e) {
096: return Registry.REGISTRY_PORT;
097: }
098: }
099:
100: /**
101: * Binds the servlet to the registry. Creates the registry if necessary.
102: * Logs any errors.
103: */
104: protected void bind() {
105: // Try to find the appropriate registry already running
106: try {
107: registry = LocateRegistry.getRegistry(getRegistryPort());
108: registry.list(); // Verify it's alive and well
109: } catch (Exception e) {
110: // Couldn't get a valid registry
111: registry = null;
112: }
113:
114: // If we couldn't find it, we need to create it.
115: // (Equivalent to running "rmiregistry")
116: if (registry == null) {
117: try {
118: registry = LocateRegistry
119: .createRegistry(getRegistryPort());
120: } catch (Exception e) {
121: log("Could not get or create RMI registry on port "
122: + getRegistryPort() + ": " + e.getMessage());
123: return;
124: }
125: }
126:
127: // If we get here, we must have a valid registry.
128: // Now register this servlet instance with that registry.
129: try {
130: registry.rebind(getRegistryName(), this );
131: } catch (Exception e) {
132: log("Could not bind to RMI registry: " + e.getMessage());
133: return;
134: }
135: }
136:
137: /**
138: * Unbinds the servlet from the registry.
139: * Logs any errors.
140: */
141: protected void unbind() {
142: try {
143: if (registry != null)
144: registry.unbind(getRegistryName());
145: } catch (Exception e) {
146: log("Problem unbinding from RMI registry: "
147: + e.getMessage());
148: }
149: }
150: }
|