KickJava   Java API By Example, From Geeks To Geeks.

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


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.TCKind JavaDoc;
24
25 import org.jacorb.orb.TypeCode;
26
27 import java.util.*;
28
29 import org.apache.avalon.framework.logger.Logger;
30
31 /**
32  * @author Gerald Brose, FU Berlin
33  * @version $Id: TypeCodeUtil.java,v 1.15 2004/10/18 13:12:41 simon.mcqueen Exp $
34  */

35  
36 public class TypeCodeUtil
37 {
38     private static Hashtable cache = new Hashtable();
39
40     static
41     {
42             cache.put( "java.lang.String", new TypeCode( TCKind._tk_string ));
43             cache.put( "org.omg.CORBA.String", new TypeCode( TCKind._tk_string ));
44             cache.put( "java.lang.Void", new TypeCode( TCKind._tk_void ));
45             cache.put( "java.lang.Long",new TypeCode( TCKind._tk_longlong ));
46             cache.put( "java.lang.Integer",new TypeCode( TCKind._tk_long ));
47             cache.put( "java.lang.Short",new TypeCode( TCKind._tk_short ));
48             cache.put( "java.lang.Float",new TypeCode( TCKind._tk_float ));
49             cache.put( "java.lang.Double",new TypeCode( TCKind._tk_double ));
50             cache.put( "java.lang.Boolean",new TypeCode( TCKind._tk_boolean ));
51             cache.put( "java.lang.Byte" ,new TypeCode( TCKind._tk_octet));
52
53             cache.put( "org.omg.CORBA.Any", new TypeCode( TCKind._tk_any ));
54             cache.put( "java.lang.Character",new TypeCode( TCKind._tk_char));
55             cache.put( "org.omg.CORBA.TypeCode",new TypeCode( TCKind._tk_TypeCode));
56             cache.put( "org.omg.CORBA.Principal",new TypeCode( TCKind._tk_Principal));
57             cache.put( "org.omg.CORBA.Object",
58                        new TypeCode( TCKind._tk_objref,
59                                      "IDL:omg.org/CORBA/Object:1.0",
60                                      "IDL:omg.org/CORBA/Object:1.0" ));
61  
62     }
63
64
65     /**
66      * get a TypeCode for Class c. An object o of this class is needed
67      * in order to get at nested types, as e.g. in array of arrays of arrays
68      */

69
70     public static org.omg.CORBA.TypeCode JavaDoc getTypeCode( Class JavaDoc c,
71                                                       java.lang.Object JavaDoc o,
72                                                       Logger logger )
73         throws ClassNotFoundException JavaDoc
74     {
75         return getTypeCode( c, null, o, null, logger );
76     }
77
78     /**
79      * get a TypeCode for Class c. An object o of this class is needed
80      * in order to get at nested types, as e.g. in array of arrays of arrays
81      */

82
83     public static org.omg.CORBA.TypeCode JavaDoc getTypeCode( Class JavaDoc c,
84                                                       ClassLoader JavaDoc classLoader,
85                                                       java.lang.Object JavaDoc o,
86                                                       String JavaDoc idlName,
87                                                       Logger logger )
88         throws ClassNotFoundException JavaDoc
89     {
90         String JavaDoc typeName = c.getName();
91
92         if (logger.isDebugEnabled())
93         {
94             logger.debug("TypeCodes.getTypeCode for class : " +
95                          typeName + " idlName: " + idlName);
96         }
97
98         ClassLoader JavaDoc loader;
99         if( classLoader != null )
100             loader = classLoader;
101         else
102             loader = c.getClassLoader(); // important for ir
103

104         org.omg.CORBA.TypeCode JavaDoc _tc =
105             (org.omg.CORBA.TypeCode JavaDoc)cache.get( typeName );
106         if( _tc != null )
107         {
108             //System.out.println("[ cached TypeCode ]");
109
return _tc;
110         }
111
112         if( idlName != null )
113         {
114             _tc = (org.omg.CORBA.TypeCode JavaDoc)cache.get( idlName );
115             if( _tc != null )
116             {
117                 //System.out.println("[ cached TypeCode ]");
118
return _tc;
119             }
120         }
121
122         // debug:
123
// System.out.println("- TypeCodes.getTypeCode for class : " + c.getName() );
124
if( c.isPrimitive() )
125         {
126             if( typeName.equals("void"))
127                 return new TypeCode( TCKind._tk_void );
128             if( typeName.equals("int"))
129                 return new TypeCode( TCKind._tk_long );
130             if( typeName.equals("long"))
131                 return new TypeCode( TCKind._tk_longlong );
132             if( typeName.equals("short"))
133                 return new TypeCode( TCKind._tk_short );
134             if( typeName.equals("float"))
135                 return new TypeCode( TCKind._tk_float );
136             if( typeName.equals("double"))
137                 return new TypeCode( TCKind._tk_double );
138             if( typeName.equals("boolean"))
139                 return new TypeCode( TCKind._tk_boolean );
140             if( typeName.equals("byte") )
141                 return new TypeCode( TCKind._tk_octet );
142             if( typeName.equals("char") )
143                 return new TypeCode( TCKind._tk_char );
144             if( typeName.equals("wchar") )
145                 return new TypeCode( TCKind._tk_wchar );
146             else
147             {
148                 System.err.println("- TypeCode.getTypeCode, primitive class not found " +
149                                    typeName );
150                 return null;
151             }
152         }
153
154         /* else */
155
156         Class JavaDoc tcClass = null;
157         Class JavaDoc idlEntity = null;
158         try
159         {
160             //#ifjdk 1.2
161
tcClass = Class.forName("org.omg.CORBA.TypeCode", true, loader );
162                 idlEntity = Class.forName("org.omg.CORBA.portable.IDLEntity", true, loader);
163             //#else
164
//# tcClass = Class.forName ("org.omg.CORBA.TypeCode");
165
//# idlEntity = Class.forName ("org.omg.CORBA.portable.IDLEntity");
166
//#endif
167

168             //tcClass = loader.loadClass( "org.omg.CORBA.TypeCode" );
169
// idlEntity = loader.loadClass( "org.omg.CORBA.portable.IDLEntity" );
170
}
171         catch ( ClassNotFoundException JavaDoc ce )
172         {
173             logger.fatalError("Can't load org.jacorb base classes!", ce);
174             throw ce;
175             //System.exit(1);
176
}
177         int field_size = 0;
178
179         if ( tcClass.isAssignableFrom(c))
180         {
181             /*
182             try
183             {
184             */

185                 return new TypeCode( TCKind._tk_TypeCode );
186                 /*
187             }
188             catch ( Exception e )
189             {
190                 e.printStackTrace();
191                 return null;
192             }
193                 */

194         }
195         else
196         {
197
198             if( idlName != null && idlName.length() > 0 )
199             {
200                 try
201                 {
202                     if( idlName.equals( "java.lang.String"))
203                         return new TypeCode( TCKind._tk_string);
204                     else if( idlName.equals( "org.omg.CORBA.Boolean"))
205                         return new TypeCode(TCKind._tk_boolean);
206                     else if( idlName.equals( "org.omg.CORBA.Byte"))
207                         return new TypeCode(TCKind._tk_octet);
208                     else if( idlName.equals( "org.omg.CORBA.Short"))
209                         return new TypeCode(TCKind._tk_short);
210                     else if( idlName.equals( "org.omg.CORBA.Long"))
211                         return new TypeCode(TCKind._tk_longlong);
212                     else if( idlName.equals( "org.omg.CORBA.Int"))
213                         return new TypeCode(TCKind._tk_long);
214                     else if( idlName.equals( "org.omg.CORBA.String"))
215                         return new TypeCode(TCKind._tk_string);
216                     else if( idlName.equals( "org.omg.CORBA.Char"))
217                         return new TypeCode(TCKind._tk_char);
218                     else if( idlName.equals( "org.omg.CORBA.Float"))
219                         return new TypeCode(TCKind._tk_float);
220                     else if( idlName.equals( "org.omg.CORBA.Double"))
221                         return new TypeCode(TCKind._tk_double);
222                     else if( idlName.equals( "org.omg.CORBA.Any"))
223                         return new TypeCode(TCKind._tk_any);
224                     else if( idlName.equals( "org.omg.CORBA.Object"))
225                         return new TypeCode(TCKind._tk_objref);
226                     else if( idlName.equals( "org.omg.CORBA.TypeCode"))
227                         return new TypeCode(TCKind._tk_TypeCode);
228                     
229                     //#ifjdk 1.2
230
Class JavaDoc type = Class.forName( idlName + "Helper", true, loader);
231                     //#else
232
//# Class type = Class.forName( idlName + "Helper" );
233
//#endif
234

235                     return (org.omg.CORBA.TypeCode JavaDoc)type.getDeclaredMethod(
236                                                     "type",
237                                                     (Class JavaDoc[]) null).invoke( null, (Object JavaDoc[]) null );
238                 }
239                 catch( ClassNotFoundException JavaDoc cnfe )
240                 {
241                     logger.debug("Caught Exception", cnfe );
242                     throw new RuntimeException JavaDoc("Could not create TypeCode for: " +
243                                                c.getName() + ", no helper class for " + idlName );
244                 }
245                 catch( Exception JavaDoc e )
246                 {
247                     logger.error("Caught Exception", e );
248                 }
249             }
250
251             // debug: System.out.println("TypeCodes else: " + c.getName());
252
if( idlEntity.isAssignableFrom(c))
253             {
254                 try
255                 {
256                     Class JavaDoc resultHelperClass =
257                             //#ifjdk 1.2
258
Class.forName( c.getName()+ "Helper", true, loader);
259                             //#else
260
//# Class.forName( c.getName() + "Helper" );
261
//#endif
262

263                     return (org.omg.CORBA.TypeCode JavaDoc)
264                         resultHelperClass.getDeclaredMethod(
265                                                     "type",
266                                                     (Class JavaDoc[]) null).invoke( null, (Object JavaDoc[]) null );
267                 }
268                 catch( Exception JavaDoc cnfe )
269                 {
270                     logger.error("Caught Exception", cnfe);
271                     throw new RuntimeException JavaDoc("Could not create TypeCode for: " +
272                                                c.getName() );
273                 }
274             }
275             else
276             {
277                 throw new RuntimeException JavaDoc("Could not create TypeCode for: " +
278                                            c.getName() + ", not an IDLEntity" );
279             }
280         }
281     }
282     
283     private static String JavaDoc idToIDL( String JavaDoc s )
284     {
285         if( s.startsWith("IDL:"))
286             s = s.substring( 4, s.lastIndexOf(":") );
287         else
288             s = s.replace('.','/') + ":1.0";
289         
290         StringBuffer JavaDoc sb = new StringBuffer JavaDoc( s );
291         int i = 0;
292         while( i < sb.length() )
293         {
294             if( sb.charAt(i) == '/' )
295             {
296                 sb.setCharAt(i,':');
297                 sb.insert(i,':');
298             }
299             i++;
300         }
301         return sb.toString();
302     }
303 }
304
Popular Tags