KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > ir > Contained


1 package org.jacorb.ir;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-2004 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 import org.omg.CORBA.*;
24 import org.omg.PortableServer.POA JavaDoc;
25
26 import java.lang.reflect.*;
27
28 import org.apache.avalon.framework.logger.Logger;
29
30 /**
31  * @version $Id: Contained.java,v 1.12 2004/10/18 13:12:41 simon.mcqueen Exp $
32  */

33
34 public abstract class Contained
35     extends IRObject
36     implements org.omg.CORBA.ContainedOperations JavaDoc
37 {
38     protected String JavaDoc id;
39
40     /* IDL name from the root of the class path to the leaf */
41     protected String JavaDoc absolute_name;
42     /* from the root of the class path to the leaf */
43     String JavaDoc full_name;
44     protected String JavaDoc version = "1.0";
45
46     protected org.omg.CORBA.Container JavaDoc defined_in;
47     protected org.omg.CORBA.Repository JavaDoc containing_repository;
48
49     private static Class JavaDoc intfClass;
50     private static Class JavaDoc idlClass;
51     private static Class JavaDoc stubClass;
52     private static Class JavaDoc exceptClass;
53     private static boolean class_init;
54
55     public Contained()
56     {
57     }
58
59     public Contained( String JavaDoc _id,
60                       String JavaDoc _name,
61                       String JavaDoc _version,
62                       org.omg.CORBA.Container JavaDoc _defined_in,
63                       String JavaDoc _absolute_name,
64                       org.omg.CORBA.Repository JavaDoc _containing_repository )
65     {
66         id = _id;
67         name = _name;
68         version = _version;
69         defined_in = _defined_in;
70         absolute_name = _absolute_name;
71         containing_repository = _containing_repository;
72     }
73
74
75     public static Contained createContained( Class JavaDoc c,
76                                              String JavaDoc path,
77                                              org.omg.CORBA.Container JavaDoc _defined_in,
78                                              org.omg.CORBA.Repository JavaDoc ir,
79                                              Logger logger,
80                                              ClassLoader JavaDoc loader,
81                                              POA JavaDoc poa )
82     {
83         if( !class_init )
84         {
85             try
86             {
87                 intfClass =
88                     loader.loadClass("org.omg.CORBA.Object");
89                 idlClass =
90                     loader.loadClass("org.omg.CORBA.portable.IDLEntity");
91                 stubClass =
92                     loader.loadClass("org.omg.CORBA.portable.ObjectImpl");
93                 exceptClass =
94                     loader.loadClass("org.omg.CORBA.UserException");
95                 class_init = true;
96             }
97             catch ( ClassNotFoundException JavaDoc cnf )
98             {
99                 // debug: cnf.printStackTrace();
100
}
101         }
102
103         if( stubClass.isAssignableFrom( c ) )
104         {
105             return null; // don't care for stubs
106
}
107         else if( c.isInterface())
108         {
109             if( intfClass.isAssignableFrom( c ) )
110             {
111                 try
112                 {
113                     Class JavaDoc helperClass =
114                         loader.loadClass(c.getName() + "Helper");
115
116                     if( helperClass == null )
117                     {
118                         return null;
119                     }
120
121                     org.jacorb.ir.InterfaceDef idef =
122                         new org.jacorb.ir.InterfaceDef( c,
123                                                         helperClass,
124                                                         path,
125                                                         _defined_in,
126                                                         ir,
127                                                         loader,
128                                                         poa,
129                                                         logger);
130
131                     return idef;
132                 }
133                 catch ( ClassNotFoundException JavaDoc e )
134                 {
135                     // debug: e.printStackTrace();
136
return null;
137                 }
138             }
139             else
140             {
141                 try
142                 {
143                     Field f = c.getDeclaredField("value");
144                     return new org.jacorb.ir.ConstantDef( c,
145                                                           _defined_in,
146                                                           ir,
147                                                           logger);
148                 }
149                 catch( NoSuchFieldException JavaDoc nsfe )
150                 {
151                     // org.jacorb.util.Debug.output(2, nsfe );
152
return null;
153                 }
154             }
155         }
156         else if( exceptClass.isAssignableFrom( c ))
157         {
158             /*
159             try
160             {
161             */

162                 return new org.jacorb.ir.ExceptionDef(c,
163                                                       _defined_in,
164                                                       ir,
165                                                       loader,
166                                                       poa,
167                                                       logger);
168                 /*
169             }
170             catch ( Exception e )
171             {
172                 // debug:
173                 e.printStackTrace();
174                 return null;
175             }
176                 */

177         }
178         else if( idlClass.isAssignableFrom( c ) )
179         {
180             try
181             {
182                 Class JavaDoc helperClass =
183                     loader.loadClass( c.getName()+"Helper");
184
185                 org.omg.CORBA.TypeCode JavaDoc tc =
186                     (org.omg.CORBA.TypeCode JavaDoc)helperClass.getDeclaredMethod("type", (Class JavaDoc[]) null).invoke(null, (java.lang.Object JavaDoc[]) null);
187                 switch( tc.kind().value())
188                 {
189                 case org.omg.CORBA.TCKind._tk_struct:
190                     return new org.jacorb.ir.StructDef( c,
191                                                         path,
192                                                         _defined_in,
193                                                         ir,
194                                                         logger,
195                                                         loader,
196                                                         poa );
197                 case org.omg.CORBA.TCKind._tk_enum:
198                     return new org.jacorb.ir.EnumDef(
199                                                      c,
200                                                      _defined_in,
201                                                      ir,
202                                                      loader );
203                 case org.omg.CORBA.TCKind._tk_union:
204                     return new org.jacorb.ir.UnionDef( c,
205                                                        path,
206                                                        _defined_in,
207                                                        ir,
208                                                        loader,
209                                                        logger,
210                                                        poa);
211                 default:
212                     return null;
213                 }
214             }
215             catch( ClassNotFoundException JavaDoc e )
216             {
217                 // may happen for pseudo IDL
218
logger.warn("Caught Exception", e);
219             }
220             catch( Exception JavaDoc e )
221             {
222                 logger.error("Caught Exception", e);
223             }
224             return null;
225         }
226         else if( c.getName().endsWith("Helper"))
227         {
228             try
229             {
230                 org.omg.CORBA.TypeCode JavaDoc tc =
231                     (org.omg.CORBA.TypeCode JavaDoc)c.getDeclaredMethod("type", (Class JavaDoc[]) null).invoke(null, (java.lang.Object JavaDoc[]) null);
232                 if( tc.kind() == org.omg.CORBA.TCKind.tk_alias )
233                 {
234                     return new AliasDef(tc,
235                                         _defined_in,
236                                         ir,
237                                         logger,
238                                         poa);
239                 }
240             }
241             catch( Exception JavaDoc e )
242             {
243             }
244             return null;
245         }
246         else
247         {
248             return null;
249         }
250     }
251
252     public static org.omg.CORBA.Contained JavaDoc createContainedReference(
253         Contained containedObject,
254         Logger logger,
255         POA JavaDoc poa )
256     {
257         if ( containedObject == null)
258         {
259             throw new INTF_REPOS ("Precondition violated in Contained createContainedReference");
260         }
261
262         org.omg.PortableServer.Servant JavaDoc servant = null;
263
264         switch ( containedObject.def_kind().value() )
265         {
266         case org.omg.CORBA.DefinitionKind._dk_Interface:
267             servant =
268                 new org.omg.CORBA.InterfaceDefPOATie( (org.omg.CORBA.InterfaceDefOperations JavaDoc)containedObject );
269             break;
270         case org.omg.CORBA.DefinitionKind._dk_Exception:
271             servant =
272                 new org.omg.CORBA.ExceptionDefPOATie( (org.omg.CORBA.ExceptionDefOperations JavaDoc)containedObject );
273             break;
274         case org.omg.CORBA.DefinitionKind._dk_Struct:
275             servant =
276                 new org.omg.CORBA.StructDefPOATie( (org.omg.CORBA.StructDefOperations JavaDoc)containedObject );
277             break;
278         case org.omg.CORBA.DefinitionKind._dk_Enum:
279             servant =
280                 new org.omg.CORBA.EnumDefPOATie( (org.omg.CORBA.EnumDefOperations JavaDoc)containedObject );
281             break;
282         case org.omg.CORBA.DefinitionKind._dk_Union:
283             servant =
284                 new org.omg.CORBA.UnionDefPOATie( (org.omg.CORBA.UnionDefOperations JavaDoc)containedObject );
285             break;
286         case org.omg.CORBA.DefinitionKind._dk_Module:
287             servant =
288                 new org.omg.CORBA.ModuleDefPOATie( (org.omg.CORBA.ModuleDefOperations JavaDoc)containedObject );
289             break;
290         case org.omg.CORBA.DefinitionKind._dk_Alias:
291             servant =
292                 new org.omg.CORBA.AliasDefPOATie( (org.omg.CORBA.AliasDefOperations JavaDoc)containedObject );
293             break;
294         case org.omg.CORBA.DefinitionKind._dk_Constant:
295             servant =
296                 new org.omg.CORBA.ConstantDefPOATie( (org.omg.CORBA.ConstantDefOperations JavaDoc)containedObject );
297             break;
298         default:
299             logger.warn("WARNING, createContainedReference returns null for dk " +
300                         containedObject.def_kind().value() );
301             return null;
302         }
303
304         try
305         {
306             org.omg.CORBA.Contained JavaDoc containedRef =
307                 org.omg.CORBA.ContainedHelper.narrow(poa.servant_to_reference( servant ));
308
309             containedObject.setReference( containedRef );
310             return containedRef;
311         }
312         catch( Exception JavaDoc e )
313         {
314             e.printStackTrace();
315             return null;
316         }
317     }
318
319
320     public java.lang.String JavaDoc id()
321     {
322         return id;
323     }
324     public void id(java.lang.String JavaDoc a)
325     {
326         id = a;
327     }
328
329     public java.lang.String JavaDoc name()
330     {
331         return name;
332     }
333
334     public void name(java.lang.String JavaDoc a)
335     {
336         name = a;
337     }
338
339     public java.lang.String JavaDoc version()
340     {
341         return version;
342     }
343
344     public void version(java.lang.String JavaDoc a)
345     {
346         version = a;
347     }
348
349     public org.omg.CORBA.Container JavaDoc defined_in()
350     {
351         return defined_in;
352     }
353
354     public java.lang.String JavaDoc absolute_name()
355     {
356         return absolute_name;
357     }
358
359     public org.omg.CORBA.Repository JavaDoc containing_repository()
360     {
361         return containing_repository;
362     }
363
364     public abstract org.omg.CORBA.ContainedPackage.Description describe();
365
366
367     public void move(org.omg.CORBA.Container JavaDoc new_container,
368                      java.lang.String JavaDoc new_name,
369                      java.lang.String JavaDoc new_version)
370     {
371         if( defined_in != null )
372             ; // remove this object
373

374         defined_in = new_container;
375         version = new_version;
376         name = new_name;
377     }
378 }
379
Popular Tags