KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > Containers > ComponentServantImpl


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2001-2005 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): Mathieu Vadet.
23 Contributor(s): Sylvain Leblanc, Romain Rouvoy, Philippe Merle.
24
25 ====================================================================*/

26
27 package org.objectweb.openccm.Containers;
28
29 import org.objectweb.openccm.Containers.MetaInformation.ComponentInstance;
30
31 /**
32  * This interface ??????.
33  *
34  * @author <a HREF="mailto:Mathieu.Vadet@lifl.fr">Mathieu Vadet</a>
35  *
36  * @version 0.3
37  */

38
39 public class ComponentServantImpl
40        extends org.omg.CORBA.LocalObject JavaDoc
41        implements ComponentServant,
42                   ActivationContext
43 {
44     // ==================================================================
45
//
46
// Internal state.
47
//
48
// ==================================================================
49

50     /**
51      **
52      **/

53     protected HomeServant the_home_servant_;
54
55     /**
56      **
57      **/

58     protected ComponentExecutor the_component_executor_;
59
60   /** MODIF MetaInformation
61    **
62    ** The component instance MI
63    **/

64   
65   protected ComponentInstance the_component_instance_MI;
66
67     /**
68      **
69      **/

70     protected org.objectweb.ccm.util.WeakTable active_objects_;
71
72     /**
73      **
74      **/

75     protected org.objectweb.ccm.util.WeakTable activation_coords_;
76
77     /**
78      **
79      **/

80     protected org.omg.CORBA.Object JavaDoc[] the_port_references_;
81
82     /**
83      **
84      **/

85     protected org.omg.Components.CCMObject the_component_ref_;
86
87     /**
88      **
89      **/

90     protected org.omg.Components.EnterpriseComponent the_component_instance_;
91
92     /**
93      **
94      **/

95     protected short state_;
96
97     /**
98      **
99      **/

100     protected org.omg.Components.PrimaryKeyBase the_primary_key_;
101
102     /**
103      **
104      **/

105     protected ServantLocatorImpl the_servant_locator_;
106
107     // ==================================================================
108
//
109
// Constructor.
110
//
111
// ==================================================================
112

113     /**
114      ** The constructor.
115      **
116      **/

117     public
118     ComponentServantImpl(ServantLocatorImpl locator,
119                          HomeServant home_servant,
120                          org.omg.Components.EnterpriseComponent instance,
121                          org.omg.Components.PrimaryKeyBase key)
122     {
123         the_servant_locator_ = locator;
124         the_home_servant_ = home_servant;
125         the_primary_key_ = key;
126         the_component_executor_ = null;
127         the_port_references_ = new org.omg.CORBA.Object JavaDoc[home_servant.the_home_executor()._the_port_uids().length];
128         for (int i=0;i<the_port_references_.length;i++)
129             the_port_references_[i] = null;
130         the_component_ref_ = null;
131         active_objects_ = null;
132         activation_coords_ = null;
133         state_ = ActivationContext.DEACTIVATED;
134         
135         //////////////////// MODIF MI
136

137         // component instance creation
138

139         if(home_servant.the_home_instance() != null)
140         {
141           the_component_instance_MI = home_servant.
142                                       the_home_instance().
143                                       create_component_instance();
144         }
145         //////////////////// FIN MODIF MI
146

147         initializeComponentExecutor(instance);
148     }
149
150     // ==================================================================
151
//
152
// Internal methods.
153
//
154
// ==================================================================
155

156     /**
157      * Cast an EntrepriseComponent executor to a SessionComponent executor.
158      *
159      * @param executor The EntrepriseComponent executor.
160      *
161      * @return As a SessionComponent or null.
162      */

163     protected org.omg.Components.SessionComponent
164     castToSessionComponent(org.omg.Components.EnterpriseComponent executor)
165     {
166         return (executor instanceof org.omg.Components.SessionComponent)?
167       (org.omg.Components.SessionComponent)executor : null;
168     }
169
170     /**
171      **
172      **/

173     protected void
174     initializeComponentExecutor(org.omg.Components.EnterpriseComponent instance)
175     {
176         if (instance==null)
177             return ;
178
179         // create the interceptor hashtable.
180
active_objects_ = new org.objectweb.ccm.util.WeakTable();
181
182         // create the activation coordinators hashtable.
183
activation_coords_ = new org.objectweb.ccm.util.WeakTable();
184
185         HomeExecutorBase home = the_home_servant_.the_home_executor();
186
187         // create the executor.
188
the_component_executor_ = home._get_component_executor();
189
190         // set the executor as the context for the enterprise component.
191
org.omg.Components.SessionComponent sc = castToSessionComponent(instance);
192     if(sc != null)
193     {
194             try
195             {
196                 sc.set_session_context((org.omg.Components.SessionContext)the_component_executor_);
197             } catch(org.omg.Components.CCMException ex) {
198               //
199
// TODO : what ????
200
//
201
throw new Error JavaDoc("Activation failed");
202             }
203     }
204
205         // check if the instance is a ExecutorLocator.
206
org.omg.Components.ExecutorLocator locator = null;
207         if (instance instanceof org.omg.Components.ExecutorLocator)
208             locator = (org.omg.Components.ExecutorLocator)instance;
209         // else create a wrapper.
210
else
211             locator = home._get_executor_locator(instance);
212
213         // set the infos needed by the component executor.
214
the_component_executor_._executor_locator(locator);
215         the_component_executor_._component_servant(this);
216     }
217
218
219     // ==================================================================
220
//
221
// Public methods for the ComponentServant interface.
222
//
223
// ==================================================================
224

225     /**
226      **
227      **/

228     public org.omg.CORBA.Object JavaDoc
229     get_reference(java.lang.String JavaDoc uid)
230     {
231         HomeExecutorBase home = the_home_servant_.the_home_executor();
232         java.lang.String JavaDoc comp_uid = home._the_component_uid();
233         if (comp_uid.equals(uid))
234             return the_component_ref_;
235
236         java.lang.String JavaDoc[] uids = home._the_port_uids();
237         int i=0;
238         while (!uids[i].equals(uid))
239             i++;
240
241         if (the_port_references_[i]==null)
242             the_port_references_[i] = the_servant_locator_.createPortReference(this, uid);
243
244         return the_port_references_[i];
245     }
246
247     /**
248      **
249      **/

250     public Interceptor
251     get_native_interface(java.lang.String JavaDoc uid)
252     {
253         HomeExecutorBase home = the_home_servant_.the_home_executor();
254
255         // Pass through the activation interception first.
256
// Check if the coordinator has already been created.
257
/*
258  * TODO : think more about this point
259  *
260         ActivationCoordinator coord = (ActivationCoordinator)activation_coords_.get(uid);
261         if (coord==null)
262         {
263             ActivationController[] ctrls = null;
264             SystemConfiguration comp_config = the_home_servant_.the_component_configuration();
265             coord = comp_config.create_activation_coordinator(uid, the_home_servant_.the_home_finder());
266             ctrls = comp_config.create_activation_controllers(uid, the_home_servant_.the_home_finder());
267             for (int i=0;i<ctrls.length;i++)
268                 coord.add(ctrls[i]);
269
270             // store the coordinator.
271             activation_coords_.put(uid, coord);
272         }
273
274         // call the preinvoke operation
275         coord.preinvoke(this);
276  */

277
278         // check if the associated interceptor is not activated
279
Interceptor inter = (Interceptor)active_objects_.get(uid);
280
281         if (inter==null)
282         {
283             // retrieve the interceptor
284
inter = home._get_interceptor(uid);
285
286             // create the call context for the interceptor and set the needed infos
287

288 // Updated for Bug #466.
289
// Contributor: Philippe Merle
290

291 // CallContext context = new ComponentCallContext(the_home_servant_, uid);
292
CallContext context = new ComponentCallContext(this, the_home_servant_, uid);
293
294 // Update end.
295

296             inter._call_context(context);
297
298             // store the interceptor
299
active_objects_.put(uid, inter);
300         }
301
302         // retrieve the name.
303
int idx = uid.lastIndexOf('/');
304         if (idx==-1) idx = uid.indexOf(':');
305         int idx2 = uid.lastIndexOf(':');
306         java.lang.String JavaDoc name = uid.substring(idx+1, idx2);
307
308         // obtain the executor.
309
try
310         {
311             org.omg.CORBA.Object JavaDoc exe =
312                  the_component_executor_._executor_locator().obtain_executor(name);
313
314             // special case for the component itself
315
if (uid.equals(home._the_component_uid()))
316             {
317                 the_component_executor_._delegate((org.omg.Components.EnterpriseComponent)exe);
318                 inter._delegate((java.lang.Object JavaDoc)the_component_executor_);
319             }
320             else
321             {
322                 inter._delegate(home._get_executor_wrapper(exe, uid));
323             }
324         }
325         catch(org.omg.Components.CCMException ex)
326         {
327             //
328
// TODO : what ???
329
//
330
}
331
332         return inter;
333     }
334
335     /**
336      **
337      **/

338     public void
339     release_native_interface(Interceptor inter)
340     {
341         HomeExecutorBase home = the_home_servant_.the_home_executor();
342         java.lang.String JavaDoc uid = inter._call_context().uid();
343
344         // Pass through the activation interception first.
345
// The coordinator should always exist.
346
/*
347  * TODO : think more about this point
348  *
349         ActivationCoordinator coord = (ActivationCoordinator)activation_coords_.get(uid);
350
351         // call the postinvoke operation
352         coord.postinvoke(this);
353 */

354
355         try
356         {
357             org.omg.CORBA.Object JavaDoc exe = null;
358             // special case for the component itself
359
if (uid.equals(home._the_component_uid()))
360             {
361                 exe = (org.omg.CORBA.Object JavaDoc)the_component_executor_._delegate();
362
363 // The next line was be commented to avoid multi-threading problems when
364
// several threads execute operation calls in parallel on a same component.
365
// Previously, a CORBA::UNKNOWN exception could be thrown due to a NullPointerException.
366
//
367
// the_component_executor_._delegate(null);
368
//
369
}
370             else
371             {
372                 exe = (org.omg.CORBA.Object JavaDoc)inter._delegate();
373             }
374             the_component_executor_._executor_locator().release_executor(exe);
375
376 // The next line was be commented to avoid multi-threading problems when
377
// several threads execute operation calls in parallel on a same component.
378
// Previously, a CORBA::UNKNOWN exception could be thrown due to a NullPointerException.
379
//
380
// inter._delegate(null);
381
//
382

383         }
384         catch(org.omg.Components.CCMException ex)
385         {
386              //
387
// TODO : what ???
388
//
389
}
390     }
391
392     /**
393      **
394      **/

395     public ActivationContext
396     activation_context()
397     {
398         return this;
399     }
400
401     /**
402      **
403      **/

404     public HomeServant
405     the_home_servant()
406     {
407         return the_home_servant_;
408     }
409
410     /**
411      **
412      **/

413     public ComponentExecutor
414     the_component_executor()
415     {
416         return the_component_executor_;
417     }
418
419     /**
420      **
421      **/

422     public org.omg.Components.CCMObject
423     the_component_ref()
424     {
425         return the_component_ref_;
426     }
427
428     /**
429      **
430      **/

431     public org.omg.Components.PrimaryKeyBase
432     the_registration_info()
433     {
434         return the_primary_key_;
435     }
436
437     // ==================================================================
438
//
439
// Public methods for the ActivationContext interface.
440
//
441
// ==================================================================
442

443     //
444
// IDL:goal.lifl.fr/OpenCCM/Containers/ActivationContext/state:1.0
445
//
446
/**
447      **
448      **/

449     public void
450     state(short new_state)
451     {
452         // if no state changes then do nothing.
453
if (state_==new_state)
454             return ;
455
456         //
457
// TODO : change to be poolable also
458
// at this time, only instance mode is supported.
459
//
460
switch (new_state)
461         {
462             case DEACTIVATED :
463                 // release the executor.
464
the_component_executor_ = null;
465                 // the_component_instance_ = null;
466
active_objects_ = null;
467                 activation_coords_ = null;
468                 break;
469             case ACTIVATED :
470                  // check if we need to create a new executor.
471
if ((the_component_executor_==null) && (the_component_instance_==null))
472                 {
473                     // create a new component instance.
474
HomeExecutorBase home = null;
475                     home = the_home_servant_.the_home_executor();
476
477                     // create an instance.
478
try
479                     {
480                         if (!home._has_primary_key())
481                             the_component_instance_ = ((HomeExecutor)home)._get_enterprise_component();
482                         else
483                             the_component_instance_ = ((HomeExecutorWithPK)home)._get_enterprise_component(the_primary_key_);
484                     }
485                     catch(org.omg.Components.CCMException ex)
486                     {
487                         //
488
// TODO : what ????
489
//
490
throw new Error JavaDoc("Creation of instance failed !");
491                     }
492
493                     // create the executor.
494
initializeComponentExecutor(the_component_instance_);
495                 }
496                 else if (the_component_executor_==null)
497                 {
498                     // create the executor.
499
initializeComponentExecutor(the_component_instance_);
500                 }
501                 break;
502             default :
503                 break;
504         }
505
506         // change the state.
507
state_ = new_state;
508     }
509
510     //
511
// IDL:goal.lifl.fr/OpenCCM/Containers/ActivationContext/state:1.0
512
//
513
/**
514      **
515      **/

516     public short
517     state()
518     {
519         return state_;
520     }
521
522     // ==================================================================
523
//
524
// Public methods.
525
//
526
// ==================================================================
527

528     /**
529      **
530      **/

531     public void
532     setComponentRef(org.omg.Components.CCMObject ref)
533     {
534         the_component_ref_ = ref;
535     }
536
537     /**
538      **
539      **/

540     public org.omg.CORBA.Object JavaDoc
541     getReferenceWithInstance(org.omg.Components.EnterpriseComponent instance)
542     {
543         if (the_component_instance_==instance)
544             return the_component_ref_;
545
546         return null;
547     }
548
549     /**
550      ** Remove the associated component.
551      **/

552     public void
553     remove()
554         throws org.omg.Components.RemoveFailure
555     {
556         the_servant_locator_.removeReference(the_component_ref_);
557
558         org.omg.Components.EnterpriseComponent delegate = the_component_executor_._delegate();
559
560     org.omg.Components.SessionComponent sc = castToSessionComponent(delegate);
561         if(sc != null)
562         {
563             try
564             {
565                 sc.ccm_remove();
566             } catch (org.omg.Components.CCMException ex) {
567           // TODO: log exception.
568
// set the reason field of the RemoveFailure exception.
569
throw new org.omg.Components.RemoveFailure();
570             }
571         }
572     }
573
574     /* (non-Javadoc)
575      * @see org.objectweb.openccm.Containers.ComponentServant#the_component_instance()
576      */

577     public ComponentInstance the_component_instance()
578     {
579       return the_component_instance_MI;
580     }
581 }
582
Popular Tags