1 28 package org.objectweb.carol.jndi.ns; 29 30 import java.rmi.RemoteException ; 31 import java.rmi.registry.LocateRegistry ; 32 import java.rmi.registry.Registry ; 33 import java.rmi.server.UnicastRemoteObject ; 34 35 import org.objectweb.carol.jndi.registry.ManageableRegistry; 36 import org.objectweb.carol.jndi.registry.RMIFixedPortFirewallSocketFactory; 37 import org.objectweb.carol.rmi.util.PortNumber; 38 import org.objectweb.carol.util.configuration.CarolDefaultValues; 39 import org.objectweb.carol.util.configuration.TraceCarol; 40 41 45 public class JRMPRegistry extends AbsRegistry implements NameService { 46 47 50 private static final int DEFAULT_PORT_NUMBER = 1099; 51 52 55 private static int objectPort = 0; 56 57 60 private static Registry registry = null; 61 62 65 public JRMPRegistry() { 66 super(DEFAULT_PORT_NUMBER); 67 } 68 69 74 public void start() throws NameServiceException { 75 if (TraceCarol.isDebugJndiCarol()) { 76 TraceCarol.debugJndiCarol("JRMPRegistry.start() on port:" + getPort()); 77 } 78 try { 79 if (System.getProperty(CarolDefaultValues.SERVER_MODE, "false").equalsIgnoreCase("true")) { 81 if (getConfigProperties() != null) { 82 String propertyName = CarolDefaultValues.SERVER_JRMP_PORT; 83 objectPort = PortNumber.strToint(getConfigProperties().getProperty(propertyName, "0"), 84 propertyName); 85 } else { 86 TraceCarol.debugCarol("No properties '" + CarolDefaultValues.SERVER_JRMP_PORT 87 + "' defined in carol.properties file."); 88 } 89 } 90 if (objectPort > 0) { 91 RMIFixedPortFirewallSocketFactory.register(objectPort); 92 } 93 94 95 if (!isStarted()) { 96 97 if (objectPort > 0) { 98 TraceCarol.infoCarol("Using JRMP fixed server port number '" + objectPort + "'."); 99 } 100 101 if (getPort() >= 0) { 102 registry = ManageableRegistry.createManagableRegistry(getPort(), objectPort); 103 Runtime.getRuntime().addShutdownHook(new Thread () { 105 106 public void run() { 107 try { 108 JRMPRegistry.this.stop(); 109 } catch (Exception e) { 110 TraceCarol.error("JRMPRegistry ShutdownHook problem", e); 111 } 112 } 113 }); 114 } else { 115 if (TraceCarol.isDebugJndiCarol()) { 116 TraceCarol.debugJndiCarol("Can't start JRMPRegistry, port=" + getPort() + " is < 0"); 117 } 118 } 119 } else { 120 if (TraceCarol.isDebugJndiCarol()) { 121 TraceCarol.debugJndiCarol("JRMPRegistry is already start on port:" + getPort()); 122 } 123 } 124 } catch (Exception e) { 125 throw new NameServiceException("can not start rmi registry: " + e); 126 } 127 } 128 129 134 public void stop() throws NameServiceException { 135 if (TraceCarol.isDebugJndiCarol()) { 136 TraceCarol.debugJndiCarol("JRMPRegistry.stop()"); 137 } 138 try { 139 if (registry != null) { 140 UnicastRemoteObject.unexportObject(registry, true); 141 } 142 registry = null; 143 } catch (Exception e) { 144 throw new NameServiceException("can not stop rmi registry: " + e); 145 } 146 } 147 148 152 public static boolean isLocal() { 153 return (registry != null); 154 } 155 156 160 public boolean isStarted() { 161 if (registry != null) { 162 return true; 163 } 164 try { 165 LocateRegistry.getRegistry(getPort()).list(); 166 } catch (RemoteException re) { 167 return false; 168 } 169 return true; 170 } 171 172 175 public static Registry getRegistry() { 176 return registry; 177 } 178 } | Popular Tags |