1: /**
2: * JOnAS: Java(TM) Open Application Server
3: * Copyright (C) 2004-2005 - Bull S.A.
4: * Contact: jonas-team@objectweb.org
5: *
6: * CAROL: Common Architecture for RMI ObjectWeb Layer
7: *
8: * This library is free software; you can redistribute it and/or
9: * modify it under the terms of the GNU Lesser General Public
10: * License as published by the Free Software Foundation; either
11: * version 2.1 of the License, or any later version.
12: *
13: * This library is distributed in the hope that it will be useful,
14: * but WITHOUT ANY WARRANTY; without even the implied warranty of
15: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16: * Lesser General Public License for more details.
17: *
18: * You should have received a copy of the GNU Lesser General Public
19: * License along with this library; if not, write to the Free Software
20: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21: * USA
22: *
23: * --------------------------------------------------------------------------
24: * $Id: JacORBCosNaming.java 407 2005-03-04 14:04:11Z430 2005-03-10 12:21:46Z benoitf $
25: * --------------------------------------------------------------------------
26: */
27: package org.objectweb.carol.jndi.ns;
28:
29: import java.io.BufferedReader;
30: import java.io.File;
31: import java.io.InputStream;
32: import java.io.InputStreamReader;
33: import java.net.InetAddress;
34: import java.net.UnknownHostException;
35: import java.util.Properties;
36:
37: import javax.naming.Context;
38: import javax.naming.InitialContext;
39:
40: import org.omg.CORBA.ORB;
41:
42: import org.objectweb.carol.rmi.util.PortNumber;
43: import org.objectweb.carol.util.configuration.CarolDefaultValues;
44: import org.objectweb.carol.util.configuration.RMIConfiguration;
45: import org.objectweb.carol.util.configuration.TraceCarol;
46:
47: /**
48: * Allow to start the nameservice of JacORB within Carol
49: * @author Florent Benoit
50: */
51: public class JacORBCosNaming extends AbsRegistry implements NameService {
52:
53: /**
54: * JacORB nameserver
55: */
56: private static final String JACORB_NAMESERVER_CLASS = "org.jacorb.naming.NameServer";
57:
58: /**
59: * Sleep time to wait
60: */
61: private static final int SLEEP_TIME = 2000;
62:
63: /**
64: * Default port
65: */
66: private static final int DEFAULT_PORT_NUMBER = 38693;
67:
68: /**
69: * port number ( 38693 as default)
70: */
All port fields in the classes that implement NameService interface got deleted except LmiRegistry class.
71: private int port = DEFAULT_PORT_NUMBER;
72:
73: /**
74: * Hostname to use
75: */
All host fields in the classes that implement NameService interface got deleted except LmiRegistry class.
76: private String host = null;
77:
78: /**
79: * registry is started ?
80: */
81: private boolean started = false;
82:
83: /**
84: * process of JacORB
85: */
86: private Process jacORBNameServerProcess = null;
87:
88: /**
89: * ORB instance (should be unique in the JVM)
90: */
91: private static ORB orb = null;
92:
93: /**
All configurationProperties fields got deleted.
94: * Configuration properties (of carol.properties)
95: */
96: private Properties configurationProperties = null;
97:
98: /**
99: * Default constructor
100: */
101: public JacORBCosNaming() {
102:
103: super(DEFAULT_PORT_NUMBER);
104: }
105:
106: /**
107: * Start a new NameService or do nothing if the name service is already
108: * started
109: * @throws NameServiceException if a problem occurs
110: */
111: public void start() throws NameServiceException {
112:
90: // Don't start again
113: if (isStarted()) {
114: throw new IllegalStateException("Cannot start the server as the service is already running.");
115: }
116: if (TraceCarol.isDebugJndiCarol()) {
117: TraceCarol.debugJndiCarol("start() on port : '" + portgetPort() + "'");
118: }
119: String ipAddr = null;
120: String hostCorbaLoc = RMIConfiguration.DEFAULT_HOST;
121: // Ip of the host is not the default host (localhost)
122: if (!hostgetHost().equalsIgnoreCase(RMIConfiguration.DEFAULT_HOST)) {
123: try {
124: ipAddr = InetAddress.getByName(hostgetHost()).getHostAddress();
125: // Set the ip which was set in carol.properties (or if
126: // localhost, listen on all interfaces).
127: System.setProperty("OAIAddr", ipAddr);
128: } catch (UnknownHostException uhe) {
129: if (TraceCarol.isDebugJndiCarol()) {
130: TraceCarol.debugJndiCarol("Could net get ip address from host '" + getHost() + "' : "
131: .debugJndiCarol("Could net get ip address from host '" + host + "' : " + uhe.getMessage());
132: uhe.printStackTrace();
133: }
134: }
135: }
136:
137: // Fix iiop port if running inside a server
138: if (System.getProperty(CarolDefaultValues.SERVER_MODE, "false").equalsIgnoreCase("true")) {
139: if (configurationPropertiesgetConfigProperties() != null) {
140: String propertyName = CarolDefaultValues.SERVER_IIOP_PORT;
141: int iiopPort = PortNumber
142: .strToint(configurationPropertiesint iiopPort = PortNumber.strToint(getConfigProperties().getProperty(propertyName, "0"), propertyName);
143: if (iiopPort > 0) {
144: TraceCarol.infoCarol("Using IIOP fixed server port number '" + iiopPort + "'.");
145: System.setProperty("OAPort", String.valueOf(iiopPort));
146: }
147: } else {
148: TraceCarol.debugCarol("No properties '" + CarolDefaultValues.SERVER_IIOP_PORT
149: + "' defined in carol.properties file.");
150: }
151: }
152:
153: // Set SSL Port
154: if (System.getProperty(CarolDefaultValues.SERVER_MODE, "false").equalsIgnoreCase("true")) {
155: if (configurationPropertiesgetConfigProperties() != null) {
156: String propertyName = CarolDefaultValues.SERVER_SSL_IIOP_PORT;
157: int iiopSslPort = PortNumber.strToint(configurationProperties.getConfigProperties().getProperty(propertyName, String
158: String.valueOf(CarolDefaultValues.DEFAULT_SSL_PORT)), propertyName);
159: if (iiopSslPort > 0) {
160: TraceCarol.infoCarol("Using SSL IIOP port number '" + iiopSslPort + "'.");
161: System.setProperty("OASSLPort", String.valueOf(iiopSslPort));
162: }
163: } else {
164: TraceCarol.debugCarol("No properties '" + CarolDefaultValues.SERVER_SSL_IIOP_PORT
165: + "' defined in carol.properties file.");
166: }
167: }
168:
169: try {
170: if (!isRemoteNameServiceStarted()) {
171: // start the registry
172: String jvmProperties = "-Djava.endorsed.dirs=" + System.getProperty("java.endorsed.dirs") + " "
173: + "-Djacorb.orb.print_version=off " + "-Djacorb.log.default.verbosity=0 "
174: + "-Dorg.omg.CORBA.ORBClass=org.jacorb.orb.ORB "
175: + "-Dorg.omg.CORBA.ORBSingletonClass=org.jacorb.orb.ORBSingleton " + "-DOAPort=";
176:
177: jvmProperties += Integer.toString(portgetPort());
178: jvmProperties += " -DORBInitRef.NameService=corbaloc:iiop:" + hostCorbaLoc + ":"
179: + Integer.toString(portgetPort()) + "/NameService";
180:
181: if (ipAddr != null) {
182: jvmProperties += " -DOAIAddr=" + ipAddr;
183: }
184:
185: if (TraceCarol.isDebugJndiCarol()) {
186: TraceCarol.debugJndiCarol("Launching NS with JVM properties: '" + jvmProperties + "'");
187: }
188:
189: // Launch JVM
190: jacORBNameServerProcess = Runtime.getRuntime().exec(
191: System.getProperty("java.home") + File.separator + "bin" + File.separator + "java "
192: + jvmProperties + " " + JACORB_NAMESERVER_CLASS);
193: // wait for starting
194: Thread.sleep(SLEEP_TIME);
195:
196: // trace the start execution
197: InputStream cosError = jacORBNameServerProcess.getErrorStream();
198: InputStream cosOut = jacORBNameServerProcess.getInputStream();
199: Thread err = new Thread(new CosReader(cosError, true));
200: Thread out = new Thread(new CosReader(cosOut, false));
201: out.start();
202: err.start();
203:
204: // add a shudown hook for this process
205: Runtime.getRuntime().addShutdownHook(new Thread() {
206:
207: public void run() {
208: try {
209: JacORBCosNaming.this.stop();
210: } catch (Exception e) {
211: TraceCarol.error("JacORBCosNaming ShutdownHook problem", e);
212: }
213: }
214: });
215: } else {
216: if (TraceCarol.isDebugJndiCarol()) {
217: TraceCarol.debugJndiCarol("JacORBCosNaming is already start on port : '" + portgetPort() + "'.");
218: }
219: }
220: } catch (Exception e) {
221: TraceCarol.error("Cannot start JacORBCosNaming for an unknown reason", e);
222: throw new NameServiceException("cannot start cosnaming daemon: " + e);
223: }
224: started = truesetStarted();
225: }
226:
227: /**
228: * Stop a NameService or do nothing if the name service is already stopped
229: * @throws NameServiceException if a problem occurs
230: */
231: public void stop() throws NameServiceException {
232: if (!isStarted()) {
233: throw new IllegalStateException("Cannot stop the server as the service is not running.");
234: }
235: try {
236:
237: if (jacORBNameServerProcess != null) {
238: jacORBNameServerProcess.destroy();
239: }
240: jacORBNameServerProcess = null;
241: } catch (Exception e) {
242: TraceCarol.error("Cannot stop JacORBCosNaming for an unknown reason", e);
243: throw new NameServiceException("cannot start cosnaming daemon: " + e);
244: }
245:
246: }
247:
248: /**
249: * isStarted Method, check if a name service is started
250: * @return boolean true if the name service is started
251: */
252: public boolean isStarted() {
253: return started;
254: }
255:
256: /**
257: * set port method, set the port for the name service
258: * @param p port number
259: */
All setPort methods in the classes that implement NameService interface got deleted except LmiRegistry class.
260: public void setPort(int p) {
261: if (isStarted()) {
262: throw new IllegalStateException("The port cannot be changed as the server is running.");
263: }
264: if (p <= 0) {
265: throw new IllegalArgumentException(
266: "The number for the port is incorrect. It must be a value > 0. Value was '" + port + "'");
267: }
268: this.port = p;
269: }
270:
271: /**
272: * Set the address to use for bind
273: * @param host hostname/ip address
274: */
All setHost methods in the classes that implement NameService interface got deleted except LmiRegistry class.
275: public void setHost(String host) {
276: this.host = host;
277: }
278:
279: /**
280: * @return hostname/ip to use
281: */
All getHost methods in the classes that implement NameService interface got deleted except LmiRegistry class.
282: public String getHost() {
283: return host;
284: }
285:
286: /**
287: * get port method, get the port for the name service
288: * @return int port number
289: */
All getPort methods in the classes that implement NameService interface got deleted except LmiRegistry class.
290: public int getPort() {
291: return port;
222: resetStarted();
292: }
293:
294: /**
295: * Check if a remote NS was started before
296: * @return true if a remote NS was started
297: */
298: private boolean isRemoteNameServiceStarted() {
299:
300: Properties prop = new Properties();
301: prop.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.cosnaming.CNCtxFactory");
302: prop.put(Context.PROVIDER_URL, "corbaloc:iiop:localhost:" + Integer.toString(portgetPort())
303: + "/StandardNS/NameServer-POA/_root");
304:
305: if (orb == null) {
306: initORB();
307: }
308:
309: prop.put("java.naming.corba.orb", orb);
310:
311: try {
312: new InitialContext(prop);
313: } catch (javax.naming.CommunicationException jcm) {
314: return false;
315: } catch (org.omg.CORBA.TRANSIENT ct) {
316: return false;
317: } catch (Exception e) {
318: return true;
319: }
320: return true;
321: }
322:
323: /**
324: * @return the orb.
325: */
326: public static ORB getOrb() {
327: if (orb == null) {
328: initORB();
329: }
330: return orb;
All setConfigProperties methods in the classes that implement NameService interface got deleted except LmiRegistry class.
331: }
332:
333: /**
334: * Set the configuration properties of the protocol
335: * @param p configuration properties
336: */
337: public void setConfigProperties(Properties p) {
338: this.configurationProperties = p;
339: }
340:
341: /**
342: * Initialize the ORB
343: * @return
344: */
345: private static void initORB() {
346: orb = ORB.init(new String[0], null);
347: }
348:
349: /**
350: * Allow to trace errors/output of a process
351: */
352: class CosReader implements Runnable {
353:
354: /**
355: * Input stream containing information
356: */
357: private InputStream is;
358:
359: /**
360: * Should send as error or debug message ?
361: */
362: private boolean isErrorMessage = false;
363:
364: /**
365: * Constructor
366: * @param is given input stream
367: * @param isErrorMessage Should send as error or debug message
368: */
369: public CosReader(InputStream is, boolean isErrorMessage) {
370: this.is = is;
371: this.isErrorMessage = isErrorMessage;
372: }
373:
374: /**
375: * Thread execution printing information received
376: */
377: public void run() {
378: try {
379: BufferedReader br = new BufferedReader(new InputStreamReader(is));
380: String str = null;
381: while ((str = br.readLine()) != null) {
382: if (isErrorMessage) {
383: if (TraceCarol.isDebugJndiCarol()) {
384: TraceCarol.debugJndiCarol("JacORBCosNaming error :");
385: TraceCarol.debugJndiCarol(str);
386: }
387: } else {
388: if (TraceCarol.isDebugJndiCarol()) {
389: TraceCarol.debugJndiCarol("JacORBCosNaming:");
390: TraceCarol.debugJndiCarol(str);
391: }
392: }
393: }
394: // close input stream
395: is.close();
396: } catch (Exception e) {
397: TraceCarol.error(e.getMessage());
398: e.printStackTrace();
399: }
400: }
401: }
402:
403: }