1 package org.objectweb.carol.jndi.ns; 2 3 import java.rmi.RemoteException ; 4 import java.rmi.registry.LocateRegistry ; 5 import java.rmi.registry.Registry ; 6 import java.rmi.server.UnicastRemoteObject ; 7 8 import org.objectweb.carol.jndi.registry.ManageableRegistry; 9 import org.objectweb.carol.jndi.registry.RMIFixedPortFirewallSocketFactory; 10 import org.objectweb.carol.rmi.util.PortNumber; 11 import org.objectweb.carol.util.configuration.CarolDefaultValues; 12 import org.objectweb.carol.util.configuration.TraceCarol; 13 14 19 20 public class IRMIRegistry extends AbsRegistry implements NameService { 21 22 25 private static final int DEFAULT_PORT_NUMBER = 1098; 26 27 30 private static int objectPort = 0; 31 32 35 private static Registry registry = null; 36 37 40 public IRMIRegistry() { 41 super(DEFAULT_PORT_NUMBER); 42 } 43 44 49 public void start() throws NameServiceException { 50 if (TraceCarol.isDebugJndiCarol()) { 51 TraceCarol.debugJndiCarol("IRMIRegistry.start() on port:" + getPort()); 52 } 53 try { 54 if (System.getProperty(CarolDefaultValues.SERVER_MODE, "false").equalsIgnoreCase("true")) { 56 if (getConfigProperties() != null) { 57 String propertyName = CarolDefaultValues.SERVER_IRMI_PORT; 58 objectPort = PortNumber.strToint(getConfigProperties().getProperty(propertyName, "0"), 59 propertyName); 60 } else { 61 TraceCarol.debugCarol("No properties '" + CarolDefaultValues.SERVER_IRMI_PORT 62 + "' defined in carol.properties file."); 63 } 64 } 65 if (objectPort > 0) { 66 RMIFixedPortFirewallSocketFactory.register(objectPort); 67 } 68 69 70 if (!isStarted()) { 71 72 if (objectPort > 0) { 73 TraceCarol.infoCarol("Using IRMI fixed server port number '" + objectPort + "'."); 74 } 75 76 if (getPort() >= 0) { 77 registry = ManageableRegistry.createManagableRegistry(getPort(), objectPort); 78 Runtime.getRuntime().addShutdownHook(new Thread () { 80 81 public void run() { 82 try { 83 IRMIRegistry.this.stop(); 84 } catch (Exception e) { 85 TraceCarol.error("IRMIRegistry ShutdownHook problem", e); 86 } 87 } 88 }); 89 } else { 90 if (TraceCarol.isDebugJndiCarol()) { 91 TraceCarol.debugJndiCarol("Can't start IRMIRegistry, port=" + getPort() + " is < 0"); 92 } 93 } 94 } else { 95 if (TraceCarol.isDebugJndiCarol()) { 96 TraceCarol.debugJndiCarol("IRMIRegistry is already start on port:" + getPort()); 97 } 98 } 99 } catch (Exception e) { 100 throw new NameServiceException("can not start rmi registry: " + e); 101 } 102 } 103 104 109 public void stop() throws NameServiceException { 110 if (TraceCarol.isDebugJndiCarol()) { 111 TraceCarol.debugJndiCarol("IRMIRegistry.stop()"); 112 } 113 try { 114 if (registry != null) { 115 UnicastRemoteObject.unexportObject(registry, true); 116 } 117 registry = null; 118 } catch (Exception e) { 119 throw new NameServiceException("can not stop rmi registry: " + e); 120 } 121 } 122 123 127 public static boolean isLocal() { 128 return (registry != null); 129 } 130 131 135 public boolean isStarted() { 136 if (registry != null) { 137 return true; 138 } 139 try { 140 LocateRegistry.getRegistry(getPort()).list(); 141 } catch (RemoteException re) { 142 return false; 143 } 144 return true; 145 } 146 147 150 public static Registry getRegistry() { 151 return registry; 152 } 153 154 } 155 | Popular Tags |