1 28 package org.objectweb.carol.jndi.ns; 29 30 import java.io.InputStream ; 31 import java.util.Properties ; 32 33 import javax.naming.Context ; 34 import javax.naming.InitialContext ; 35 36 import org.omg.CORBA.ORB ; 37 38 import org.objectweb.carol.util.configuration.TraceCarol; 39 40 46 public class IIOPCosNaming extends AbsRegistry implements NameService { 47 48 51 private static final int DEFAULT_PORT_NUMBER = 12350; 52 53 56 private static final int SLEEP_TIME = 2000; 57 58 61 private Process cosNamingProcess = null; 62 63 66 private static ORB orb = null; 67 68 71 public IIOPCosNaming() { 72 super(DEFAULT_PORT_NUMBER); 73 } 74 75 80 public void start() throws NameServiceException { 81 if (TraceCarol.isDebugJndiCarol()) { 82 TraceCarol.debugJndiCarol("IIOPCosNaming.start() on port:" + getPort()); 83 } 84 try { 85 if (!isStarted()) { 86 if (getPort() >= 0) { 88 cosNamingProcess = Runtime.getRuntime().exec( 89 System.getProperty("java.home") + System.getProperty("file.separator") + "bin" 90 + System.getProperty("file.separator") + "tnameserv -ORBInitialPort " + getPort()); 91 Thread.sleep(SLEEP_TIME); 93 94 InputStream cosError = cosNamingProcess.getErrorStream(); 96 if (cosError.available() != 0) { 97 byte[] b = new byte[cosError.available()]; 98 cosError.read(b); 99 cosError.close(); 100 throw new NameServiceException("can not start cosnaming daemon:" + new String (b)); 101 } 102 103 InputStream cosOut = cosNamingProcess.getInputStream(); 104 if (cosOut.available() != 0) { 105 byte[] b = new byte[cosOut.available()]; 106 cosOut.read(b); 107 cosOut.close(); 108 if (TraceCarol.isDebugJndiCarol()) { 109 TraceCarol.debugJndiCarol("IIOPCosNaming:"); 110 TraceCarol.debugJndiCarol(new String (b)); 111 } 112 } 113 114 Runtime.getRuntime().addShutdownHook(new Thread () { 116 117 public void run() { 118 try { 119 IIOPCosNaming.this.stop(); 120 } catch (Exception e) { 121 TraceCarol.error("IIOPCosNaming ShutdownHook problem", e); 122 } 123 } 124 }); 125 } else { 126 if (TraceCarol.isDebugJndiCarol()) { 127 TraceCarol.debugJndiCarol("Can't start IIOPCosNaming, port=" + getPort() + " is < 0"); 128 } 129 } 130 } else { 131 if (TraceCarol.isDebugJndiCarol()) { 132 TraceCarol.debugJndiCarol("IIOPCosNaming is already start on port:" + getPort()); 133 } 134 } 135 } catch (Exception e) { 136 TraceCarol.error("Can not start IIOPCosNaming for an unknow Reason", e); 137 throw new NameServiceException("can not start cosnaming daemon: " + e); 138 } 139 } 140 141 146 public void stop() throws NameServiceException { 147 if (TraceCarol.isDebugJndiCarol()) { 148 TraceCarol.debugJndiCarol("IIOPCosNaming.stop()"); 149 } 150 try { 151 if (cosNamingProcess != null) { 153 cosNamingProcess.destroy(); 154 } 155 cosNamingProcess = null; 156 } catch (Exception e) { 157 throw new NameServiceException("can not stop cosnaming daemon: " + e); 158 } 159 } 160 161 165 public boolean isStarted() { 166 if (cosNamingProcess != null) { 167 return true; 168 } 169 Properties prop = new Properties (); 170 prop.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.cosnaming.CNCtxFactory"); 171 prop.put(Context.PROVIDER_URL, "iiop://localhost:" + getPort()); 172 173 if (orb == null) { 174 initORB(); 175 } 176 177 prop.put("java.naming.corba.orb", orb); 178 179 try { 180 new InitialContext (prop); 181 } catch (javax.naming.NamingException ex) { 182 return false; 183 } 184 return true; 185 } 186 187 190 public static ORB getOrb() { 191 if (orb == null) { 192 initORB(); 193 } 194 return orb; 195 } 196 197 201 private static void initORB() { 202 orb = ORB.init(new String [0], null); 203 } 204 205 } 206 | Popular Tags |