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: * and, additionally, accept raw socket connections. Includes the
019: * functionality from both RemoteHttpServlet and DaemonHttpServlet, by
020: * extending DaemonHttpServlet and re-implementing RemoteHttpServlet.
021: *
022: * @see com.oreilly.servlet.RemoteHttpServlet
023: * @see com.oreilly.servlet.DaemonHttpServlet
024: *
025: * @author <b>Jason Hunter</b>, Copyright © 1998
026: * @version 1.0, 98/09/18
027: */
028: public abstract class RemoteDaemonHttpServlet extends DaemonHttpServlet
029: implements Remote {
030: /**
031: * The registry for the servlet
032: */
033: protected Registry registry;
034:
035: /**
036: * Begins the servlet's RMI operations and begins a thread listening for
037: * socket connections.
038: * Subclasses that override this method must be sure to first call
039: * <tt>super.init(config)</tt>.
040: *
041: * @param config the servlet config
042: * @exception ServletException if a servlet exception occurs
043: */
044: public void init(ServletConfig config) throws ServletException {
045: super .init(config);
046: try {
047: UnicastRemoteObject.exportObject(this );
048: bind();
049: } catch (RemoteException e) {
050: log("Problem binding to RMI registry: " + e.getMessage());
051: }
052: }
053:
054: /**
055: * Halts the servlet's RMI operations and halts the thread listening for
056: * socket connections. Subclasses that
057: * override this method must be sure to first call <tt>super.destroy()</tt>.
058: */
059: public void destroy() {
060: super .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("humbug Could not bind to RMI registry: "
133: + e.getMessage());
134: return;
135: }
136: }
137:
138: /**
139: * Unbinds the servlet from the registry.
140: * Logs any errors.
141: */
142: protected void unbind() {
143: try {
144: if (registry != null)
145: registry.unbind(getRegistryName());
146: } catch (Exception e) {
147: log("Problem unbinding from RMI registry: "
148: + e.getMessage());
149: }
150: }
151:
152: }
|