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 28 public abstract class RemoteDaemonHttpServlet extends DaemonHttpServlet 29 implements Remote { 30 33 protected Registry registry; 34 35 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 60 public void destroy() { 61 super.destroy(); 62 unbind(); 63 } 64 65 72 protected String getRegistryName() { 73 String name = getInitParameter("registryName"); 75 if (name != null) return name; 76 77 return this.getClass().getName(); 79 } 80 81 88 protected int getRegistryPort() { 89 try { return Integer.parseInt(getInitParameter("registryPort")); } 91 92 catch (NumberFormatException e) { return Registry.REGISTRY_PORT; } 94 } 95 96 100 protected void bind() { 101 try { 103 registry = LocateRegistry.getRegistry(getRegistryPort()); 104 registry.list(); } 106 catch (Exception e) { 107 registry = null; 109 } 110 111 if (registry == null) { 114 try { 115 registry = LocateRegistry.createRegistry(getRegistryPort()); 116 } 117 catch (Exception e) { 118 log("Could not get or create RMI registry on port " + 119 getRegistryPort() + ": " + e.getMessage()); 120 return; 121 } 122 } 123 124 try { 127 registry.rebind(getRegistryName(), this); 128 } 129 catch (Exception e) { 130 log("humbug Could not bind to RMI registry: " + e.getMessage()); 131 return; 132 } 133 } 134 135 139 protected void unbind() { 140 try { 141 if (registry != null) registry.unbind(getRegistryName()); 142 } 143 catch (Exception e) { 144 log("Problem unbinding from RMI registry: " + e.getMessage()); 145 } 146 } 147 148 } 149 | Popular Tags |