1 25 package org.objectweb.carol.jndi.ns; 26 27 import java.io.BufferedReader ; 28 import java.io.File ; 29 import java.io.InputStream ; 30 import java.io.InputStreamReader ; 31 import java.net.InetAddress ; 32 import java.net.UnknownHostException ; 33 import java.util.Properties ; 34 35 import javax.naming.Context ; 36 import javax.naming.InitialContext ; 37 38 import org.omg.CORBA.ORB ; 39 40 import org.objectweb.carol.rmi.util.PortNumber; 41 import org.objectweb.carol.util.configuration.CarolDefaultValues; 42 import org.objectweb.carol.util.configuration.TraceCarol; 43 44 48 public class JacORBCosNaming extends AbsRegistry implements NameService { 49 50 53 private static final String JACORB_NAMESERVER_CLASS = "org.jacorb.naming.NameServer"; 54 55 58 private static final int SLEEP_TIME = 2000; 59 60 63 private static final int DEFAULT_PORT_NUMBER = 38693; 64 65 68 private Process jacORBNameServerProcess = null; 69 70 73 private static ORB orb = null; 74 75 78 public JacORBCosNaming() { 79 super(DEFAULT_PORT_NUMBER); 80 } 81 82 87 public void start() throws NameServiceException { 88 89 if (isStarted()) { 91 throw new IllegalStateException ("Cannot start the server as the service is already running."); 92 } 93 if (TraceCarol.isDebugJndiCarol()) { 94 TraceCarol.debugJndiCarol("start() on port : '" + getPort() + "'"); 95 } 96 String ipAddr = null; 97 String hostCorbaLoc = CarolDefaultValues.DEFAULT_HOST; 98 if (!getHost().equalsIgnoreCase(CarolDefaultValues.DEFAULT_HOST)) { 100 try { 101 ipAddr = InetAddress.getByName(getHost()).getHostAddress(); 102 System.setProperty("OAIAddr", ipAddr); 105 } catch (UnknownHostException uhe) { 106 if (TraceCarol.isDebugJndiCarol()) { 107 TraceCarol.debugJndiCarol("Could net get ip address from host '" + getHost() + "' : " 108 + uhe.getMessage()); 109 uhe.printStackTrace(); 110 } 111 } 112 } 113 114 if (System.getProperty(CarolDefaultValues.SERVER_MODE, "false").equalsIgnoreCase("true")) { 116 if (getConfigProperties() != null) { 117 String propertyName = CarolDefaultValues.SERVER_IIOP_PORT; 118 int iiopPort = PortNumber.strToint(getConfigProperties().getProperty(propertyName, "0"), propertyName); 119 if (iiopPort > 0) { 120 TraceCarol.infoCarol("Using IIOP fixed server port number '" + iiopPort + "'."); 121 System.setProperty("OAPort", String.valueOf(iiopPort)); 122 } 123 } else { 124 TraceCarol.debugCarol("No properties '" + CarolDefaultValues.SERVER_IIOP_PORT 125 + "' defined in carol.properties file."); 126 } 127 } 128 129 if (System.getProperty(CarolDefaultValues.SERVER_MODE, "false").equalsIgnoreCase("true")) { 131 if (getConfigProperties() != null) { 132 String propertyName = CarolDefaultValues.SERVER_SSL_IIOP_PORT; 133 int iiopSslPort = PortNumber.strToint(getConfigProperties().getProperty(propertyName, 134 String.valueOf(CarolDefaultValues.DEFAULT_SSL_PORT)), propertyName); 135 if (iiopSslPort > 0) { 136 TraceCarol.debugCarol("Using SSL IIOP port number '" + iiopSslPort + "'."); 137 System.setProperty("OASSLPort", String.valueOf(iiopSslPort)); 138 } 139 } else { 140 TraceCarol.debugCarol("No properties '" + CarolDefaultValues.SERVER_SSL_IIOP_PORT 141 + "' defined in carol.properties file."); 142 } 143 } 144 145 try { 146 if (!isRemoteNameServiceStarted()) { 147 String jvmProperties = "-Djava.endorsed.dirs=" + System.getProperty("java.endorsed.dirs") + " " 149 + "-Djacorb.orb.print_version=off " + "-Djacorb.log.default.verbosity=0 " 150 + "-Dorg.omg.CORBA.ORBClass=org.jacorb.orb.ORB " 151 + "-Dorg.omg.CORBA.ORBSingletonClass=org.jacorb.orb.ORBSingleton " + "-DOAPort="; 152 153 jvmProperties += Integer.toString(getPort()); 154 jvmProperties += " -DORBInitRef.NameService=corbaloc:iiop:" + hostCorbaLoc + ":" 155 + Integer.toString(getPort()) + "/NameService"; 156 157 if (ipAddr != null) { 158 jvmProperties += " -DOAIAddr=" + ipAddr; 159 } 160 161 if (TraceCarol.isDebugJndiCarol()) { 162 TraceCarol.debugJndiCarol("Launching NS with JVM properties: '" + jvmProperties + "'"); 163 } 164 165 jacORBNameServerProcess = Runtime.getRuntime().exec( 167 System.getProperty("java.home") + File.separator + "bin" + File.separator + "java " 168 + jvmProperties + " " + JACORB_NAMESERVER_CLASS); 169 Thread.sleep(SLEEP_TIME); 171 172 InputStream cosError = jacORBNameServerProcess.getErrorStream(); 174 InputStream cosOut = jacORBNameServerProcess.getInputStream(); 175 Thread err = new Thread (new CosReader(cosError, true)); 176 Thread out = new Thread (new CosReader(cosOut, false)); 177 out.start(); 178 err.start(); 179 180 Runtime.getRuntime().addShutdownHook(new Thread () { 182 183 public void run() { 184 try { 185 if (JacORBCosNaming.this.isStarted()) { 186 JacORBCosNaming.this.stop(); 187 } 188 } catch (Exception e) { 189 TraceCarol.error("JacORBCosNaming ShutdownHook problem", e); 190 } 191 } 192 }); 193 } else { 194 if (TraceCarol.isDebugJndiCarol()) { 195 TraceCarol.debugJndiCarol("JacORBCosNaming is already start on port : '" + getPort() + "'."); 196 } 197 } 198 } catch (Exception e) { 199 TraceCarol.error("Cannot start JacORBCosNaming for an unknown reason", e); 200 throw new NameServiceException("cannot start cosnaming daemon: " + e); 201 } 202 setStarted(); 203 } 204 205 209 public void stop() throws NameServiceException { 210 if (!isStarted()) { 211 throw new IllegalStateException ("Cannot stop the server as the service is not running."); 212 } 213 try { 214 215 if (jacORBNameServerProcess != null) { 216 jacORBNameServerProcess.destroy(); 217 } 218 jacORBNameServerProcess = null; 219 } catch (Exception e) { 220 TraceCarol.error("Cannot stop JacORBCosNaming for an unknown reason", e); 221 throw new NameServiceException("cannot start cosnaming daemon: " + e); 222 } 223 resetStarted(); 224 } 225 226 230 private boolean isRemoteNameServiceStarted() { 231 232 Properties prop = new Properties (); 233 prop.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.cosnaming.CNCtxFactory"); 234 prop.put(Context.PROVIDER_URL, "corbaloc:iiop:localhost:" + Integer.toString(getPort()) 235 + "/StandardNS/NameServer-POA/_root"); 236 237 if (orb == null) { 238 initORB(); 239 } 240 241 prop.put("java.naming.corba.orb", orb); 242 243 try { 244 new InitialContext (prop); 245 } catch (javax.naming.CommunicationException jcm) { 246 return false; 247 } catch (org.omg.CORBA.TRANSIENT ct) { 248 return false; 249 } catch (Exception e) { 250 return true; 251 } 252 return true; 253 } 254 255 258 public static ORB getOrb() { 259 if (orb == null) { 260 initORB(); 261 } 262 return orb; 263 } 264 265 269 private static void initORB() { 270 orb = ORB.init(new String [0], null); 271 } 272 273 276 class CosReader implements Runnable { 277 278 281 private InputStream is; 282 283 286 private boolean isErrorMessage = false; 287 288 293 public CosReader(InputStream is, boolean isErrorMessage) { 294 this.is = is; 295 this.isErrorMessage = isErrorMessage; 296 } 297 298 301 public void run() { 302 try { 303 BufferedReader br = new BufferedReader (new InputStreamReader (is)); 304 String str = null; 305 while ((str = br.readLine()) != null) { 306 if (isErrorMessage) { 307 if (TraceCarol.isDebugJndiCarol()) { 308 TraceCarol.debugJndiCarol("JacORBCosNaming error :"); 309 TraceCarol.debugJndiCarol(str); 310 } 311 } else { 312 if (TraceCarol.isDebugJndiCarol()) { 313 TraceCarol.debugJndiCarol("JacORBCosNaming:"); 314 TraceCarol.debugJndiCarol(str); 315 } 316 } 317 } 318 is.close(); 320 } catch (Exception e) { 321 TraceCarol.error(e.getMessage()); 322 e.printStackTrace(); 323 } 324 } 325 } 326 327 } | Popular Tags |