KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > oreilly > servlet > RemoteDaemonHttpServlet


1 // Copyright (C) 1998-2001 by Jason Hunter <jhunter_AT_acm_DOT_org>.
2
// All rights reserved. Use of this class is limited.
3
// Please see the LICENSE for more information.
4

5 package com.oreilly.servlet;
6
7 import java.io.*;
8 import java.net.*;
9 import java.rmi.*;
10 import java.rmi.server.*;
11 import java.rmi.registry.*;
12 import java.util.*;
13 import javax.servlet.*;
14 import javax.servlet.http.*;
15
16 /**
17  * A superclass for any HTTP servlet that wishes to act as an RMI server
18  * and, additionally, accept raw socket connections. Includes the
19  * functionality from both RemoteHttpServlet and DaemonHttpServlet, by
20  * extending DaemonHttpServlet and re-implementing RemoteHttpServlet.
21  *
22  * @see com.oreilly.servlet.RemoteHttpServlet
23  * @see com.oreilly.servlet.DaemonHttpServlet
24  *
25  * @author <b>Jason Hunter</b>, Copyright &#169; 1998
26  * @version 1.0, 98/09/18
27  */

28 public abstract class RemoteDaemonHttpServlet extends DaemonHttpServlet
29                                               implements Remote {
30   /**
31    * The registry for the servlet
32    */

33   protected Registry registry;
34
35   /**
36    * Begins the servlet's RMI operations and begins a thread listening for
37    * socket connections.
38    * Subclasses that override this method must be sure to first call
39    * <tt>super.init(config)</tt>.
40    *
41    * @param config the servlet config
42    * @exception ServletException if a servlet exception occurs
43    */

44   public void init(ServletConfig config) throws ServletException {
45     super.init(config);
46     try {
47       UnicastRemoteObject.exportObject(this);
48       bind();
49     }
50     catch (RemoteException e) {
51       log("Problem binding to RMI registry: " + e.getMessage());
52     }
53   }
54
55   /**
56    * Halts the servlet's RMI operations and halts the thread listening for
57    * socket connections. Subclasses that
58    * override this method must be sure to first call <tt>super.destroy()</tt>.
59    */

60   public void destroy() {
61     super.destroy();
62     unbind();
63   }
64
65   /**
66    * Returns the name under which the servlet should be bound in the
67    * registry. By default the name is the servlet's class name. This
68    * can be overridden with the <tt>registryName</tt> init parameter.
69    *
70    * @return the name under which the servlet should be bound in the registry
71    */

72   protected String JavaDoc getRegistryName() {
73     // First name choice is the "registryName" init parameter
74
String JavaDoc name = getInitParameter("registryName");
75     if (name != null) return name;
76
77     // Fallback choice is the name of this class
78
return this.getClass().getName();
79   }
80
81   /**
82    * Returns the port where the registry should be running. By default
83    * the port is the default registry port (1099). This can be
84    * overridden with the <tt>registryPort</tt> init parameter.
85    *
86    * @return the port for the registry
87    */

88   protected int getRegistryPort() {
89     // First port choice is the "registryPort" init parameter
90
try { return Integer.parseInt(getInitParameter("registryPort")); }
91
92     // Fallback choice is the default registry port (1099)
93
catch (NumberFormatException JavaDoc e) { return Registry.REGISTRY_PORT; }
94   }
95
96   /**
97    * Binds the servlet to the registry. Creates the registry if necessary.
98    * Logs any errors.
99    */

100   protected void bind() {
101     // Try to find the appropriate registry already running
102
try {
103       registry = LocateRegistry.getRegistry(getRegistryPort());
104       registry.list(); // Verify it's alive and well
105
}
106     catch (Exception JavaDoc e) {
107       // Couldn't get a valid registry
108
registry = null;
109     }
110
111     // If we couldn't find it, we need to create it.
112
// (Equivalent to running "rmiregistry")
113
if (registry == null) {
114       try {
115         registry = LocateRegistry.createRegistry(getRegistryPort());
116       }
117       catch (Exception JavaDoc e) {
118         log("Could not get or create RMI registry on port " +
119             getRegistryPort() + ": " + e.getMessage());
120         return;
121       }
122     }
123
124     // If we get here, we must have a valid registry.
125
// Now register this servlet instance with that registry.
126
try {
127       registry.rebind(getRegistryName(), this);
128     }
129     catch (Exception JavaDoc e) {
130       log("humbug Could not bind to RMI registry: " + e.getMessage());
131       return;
132     }
133   }
134
135   /**
136    * Unbinds the servlet from the registry.
137    * Logs any errors.
138    */

139   protected void unbind() {
140     try {
141       if (registry != null) registry.unbind(getRegistryName());
142     }
143     catch (Exception JavaDoc e) {
144       log("Problem unbinding from RMI registry: " + e.getMessage());
145     }
146   }
147
148 }
149
Popular Tags