1 28 package org.objectweb.carol.jndi.ns; 29 30 import java.util.Enumeration ; 31 import java.util.Hashtable ; 32 33 import org.objectweb.carol.util.configuration.ConfigurationRepository; 34 import org.objectweb.carol.util.configuration.Protocol; 35 import org.objectweb.carol.util.configuration.ProtocolConfiguration; 36 import org.objectweb.carol.util.configuration.TraceCarol; 37 38 43 public class NameServiceManager { 44 45 48 private static final int SLEEP_VALUE = 10000; 49 50 53 private static Hashtable nsTable; 54 55 58 private static NameServiceManager current = new NameServiceManager(); 59 60 63 private NameServiceManager() { 64 if (TraceCarol.isDebugJndiCarol()) { 65 TraceCarol.debugJndiCarol("NameServiceManager.NameServiceManager()"); 66 } 67 68 nsTable = new Hashtable (); 70 ProtocolConfiguration[] protocolConfigurations = ConfigurationRepository.getConfigurations(); 71 for (int c = 0; c < protocolConfigurations.length; c++) { 72 ProtocolConfiguration protocolConfiguration = protocolConfigurations[c]; 73 String configurationName = protocolConfiguration.getName(); 74 Protocol protocol = protocolConfiguration.getProtocol(); 75 NameService nsC = null; 76 try { 77 nsC = (NameService) Class.forName(protocol.getRegistryClassName()).newInstance(); 78 } catch (Exception e) { 79 String msg = "Cannot instantiate registry class '" + protocol.getRegistryClassName() + "'"; 80 TraceCarol.error(msg, e); 81 } 82 nsC.setPort(protocolConfiguration.getPort()); 83 nsC.setHost(protocolConfiguration.getHost()); 84 nsC.setConfigProperties(protocolConfiguration.getProperties()); 85 nsTable.put(configurationName, nsC); 87 } 88 89 } 90 91 95 public static NameServiceManager getNSManagerCurrent() { 96 if (TraceCarol.isDebugJndiCarol()) { 97 TraceCarol.debugJndiCarol("NameServiceManager.getNSManagerCurrent()"); 98 } 99 return current; 100 } 101 102 106 public static void startNS() throws NameServiceException { 107 if (TraceCarol.isDebugJndiCarol()) { 108 TraceCarol.debugJndiCarol("NameServiceManager.startNS()"); 109 } 110 for (Enumeration e = nsTable.keys(); e.hasMoreElements();) { 112 String k = (String ) e.nextElement(); 113 NameService currentNS = (NameService) nsTable.get(k); 114 if (currentNS.isStarted()) { 115 throw new NameServiceException("The " + k + " name service is already started"); 116 } 117 } 118 startNonStartedNS(); 120 } 121 122 125 public static void startNonStartedNS() { 126 if (TraceCarol.isDebugJndiCarol()) { 127 TraceCarol.debugJndiCarol("NameServiceManager.startNonStartedNS()"); 128 } 129 for (Enumeration e = nsTable.keys(); e.hasMoreElements();) { 131 String k = (String ) e.nextElement(); 132 NameService currentNS = (NameService) nsTable.get(k); 133 134 try { 135 currentNS.start(); 136 if (TraceCarol.isInfoCarol()) { 137 TraceCarol.infoCarol("Name service for " + k + " is started on port " + currentNS.getPort()); 138 } 139 } catch (NameServiceException nse) { 140 if (TraceCarol.isDebugJndiCarol()) { 142 TraceCarol 143 .debugJndiCarol("NameServiceManager.startNonStartedNS() can not start name service: " + k); 144 } 145 } 146 } 147 } 148 149 153 public static void stopNS() throws NameServiceException { 154 if (TraceCarol.isDebugJndiCarol()) { 155 TraceCarol.debugJndiCarol("NameServiceManager.stopNS()"); 156 } 157 for (Enumeration e = nsTable.keys(); e.hasMoreElements();) { 159 String k = (String ) e.nextElement(); 160 NameService currentNS = (NameService) nsTable.get(k); 161 currentNS.stop(); 162 } 163 } 164 165 169 public static void main(String [] args) { 170 171 TraceCarol.configure(); 173 174 try { 175 NameServiceManager.startNonStartedNS(); 176 Runtime.getRuntime().addShutdownHook(new Thread () { 178 179 public void run() { 180 try { 181 NameServiceManager.stopNS(); 182 } catch (Exception e) { 183 TraceCarol.error("Carol Naming ShutdownHook problem", e); 184 } 185 } 186 }); 187 while (true) { 188 Thread.sleep(SLEEP_VALUE); 189 } 190 } catch (Exception e) { 191 e.printStackTrace(); 192 } 193 } 194 } | Popular Tags |