KickJava   Java API By Example, From Geeks To Geeks.

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


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,v 1.11 2005/03/10 12:21:46 benoitf Exp $
26  * --------------------------------------------------------------------------
27  */

28 package org.objectweb.carol.jndi.ns;
29
30 import java.io.InputStream JavaDoc;
31 import java.util.Properties JavaDoc;
32
33 import javax.naming.Context JavaDoc;
34 import javax.naming.InitialContext JavaDoc;
35
36 import org.omg.CORBA.ORB JavaDoc;
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
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      */

51     private static final int DEFAULT_PORT_NUMBER = 12350;
52
53     /**
54      * Sleep time to wait
55      */

56     private static final int SLEEP_TIME = 2000;
57
58     /**
59      * process of the cosnaming
60      */

61     private Process JavaDoc cosNamingProcess = null;
62
63     /**
64      * Unique instance of the ORB running in the JVM
65      */

66     private static ORB JavaDoc orb = null;
67
68     /**
69      * Default constructor
70      */

71     public IIOPCosNaming() {
72         super(DEFAULT_PORT_NUMBER);
73     }
74
75     /**
76      * start Method, Start a new NameService or do nothing if the name service
77      * is all ready start
78      * @throws NameServiceException if a problem occurs
79      */

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                 // start a new orbd procees
87
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                     // wait for starting
92
Thread.sleep(SLEEP_TIME);
93
94                     // trace the start execution
95
InputStream JavaDoc 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 JavaDoc(b));
101                     }
102
103                     InputStream JavaDoc 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 JavaDoc(b));
111                         }
112                     }
113
114                     // add a shudown hook for this process
115
Runtime.getRuntime().addShutdownHook(new Thread JavaDoc() {
116
117                         public void run() {
118                             try {
119                                 IIOPCosNaming.this.stop();
120                             } catch (Exception JavaDoc 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 JavaDoc 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     /**
142      * stop Method, Stop a NameService or do nothing if the name service is all
143      * ready stop
144      * @throws NameServiceException if a problem occure
145      */

146     public void stop() throws NameServiceException {
147         if (TraceCarol.isDebugJndiCarol()) {
148             TraceCarol.debugJndiCarol("IIOPCosNaming.stop()");
149         }
150         try {
151             // stop orbd procees
152
if (cosNamingProcess != null) {
153                 cosNamingProcess.destroy();
154             }
155             cosNamingProcess = null;
156         } catch (Exception JavaDoc e) {
157             throw new NameServiceException("can not stop cosnaming daemon: " + e);
158         }
159     }
160
161     /**
162      * isStarted Method, check if a name service is started
163      * @return boolean true if the name service is started
164      */

165     public boolean isStarted() {
166         if (cosNamingProcess != null) {
167             return true;
168         }
169         Properties JavaDoc prop = new Properties JavaDoc();
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 JavaDoc(prop);
181         } catch (javax.naming.NamingException JavaDoc ex) {
182             return false;
183         }
184         return true;
185     }
186
187     /**
188      * @return the orb.
189      */

190     public static ORB JavaDoc getOrb() {
191         if (orb == null) {
192             initORB();
193         }
194         return orb;
195     }
196
197     /**
198      * Initialize the ORB
199      * @return
200      */

201     private static void initORB() {
202         orb = ORB.init(new String JavaDoc[0], null);
203     }
204
205 }
206
Popular Tags