KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > corba > TheORBSingleton


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2004 INRIA & USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Philippe Merle.
23 Contributor(s): Areski Flissi, Christophe Demarey.
24
25 ====================================================================*/

26
27 package org.objectweb.openccm.corba;
28
29 // Package dependencies
30
import java.net.InetAddress JavaDoc;
31 import java.net.DatagramPacket JavaDoc;
32 import java.net.DatagramSocket JavaDoc;
33 import java.net.MulticastSocket JavaDoc;
34 import java.net.SocketException JavaDoc;
35 import java.net.UnknownHostException JavaDoc;
36
37 import java.io.IOException JavaDoc;
38
39 /**
40  * This class encapsulates the CORBA::ORB singleton object.
41  *
42  * @author <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>
43  *
44  * @version 0.3
45  */

46
47 public abstract class TheORBSingleton
48 {
49     // ==================================================================
50
//
51
// Internal states.
52
//
53
// ==================================================================
54

55     /**
56      * To store the CORBA::ORB singleton object.
57      */

58     private static org.omg.CORBA.ORB JavaDoc orb_ = null;
59
60     /**
61      * To store initial object references.
62      */

63     private static java.util.Hashtable JavaDoc initialReferences_ =
64         new java.util.Hashtable JavaDoc();
65     // TODO: Must be removed when the operation
66
// CORBA::ORB::register_initial_reference()
67
// will be standardized by the OMG's ORB Core RTF.
68

69     // ==================================================================
70
//
71
// Constructor.
72
//
73
// ==================================================================
74

75     /**
76      * The default constructor.
77      */

78     protected TheORBSingleton()
79     {
80     }
81
82     // ==================================================================
83
//
84
// Internal methods.
85
//
86
// ==================================================================
87

88     /**
89      * Initialize the ORB.
90      *
91      * @param args The command-line arguments.
92      * @param orbClass The ORB class name.
93      * @param orbSingletonClass The ORB singleton class name.
94      */

95     protected static void initialize(
96         String JavaDoc[] args,
97         String JavaDoc orbClass,
98         String JavaDoc orbSingletonClass)
99     {
100
101         java.util.Properties JavaDoc properties = System.getProperties();
102         properties.put("org.omg.CORBA.ORBClass", orbClass);
103         properties.put("org.omg.CORBA.ORBSingletonClass", orbSingletonClass);
104
105         // Initialize the CORBA::ORB.
106
setORB(org.omg.CORBA.ORB.init(args, properties));
107     }
108
109     /**
110      * Requests an IOR from a multicast URL.
111      *
112      * @param location - The multicast group address of the IOR file.
113      *
114      * @return The IOR.
115      */

116     public static String JavaDoc
117     request_ior(String JavaDoc location)
118     {
119
120         // Start a multicast client request
121

122         String JavaDoc file =
123                         location.substring(
124                             location.indexOf("//") + 2).substring(
125                             location.substring(
126                                 location.indexOf("//") + 2).indexOf(
127                                 "/")
128                                 + 1);
129         String JavaDoc addr =
130                         location.substring(
131                             location.indexOf("//") + 2).substring(
132                             0,
133                             location.substring(
134                                 location.indexOf("//") + 2).indexOf(
135                                 "/"));
136
137         // Define default used ports for sockets
138
int s_port = 8888;
139         int ms_port = 8880;
140
141         // Sending request to server to get IOR
142

143         InetAddress JavaDoc groupAddress = null;
144         DatagramSocket JavaDoc socket;
145         MulticastSocket JavaDoc msocket;
146
147         try {
148             groupAddress = InetAddress.getByName(addr);
149         } catch (UnknownHostException JavaDoc ex) {
150             System.out.println("Unknown host" + ex.getMessage());
151         }
152
153         String JavaDoc message = "NameService.IOR";
154
155         // Send the name of the IOR file to request
156
// default is NameService.IOR
157
message = file;
158
159         System.out.println("Sending multicast request to get IOR...");
160         DatagramPacket JavaDoc send_request =
161             new DatagramPacket JavaDoc(
162                 message.getBytes(),
163                 message.getBytes().length,
164                 groupAddress,
165                 ms_port);
166         try {
167             msocket = new MulticastSocket JavaDoc();
168             msocket.send(send_request);
169             msocket.disconnect();
170             msocket.close();
171             msocket.close();
172
173         } catch (IOException JavaDoc e) {
174             System.err.println(e.getMessage());
175         }
176
177         // Receiving response from server
178
byte[] mem = new byte[1000];
179         String JavaDoc ior_recv = null;
180         try {
181             socket = new DatagramSocket JavaDoc(s_port + 1);
182             DatagramPacket JavaDoc recv = new DatagramPacket JavaDoc(mem, mem.length);
183             socket.receive(recv);
184             ior_recv = new String JavaDoc(mem, 0, recv.getLength() - 2);
185             System.out.println(
186                 "Receiving IOR from server "
187                     + recv.getAddress().getHostName()
188                     + ":"
189                     + recv.getPort());
190             socket.disconnect();
191             socket.close();
192         } catch (SocketException JavaDoc e) {
193         } catch (IOException JavaDoc e) {
194         }
195
196         return ior_recv;
197
198     }
199
200     /**
201      * Remove ORB arguments.
202      *
203      * @param args The command-line arguments.
204      *
205      * @return The command line arguments without OpenORB options.
206      */

207     protected static String JavaDoc[]
208     remove_ORB_arguments(String JavaDoc[] args) {
209         int nb = 0;
210
211         // Copy arguments without ORB ones.
212
String JavaDoc[] tmp = new String JavaDoc[args.length];
213         for (int i = 0; i < args.length; i++) {
214             if (args[i].startsWith("-ORBInitRef")) {
215                 i++;
216             } else if (!args[i].startsWith("-ORB")) {
217                 tmp[nb] = args[i];
218                 nb++;
219             }
220         }
221
222         // Construct the returned array.
223
args = new String JavaDoc[nb];
224         for (int i = 0; i < nb; i++) {
225             args[i] = tmp[i];
226         }
227
228         // Return arguments without ORB ones.
229
return args;
230     }
231
232     // ==================================================================
233
//
234
// Public methods.
235
//
236
// ==================================================================
237

238     /**
239      * Obtain the CORBA::ORB singleton object.
240      *
241      * @return The CORBA::ORB singleton object.
242      */

243     public static org.omg.CORBA.ORB JavaDoc
244     getORB()
245     {
246         if (orb_ == null) {
247             orb_ = org.omg.CORBA.ORB.init();
248         }
249         return orb_;
250     }
251
252     /**
253      * Set the CORBA::ORB singleton object.
254      *
255      * @param orb The CORBA::ORB singleton object.
256      */

257     public static void
258     setORB(org.omg.CORBA.ORB JavaDoc orb)
259     {
260         orb_ = orb;
261     }
262
263     /**
264      * Resolve a initial object reference.
265      *
266      * @param name The initial object name.
267      *
268      * @return The initial object reference.
269      */

270     public static org.omg.CORBA.Object JavaDoc
271     resolve_initial_reference(String JavaDoc name)
272     {
273         // TODO: Must be removed when the operation
274
// CORBA::ORB::register_initial_reference()
275
// will be standardized by the OMG's ORB Core RTF.
276
//
277
Object JavaDoc object = initialReferences_.get(name);
278         if (object != null) {
279             return (org.omg.CORBA.Object JavaDoc) object;
280         }
281
282         try {
283             return getORB().resolve_initial_references(name);
284         } catch (org.omg.CORBA.ORBPackage.InvalidName JavaDoc exc) {
285             // Wrap the CORBA::ORB::InvalidName exception.
286
throw new UserExceptionWrapper(exc);
287         }
288     }
289
290     /**
291      * Register a initial object reference.
292      *
293      * @param name The initial object reference name.
294      * @param object The initial object reference.
295      */

296     public static void
297     register_initial_reference( String JavaDoc name,
298                                 org.omg.CORBA.Object JavaDoc object)
299     {
300         // TODO: Must be removed when the operation
301
// CORBA::ORB::register_initial_reference()
302
// will be standardized by the OMG's ORB Core RTF.
303
//
304
initialReferences_.put(name, object);
305
306         // TODO: Must be replaced by
307
//
308
// getORB().register_initial_reference(name, object);
309
//
310
// when the operation CORBA::ORB::register_initial_reference()
311
// will be standardized by the OMG's ORB Core RTF.
312
}
313
314     /**
315      * Run the CORBA::ORB singleton object.
316      */

317     public static void
318     run()
319     {
320         getORB().run();
321     }
322
323     /**
324      * Shutdown the CORBA::ORB singleton object.
325      *
326      * @param wait_for_completion
327      * Must (or not) wait for completion of current requests.
328      */

329     public static void
330     shutdown(boolean wait_for_completion)
331     {
332         getORB().shutdown(wait_for_completion);
333     }
334
335     /**
336      * Create a CORBA::Any.
337      *
338      * @return The created CORBA::Any.
339      */

340     public static org.omg.CORBA.Any JavaDoc
341     create_any()
342     {
343         return getORB().create_any();
344     }
345
346     /**
347      * Register a valuetype factory.
348      *
349      * @param id The valuetype repository Id.
350      * @param factory The valuetype factory.
351      */

352     public static void register_value_factory(
353         String JavaDoc id,
354         org.omg.CORBA.portable.ValueFactory JavaDoc factory)
355     {
356         ((org.omg.CORBA_2_3.ORB JavaDoc) getORB()).register_value_factory(id, factory);
357     }
358
359     /**
360      * Save an object reference into a file.
361      *
362      * @param object The object reference.
363      * @param fileName The file name where the IOR will be stored.
364      */

365     public static void
366     save_IOR(org.omg.CORBA.Object JavaDoc object, String JavaDoc fileName)
367     {
368         try {
369             // Obtain the IOR of the object reference.
370
String JavaDoc ior = getORB().object_to_string(object);
371
372             // Instantiate the output file and stream.
373
java.io.FileOutputStream JavaDoc file =
374                 new java.io.FileOutputStream JavaDoc(fileName);
375             java.io.PrintWriter JavaDoc out = new java.io.PrintWriter JavaDoc(file);
376
377             // Output the IOR.
378
out.println(ior);
379
380             // Flush the stream and close the file.
381
out.flush();
382             file.close();
383         } catch (java.io.IOException JavaDoc exc) {
384             // Wrap the exception.
385
throw new java.lang.Error JavaDoc(exc.getMessage());
386         }
387     }
388
389     /**
390      * Convert a string to an object reference.
391      *
392      * @param string The stringified object reference.
393      *
394      * @return The object reference.
395      */

396     public static org.omg.CORBA.Object JavaDoc
397     string_to_object(String JavaDoc string)
398     {
399         return getORB().string_to_object(string);
400     }
401
402     /**
403      * Create the CORBA::VabueBase TypeCode.
404      */

405     public static org.omg.CORBA.TypeCode JavaDoc
406     create_valuebase_tc()
407     {
408         return getORB().create_value_tc(
409             "IDL:omg.org/CORBA/ValueBase:1.0",
410             "ValueBase",
411             org.omg.CORBA.VM_ABSTRACT.value,
412             null,
413             new org.omg.CORBA.ValueMember JavaDoc[0]);
414     }
415
416     /**
417      * Obtain the null valuetype TypeCode.
418      */

419     public static org.omg.CORBA.TypeCode JavaDoc
420     get_null_valuebase_tc()
421     {
422         return null;
423     }
424 }
425
Popular Tags