KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
18  * A superclass for any HTTP servlet that wishes to act as an RMI server.
19  * RemoteHttpServlet begins listening for RMI calls in its
20  * <tt>init()</tt> method and stops listening in its <tt>destroy()</tt>
21  * method. To register itself it uses the registry on the local machine
22  * on the port determined by <tt>getRegistryPort()</tt>. It registers
23  * under the name determined by <tt>getRegistryName()</tt>.
24  *
25  * @see com.oreilly.servlet.RemoteDaemonHttpServlet
26  *
27  * @author <b>Jason Hunter</b>, Copyright &#169; 1998
28  * @version 1.0, 98/09/18
29  */

30 public abstract class RemoteHttpServlet extends HttpServlet
31                                         implements Remote {
32   /**
33    * The registry for the servlet
34    */

35   protected Registry registry;
36
37   /**
38    * Begins the servlet's RMI operations. Causes the servlet to export
39    * itself and then bind itself to the registry. Logs any errors.
40    * Subclasses that override this method must be sure to first call
41    * <tt>super.init(config)</tt>.
42    *
43    * @param config the servlet config
44    * @exception ServletException if a servlet exception occurs
45    */

46   public void init(ServletConfig config) throws ServletException {
47     super.init(config);
48     try {
49       UnicastRemoteObject.exportObject(this);
50       bind();
51     }
52     catch (RemoteException e) {
53       log("Problem binding to RMI registry: " + e.getMessage());
54     }
55   }
56
57   /**
58    * Halts the servlet's RMI operations. Causes the servlet to
59    * unbind itself from the registry. Logs any errors. Subclasses that
60    * override this method must be sure to first call <tt>super.destroy()</tt>.
61    */

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

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

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

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

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