001: package org.jacorb.ir;
002:
003: /*
004: * JacORB - a free Java ORB
005: *
006: * Copyright (C) 1997-2004 Gerald Brose.
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Library General Public License for more details.
017: *
018: * You should have received a copy of the GNU Library General Public
019: * License along with this library; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: */
022:
023: import org.omg.CORBA.TCKind;
024:
025: import org.jacorb.orb.TypeCode;
026:
027: import java.util.*;
028:
029: import org.apache.avalon.framework.logger.Logger;
030:
031: /**
032: * @author Gerald Brose, FU Berlin
033: * @version $Id: TypeCodeUtil.java,v 1.15 2004/10/18 13:12:41 simon.mcqueen Exp $
034: */
035:
036: public class TypeCodeUtil {
037: private static Hashtable cache = new Hashtable();
038:
039: static {
040: cache.put("java.lang.String", new TypeCode(TCKind._tk_string));
041: cache.put("org.omg.CORBA.String", new TypeCode(
042: TCKind._tk_string));
043: cache.put("java.lang.Void", new TypeCode(TCKind._tk_void));
044: cache.put("java.lang.Long", new TypeCode(TCKind._tk_longlong));
045: cache.put("java.lang.Integer", new TypeCode(TCKind._tk_long));
046: cache.put("java.lang.Short", new TypeCode(TCKind._tk_short));
047: cache.put("java.lang.Float", new TypeCode(TCKind._tk_float));
048: cache.put("java.lang.Double", new TypeCode(TCKind._tk_double));
049: cache
050: .put("java.lang.Boolean", new TypeCode(
051: TCKind._tk_boolean));
052: cache.put("java.lang.Byte", new TypeCode(TCKind._tk_octet));
053:
054: cache.put("org.omg.CORBA.Any", new TypeCode(TCKind._tk_any));
055: cache.put("java.lang.Character", new TypeCode(TCKind._tk_char));
056: cache.put("org.omg.CORBA.TypeCode", new TypeCode(
057: TCKind._tk_TypeCode));
058: cache.put("org.omg.CORBA.Principal", new TypeCode(
059: TCKind._tk_Principal));
060: cache.put("org.omg.CORBA.Object", new TypeCode(
061: TCKind._tk_objref, "IDL:omg.org/CORBA/Object:1.0",
062: "IDL:omg.org/CORBA/Object:1.0"));
063:
064: }
065:
066: /**
067: * get a TypeCode for Class c. An object o of this class is needed
068: * in order to get at nested types, as e.g. in array of arrays of arrays
069: */
070:
071: public static org.omg.CORBA.TypeCode getTypeCode(Class c,
072: java.lang.Object o, Logger logger)
073: throws ClassNotFoundException {
074: return getTypeCode(c, null, o, null, logger);
075: }
076:
077: /**
078: * get a TypeCode for Class c. An object o of this class is needed
079: * in order to get at nested types, as e.g. in array of arrays of arrays
080: */
081:
082: public static org.omg.CORBA.TypeCode getTypeCode(Class c,
083: ClassLoader classLoader, java.lang.Object o,
084: String idlName, Logger logger)
085: throws ClassNotFoundException {
086: String typeName = c.getName();
087:
088: if (logger.isDebugEnabled()) {
089: logger.debug("TypeCodes.getTypeCode for class : "
090: + typeName + " idlName: " + idlName);
091: }
092:
093: ClassLoader loader;
094: if (classLoader != null)
095: loader = classLoader;
096: else
097: loader = c.getClassLoader(); // important for ir
098:
099: org.omg.CORBA.TypeCode _tc = (org.omg.CORBA.TypeCode) cache
100: .get(typeName);
101: if (_tc != null) {
102: //System.out.println("[ cached TypeCode ]");
103: return _tc;
104: }
105:
106: if (idlName != null) {
107: _tc = (org.omg.CORBA.TypeCode) cache.get(idlName);
108: if (_tc != null) {
109: //System.out.println("[ cached TypeCode ]");
110: return _tc;
111: }
112: }
113:
114: // debug:
115: // System.out.println("- TypeCodes.getTypeCode for class : " + c.getName() );
116: if (c.isPrimitive()) {
117: if (typeName.equals("void"))
118: return new TypeCode(TCKind._tk_void);
119: if (typeName.equals("int"))
120: return new TypeCode(TCKind._tk_long);
121: if (typeName.equals("long"))
122: return new TypeCode(TCKind._tk_longlong);
123: if (typeName.equals("short"))
124: return new TypeCode(TCKind._tk_short);
125: if (typeName.equals("float"))
126: return new TypeCode(TCKind._tk_float);
127: if (typeName.equals("double"))
128: return new TypeCode(TCKind._tk_double);
129: if (typeName.equals("boolean"))
130: return new TypeCode(TCKind._tk_boolean);
131: if (typeName.equals("byte"))
132: return new TypeCode(TCKind._tk_octet);
133: if (typeName.equals("char"))
134: return new TypeCode(TCKind._tk_char);
135: if (typeName.equals("wchar"))
136: return new TypeCode(TCKind._tk_wchar);
137: else {
138: System.err
139: .println("- TypeCode.getTypeCode, primitive class not found "
140: + typeName);
141: return null;
142: }
143: }
144:
145: /* else */
146:
147: Class tcClass = null;
148: Class idlEntity = null;
149: try {
150: //#ifjdk 1.2
151: tcClass = Class.forName("org.omg.CORBA.TypeCode", true,
152: loader);
153: idlEntity = Class.forName(
154: "org.omg.CORBA.portable.IDLEntity", true, loader);
155: //#else
156: //# tcClass = Class.forName ("org.omg.CORBA.TypeCode");
157: //# idlEntity = Class.forName ("org.omg.CORBA.portable.IDLEntity");
158: //#endif
159:
160: //tcClass = loader.loadClass( "org.omg.CORBA.TypeCode" );
161: // idlEntity = loader.loadClass( "org.omg.CORBA.portable.IDLEntity" );
162: } catch (ClassNotFoundException ce) {
163: logger
164: .fatalError("Can't load org.jacorb base classes!",
165: ce);
166: throw ce;
167: //System.exit(1);
168: }
169: int field_size = 0;
170:
171: if (tcClass.isAssignableFrom(c)) {
172: /*
173: try
174: {
175: */
176: return new TypeCode(TCKind._tk_TypeCode);
177: /*
178: }
179: catch ( Exception e )
180: {
181: e.printStackTrace();
182: return null;
183: }
184: */
185: } else {
186:
187: if (idlName != null && idlName.length() > 0) {
188: try {
189: if (idlName.equals("java.lang.String"))
190: return new TypeCode(TCKind._tk_string);
191: else if (idlName.equals("org.omg.CORBA.Boolean"))
192: return new TypeCode(TCKind._tk_boolean);
193: else if (idlName.equals("org.omg.CORBA.Byte"))
194: return new TypeCode(TCKind._tk_octet);
195: else if (idlName.equals("org.omg.CORBA.Short"))
196: return new TypeCode(TCKind._tk_short);
197: else if (idlName.equals("org.omg.CORBA.Long"))
198: return new TypeCode(TCKind._tk_longlong);
199: else if (idlName.equals("org.omg.CORBA.Int"))
200: return new TypeCode(TCKind._tk_long);
201: else if (idlName.equals("org.omg.CORBA.String"))
202: return new TypeCode(TCKind._tk_string);
203: else if (idlName.equals("org.omg.CORBA.Char"))
204: return new TypeCode(TCKind._tk_char);
205: else if (idlName.equals("org.omg.CORBA.Float"))
206: return new TypeCode(TCKind._tk_float);
207: else if (idlName.equals("org.omg.CORBA.Double"))
208: return new TypeCode(TCKind._tk_double);
209: else if (idlName.equals("org.omg.CORBA.Any"))
210: return new TypeCode(TCKind._tk_any);
211: else if (idlName.equals("org.omg.CORBA.Object"))
212: return new TypeCode(TCKind._tk_objref);
213: else if (idlName.equals("org.omg.CORBA.TypeCode"))
214: return new TypeCode(TCKind._tk_TypeCode);
215:
216: //#ifjdk 1.2
217: Class type = Class.forName(idlName + "Helper",
218: true, loader);
219: //#else
220: //# Class type = Class.forName( idlName + "Helper" );
221: //#endif
222:
223: return (org.omg.CORBA.TypeCode) type
224: .getDeclaredMethod("type", (Class[]) null)
225: .invoke(null, (Object[]) null);
226: } catch (ClassNotFoundException cnfe) {
227: logger.debug("Caught Exception", cnfe);
228: throw new RuntimeException(
229: "Could not create TypeCode for: "
230: + c.getName()
231: + ", no helper class for "
232: + idlName);
233: } catch (Exception e) {
234: logger.error("Caught Exception", e);
235: }
236: }
237:
238: // debug: System.out.println("TypeCodes else: " + c.getName());
239: if (idlEntity.isAssignableFrom(c)) {
240: try {
241: Class resultHelperClass =
242: //#ifjdk 1.2
243: Class.forName(c.getName() + "Helper", true, loader);
244: //#else
245: //# Class.forName( c.getName() + "Helper" );
246: //#endif
247:
248: return (org.omg.CORBA.TypeCode) resultHelperClass
249: .getDeclaredMethod("type", (Class[]) null)
250: .invoke(null, (Object[]) null);
251: } catch (Exception cnfe) {
252: logger.error("Caught Exception", cnfe);
253: throw new RuntimeException(
254: "Could not create TypeCode for: "
255: + c.getName());
256: }
257: } else {
258: throw new RuntimeException(
259: "Could not create TypeCode for: " + c.getName()
260: + ", not an IDLEntity");
261: }
262: }
263: }
264:
265: private static String idToIDL(String s) {
266: if (s.startsWith("IDL:"))
267: s = s.substring(4, s.lastIndexOf(":"));
268: else
269: s = s.replace('.', '/') + ":1.0";
270:
271: StringBuffer sb = new StringBuffer(s);
272: int i = 0;
273: while (i < sb.length()) {
274: if (sb.charAt(i) == '/') {
275: sb.setCharAt(i, ':');
276: sb.insert(i, ':');
277: }
278: i++;
279: }
280: return sb.toString();
281: }
282: }
|