KickJava   Java API By Example, From Geeks To Geeks.

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


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 implementation of the <tt>ComponentPOAService</tt> OMG IDL interface.</p>
36  **/

37 public class ComponentPOAServiceImpl
38 extends org.omg.CORBA.LocalObject JavaDoc
39 implements ComponentPOAService
40 {
41     // component poa service
42
static final private String JavaDoc _class_name = "ComponentPOAServiceImpl";
43     static final private String JavaDoc _service_id = ComponentPOAService.SERVICE_ID;
44
45     private ORBService _orb_service;
46     private ServicesSet _services_set;
47     private org.omg.PortableServer.POA JavaDoc _home_poa;
48     private org.omg.PortableServer.POA JavaDoc _component_poa;
49     private ComponentServantActivatorImpl _servant_activator;
50
51     // default constructor
52
public
53     ComponentPOAServiceImpl()
54     {
55         // component poa service
56
_orb_service = null;
57         _services_set = null;
58         _home_poa = null;
59         _component_poa = null;
60         _servant_activator = null;
61     }
62
63     //
64
// internal operation
65
//
66

67     static public CallInterceptorFactory
68     repid2Factory(String JavaDoc repid)
69     {
70         // NOTE: obtain the interceptor factory entrypoint from the home repository id
71
final String JavaDoc opname = "repid2Factory";
72
73         // Repid format: IDL:m1/.../mn/home:1.0 (without prefix)
74
// IDL:prefix/m1/.../mn/home:1.0 (with prefix)
75

76         int idx1 = repid.indexOf(':');
77         int idx2 = repid.lastIndexOf(':');
78         // body = home or m1/.../mn/home or prefix/m1/.../mn/home (prefix=p1.p2.pn)
79
// java class = home or m1.m2.mn.home_IFact or pn.p2.p1.m1.m2.mn.home_IFact
80
String JavaDoc body = repid.substring(idx1+1, idx2);
81         String JavaDoc entrypt = null;
82
83         idx1 = body.indexOf('.');
84         if (idx1==-1) {
85             // replace '/' by '.'
86
entrypt = body.replace('/', '.');
87         } else {
88             String JavaDoc res = "";
89             idx2 = 0;
90             while (idx1!=-1) {
91                 res = body.substring(idx2, idx1+1) + res;
92                 idx2 = idx1+1;
93                 idx1 = body.indexOf('.', idx2);
94             }
95
96             int idx3 = body.indexOf('/', idx2);
97             res = body.substring(idx2, idx3) + '.' + res;
98             entrypt = res+body.substring(idx3+1).replace('/', '.');
99         }
100
101         entrypt = entrypt+"_IFact.create_factory";
102
103         Object JavaDoc obj = TheClassLoader.newInstance(entrypt);
104         if (obj==null) {
105             // NOTE: log a message
106
final String JavaDoc msg = "IGNORE (interceptor factory can not be created with entrypoint: "+entrypt+")";
107             TheLogger.debug(_class_name, opname, msg);
108             return null;
109         }
110
111         // check if obj is effectively a interceptor factory
112
if (!(obj instanceof CallInterceptorFactory)) {
113             // NOTE: log a message
114
final String JavaDoc msg = "IGNORE (object is not an interceptor factory: "+obj.getClass()+")";
115             TheLogger.debug(_class_name, opname, msg);
116             return null;
117         }
118
119         return (CallInterceptorFactory)obj;
120     }
121
122     //
123
// friend operations (for factory)
124
//
125

126     final protected void
127     setHomePOA(org.omg.PortableServer.POA JavaDoc poa)
128     {
129         _home_poa = poa;
130     }
131
132     final protected void
133     setComponentPOA(org.omg.PortableServer.POA JavaDoc poa)
134     {
135         _component_poa = poa;
136
137         // obtain servant manager from POA
138
try {
139             _servant_activator = (ComponentServantActivatorImpl)poa.get_servant_manager();
140         } catch (Exception JavaDoc ex) {
141             // should not happen
142
final String JavaDoc opname = "setComponentPOA";
143             TheLogger.error(_class_name, opname, "FAILED", ex);
144         }
145     }
146
147     //
148
// IDL:objectweb.org/corba/runtime/SystemComponent:1.0
149
//
150

151     final public void
152     system_configuration_complete(SystemConfiguration cfg)
153     {
154         ComponentPOAConfiguration poacfg = (ComponentPOAConfiguration)cfg;
155
156         // extract services
157
_orb_service = poacfg.orb_service();
158         _services_set = poacfg.services_set();
159
160         // set it also to the servant activator
161
_servant_activator.setConfiguration(_orb_service, _services_set);
162     }
163
164     final public void
165     destroy()
166     {
167         // destroy the POA
168
// here, we have set:
169
// - 'etherealize_objects' param as true
170
// - 'wait_for_completion' param as true
171
_component_poa.destroy(true, true);
172         _home_poa.destroy(true, true);
173     }
174
175     //
176
// IDL:objectweb.org/corba/runtime/SystemService:1.0
177
//
178

179     final public String JavaDoc
180     service_id()
181     {
182         return _service_id;
183     }
184
185     //
186
// IDL:objectweb.org/ccm/runtime/ComponentPOAService:1.0
187
//
188

189     final public org.omg.Components.CCMHome
190     activate_home(String JavaDoc uuid)
191     {
192         // create a dummy id
193
byte[] id = Double.toString(Math.random()).getBytes();
194
195         return activate_home_with_id(id, uuid);
196     }
197
198     final public org.omg.Components.CCMHome
199     activate_home_with_id(byte[] id, String JavaDoc uuid)
200     {
201         final String JavaDoc opname = "activate_home_with_id";
202
203         // load code archive
204
// obtain archive location from component archive
205
org.omg.CORBA.Object JavaDoc obj = null;
206         org.coach.ECM.ExtComponentInstallation extci = null;
207         org.coach.ECM.ComponentArchive carch = null;
208
209         obj = _orb_service.resolve_initial_references("ExtComponentInstallation");
210         extci = org.coach.ECM.ExtComponentInstallationHelper.narrow(obj);
211         carch = extci.get_component_archive(uuid);
212
213         // manage dependencies
214
// valuefactories
215
org.coach.ECM.ValuetypeFactoryDependency[] vfes = carch.get_valuetype_factories();
216         org.coach.ECM.ValuetypeFactoryDependency vfe = null;
217         for (int i=0;i<vfes.length;i++) {
218             vfe = vfes[i];
219
220             // add code location to class loader
221
TheClassLoader.addResource(vfe.code_location);
222
223             // create value factory
224
Object JavaDoc ovf = TheClassLoader.newInstance(vfe.factory_entrypoint);
225             org.omg.CORBA.portable.ValueFactory JavaDoc vf = (org.omg.CORBA.portable.ValueFactory JavaDoc)obj;
226
227             // register
228
ValueFactory fact = new ValueFactoryImpl(vfe.repid, vf);
229             _orb_service.register_valuefactory(fact);
230         }
231
232
233         // add code to class loader
234
TheClassLoader.addResource(carch.code_location());
235
236         // create instance from entrypoint
237
// normally, it should be a HomeExecutor instance
238
org.omg.Components.HomeExecutor hexe = null;
239         Object JavaDoc jobj = TheClassLoader.newInstance(carch.code_entrypoint());
240
241         // check if null
242
if (jobj==null) {
243             // NOTE: TODO: should be reported as an exception
244
final String JavaDoc msg = "FAILED (invalid entrypoint)";
245             TheLogger.error(_class_name, opname, msg);
246         }
247
248         if (jobj instanceof org.omg.Components.HomeExecutor) {
249             hexe = (org.omg.Components.HomeExecutor)jobj;
250         } else {
251             // NOTE: TODO: should be reported as an exception
252
final String JavaDoc msg = "FAILED (not a Components::HomeExecutor)";
253             TheLogger.error(_class_name, opname, msg);
254         }
255
256         // activate home
257
// NOTE: must be done before any narrow (else OBJECT_NOT_EXISTS)
258
// NOTE: the home executor MUST inherit from the ::Components::Servant
259
if (hexe instanceof org.omg.Components.Servant) {
260             try {
261                 org.omg.Components.Servant servant = (org.omg.Components.Servant)hexe;
262                 _home_poa.activate_object_with_id(id, servant.as_native_servant());
263             } catch (Exception JavaDoc ex) {
264                 // NOTE: should not happen
265
TheLogger.error(_class_name, opname, "FAILED", ex);
266                 return null;
267             }
268         } else {
269             TheLogger.error(_class_name, opname, "FAILED (not a CCM servant)");
270             return null;
271         }
272
273         // create ref
274
String JavaDoc hrepid = carch.home_repid();
275         org.omg.CORBA.Object JavaDoc ref = _home_poa.create_reference_with_id(id, hrepid);
276         org.omg.Components.CCMHome href = org.omg.Components.CCMHomeHelper.narrow(ref);
277
278         // create ECM context
279
// NOTE: the interceptor factory is given to the context (for stub interceptors creation)
280
// and the servant activator (for sk interceptors creation)
281
CallInterceptorFactory ifact = repid2Factory(hrepid);
282         if (ifact!=null) {
283             ifact.setContextInfo(_orb_service, _services_set, uuid);
284         }
285
286         HomeLocalEventsImpl levts = new HomeLocalEventsImpl(ifact);
287         HomeLocalReceptaclesImpl lrecs = new HomeLocalReceptaclesImpl(ifact);
288         // NOTE: TODO: how can we select the right context (monolithic or segmented ?)
289
org.omg.Components.CCM2Context ctx = new HomeSession2ContextImpl(href, _orb_service, this, _services_set, levts, lrecs);
290
291         // set context
292
hexe.set_ccm_context(ctx);
293
294         // register home to servant activator
295
_servant_activator.registerHome(id, uuid, hexe, ifact, ctx);
296
297         return href;
298     }
299
300     final public void
301     passivate_home(org.omg.Components.CCMHome href)
302     {
303         try {
304             // deactivate so that future call leads to incarnation
305
byte[] id = _home_poa.reference_to_id(href);
306             _home_poa.deactivate_object(id);
307         } catch (Exception JavaDoc ex) {
308             // ignore (log debug message as this is nevertheless not a normal behaviour)
309
final String JavaDoc opname = "passivate_component";
310             TheLogger.debug(_class_name, opname, "IGNORE", ex);
311         }
312     }
313
314     //
315
// IDL:org/objectweb/ccm/runtime/ComponentPOAService:1.0
316
//
317

318     final public org.omg.CORBA.Object JavaDoc
319     create_ref_with_id(byte[] id, String JavaDoc repid)
320     {
321         // register to servant activator
322
_servant_activator.registerComponent(id);
323
324         // create a ref
325
return _component_poa.create_reference_with_id(id, repid);
326     }
327
328     final public byte[]
329     get_id_from_ref(org.omg.CORBA.Object JavaDoc ref)
330     throws InvalidReference
331     {
332         try {
333             return _component_poa.reference_to_id(ref);
334         } catch (Exception JavaDoc ex) {
335             final String JavaDoc opname = "get_id_from_ref";
336             TheLogger.debug(_class_name, opname, "FAILED (rethrown)", ex);
337             throw new InvalidReference();
338         }
339     }
340
341     final public void
342     passivate_component(byte[] id)
343     {
344         final String JavaDoc opname = "passivate_component";
345         try {
346             // deactivate so that future call leads to incarnation
347
_component_poa.deactivate_object(id);
348         } catch (Exception JavaDoc ex) {
349             // ignore (log debug message as this is nevertheless not a normal behaviour)
350
TheLogger.debug(_class_name, opname, "IGNORE", ex);
351         }
352     }
353
354     final public void
355     remove_component(org.omg.Components.CCMObject comp)
356     {
357         final String JavaDoc opname = "remove_component";
358         try {
359             // get id from ref
360
byte[] id = _component_poa.reference_to_id(comp);
361
362             // unregister from servant activator
363
_servant_activator.unregisterComponent(id);
364
365             // deactivate so that future call leads to incarnation
366
_component_poa.deactivate_object(id);
367         } catch (Exception JavaDoc ex) {
368             // ignore (log debug message as this is nevertheless not a normal behaviour)
369
TheLogger.debug(_class_name, opname, "IGNORE", ex);
370         }
371     }
372 }
373
Popular Tags