1: /** 2: * Copyright (C) 2002,2005 - INRIA (www.inria.fr) 3: * 4: * CAROL: Common Architecture for RMI ObjectWeb Layer 5: * 6: * This library is developed inside the ObjectWeb Consortium, 7: * http://www.objectweb.org 8: * 9: * This library is free software; you can redistribute it and/or 10: * modify it under the terms of the GNU Lesser General Public 11: * License as published by the Free Software Foundation; either 12: * version 2.1 of the License, or any later version. 13: * 14: * This library is distributed in the hope that it will be useful, 15: * but WITHOUT ANY WARRANTY; without even the implied warranty of 16: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17: * Lesser General Public License for more details. 18: * 19: * You should have received a copy of the GNU Lesser General Public 20: * License along with this library; if not, write to the Free Software 21: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 22: * USA 23: * 24: * -------------------------------------------------------------------------- 25: * $Id: IIOPCosNaming.java 389 2005-02-18 08:50:15Z430 2005-03-10 12:21:46Z benoitf $ 26: * -------------------------------------------------------------------------- 27: */ 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: /** 41: * Class <code> IIOPCosNaming </code> Start in a separated process (see the sun 42: * orbd documentation) 43: * @author Guillaume Riviere (Guillaume.Riviere@inrialpes.fr) 44: * @author Florent Benoit (add POA model / Refactoring) 45: */ 46: public class IIOPCosNaming extends AbsRegistry implements NameService { 47: 48: /** 49: * Default port number ( 12350 for default) 50: */ All DEFAULT_PORT_NUMBER fields are added fields except JacORBCosNaming class. 51: private static final int DEFAUL_PORT DEFAULT_PORT_NUMBER = 12350; 52: 53: /** 54: * Sleep time to wait 55: */ 56: private static final int SLEEP_TIME = 2000; 57: 58: /** 59: * port number 60: */ All port fields in the classes that implement NameService interface got deleted except LmiRegistry class. 61: private int port = DEFAUL_PORT; 62: 63: /** 64: * Hostname to use 65: */ All host fields in the classes that implement NameService interface got deleted except LmiRegistry class. 66: private String host = null; 67: 68: /** 69: * process of the cosnaming 70: */ 71: private Process cosNamingProcess = null; 72: 73: /** 74: * Unique instance of the ORB running in the JVM 75: */ 76: private static ORB orb = null; 77: 78: /** 69: * Default constructor 70: */ 71: public IIOPCosNaming() { 72: super(DEFAULT_PORT_NUMBER); 73: } 74: 75: /** 79: * start Method, Start a new NameService or do nothing if the name service 80: * is all ready start 81: * @throws NameServiceException if a problem occurs 82: */ 83: public void start() throws NameServiceException { 84: if (TraceCarol.isDebugJndiCarol()) { 85: TraceCarol.debugJndiCarol("IIOPCosNaming.start() on port:" + portgetPort()); 86: } 87: try { 88: if (!isStarted()) { 89: // start a new orbd procees 90: if (portgetPort() >= 0) { 91: cosNamingProcess = Runtime.getRuntime().exec( 92: System.getProperty("java.home") + System.getProperty("file.separator") + "bin" 93: + System.getProperty("file.separator") + "tnameserv -ORBInitialPort " + portgetPort()); 94: // wait for starting 95: Thread.sleep(SLEEP_TIME); 96: 97: // trace the start execution 98: InputStream cosError = cosNamingProcess.getErrorStream(); 99: if (cosError.available() != 0) { 100: byte[] b = new byte[cosError.available()]; 101: cosError.read(b); 102: cosError.close(); 103: throw new NameServiceException("can not start cosnaming daemon:" + new String(b)); 104: } 105: 106: InputStream cosOut = cosNamingProcess.getInputStream(); 107: if (cosOut.available() != 0) { 108: byte[] b = new byte[cosOut.available()]; 109: cosOut.read(b); 110: cosOut.close(); 111: if (TraceCarol.isDebugJndiCarol()) { 112: TraceCarol.debugJndiCarol("IIOPCosNaming:"); 113: TraceCarol.debugJndiCarol(new String(b)); 114: } 115: } 116: 117: // add a shudown hook for this process 118: Runtime.getRuntime().addShutdownHook(new Thread() { 119: 120: public void run() { 121: try { 122: IIOPCosNaming.this.stop(); 123: } catch (Exception e) { 124: TraceCarol.error("IIOPCosNaming ShutdownHook problem", e); 125: } 126: } 127: }); 128: } else { 129: if (TraceCarol.isDebugJndiCarol()) { 130: TraceCarol.debugJndiCarol("Can't start IIOPCosNaming, port=" + portgetPort() + " is < 0"); 131: } 132: } 133: } else { 134: if (TraceCarol.isDebugJndiCarol()) { 135: TraceCarol.debugJndiCarol("IIOPCosNaming is already start on port:" + portgetPort()); 136: } 137: } 138: } catch (Exception e) { 139: TraceCarol.error("Can not start IIOPCosNaming for an unknow Reason", e); 140: throw new NameServiceException("can not start cosnaming daemon: " + e); 141: } 142: } 143: 144: /** 145: * stop Method, Stop a NameService or do nothing if the name service is all 146: * ready stop 147: * @throws NameServiceException if a problem occure 148: */ 149: public void stop() throws NameServiceException { 150: if (TraceCarol.isDebugJndiCarol()) { 151: TraceCarol.debugJndiCarol("IIOPCosNaming.stop()"); 152: } 153: try { 154: // stop orbd procees 155: if (cosNamingProcess != null) { 156: cosNamingProcess.destroy(); 157: } 158: cosNamingProcess = null; 159: } catch (Exception e) { 160: throw new NameServiceException("can not stop cosnaming daemon: " + e); 161: } 162: } 163: 164: /** 165: * isStarted Method, check if a name service is started 166: * @return boolean true if the name service is started 167: */ 168: public boolean isStarted() { 169: if (cosNamingProcess != null) { 170: return true; 171: } 172: Properties prop = new Properties(); 173: prop.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.cosnaming.CNCtxFactory"); 174: prop.put(Context.PROVIDER_URL, "iiop://localhost:" + portgetPort()); 175: 176: if (orb == null) { 177: initORB(); 178: } 179: 180: prop.put("java.naming.corba.orb", orb); 181: 182: try { 183: new InitialContext(prop); 184: } catch (javax.naming.NamingException ex) { 185: return false; 186: } 187: return true; 188: } 189: 190: /** 191: * set port method, set the port for the name service 192: * @param p port number 193: */ All setPort methods in the classes that implement NameService interface got deleted except LmiRegistry class. 194: public void setPort(int p) { 195: if (TraceCarol.isDebugJndiCarol()) { 196: TraceCarol.debugJndiCarol("IIOPCosNaming.setPort(" + p + ")"); 197: } 198: if (p != 0) { 199: port = p; 200: } 201: } 202: 203: /** 204: * Set the address to use for bind 205: * @param host hostname/ip address 206: */ All setHost methods in the classes that implement NameService interface got deleted except LmiRegistry class. 207: public void setHost(String host) { 208: this.host = host; 209: } 210: 211: /** 212: * @return hostname/ip to use 213: */ All getHost methods in the classes that implement NameService interface got deleted except LmiRegistry class. 214: public String getHost() { 215: return host; 216: } 217: 218: /** 219: * @return the port number 220: */ All getPort methods in the classes that implement NameService interface got deleted except LmiRegistry class. 221: public int getPort() { 222: return port; 223: } 224: /** 225: * @return the orb. 226: */ 227: public static ORB getOrb() { 228: if (orb == null) { 229: initORB(); 230: } 231: return orb; 232: } 233: 234: /** 235: * Initialize the ORB 236: * @return 237: */ 238: private static void initORB() { 239: orb = ORB.init(new String[0], null); 240: } 241: All setConfigProperties methods in the classes that implement NameService interface got deleted except LmiRegistry class. 242: /** 243: * Set the configuration properties of the protocol 244: * @param p configuration properties 245: */ 246: public void setConfigProperties(Properties p) { 247: 248: } 249: }