KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > ir3 > Contained_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
32 /**
33  * Implementation of IDL:omg.org/CORBA/Contained:1.0.
34  *
35  * @author <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>
36  * <a HREF="mailto:Mathieu.Vadet@lifl.fr">Mathieu Vadet</a>
37  *
38  * @version 0.5
39  */

40
41 abstract public class Contained_impl
42               extends IRObject_impl
43            implements ContainedOperations
44 {
45     // ==================================================================
46
//
47
// Internal state.
48
//
49
// ==================================================================
50

51     /** The container of this contained object. */
52     protected Container_impl container_;
53
54     /** The repository id. */
55     protected String JavaDoc id_;
56
57     /** The name. */
58     protected String JavaDoc name_;
59
60     /** The version. */
61     protected String JavaDoc version_;
62
63     // ==================================================================
64
//
65
// Constructor.
66
//
67
// ==================================================================
68

69     /** TODO */
70     public
71     Contained_impl(IFR ifr,
72                    Container_impl container)
73     {
74         // Calls the IRObject_impl contructor.
75
super(ifr);
76
77         // Sets the container.
78
container_ = container;
79
80         // Sets the id, name, version to empty strings
81
id_ = "";
82         name_ = "";
83         version_ = "";
84     }
85
86     // ==================================================================
87
//
88
// For org.objectweb.util.api.Identifiable interface.
89
//
90
// ==================================================================
91

92     /**
93      * Obtains its identity.
94      *
95      * @return Its identity.
96      */

97     public String JavaDoc
98     getIdentity()
99     {
100         return id_;
101     }
102
103     // ==================================================================
104
//
105
// Internal methods for IRObject_impl class.
106
//
107
// ==================================================================
108

109     /**
110      * TODO
111      */

112     protected boolean
113     isUnderDestruction()
114     {
115         // Returns true if this object is already in destruction.
116
if(super.isUnderDestruction()) return true;
117
118         // Else returns false if this object has no container.
119
if(container_ == null) return false;
120
121         // Else returns is the container under destruction?
122
return container_.isUnderDestruction();
123     }
124
125     // ==================================================================
126
//
127
// Public methods.
128
//
129
// ==================================================================
130

131     /**
132      * Obtain its CORBA::Contained object reference.
133      */

134     public Contained
135     asContained()
136     {
137         return ContainedHelper.narrow(asObject());
138     }
139
140     /**
141      * Obtain the repository ID of the container.
142      */

143     public String JavaDoc
144     getContainerID()
145     {
146       if (container_ == null) return "";
147       return container_.id();
148     }
149
150     /**
151      * Obtain the base repository ID of the container.
152      */

153     public String JavaDoc
154     getBaseContainerID()
155     {
156       if (container_ == null) return "IDL:";
157       return computeBaseID(container_.id());
158     }
159
160     /**
161      * Obtain the base repository ID.
162      */

163     public String JavaDoc
164     getBaseID()
165     {
166       return computeBaseID(id());
167     }
168
169     /**
170      * Compute a base repository ID + '/'.
171      */

172     public String JavaDoc
173     computeBaseID(String JavaDoc id)
174     {
175       return computeIDWithoutVersion(id) + '/';
176     }
177
178     /**
179      * Compute a base repository ID without version.
180      */

181     public String JavaDoc
182     computeIDWithoutVersion(String JavaDoc id)
183     {
184       int idx = id.lastIndexOf(':');
185       if(idx == -1) return id;
186       return id.substring(0, idx);
187     }
188
189     /**
190      * To set the value field of the CORBA::Contained::Description struct.
191      * By default, set as a CORBA::TypeDescription.
192      * Must be implemented into subclasses.
193      */

194     public void
195     setDescriptionValue(Any any)
196     {
197         // create the CORBA::TypeDescription.
198
TypeDescription description = new TypeDescription();
199         description.name = name();
200         description.id = id();
201         description.defined_in = getContainerID();
202         description.version = version();
203         description.type = type();
204
205         // insert it into the any.
206
TypeDescriptionHelper.insert(any, description);
207     }
208
209     // ==================================================================
210
//
211
// Internal methods for managing dependencies.
212
//
213
// ==================================================================
214

215     /**
216      * Cuts dependencies to other objects.
217      */

218     protected void
219     cutDependencies()
220     {
221         // Removes it from the repository.
222
if(id_.length() != 0)
223             getIFR().getRepository().removeRID(id_);
224
225         // Removes it from the container.
226
if (container_ != null)
227             container_.removeContained(this);
228
229         // Calls the IRObject_impl::cutDependencies method.
230
super.cutDependencies();
231     }
232
233     // ==================================================================
234
//
235
// For CORBA::IRObject interface.
236
//
237
// ==================================================================
238

239     // ==================================================================
240
//
241
// For CORBA::Contained interface.
242
//
243
// ==================================================================
244

245     /**
246      * IDL:omg.org/CORBA/Contained/id:1.0
247      */

248     public String JavaDoc
249     id()
250     {
251         return id_;
252     }
253
254     /**
255      * IDL:omg.org/CORBA/Contained/id:1.0
256      */

257     public void
258     id(String JavaDoc val)
259     {
260         // if not changed then return immediatelly.
261
if (val.equals(id_)) return;
262
263         // search if val is an already used RID.
264
Contained contained = getIFR().getRepository().lookup_id(val);
265         // if yes then raise an RID AlreadyDefined exception.
266
if (contained != null)
267             throw exceptionRidAlreadyDefined(val);
268
269         // remove it from the repository.
270
if(id_.length() != 0)
271             getIFR().getRepository().removeRID(id_);
272
273         // set the id.
274
id_ = val;
275
276         // add it to the repository.
277
if(id_.length() != 0)
278             getIFR().getRepository().addRID(id_, this);
279     }
280
281     /**
282      * IDL:omg.org/CORBA/Contained/name:1.0
283      */

284     public String JavaDoc
285     name()
286     {
287         return name_;
288     }
289
290     /**
291      * IDL:omg.org/CORBA/Contained/name:1.0
292      */

293     public void
294     name(String JavaDoc val)
295     {
296         // if not changed then return immediatelly.
297
if (val.equals(name_)) return;
298
299         // check for name redefinition with the container name
300
if (val.equalsIgnoreCase(container_.name()))
301             throw exceptionNameAlreadyUsedByImmediateScope(val);
302
303         // check if the name already exists in the container.
304
// Exclude inherited names.
305
container_.checkName(val, true, false);
306
307         name_ = val;
308     }
309
310     /**
311      * IDL:omg.org/CORBA/Contained/version:1.0
312      */

313     public String JavaDoc
314     version()
315     {
316         return version_;
317     }
318
319     /**
320      * IDL:omg.org/CORBA/Contained/version:1.0
321      */

322     public void
323     version(String JavaDoc val)
324     {
325         version_ = val;
326     }
327
328     /**
329      * IDL:omg.org/CORBA/Contained/defined_in:1.0
330      */

331     public Container
332     defined_in()
333     {
334         return container_.asContainer();
335     }
336
337     /**
338      * IDL:omg.org/CORBA/Contained/absolute_name:1.0
339      */

340     public String JavaDoc
341     absolute_name()
342     {
343         // If this is an CORBA::Repository then return an empty string.
344
if (container_ == null) return "";
345
346         return container_.absolute_name() + "::" + name_;
347     }
348
349     /**
350      * IDL:omg.org/CORBA/Contained/containing_repository:1.0
351      */

352     public Repository
353     containing_repository()
354     {
355         return getIFR().getRepository().asRepository();
356     }
357
358     /**
359      * IDL:omg.org/CORBA/Contained/describe:1.0
360      */

361     public org.omg.CORBA.ContainedPackage.Description
362     describe()
363     {
364         // obtain the value description field.
365
Any any = org.objectweb.openccm.corba.TheORB.create_any();
366         setDescriptionValue(any);
367
368         // return a CORBA::Contained::Description
369
return new org.omg.CORBA.ContainedPackage.Description(def_kind(), any);
370     }
371
372     /**
373      * IDL:omg.org/CORBA/Contained/move:1.0
374      */

375     public void
376     move(Container new_container,
377          String JavaDoc new_name,
378          String JavaDoc new_version)
379     {
380         // Must be a non null container reference.
381
if (new_container == null)
382             throw exceptionBadParam("IDL:omg.org/CORBA/Contained/move:1.0");
383
384         // Must be a local container.
385
Container_impl newContainer = castToLocal(new_container);
386
387         // Remember the old name, version and container.
388
String JavaDoc oldName = name_;
389         String JavaDoc oldVersion = version_;
390         Container_impl oldContainer = container_;
391
392         // Add to the new container.
393
// This raises an invalid container exception if the kind of
394
// this contained object is not accepted by the new container.
395
oldContainer.removeContained(this);
396         try
397         {
398             newContainer.addContained(this);
399             container_ = newContainer;
400         }
401         catch(SystemException ex)
402         {
403             //
404
// Rollback to previous state if there is an exception!
405
//
406
oldContainer.addContained(this);
407             container_ = oldContainer;
408             throw ex;
409         }
410
411         try
412         {
413             //
414
// Change name and version.
415
//
416
name(new_name);
417             version(new_version);
418         }
419         catch(SystemException ex)
420         {
421             //
422
// Rollback to previous state if there is an exception!
423
//
424
oldContainer.addContained(this);
425             newContainer.removeContained(this);
426             container_ = oldContainer;
427             name(oldName);
428             version(oldVersion);
429             throw ex;
430         }
431     }
432 }
433
Popular Tags