KickJava   Java API By Example, From Geeks To Geeks.

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


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>PortableServer::ServantActivator</tt> OMG IDL interface.</p>
36  **
37  ** @see org.objectweb.ccm.runtime.ComponentPOAServiceImpl ComponentPOAServiceImpl
38  **/

39 class ComponentServantActivatorImpl
40 extends org.omg.CORBA.LocalObject JavaDoc
41 implements org.omg.PortableServer.ServantActivator JavaDoc
42 {
43     //
44
static final private String JavaDoc _class_name = "ComponentServantActivatorImpl";
45     private ORBService _orb_service;
46     private ServicesSet _services_set;
47     private java.util.HashMap JavaDoc _id2home; // (home object id, home exe)
48
private java.util.HashMap JavaDoc _component_ids; // (object id, null)
49
private java.util.HashMap JavaDoc _id2pcomp; // (object id, passivate component)
50
private java.util.HashMap JavaDoc _id2acomp; // (object id, activated component)
51

52     private MonolithicIdCodec _midcodec;
53     private SegmentedIdCodec _sidcodec;
54
55     public
56     ComponentServantActivatorImpl()
57     {
58         //
59
_orb_service = null;
60         _services_set = null;
61         _id2home = new java.util.HashMap JavaDoc();
62         _component_ids = new java.util.HashMap JavaDoc();
63         _id2pcomp = new java.util.HashMap JavaDoc();
64         _id2acomp = new java.util.HashMap JavaDoc();
65
66         _midcodec = null;
67         _sidcodec = null;
68     }
69
70     //
71
// internal operations
72
//
73

74     private org.omg.Components.ExecutorSegmentBase
75     lookupPassivated(byte[] id)
76     {
77         final String JavaDoc opname = "lookupPassivated";
78
79         // check if the request targets a passivated executor
80
String JavaDoc pcomp = (String JavaDoc)_id2pcomp.remove(new String JavaDoc(id));
81         if (pcomp!=null) {
82             // unmarshall executor
83
try {
84                 java.io.ByteArrayInputStream JavaDoc bin= new java.io.ByteArrayInputStream JavaDoc(pcomp.getBytes());
85                 java.io.ObjectInputStream JavaDoc oin = new java.io.ObjectInputStream JavaDoc(bin);
86                 Object JavaDoc obj = oin.readObject();
87
88                 if (obj instanceof org.omg.Components.ExecutorSegmentBase) {
89                     return (org.omg.Components.ExecutorSegmentBase)obj;
90                 } else {
91                     // NOTE: should not happen
92
TheLogger.error(_class_name, opname, "FAILED (not a executor segment: "+obj.getClass()+"");
93                     return null;
94                 }
95             } catch (Exception JavaDoc ex) {
96                 // NOTE: what can be done if serialization fails ?
97
TheLogger.error(_class_name, opname, "FAILED", ex);
98                 return null;
99             }
100         }
101
102         return null;
103     }
104
105     //
106
// friend operations
107
//
108

109     final protected void
110     setConfiguration(ORBService orbs, ServicesSet sset)
111     {
112         _orb_service = orbs;
113         _services_set = sset;
114
115         // create codecs
116
_midcodec = new MonolithicIdCodec(_orb_service);
117         _sidcodec = new SegmentedIdCodec(_orb_service);
118     }
119
120     synchronized final protected void
121     registerHome(byte[] id, String JavaDoc uuid,
122                  org.omg.Components.HomeExecutor hexe,
123                  CallInterceptorFactory ifact,
124                  org.omg.Components.CCM2Context ctx)
125     {
126         CSAStorage storage = null;
127         if (ifact!=null) {
128             storage = new CSAStorage(hexe, ctx, ifact);
129         } else {
130             storage = new CSAStorage(hexe, ctx);
131         }
132
133         // store
134
_id2home.put(new String JavaDoc(id), storage);
135     }
136
137     synchronized final protected void
138     registerComponent(byte[] id)
139     {
140         // store
141
_component_ids.put(new String JavaDoc(id), null);
142     }
143
144     synchronized final protected void
145     unregisterComponent(byte[] id)
146     {
147         // remove from storage
148
_component_ids.remove(new String JavaDoc(id));
149     }
150
151     //
152
// IDL:omg.org/PortableServer/ServantManager:1.0
153
//
154

155     //
156
// IDL:omg.org/PortableServer/ServantActivator:1.0
157
//
158

159     // NOTE: no sychronisation required as the POA shall serialize
160
// incarnation requests and incarnate/etherealize are mutually exclusive
161
final public org.omg.PortableServer.Servant JavaDoc
162     incarnate(byte[] id, org.omg.PortableServer.POA JavaDoc poa)
163     throws org.omg.PortableServer.ForwardRequest JavaDoc
164     {
165         final String JavaDoc opname = "incarnate";
166         try {
167             return unsafe_incarnate(id, poa);
168         } catch (org.omg.PortableServer.ForwardRequest JavaDoc ex) {
169             // rethrow
170
throw ex;
171         } catch (Throwable JavaDoc ex) {
172             TheLogger.debug(_class_name, opname, "FAILED", ex);
173             return null;
174         }
175     }
176
177     final public org.omg.PortableServer.Servant JavaDoc
178     unsafe_incarnate(byte[] id, org.omg.PortableServer.POA JavaDoc poa)
179     throws org.omg.PortableServer.ForwardRequest JavaDoc
180     {
181         final String JavaDoc opname = "unsafe_incarnate";
182
183         // check that id is known
184
if (!_component_ids.containsKey(new String JavaDoc(id))) {
185             // either the id comes from a reference created by another POA, or the reference was removed
186
// in both case, return null so that OBJECT_NOT_EXIST (or BAD_INV_ORDER ?) may be thrown
187
final String JavaDoc msg = "FAILED (unknown id)";
188             TheLogger.debug(_class_name, opname, msg);
189             return null;
190         }
191
192         // create a new executor for this reference
193
org.omg.Components.ExecutorSegmentBase cexe = null;
194         CSAStorage storage = null;
195
196         // get the component executor
197
// try monolithic id
198
MonolithicId mid = _midcodec.decode(id);
199         if (mid!=null) {
200             // NOTE: no class cast should occur
201
storage = (CSAStorage)_id2home.get(new String JavaDoc(mid.home_id));
202             org.omg.Components.HomeExecutor hexe = storage.home_executor;
203
204             // check if a passivate executor exists
205
cexe = lookupPassivated(id);
206
207             if (cexe==null) {
208                 // create a new component executor
209
cexe = hexe.create_component_segment(org.omg.Components.COMPONENT_ID.value);
210             }
211         }
212
213         // try segmented if mid==null
214
SegmentedId sid = null;
215         if (mid==null) {
216             // NOTE: sid can't be null
217
sid = _sidcodec.decode(id);
218
219             // NOTE: no class cast should occur
220
storage = (CSAStorage)_id2home.get(new String JavaDoc(sid.home_id));
221             org.omg.Components.HomeExecutor hexe = storage.home_executor;
222
223             // check if a passivate executor exists
224
cexe = lookupPassivated(id);
225
226             if (cexe==null) {
227                 // create a new component executor
228
cexe = hexe.create_component_segment(sid.segment_id);
229             }
230         }
231
232         // check if it supports SessionComponent callback
233
if (cexe instanceof org.omg.Components.SessionComponent) {
234             org.omg.Components.SessionComponent scomp = (org.omg.Components.SessionComponent)cexe;
235             org.omg.Components.SessionContext sessionctx = null;
236
237             // find context
238
if (mid!=null) {
239                 // NOTE: create a new context each time
240
HomeSession2ContextImpl hctx = (HomeSession2ContextImpl)storage.ccm2context;
241                 Session2ContextImpl segctx = new Session2ContextImpl(hctx, mid.component_id);
242                 sessionctx = segctx;
243             } else {
244                 // segmented case (use main segment id as key)
245
org.omg.Components.SegmentDescr[] descrs = sid.segment_descrs;
246                 byte[] mainsegid = null;
247                 for (int i=0;i<descrs.length;i++) {
248                     if (descrs[i].segment_id==org.omg.Components.COMPONENT_ID.value) {
249                         mainsegid = descrs[i].object_id;
250                         break;
251                     }
252                 }
253
254                 // NOTE: create a new context each time
255
HomeSegmentedSession2ContextImpl hctx = (HomeSegmentedSession2ContextImpl)storage.ccm2context;
256                 SegmentedSession2ContextImpl segctx = new SegmentedSession2ContextImpl(hctx, mainsegid);
257                 sessionctx = segctx;
258             }
259
260             try {
261                 scomp.set_session_context(sessionctx);
262                 scomp.ccm_activate();
263             } catch (org.omg.Components.CCMException ex) {
264                 // NOTE: what can be done if activation fails ?
265
TheLogger.debug(_class_name, opname, "FAILED", ex);
266             }
267         } else {
268             // log
269
final String JavaDoc msg = "IGNORE (SessionComponent not supported)";
270             TheLogger.debug(_class_name, opname, msg);
271         }
272
273         // store
274
_id2acomp.put(new String JavaDoc(id), cexe);
275
276         // check if interceptors are available
277
if (storage.interceptor_factory!=null) {
278             org.omg.PortableServer.Servant JavaDoc poaserv = null;
279             // check if monolithic or segmented
280
CallInterceptorFactory ifact = storage.interceptor_factory;
281             if (sid!=null) {
282                 poaserv = ifact.createSkeletonInterceptor(sid.segment_id, cexe);
283             } else {
284                 poaserv = ifact.createSkeletonInterceptor(org.omg.Components.COMPONENT_ID.value, cexe);
285             }
286
287             return poaserv;
288         } else {
289             // log a message
290
TheLogger.debug(_class_name, opname, "IGNORE (no interceptor factory)");
291         }
292
293         if (cexe instanceof org.omg.Components.Servant) {
294             org.omg.Components.Servant servant = (org.omg.Components.Servant)cexe;
295             return servant.as_native_servant();
296         } else {
297             TheLogger.error(_class_name, opname, "FAILED (not a CCM servant)");
298             return null;
299         }
300     }
301
302     // NOTE: no sychronisation required as the POA shall serialize
303
// etherealization requests and incarnate/etherealize are mutually exclusive
304
final public void
305     etherealize(byte[] id,
306                 org.omg.PortableServer.POA JavaDoc poa,
307                 org.omg.PortableServer.Servant JavaDoc servant,
308                 boolean cleanup_in_progress,
309                 boolean remaining_activations)
310     {
311         // check if removed
312
final String JavaDoc opname = "etherealize";
313         final boolean removed = _component_ids.containsKey(new String JavaDoc(id))? false : true;
314         
315         // NOTE: TODO: destroy the servant if it's a call interceptor
316

317         // check if the delegate supports SessionComponent callback
318
final Object JavaDoc obj = _id2acomp.remove(new String JavaDoc(id));
319
320         if (obj instanceof org.omg.Components.SessionComponent) {
321             org.omg.Components.SessionComponent scomp = (org.omg.Components.SessionComponent)obj;
322
323             if (removed) {
324                 // NOTE: call callbacks: passivate and remove
325
try {
326                     // NOTE: no serialization/storage is needed
327
scomp.ccm_passivate();
328                     scomp.ccm_remove();
329                 } catch (org.omg.Components.CCMException ex) {
330                     // NOTE: what can be done if passivation/removal fails ?
331
TheLogger.debug(_class_name, opname, "FAILED", ex);
332                 }
333             } else {
334                 // NOTE: passivate first and then serialize
335
try {
336                     scomp.ccm_passivate();
337                 } catch (org.omg.Components.CCMException ex) {
338                     // NOTE: what can be done if passivation fails ?
339
TheLogger.debug(_class_name, opname, "FAILED", ex);
340                 }
341
342                 try {
343                     final java.io.ByteArrayOutputStream JavaDoc bout= new java.io.ByteArrayOutputStream JavaDoc();
344                     final java.io.ObjectOutputStream JavaDoc oout = new java.io.ObjectOutputStream JavaDoc(bout);
345                     oout.writeObject(scomp);
346                     oout.flush();
347                     bout.flush();
348                     // get the sequence of bytes
349
final byte[] pcomp = bout.toByteArray();
350                     oout.close();
351
352                     // store
353
_id2pcomp.put(new String JavaDoc(id), new String JavaDoc(pcomp));
354                 } catch (Exception JavaDoc ex) {
355                     // NOTE: what can be done if serialization fails ?
356
TheLogger.error(_class_name, opname, "FAILED", ex);
357                 }
358             }
359         } else {
360             // log
361
final String JavaDoc msg = "IGNORE (SessionComponent not supported)";
362             TheLogger.debug(_class_name, opname, msg);
363         }
364     }
365 }
366
367 //
368
//
369
//
370
class CSAStorage
371 {
372     // storage
373
public org.omg.Components.HomeExecutor home_executor;
374     public org.omg.Components.CCM2Context ccm2context;
375     public CallInterceptorFactory interceptor_factory;
376
377     // default constructor
378
public
379     CSAStorage(org.omg.Components.HomeExecutor hexe,
380                org.omg.Components.CCM2Context ctx)
381     {
382         // storage
383
home_executor = hexe;
384         ccm2context = ctx;
385         interceptor_factory = null;
386     }
387
388     // constructor with COPIs
389
public
390     CSAStorage(org.omg.Components.HomeExecutor hexe,
391                org.omg.Components.CCM2Context ctx,
392                CallInterceptorFactory ifact)
393     {
394         // storage
395
home_executor = hexe;
396         ccm2context = ctx;
397         interceptor_factory = ifact;
398     }
399 }
400
401
Popular Tags