1 28 package org.objectweb.carol.jndi.registry; 29 30 import java.rmi.AccessException ; 31 import java.rmi.AlreadyBoundException ; 32 import java.rmi.NotBoundException ; 33 import java.rmi.Remote ; 34 import java.rmi.RemoteException ; 35 import java.rmi.registry.Registry ; 36 import java.rmi.server.RMIClientSocketFactory ; 37 import java.rmi.server.RMIServerSocketFactory ; 38 import java.rmi.server.RMISocketFactory ; 39 import java.rmi.server.ServerNotActiveException ; 40 import java.util.Enumeration ; 41 import java.util.Hashtable ; 42 43 import sun.rmi.registry.RegistryImpl; 44 45 49 public class ManageableRegistry extends RegistryImpl { 50 51 54 private static final int INITIAL_CAPACITY = 101; 55 56 59 private Hashtable registryObjects = new Hashtable (INITIAL_CAPACITY); 60 61 64 private static boolean verbose = false; 65 66 73 private ManageableRegistry(int port, RMIClientSocketFactory csf, RMIServerSocketFactory ssf) throws RemoteException { 74 super(port, csf, ssf); 75 } 76 77 82 private ManageableRegistry(int port) throws RemoteException { 83 this(port, RMISocketFactory.getSocketFactory(), RMISocketFactory.getSocketFactory()); 84 } 85 86 90 public void setVerbose(boolean v) { 91 System.out.println("RegistryManager.setVerbose(" + v + ")"); 92 verbose = v; 93 } 94 95 102 public Remote lookup(String name) throws RemoteException , NotBoundException { 103 if (verbose) { 104 try { 105 System.out.println("ManageableRegistry.lookup(" + name + ") from client: " + getClientHost()); 106 } catch (ServerNotActiveException e) { 107 e.printStackTrace(); 108 } 109 } 110 synchronized (registryObjects) { 111 Remote obj = (Remote ) registryObjects.get(name); 112 if (obj == null) { 113 throw new NotBoundException (name); 114 } 115 return obj; 116 } 117 } 118 119 129 public void bind(String name, Remote obj) throws RemoteException , AlreadyBoundException , AccessException { 130 if (verbose) { 131 try { 132 System.out.println("ManageableRegistry.bind(" + name + ", obj)" + " from client: " + getClientHost()); 133 } catch (ServerNotActiveException e) { 134 e.printStackTrace(); 135 } 136 } 137 synchronized (registryObjects) { 138 Remote curr = (Remote ) registryObjects.get(name); 139 if (curr != null) { 140 throw new AlreadyBoundException (name); 141 } 142 registryObjects.put(name, obj); 143 } 144 } 145 146 155 public void unbind(String name) throws RemoteException , NotBoundException , AccessException { 156 if (verbose) { 157 try { 158 System.out.println("ManageableRegistry.unbind(" + name + ")" + " from client: " + getClientHost()); 159 } catch (ServerNotActiveException e) { 160 e.printStackTrace(); 161 } 162 } 163 synchronized (registryObjects) { 164 Remote obj = (Remote ) registryObjects.get(name); 165 if (obj == null) { 166 throw new NotBoundException (name); 167 } 168 registryObjects.remove(name); 169 } 170 } 171 172 181 public void rebind(String name, Remote obj) throws RemoteException , AccessException { 182 if (verbose) { 183 try { 184 System.out.println("ManageableRegistry.rebind(" + name + ", obj)" + " from client: " + getClientHost()); 185 } catch (ServerNotActiveException e) { 186 e.printStackTrace(); 187 } 188 } 189 registryObjects.put(name, obj); 190 } 191 192 199 public String [] list() throws RemoteException { 200 201 if (verbose) { 202 try { 203 System.out.println("ManageableRegistry.list()" + " from client: " + getClientHost()); 204 } catch (ServerNotActiveException e) { 205 e.printStackTrace(); 207 } 208 } 209 String [] names; 210 synchronized (registryObjects) { 211 int i = registryObjects.size(); 212 names = new String [i]; 213 Enumeration e = registryObjects.keys(); 214 while ((--i) >= 0) { 215 names[i] = (String ) e.nextElement(); 216 } 217 } 218 return names; 219 } 220 221 228 public static Registry createManagableRegistry(int port, int objectPort) throws RemoteException { 229 if (objectPort > 0) { 231 RMISocketFactory socketFactory = RMIFixedPortFirewallSocketFactory.register(objectPort); 232 return new ManageableRegistry(port, socketFactory, socketFactory); 233 } else { 234 return new ManageableRegistry(port); 235 } 236 } 237 238 241 public void purge() { 242 registryObjects.clear(); 243 } 244 245 249 public static void main(String [] args) { 250 try { 251 int regPort = Registry.REGISTRY_PORT; 252 if (args.length >= 1) { 253 regPort = Integer.parseInt(args[0]); 254 } 255 createManagableRegistry(regPort, 0); 256 System.out.println("ManageableRegistry started on port " + regPort); 257 259 } catch (Exception e) { 260 e.printStackTrace(); 261 System.exit(-1); 262 } 263 } 264 265 } | Popular Tags |