KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > ccm > runtime > ContainerFactoryImpl


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.ccm.runtime;
31
32 import org.objectweb.corba.runtime.*;
33
34 /**
35  ** <p>Default system factory implementation for the <tt>ContainerService</tt> system service.</p>
36  **/

37 public class ContainerFactoryImpl
38 extends org.omg.CORBA.LocalObject JavaDoc
39 implements SystemFactory
40 {
41     // system factory
42
static final private String JavaDoc _class_name = "ContainerFactoryImpl";
43
44     // default constructor
45
protected
46     ContainerFactoryImpl()
47     {
48     }
49
50     //
51
// entry point
52
//
53

54     static public SystemFactory
55     create_factory()
56     {
57         return new ContainerFactoryImpl();
58     }
59
60     //
61
// internal operations
62
//
63

64     static private ServiceImpl[]
65     asServices(String JavaDoc[] uuids, ORBService orbs)
66     {
67         final String JavaDoc opname = "asServices";
68         String JavaDoc msg = null;
69
70         // check if uuids is empty
71
if ((uuids==null) || (uuids.length==0)) {
72             return new ServiceImpl[0];
73         }
74
75         // NOTE: use an array list rather than an array as some uuids may be skipped
76
// (ie we can't foresee the size of the array)
77
java.util.ArrayList JavaDoc services = new java.util.ArrayList JavaDoc();
78
79         // get ExtComponentInstallation
80
// later used for CCSD, CSD, and code obtention
81
org.omg.CORBA.Object JavaDoc obj = null;
82         org.coach.ECM.ExtComponentInstallation extci = null;
83
84         obj = orbs.resolve_initial_references("ExtComponentInstallation");
85         extci = org.coach.ECM.ExtComponentInstallationHelper.narrow(obj);
86
87         ServiceImpl service = null;
88         org.coach.ECM.ServiceArchive sarchive = null;
89         for (int i=0;i<uuids.length;i++) {
90             // obtain service archive
91
sarchive = extci.get_service_archive(uuids[i]);
92
93             // create service with:
94
// - ServiceId
95
// - Implementation UUID
96
// - reference to the service set
97
String JavaDoc serviceid = sarchive.service_id();
98             service = new ServiceImpl(serviceid, uuids[i]);
99
100             // check if the service requires a corba service
101
org.coach.ECM.CORBAServiceRef rcsref = null;
102
103             try {
104                 rcsref = sarchive.get_corbaservice_ref();
105
106                 // create corba service ref
107
CORBAServiceRef csref = new CORBAServiceRef();
108                 csref.stringified_initial_reference = orbs.object_to_string(rcsref.initial_reference);
109                 csref.corba_type = rcsref.corba_type;
110                 csref.orb_registration_name = rcsref.orb_registration_name;
111
112                 // store
113
service.setCorbaServiceRef(csref);
114             } catch (org.coach.ECM.NoCORBAServiceRefRequired ex) {
115                 // ignore
116
TheLogger.debug(_class_name, opname, "IGNORE", ex);
117             }
118
119             // create service initializer
120
String JavaDoc codeloc = sarchive.code_location();
121             TheClassLoader.addResource(codeloc);
122
123             msg = "Code location for service: "+uuids[i]+" is: "+codeloc;
124             TheLogger.debug(_class_name, opname, msg);
125
126             // create new instance
127
org.coach.ECA.ServiceInitializer serviceinit = null;
128             String JavaDoc entrypt = sarchive.code_entrypoint();
129
130             msg = "Code entrypoint for service: "+uuids[i]+" is: "+entrypt;
131             TheLogger.debug(_class_name, opname, msg);
132
133             try {
134                 serviceinit = (org.coach.ECA.ServiceInitializer)TheClassLoader.newInstance(entrypt);
135             } catch (ClassCastException JavaDoc ex) {
136                 // NOTE: TODO: how to handle this case ? pass the service installation ? this failure should
137
// be reported or we consider that the case can not happen (because the archive was checked)
138
msg = "FAILED (not a service initializer)";
139                 TheLogger.debug(_class_name, opname, msg);
140                 // skip
141
continue ;
142             }
143
144             // set
145
service.setServiceInitializer(serviceinit);
146
147             // store
148
services.add(service);
149         }
150
151         //
152
return (ServiceImpl[])services.toArray(new ServiceImpl[0]);
153     }
154
155     static private ServiceImpl[]
156     asServices(String JavaDoc[] uuids, String JavaDoc[] sids, String JavaDoc[] arch_locs, String JavaDoc[] entrypts,
157                String JavaDoc[] iref_regnames, String JavaDoc[] iref_iors)
158     {
159         final String JavaDoc opname = "asServices";
160         String JavaDoc msg = null;
161
162         // NOTE: this operation should be used for the first ORB
163
// indeed, as no ORB is available, it is not possible to obtain the
164
// ExtComponentInstallation ref to query the information
165

166         // check if uuids is empty
167
if ((uuids==null) || (uuids.length==0)) {
168             return new ServiceImpl[0];
169         }
170
171         // NOTE: use an array list rather than an array as some uuids may be skipped
172
// (ie we can't foresee the size of the array)
173
java.util.ArrayList JavaDoc services = new java.util.ArrayList JavaDoc();
174         ServiceImpl service = null;
175
176         for (int i=0;i<uuids.length;i++) {
177             service = new ServiceImpl(sids[i], uuids[i]);
178
179             // check if service has an initial reference
180
if ((iref_regnames[i]!=null) && (!iref_regnames.equals(""))) {
181                 // check that IOR is set
182
if ((iref_iors[i]==null) || (iref_iors[i].equals(""))) {
183                     msg = "FAILED (no IOR for: "+iref_regnames[i]+")";
184                     TheLogger.debug(_class_name, opname, msg);
185                     // skip
186
continue ;
187                 }
188
189                 // create CORBA service ref
190
CORBAServiceRef csref = new CORBAServiceRef();
191                 csref.corba_type = "unknown";
192                 csref.orb_registration_name = iref_regnames[i];
193                 csref.stringified_initial_reference = iref_iors[i];
194                 csref.initial_reference = null;
195
196                 // store
197
service.setCorbaServiceRef(csref);
198             }
199
200             // create service initializer
201
String JavaDoc codeloc = arch_locs[i];
202             TheClassLoader.addResource(codeloc);
203
204             // create new instance
205
org.coach.ECA.ServiceInitializer serviceinit = null;
206             String JavaDoc entrypt = entrypts[i];
207
208             try {
209                 serviceinit = (org.coach.ECA.ServiceInitializer)TheClassLoader.newInstance(entrypt);
210             } catch (ClassCastException JavaDoc ex) {
211                 // NOTE: TODO: how to handle this case ? pass the service installation ? this failure should
212
// be reported or we consider that the case can not happen (because the archive was checked)
213
msg = "FAILED (not a service initializer)";
214                 TheLogger.debug(_class_name, opname, msg);
215                 // skip this uuid
216
continue ;
217             }
218
219             // set
220
service.setServiceInitializer(serviceinit);
221
222             // store
223
services.add(service);
224         }
225
226         //
227
return (ServiceImpl[])services.toArray(new ServiceImpl[0]);
228     }
229
230     static private CORBAServiceRef[]
231     asDegenServices(org.coach.ECM.DegeneratedServiceRef[] dsrefs,
232                     ORBService orbs)
233     {
234         // check if desrefs is empty
235
if ((dsrefs==null) || (dsrefs.length==0)) {
236             return new CORBAServiceRef[0];
237         }
238
239         CORBAServiceRef[] dservices = new CORBAServiceRef[dsrefs.length];
240
241         for (int i=0;i<dsrefs.length;i++) {
242             // create CORBA Service ref
243
dservices[i] = new CORBAServiceRef();
244             dservices[i].corba_type = dsrefs[i].corba_type;
245             dservices[i].orb_registration_name = dsrefs[i].orb_registration_name;
246             dservices[i].stringified_initial_reference = dsrefs[i].initial_reference;
247             dservices[i].initial_reference = orbs.string_to_object(dsrefs[i].initial_reference);
248         }
249
250         return dservices;
251     }
252
253     static private CORBAServiceRef[]
254     asDegenServices(String JavaDoc[] iref_regnames, String JavaDoc[] iref_iors)
255     {
256         // NOTE: this operation should be used for the first ORB
257
// indeed, as no ORB is available, it is not possible to obtain the
258
// ExtComponentInstallation ref to query the information
259

260         // check if empty
261
if ((iref_regnames==null) || (iref_regnames.length==0)) {
262             return new CORBAServiceRef[0];
263         }
264
265         // NOTE: use an array list rather than an array as some uuids may be skipped
266
// (ie we can't foresee the size of the array)
267
java.util.ArrayList JavaDoc dservices = new java.util.ArrayList JavaDoc();
268         CORBAServiceRef dservice = null;
269
270         for (int i=0;i<iref_regnames.length;i++) {
271             // check that IOR is set
272
if ((iref_iors[i]==null) || (iref_iors[i].equals(""))) {
273                 final String JavaDoc opname = "asDegenServices";
274                 final String JavaDoc msg = "IGNORE (no IOR for: "+iref_regnames[i]+")";
275                 TheLogger.debug(_class_name, opname, msg);
276                 // skip
277
continue ;
278             }
279
280             // create CORBA Service ref
281
dservice = new CORBAServiceRef();
282             dservice.corba_type = "unknown";
283             dservice.orb_registration_name = iref_regnames[i];
284             dservice.stringified_initial_reference = iref_iors[i];
285             dservice.initial_reference = null;
286             dservices.add(dservice);
287         }
288
289         return (CORBAServiceRef[])dservices.toArray(new CORBAServiceRef[0]);
290     }
291
292     static private ORBService
293     createORBService(ServiceImpl[] services,
294                      CORBAServiceRef[] dservices)
295     {
296         // create service init info
297
ServiceInitInfoImpl sii = new ServiceInitInfoImpl(services, dservices);
298
299         // run pre_install
300
org.coach.ECA.ServiceInitializer sinit = null;
301         for (int i=0;i<services.length;i++) {
302             sinit = services[i].getServiceInitializer();
303             sinit.pre_install(sii);
304         }
305
306         // obtain FactoryFinder service
307
FactoryFinderService ffservice = ComponentRuntime.getFFService();
308
309         // obtain factories
310
SystemFactory orbfact = ffservice.find_service_factory(ORBService.SERVICE_ID);
311
312         // build init info
313
ORBConfiguration orbcfg = sii.getORBConfiguration();
314
315         // create ORBService
316
ORBService orbservice = (ORBService)orbfact.create_system_component(orbcfg);
317         // set ORB service for post_install
318
sii.setORBService(orbservice);
319
320         // run post_install
321
for (int i=0;i<services.length;i++) {
322             sinit = services[i].getServiceInitializer();
323             sinit.post_install(sii);
324         }
325
326         //
327
return orbservice;
328     }
329
330     // NOTE: no args currently, but it will change
331
static private ComponentPOAService
332     createComponentPOAService(String JavaDoc poasid,
333                               ORBService orbs,
334                               ServicesSet sset)
335     {
336         // obtain FactoryFinder service
337
FactoryFinderService ffservice = ComponentRuntime.getFFService();
338
339         // obtain factories
340
SystemFactory poafact = ffservice.find_service_factory(poasid);
341
342         // build init info
343
ComponentPOAConfiguration poacfg = new ComponentPOAConfigurationImpl(null, null, orbs, sset);
344
345         // create service
346
ComponentPOAService poas = (ComponentPOAService)poafact.create_system_component(poacfg);
347
348         //
349
return poas;
350     }
351
352     //
353
// public operations
354
//
355

356     // create a container service with:
357
// - an already created component installation registered to the ORB service
358
// - a set of services uuids
359
// - a set of degenerated service refs
360
// NOTE: this is the default op
361
static public ContainerService
362     asContainerService(String JavaDoc[] uuids,
363                        org.coach.ECM.DegeneratedServiceRef[] dsrefs,
364                        ORBService orbs,
365                        String JavaDoc poasid)
366     {
367         ServiceImpl[] services = null;
368         CORBAServiceRef[] dservices = null;
369         ORBService orbservice = null;
370         ServicesSetImpl sset = null;
371         ComponentPOAService cpoaservice = null;
372         
373         // create services
374
services = asServices(uuids, orbs);
375
376         // create degenerated services
377
dservices = asDegenServices(dsrefs, orbs);
378
379         // create ORB service
380
// NOTE: the ORB service is created in this class as at this time
381
// the ServicesSetImpl class is mainly a storage class
382
orbservice = createORBService(services, dservices);
383
384         // create services set
385
sset = new ServicesSetImpl(services, dservices, orbservice);
386
387         // create component poa service component
388
cpoaservice = createComponentPOAService(poasid, orbservice, sset);
389
390         // create container service
391
ContainerService contservice = new ContainerServiceImpl(orbservice, cpoaservice, sset);
392
393         return contservice;
394     }
395
396     // create a container service with:
397
// - no ORB service
398
// - a set of services uuids
399
// - a set of degenerated service refs
400
// - the ExtendedComponentInstallation ref
401
// NOTE: this will create a "empty" ORB service for remote comms between this runtime
402
// and the ExtCI
403
static public ContainerService
404     asContainerService(String JavaDoc[] uuids,
405                        org.coach.ECM.DegeneratedServiceRef[] dsrefs,
406                        StringifiedInitialReference[] sirefs,
407                        String JavaDoc poasid)
408     {
409         // create an "empty" ORB service with the ExtCI ref as initial reference
410
ORBService orbservice = null;
411
412         // obtain FactoryFinder service
413
FactoryFinderService ffservice = ComponentRuntime.getFFService();
414
415         // obtain factories
416
SystemFactory orbfact = ffservice.find_service_factory(ORBService.SERVICE_ID);
417
418         // build config
419
// add stringified initial references only
420
ORBConfiguration orbcfg = new ORBConfigurationImpl(null, null, null, sirefs);
421
422         // create ORBService
423
orbservice = (ORBService)orbfact.create_system_component(orbcfg);
424
425         // use the default op with this ORB service
426
return asContainerService(uuids, dsrefs, orbservice, poasid);
427     }
428
429     // create a services set with:
430
// - no ORB service
431
// - no ExtCI ref (ie no ExtCI is installed on the node)
432
// - a set of services uuids and archive locations
433
// - a set of degenerated service refs
434
// NOTE: this should create the ExtCI
435
// TODO
436

437     //
438
// IDL:objectweb.org/corba/runtime/SystemFactory:1.0
439
//
440

441     final public SystemComponent
442     create_system_component(SystemConfiguration cfg)
443     {
444         final String JavaDoc opname = "create_system_component";
445         final String JavaDoc msg = "operation not implemented";
446         TheLogger.debug(_class_name, opname, msg);
447         throw new Error JavaDoc(msg);
448     }
449 }
450
Popular Tags