KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > jndi > ns > JacORBCosNaming


1 /**
2  * Copyright (C) 2004-2005 - Bull S.A.
3  *
4  * CAROL: Common Architecture for RMI ObjectWeb Layer
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JacORBCosNaming.java,v 1.12 2005/04/11 13:39:40 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.carol.jndi.ns;
26
27 import java.io.BufferedReader JavaDoc;
28 import java.io.File JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.io.InputStreamReader JavaDoc;
31 import java.net.InetAddress JavaDoc;
32 import java.net.UnknownHostException JavaDoc;
33 import java.util.Properties JavaDoc;
34
35 import javax.naming.Context JavaDoc;
36 import javax.naming.InitialContext JavaDoc;
37
38 import org.omg.CORBA.ORB JavaDoc;
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 /**
45  * Allow to start the nameservice of JacORB within Carol
46  * @author Florent Benoit
47  */

48 public class JacORBCosNaming extends AbsRegistry implements NameService {
49
50     /**
51      * JacORB nameserver
52      */

53     private static final String JavaDoc JACORB_NAMESERVER_CLASS = "org.jacorb.naming.NameServer";
54
55     /**
56      * Sleep time to wait
57      */

58     private static final int SLEEP_TIME = 2000;
59
60     /**
61      * Default port
62      */

63     private static final int DEFAULT_PORT_NUMBER = 38693;
64
65     /**
66      * process of JacORB
67      */

68     private Process JavaDoc jacORBNameServerProcess = null;
69
70     /**
71      * ORB instance (should be unique in the JVM)
72      */

73     private static ORB JavaDoc orb = null;
74
75     /**
76      * Default constructor
77      */

78     public JacORBCosNaming() {
79         super(DEFAULT_PORT_NUMBER);
80     }
81
82     /**
83      * Start a new NameService or do nothing if the name service is already
84      * started
85      * @throws NameServiceException if a problem occurs
86      */

87     public void start() throws NameServiceException {
88
89         // Don't start again
90
if (isStarted()) {
91             throw new IllegalStateException JavaDoc("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 JavaDoc ipAddr = null;
97         String JavaDoc hostCorbaLoc = CarolDefaultValues.DEFAULT_HOST;
98         // Ip of the host is not the default host (localhost)
99
if (!getHost().equalsIgnoreCase(CarolDefaultValues.DEFAULT_HOST)) {
100             try {
101                 ipAddr = InetAddress.getByName(getHost()).getHostAddress();
102                 // Set the ip which was set in carol.properties (or if
103
// localhost, listen on all interfaces).
104
System.setProperty("OAIAddr", ipAddr);
105             } catch (UnknownHostException JavaDoc 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         // Fix iiop port if running inside a server
115
if (System.getProperty(CarolDefaultValues.SERVER_MODE, "false").equalsIgnoreCase("true")) {
116             if (getConfigProperties() != null) {
117                 String JavaDoc 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         // Set SSL Port
130
if (System.getProperty(CarolDefaultValues.SERVER_MODE, "false").equalsIgnoreCase("true")) {
131             if (getConfigProperties() != null) {
132                 String JavaDoc 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                 // start the registry
148
String JavaDoc 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                 // Launch JVM
166
jacORBNameServerProcess = Runtime.getRuntime().exec(
167                         System.getProperty("java.home") + File.separator + "bin" + File.separator + "java "
168                                 + jvmProperties + " " + JACORB_NAMESERVER_CLASS);
169                 // wait for starting
170
Thread.sleep(SLEEP_TIME);
171
172                 // trace the start execution
173
InputStream JavaDoc cosError = jacORBNameServerProcess.getErrorStream();
174                 InputStream JavaDoc cosOut = jacORBNameServerProcess.getInputStream();
175                 Thread JavaDoc err = new Thread JavaDoc(new CosReader(cosError, true));
176                 Thread JavaDoc out = new Thread JavaDoc(new CosReader(cosOut, false));
177                 out.start();
178                 err.start();
179
180                 // add a shudown hook for this process
181
Runtime.getRuntime().addShutdownHook(new Thread JavaDoc() {
182
183                     public void run() {
184                         try {
185                             if (JacORBCosNaming.this.isStarted()) {
186                                 JacORBCosNaming.this.stop();
187                             }
188                         } catch (Exception JavaDoc 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 JavaDoc 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     /**
206      * Stop a NameService or do nothing if the name service is already stopped
207      * @throws NameServiceException if a problem occurs
208      */

209     public void stop() throws NameServiceException {
210         if (!isStarted()) {
211             throw new IllegalStateException JavaDoc("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 JavaDoc 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     /**
227      * Check if a remote NS was started before
228      * @return true if a remote NS was started
229      */

230     private boolean isRemoteNameServiceStarted() {
231
232         Properties JavaDoc prop = new Properties JavaDoc();
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 JavaDoc(prop);
245         } catch (javax.naming.CommunicationException JavaDoc jcm) {
246             return false;
247         } catch (org.omg.CORBA.TRANSIENT JavaDoc ct) {
248             return false;
249         } catch (Exception JavaDoc e) {
250             return true;
251         }
252         return true;
253     }
254
255     /**
256      * @return the orb.
257      */

258     public static ORB JavaDoc getOrb() {
259         if (orb == null) {
260             initORB();
261         }
262         return orb;
263     }
264
265     /**
266      * Initialize the ORB
267      * @return
268      */

269     private static void initORB() {
270         orb = ORB.init(new String JavaDoc[0], null);
271     }
272
273     /**
274      * Allow to trace errors/output of a process
275      */

276     class CosReader implements Runnable JavaDoc {
277
278         /**
279          * Input stream containing information
280          */

281         private InputStream JavaDoc is;
282
283         /**
284          * Should send as error or debug message ?
285          */

286         private boolean isErrorMessage = false;
287
288         /**
289          * Constructor
290          * @param is given input stream
291          * @param isErrorMessage Should send as error or debug message
292          */

293         public CosReader(InputStream JavaDoc is, boolean isErrorMessage) {
294             this.is = is;
295             this.isErrorMessage = isErrorMessage;
296         }
297
298         /**
299          * Thread execution printing information received
300          */

301         public void run() {
302             try {
303                 BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is));
304                 String JavaDoc 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                 // close input stream
319
is.close();
320             } catch (Exception JavaDoc e) {
321                 TraceCarol.error(e.getMessage());
322                 e.printStackTrace();
323             }
324         }
325     }
326
327 }
Popular Tags