1 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 30 public abstract class RemoteHttpServlet extends HttpServlet 31 implements Remote { 32 35 protected Registry registry; 36 37 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 62 public void destroy() { 63 unbind(); 64 } 65 66 73 protected String getRegistryName() { 74 String name = getInitParameter("registryName"); 76 if (name != null) return name; 77 78 return this.getClass().getName(); 80 } 81 82 89 protected int getRegistryPort() { 90 try { return Integer.parseInt(getInitParameter("registryPort")); } 92 93 catch (NumberFormatException e) { return Registry.REGISTRY_PORT; } 95 } 96 97 101 protected void bind() { 102 try { 104 registry = LocateRegistry.getRegistry(getRegistryPort()); 105 registry.list(); } 107 catch (Exception e) { 108 registry = null; 110 } 111 112 if (registry == null) { 115 try { 116 registry = LocateRegistry.createRegistry(getRegistryPort()); 117 } 118 catch (Exception e) { 119 log("Could not get or create RMI registry on port " + 120 getRegistryPort() + ": " + e.getMessage()); 121 return; 122 } 123 } 124 125 try { 128 registry.rebind(getRegistryName(), this); 129 } 130 catch (Exception e) { 131 log("Could not bind to RMI registry: " + e.getMessage()); 132 return; 133 } 134 } 135 136 140 protected void unbind() { 141 try { 142 if (registry != null) registry.unbind(getRegistryName()); 143 } 144 catch (Exception e) { 145 log("Problem unbinding from RMI registry: " + e.getMessage()); 146 } 147 } 148 } 149 | Popular Tags |