KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > ir3 > IRObject_impl


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2004 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): Philippe Merle.
23 Contributor(s): Mathieu Vadet, Christophe Demarey.
24
25 ====================================================================*/

26
27 package org.objectweb.openccm.ir3;
28
29 // Package dependencies
30
import org.omg.CORBA.*;
31 import org.omg.CORBA.ComponentIR.*;
32
33 /**
34  * Implementation of the CORBA::IRObject interface.
35  *
36  * @author <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>
37  * <a HREF="mailto:Mathieu.Vadet@lifl.fr">Mathieu Vadet</a>
38  *
39  * @version 0.4
40  */

41
42 abstract public class IRObject_impl
43               extends IFRObject
44            implements IRObjectOperations,
45                       IDLTypeOperations
46 {
47     // ==================================================================
48
//
49
// Internal state.
50
//
51
// ==================================================================
52

53     /** Boolean to mark if the object is under destruction or not. */
54     private boolean is_under_destruction_;
55
56     // ==================================================================
57
//
58
// Constructor.
59
//
60
// ==================================================================
61

62     /**
63      * The constructor.
64      */

65     public
66     IRObject_impl(IFR ifr)
67     {
68         // call the IFRObject constructor.
69
super(ifr);
70
71         // Inits internal state.
72
is_under_destruction_ = false;
73     }
74
75     // ==================================================================
76
//
77
// Internal methods.
78
//
79
// ==================================================================
80

81     /**
82      * Tests if the object is under destruction.
83      *
84      * @return True if under destruction else false.
85      */

86     protected boolean
87     isUnderDestruction()
88     {
89         return is_under_destruction_;
90     }
91
92     // ==================================================================
93
//
94
// Internal cast methods.
95
//
96
// ==================================================================
97

98     /**
99      * Warning: this operation must be implemented by subclasses.
100      * It allows us to avoid some casts while creating ::CORBA::TypeCode
101      */

102     protected TypeCode
103     recursiveType(java.util.List JavaDoc idSeq)
104     {
105         // must be implemented into subclasses when this has sense.
106
throw exceptionMustBeImplemented("IDL:omg.org/CORBA/IDLType/type:1.0");
107     }
108
109     /**
110      * Cast a CORBA::IDLType interface to an IRObject_impl.
111      */

112     protected IRObject_impl
113     castToLocal(IDLType type)
114     {
115         IRObject_impl tmp = (IRObject_impl)getIFR().toImpl(type);
116
117         if (tmp == null)
118             throw exceptionNotLocalObject();
119
120         return tmp;
121     }
122
123     /**
124      * Cast a CORBA::Container interface to a Container_impl.
125      */

126     protected Container_impl
127     castToLocal(org.omg.CORBA.Container JavaDoc container)
128     {
129         Container_impl tmp = (Container_impl)getIFR().toImpl(container);
130
131         if (tmp == null)
132             throw exceptionInvalidContainer();
133
134         return tmp;
135     }
136
137     /**
138      * Cast a CORBA::ExceptionDef interface to an ExceptionDef_impl.
139      */

140     protected ExceptionDef_impl
141     castToLocal(ExceptionDef exc)
142     {
143         ExceptionDef_impl impl = (ExceptionDef_impl)(getIFR().toImpl(exc));
144
145         if (impl == null)
146             throw exceptionNotLocalObject();
147
148         return impl;
149     }
150
151     /**
152      * Cast a CORBA::InterfaceDef interface to an InterfaceDef_impl.
153      */

154     protected InterfaceDef_impl
155     castToLocal(InterfaceDef itf)
156     {
157         InterfaceDef_impl tmp = (InterfaceDef_impl)getIFR().toImpl(itf);
158
159         if (tmp == null)
160             throw exceptionNotLocalObject();
161
162         return tmp;
163     }
164
165     /**
166      * Cast a CORBA::ValueDef interface to a ValueDef_impl.
167      */

168     protected ValueDef_impl
169     castToLocal(ValueDef value)
170     {
171         ValueDef_impl impl = (ValueDef_impl)(getIFR().toImpl(value));
172
173         if (impl == null)
174             throw exceptionNotLocalObject();
175
176         return impl;
177     }
178
179     /**
180      * Cast a CORBA::ComponentDef interface to a ComponentDef_impl.
181      */

182     protected ComponentDef_impl
183     castToLocal(ComponentDef comp)
184     {
185         ComponentDef_impl impl = (ComponentDef_impl)(getIFR().toImpl(comp));
186
187         if (impl == null)
188             throw exceptionNotLocalObject();
189
190         return impl;
191     }
192
193     /**
194      * Cast a CORBA::ComponentDef interface to an HomeDef_impl.
195      */

196     protected HomeDef_impl
197     castToLocal(HomeDef home)
198     {
199         HomeDef_impl impl = (HomeDef_impl)(getIFR().toImpl(home));
200
201         if (impl == null)
202             throw exceptionNotLocalObject();
203
204         return impl;
205     }
206
207     // ==================================================================
208
//
209
// Internal error methods.
210
//
211
// ==================================================================
212

213     /**
214      * Log errors.
215      */

216     protected void
217     logError(String JavaDoc message)
218     {
219     getIFR().log(1, this.getIdentity() + ": " + message + "!");
220     }
221
222     /**
223      * Log errors.
224      */

225     protected void
226     nextLogError(String JavaDoc message)
227     {
228     getIFR().log(1, this.getIdentity() + ": " + message);
229     }
230
231     /**
232      * Signal that an IFR object's method must be implemented.
233      */

234     protected SystemException
235     exceptionMustBeImplemented(String JavaDoc method)
236     {
237     logError(method + " not implemented");
238         return new NO_IMPLEMENT(SystemExceptionMinorValues.LIFLVMCID,
239                                 CompletionStatus.COMPLETED_NO);
240     }
241
242     /**
243      * Signal that a BAD PARAM exception.
244      */

245     protected SystemException
246     exceptionBadParam(String JavaDoc method)
247     {
248     logError(method + " BAD PARAM");
249         return new BAD_PARAM(SystemExceptionMinorValues.LIFLVMCID,
250                              CompletionStatus.COMPLETED_NO);
251     }
252
253     /**
254      * Signal that the IFR object's RID can't be redefined.
255      */

256     protected SystemException
257     exceptionRidAlreadyDefined(String JavaDoc rid)
258     {
259     logError(rid + " already defined in the IFR");
260         return new BAD_PARAM(SystemExceptionMinorValues.RidAlreadyDefined,
261                              CompletionStatus.COMPLETED_NO);
262     }
263
264     /**
265      * Signal that the IFR object's name can't be redefined.
266      */

267     protected SystemException
268     exceptionNameAlreadyUsedByImmediateScope(String JavaDoc name)
269     {
270     logError(name + " already used by immediate scope");
271         return new BAD_PARAM(SystemExceptionMinorValues.
272                              NameAlreadyUsedByImmediateScope,
273                              CompletionStatus.COMPLETED_NO);
274     }
275
276     /**
277      * Signal that the IFR object's name can't be redefined.
278      */

279     protected SystemException
280     exceptionNameAlreadyUsed(String JavaDoc name)
281     {
282     logError(name + " already used in the context");
283         return new BAD_PARAM(SystemExceptionMinorValues.NameAlreadyUsed,
284                              CompletionStatus.COMPLETED_NO);
285     }
286
287     /**
288      * Signal that the IR object is not local.
289      */

290     protected SystemException
291     exceptionNotLocalObject()
292     {
293         logError("not a local servant");
294         return new BAD_PARAM(SystemExceptionMinorValues.NotLocalObject,
295                              CompletionStatus.COMPLETED_NO);
296     }
297
298     /**
299      * Signals that the IFR object's is an invalid container.
300      */

301     protected SystemException
302     exceptionInvalidContainer()
303     {
304         logError("Target is not a valid container");
305         return new BAD_PARAM(SystemExceptionMinorValues.InvalidContainer,
306                              CompletionStatus.COMPLETED_NO);
307     }
308
309     /**
310      * Signal that a name clash in inherited context.
311      */

312     protected SystemException
313     exceptionNameClashInInheritedContext(String JavaDoc name)
314     {
315         logError(name + " name clash in inherited context");
316         return new BAD_PARAM(SystemExceptionMinorValues.
317                              NameClashInInheritedContext,
318                              CompletionStatus.COMPLETED_NO);
319     }
320
321     /**
322      * Signal that an abstract interface can only inherited
323      * from abstract interfaces.
324      */

325     protected SystemException
326     exceptionInvalidAbstractInterfaceInheritance(String JavaDoc absolute_name)
327     {
328         logError(absolute_name + " is not an abstract interface");
329         return new BAD_PARAM(SystemExceptionMinorValues.
330                              InvalidAbstractInterfaceInheritance,
331                              CompletionStatus.COMPLETED_NO);
332     }
333
334     /**
335      * Signal that a local interface can only inherited
336      * from local interfaces.
337      */

338     protected SystemException
339     exceptionInvalidLocalInterfaceInheritance(String JavaDoc absolute_name)
340     {
341         logError(absolute_name + " is not a local interface");
342         return new BAD_PARAM(SystemExceptionMinorValues.
343                              InvalidLocalInterfaceInheritance,
344                              CompletionStatus.COMPLETED_NO);
345     }
346
347     /**
348      * Signal that duplicate declarator for enums, structs, exceptions, operations.
349      */

350     protected SystemException
351     exceptionDuplicateDeclarator(String JavaDoc name, int pos)
352     {
353         logError(name + " is a duplicate declarator");
354         return new BAD_PARAM(SystemExceptionMinorValues.
355                              DuplicateDeclarator + pos,
356                              CompletionStatus.COMPLETED_NO);
357     }
358
359     /**
360      * Signal that only in params are needed.
361      */

362     protected SystemException
363     exceptionNeedOnlyInParams()
364     {
365         logError("only in params are needed");
366         return new BAD_PARAM(SystemExceptionMinorValues.NeedOnlyInParams,
367                              CompletionStatus.COMPLETED_NO);
368     }
369
370     /**
371      * Signal that no exceptions for oneway operations are excepted.
372      */

373     protected SystemException
374     exceptionNoExceptionsForOnewayOperation()
375     {
376         logError("oneway operation has no exceptions");
377         return new BAD_PARAM(SystemExceptionMinorValues.
378                              NoExceptionsForOnewayOperation,
379                              CompletionStatus.COMPLETED_NO);
380     }
381
382     /**
383      * Signal that an invalid type for value box.
384      */

385     protected SystemException
386     exceptionInvalidValueBoxType()
387     {
388         logError("invalid type for value box");
389         return new BAD_PARAM(SystemExceptionMinorValues.InvalidValueBoxType,
390                              CompletionStatus.COMPLETED_NO);
391     }
392
393     /**
394      * Signal that a BAD INV ORDER exception.
395      */

396     protected SystemException
397     exceptionBadInvOrder(String JavaDoc method)
398     {
399     logError(method + " BAD INV ORDER");
400         return new BAD_INV_ORDER(SystemExceptionMinorValues.LIFLVMCID,
401                                  CompletionStatus.COMPLETED_NO);
402     }
403
404     /**
405      * Signal that the IFR object is subject to dependency,
406      * it can't be destroyed!
407      */

408     protected SystemException
409     exceptionDependency()
410     {
411         logError("dependency exits in IFR preventing destruction of this object");
412
413         org.objectweb.util.ref.api.Referencable[] referenced_by = getReferencedBy();
414
415         if(referenced_by.length == 1)
416             nextLogError(" 1 dependency:");
417         else
418             nextLogError(" " + referenced_by.length + " dependencies:");
419
420         for(int i=0; i<referenced_by.length; i++) {
421             nextLogError(" [" + (i+1) + "] used by " + referenced_by[i].getIdentity());
422         }
423
424         return new BAD_INV_ORDER(SystemExceptionMinorValues.Dependency,
425                                  CompletionStatus.COMPLETED_NO);
426     }
427
428     /**
429      * Signal that the IFR object is indestructible.
430      */

431     protected SystemException
432     exceptionCanNotBeDestroyed()
433     {
434         logError("indestructible object");
435         return new BAD_INV_ORDER(SystemExceptionMinorValues.CanNotBeDestroyed,
436                                  CompletionStatus.COMPLETED_NO);
437     }
438
439     /**
440      * Signal that an ambiguous lookup occurs.
441      */

442     protected SystemException
443     exceptionAmbiguousLookup(String JavaDoc id1, String JavaDoc id2)
444     {
445         logError("ambiguous lookup between " + id1 + " and " + id2);
446         return new INTF_REPOS(SystemExceptionMinorValues.AmbiguousLookup,
447                               CompletionStatus.COMPLETED_NO);
448     }
449
450     /**
451      * Signal that an illegal recursion occurs.
452      */

453     protected SystemException
454     exceptionIllegalRecursion(String JavaDoc id1, String JavaDoc id2)
455     {
456         logError("illegal recursion : "+id2+" in type : "+id1);
457         return new INTF_REPOS(SystemExceptionMinorValues.IllegalRecursion,
458                               CompletionStatus.COMPLETED_NO);
459     }
460
461     // ==================================================================
462
//
463
// Internal methods for managing dependencies.
464
//
465
// ==================================================================
466

467     /**
468      * Checks if there is no dependencies.
469      */

470     protected void
471     checkDependencies()
472     {
473         // If dependencies to this object exist then
474
if (getReferencedByCounter() != 0)
475         {
476             // Visits all IR objects referencing this object.
477
org.objectweb.util.ref.api.Referencable[] referenced_by = getReferencedBy();
478             for(int i=0; i<referenced_by.length; i++) {
479                 // Raises an exception if one of the referencing object is not
480
// under destruction.
481
if(!((IRObject_impl)(referenced_by[i])).isUnderDestruction())
482                     throw exceptionDependency();
483             }
484         }
485     }
486
487     /**
488      * Cuts dependencies to other objects.
489      */

490     protected void
491     cutDependencies()
492     {
493         // Deactivates the servant.
494
setServant(null);
495     }
496
497     // ==================================================================
498
//
499
// For org.objectweb.util.api.Identifiable interface.
500
//
501
// ==================================================================
502

503     /**
504      * Obtains its identity.
505      *
506      * @return Its identity.
507      */

508     public String JavaDoc
509     getIdentity()
510     {
511         return this.toString();
512     }
513
514     // ==================================================================
515
//
516
// Public methods.
517
//
518
// ==================================================================
519

520     /**
521      * Obtain its CORBA::IRObject object reference.
522      */

523     public IRObject
524     asIRObject()
525     {
526         return IRObjectHelper.narrow(asObject());
527     }
528
529     /**
530      * Obtain its CORBA::IDLType object reference.
531      */

532     public IDLType
533     asIDLType()
534     {
535         return IDLTypeHelper.narrow(asObject());
536     }
537
538     // ==================================================================
539
//
540
// For CORBA::IRObject interface.
541
//
542
// ==================================================================
543

544     /**
545      * IDL:omg.org/CORBA/IRObject/def_kind:1.0
546      * must be implemented into subclasses.
547      */

548     abstract public DefinitionKind def_kind();
549
550     /**
551      * IDL:omg.org/CORBA/IRObject/destroy:1.0
552      */

553     public void
554     destroy()
555     {
556 // System.err.println("destroy BEGIN " + getIdentity());
557

558         try
559         {
560             // Marks as this will be destroyed.
561
is_under_destruction_ = true;
562
563             // Checks if there are some dependencies.
564
checkDependencies();
565
566             // Cuts dependencies to other objects.
567
cutDependencies();
568         }
569         catch (org.omg.CORBA.SystemException JavaDoc exc)
570         {
571             logError("Exception " + exc+toString() + " during destroy");
572 // exc.printStackTrace(System.err);
573
throw exc;
574         }
575         finally
576         {
577             // Destruction is finish.
578
is_under_destruction_ = false;
579         }
580
581 // System.err.println("destroy END " + getIdentity() + "\n");
582
}
583
584     // ==================================================================
585
//
586
// For CORBA::IDLType interface.
587
//
588
// ==================================================================
589

590     /**
591      * IDL:omg.org/CORBA/IDLType/type:1.0
592      */

593     public TypeCode
594     type()
595     {
596         // Must be implemented into subclasses when this has sense.
597
throw exceptionMustBeImplemented("IDL:omg.org/CORBA/IDLType/type:1.0");
598     }
599 }
600
Popular Tags