KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > corba > runtime > ORBFactoryImpl


1 // ====================================================================
2
//
3
// ECM: The Extensible Container Model
4
// Copyright (C) 2004 THALES
5
// Contact: openccm-ecm@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): Mathieu Vadet.
23
// Initial Funding: IST COACH European project (IST-2001-34445)
24
// http://www.ist-coach.org
25
//
26
// ====================================================================
27

28
29
30 package org.objectweb.corba.runtime;
31
32 /**
33  ** <p>Default implementation of the <tt>ORBFactory</tt> factory.</p>
34  **/

35 public class ORBFactoryImpl
36 extends org.omg.CORBA.LocalObject JavaDoc
37 implements ORBFactory
38 {
39     // system factory
40
static final private String JavaDoc _class_name = "ORBFactoryImpl";
41     private java.util.ArrayList JavaDoc _default_factories;
42     private java.util.ArrayList JavaDoc _default_initializers;
43
44     // default constructor
45
protected
46     ORBFactoryImpl()
47     {
48         // system factory
49
_default_factories = new java.util.ArrayList JavaDoc();
50         _default_initializers = new java.util.ArrayList JavaDoc();
51     }
52
53     //
54
// entry point
55
//
56

57     static public SystemFactory
58     create_factory()
59     {
60         return new ORBFactoryImpl();
61     }
62
63     //
64
// internal operations
65
//
66

67     // NOTE: this operation shall take into account the specifics of each
68
// ORB implementation
69

70     static private org.omg.CORBA.ORB JavaDoc
71     initializeORB(String JavaDoc[] params, java.util.Properties JavaDoc props)
72     {
73         return org.omg.CORBA.ORB.init(params, props);
74
75     }
76
77     // NOTE: extracts an IOR from an URN
78
static protected String JavaDoc
79     extractIOR(String JavaDoc name)
80     {
81         final String JavaDoc opname = "extractIOR";
82
83         // check if already an IOR
84
if (name.startsWith("IOR:")) {
85             return name;
86         }
87
88         // try to open as local file
89
java.io.InputStream JavaDoc in = null;
90         try {
91             in = new java.io.FileInputStream JavaDoc(name);
92         } catch (java.io.FileNotFoundException JavaDoc ex) {
93             // ignore
94
}
95
96         if (in==null) {
97             // try to open as URL
98
java.net.URL JavaDoc url = null;
99             try {
100                 url = new java.net.URL JavaDoc(name);
101                 in = url.openStream();
102             } catch (Exception JavaDoc ex) {
103                 // ignore
104
}
105         }
106
107         if (in==null) {
108             final String JavaDoc msg = "FAILED (invalid format: "+name+")";
109             TheLogger.error(_class_name, opname, msg);
110             return null;
111         }
112
113         // read reference
114
java.io.BufferedReader JavaDoc reader = null;
115         StringBuffer JavaDoc res = new StringBuffer JavaDoc();
116
117         try {
118             reader = new java.io.BufferedReader JavaDoc(new java.io.InputStreamReader JavaDoc(in));
119             while(reader.ready()) {
120                 res.append(reader.readLine());
121             }
122
123             reader.close();
124         } catch(Exception JavaDoc ex) {
125             final String JavaDoc msg = "IGNORE (resource: "+name+")";
126             TheLogger.error(_class_name, opname, msg, ex);
127             return null;
128         }
129
130         return res.toString();
131     }
132
133     // NOTE: creates a new ORB instance, activates root POA and registers it to the main thread
134
// ORB creation takes into account:
135
// - ORB initializers
136
// - stringified initial references (passed as command line arguments)
137
// - value factories (register using CORBA 2.3 ORB register_value_factory operation)
138
static private org.omg.CORBA.ORB JavaDoc
139     createORB(ORBConfiguration cfg)
140     {
141         final String JavaDoc opname = "createORB";
142
143         // create ORB
144
org.omg.CORBA.ORB JavaDoc orb = null;
145
146         // add ORBInitializer entry points to properties (Java mapping)
147
String JavaDoc[] orb_inits = cfg.orb_initializers();
148         java.util.Properties JavaDoc props = System.getProperties();
149         for (int i=0;i<orb_inits.length;i++) {
150             TheLogger.debug(_class_name, "createORB", "ORBInitializer registered: "+orb_inits[i]);
151             props.put("org.omg.PortableInterceptor.ORBInitializerClass."+orb_inits[i], "");
152         }
153
154         // add stringified initial references as arguments
155
StringifiedInitialReference[] strirefs = cfg.stringified_initial_references();
156         String JavaDoc[] args = new String JavaDoc[strirefs.length*2];
157         int j=0;
158         for (int i=0;i<strirefs.length;i++) {
159             //TheLogger.debug(_class_name, opname, "Arg #"+j+": -ORBInitRef "+strirefs[i].name+"="+strirefs[i].ref);
160
args[j] = "-ORBInitRef";
161             args[j+1] = strirefs[i].name+"="+strirefs[i].ref;
162
163             // check if ref is an URL (as OpenORB does not manage this correctly apparently)
164
//if (strirefs[i].ref.startsWith("file:")) {
165
// args[j+1] = strirefs[i].name+"="+extractIORFromFile(strirefs[i].ref);
166
//}
167
//else {
168
// args[j+1] = strirefs[i].name+"="+strirefs[i].ref;
169
//}
170

171             j=j+2;
172         }
173
174         // do NOT add initial references
175
// NOTE: non stringified initial references are added in the ORBService object
176
// (and not in the underlying ORB instance as it requires a CORBA 2.5 ORB for that)
177

178         String JavaDoc cline = "";
179         for (int i=0;i<args.length;i++) {
180             cline = cline+" "+args[i];
181         }
182
183         //TheLogger.debug(_class_name, opname, "command line: "+cline);
184
orb = initializeORB(args, props);
185
186         // activate root POA
187
try {
188             org.omg.PortableServer.POA JavaDoc rootPOA = null;
189             org.omg.CORBA.Object JavaDoc obj = orb.resolve_initial_references("RootPOA");
190             rootPOA = org.omg.PortableServer.POAHelper.narrow(obj);
191             rootPOA.the_POAManager().activate();
192         } catch (Exception JavaDoc ex) {
193             TheLogger.error(_class_name, opname, "FAILED", ex);
194             return null;
195         }
196
197         // run the ORB in the main thread
198
Runtime.getMainThread().runORB(orb);
199
200         // add value factories
201
// NOTE: requires a CORBA 2.3 ORB
202
ValueFactory[] vfacts = cfg.value_factories();
203         org.omg.CORBA_2_3.ORB JavaDoc orb23 = (org.omg.CORBA_2_3.ORB JavaDoc)orb;
204         org.omg.CORBA.portable.ValueFactory JavaDoc vfact = null;
205
206         for (int i=0;i<vfacts.length;i++) {
207             //TheLogger.debug(_class_name, opname, "Register ValueFactory: "+vfacts[i].type_id());
208
vfact = vfacts[i].value_factory();
209             orb23.register_value_factory(vfacts[i].type_id(), vfact);
210         }
211
212         return orb;
213     }
214
215     private ORBConfiguration
216     addDefaults(ORBConfiguration cfg)
217     {
218         // value factories
219
ValueFactory[] ivfacts = cfg.value_factories();
220         ValueFactory[] nfacts = null;
221         // add to default ones
222
if (_default_factories.size()==0) {
223             nfacts = ivfacts;
224         }
225         else {
226             java.util.ArrayList JavaDoc dvfacts = _default_factories;
227             java.util.ArrayList JavaDoc nvfacts = new java.util.ArrayList JavaDoc(dvfacts);
228             nvfacts.addAll(java.util.Arrays.asList(ivfacts));
229             nfacts = (ValueFactory[])nvfacts.toArray(new ValueFactory[0]);
230         }
231
232         // ORB initializers
233
String JavaDoc[] inits = cfg.orb_initializers();
234         String JavaDoc[] ninits = null;
235         // add default ones
236
if (_default_initializers.size()==0) {
237             ninits = inits;
238         }
239         else {
240             java.util.ArrayList JavaDoc vinits = _default_initializers;
241             java.util.ArrayList JavaDoc nvinits = new java.util.ArrayList JavaDoc(vinits);
242             nvinits.addAll(java.util.Arrays.asList(inits));
243             ninits = (String JavaDoc[])nvinits.toArray(new String JavaDoc[0]);
244         }
245
246         // return new configuration
247
return new ORBConfigurationImpl(ninits,
248                                         nfacts,
249                                         cfg.initial_references(),
250                                         cfg.stringified_initial_references());
251     }
252
253     //
254
// IDL:objectweb.org/corba/runtime/SystemFactory:1.0
255
//
256

257     final public SystemComponent
258     create_system_component(SystemConfiguration cfg)
259     {
260         ORBConfiguration orbcfg = (ORBConfiguration)cfg;
261
262         // add default value factories / ORB initializer to init info
263
ORBConfiguration neworbcfg = addDefaults(orbcfg);
264
265         // create new ORB instance
266
org.omg.CORBA.ORB JavaDoc orb = createORB(neworbcfg);
267
268         // create a new ORB service instance
269
ORBServiceImpl orbs = new ORBServiceImpl(orb);
270
271         // complete configuration
272
orbs.system_configuration_complete(neworbcfg);
273
274         // set initial references as they are not set on the ORB instance
275
InitialReference[] refs = neworbcfg.initial_references();
276         for (int i=0;i<refs.length;i++) {
277             orbs.register_initial_references(refs[i].name, refs[i].ref);
278         }
279
280         return orbs;
281     }
282
283     //
284
// IDL:objectweb.org/corba/runtime/ORBFactory:1.0
285
//
286

287     final public void
288     add_default_valuefactory(ValueFactory vfact)
289     {
290         _default_factories.add(vfact);
291     }
292
293     final public void
294     add_default_initializer(String JavaDoc orbinit)
295     {
296         _default_initializers.add(orbinit);
297     }
298 }
299
Popular Tags