1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47:
48:
49:
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62:
63: import ;
64: import ;
65: import ;
66:
67: import ;
68:
69:
75: public class ObjectCreator
76: {
77:
80: public static final String OMG_PREFIX = "omg.org/";
81:
82:
85: public static final String JAVA_PREFIX = "org.omg.";
86:
87:
90: public static final String CLASSPATH_PREFIX = "gnu.CORBA.";
91:
92:
97: public static Map m_names = new WeakHashMap();
98:
99:
103: public static Map m_classes = new WeakHashMap();
104:
105:
108: public static Map m_helpers = new WeakHashMap();
109:
110:
118: public static java.lang.Object createObject(String idl, String suffix)
119: {
120: synchronized (m_classes)
121: {
122: Class known = (Class) (suffix == null ? m_classes.get(idl)
123: : m_classes.get(idl + 0xff + suffix));
124: Object object;
125:
126: if (known != null)
127: {
128: try
129: {
130: return known.newInstance();
131: }
132: catch (Exception ex)
133: {
134: RuntimeException rex = new RuntimeException(idl + " suffix "
135: + suffix, ex);
136: throw rex;
137: }
138: }
139: else
140: {
141: if (suffix == null)
142: suffix = "";
143: try
144: {
145: known = forName(toClassName(JAVA_PREFIX, idl) + suffix);
146: object = known.newInstance();
147: }
148: catch (Exception ex)
149: {
150: try
151: {
152: known = forName(toClassName(CLASSPATH_PREFIX, idl)
153: + suffix);
154: object = known.newInstance();
155: }
156: catch (Exception exex)
157: {
158: return null;
159: }
160: }
161: m_classes.put(idl + 0xff + suffix, known);
162: return object;
163: }
164: }
165: }
166:
167:
176: public static SystemException readSystemException(InputStream input,
177: ServiceContext[] contexts)
178: {
179: SystemException exception;
180:
181: String idl = input.read_string();
182: int minor = input.read_ulong();
183: CompletionStatus completed = CompletionStatusHelper.read(input);
184:
185: try
186: {
187: exception = (SystemException) createObject(idl, null);
188: exception.minor = minor;
189: exception.completed = completed;
190: }
191: catch (Exception ex)
192: {
193: UNKNOWN u = new UNKNOWN("Unsupported system exception " + idl, minor,
194: completed);
195: u.initCause(ex);
196: throw u;
197: }
198:
199: try
200: {
201:
202:
203: ServiceContext uEx = ServiceContext.find(
204: ServiceContext.UnknownExceptionInfo, contexts);
205:
206: if (uEx != null)
207: {
208: BufferredCdrInput in = new BufferredCdrInput(uEx.context_data);
209: in.setOrb(in.orb());
210: if (input instanceof AbstractCdrInput)
211: {
212: ((AbstractCdrInput) input).cloneSettings(in);
213: }
214:
215: Throwable t = UnknownExceptionCtxHandler.read(in, contexts);
216: exception.initCause(t);
217: }
218: }
219: catch (Exception ex)
220: {
221:
222:
223: }
224:
225: return exception;
226: }
227:
228:
239: public static UserException readUserException(String idl, InputStream input)
240: {
241: try
242: {
243: Class helperClass = findHelper(idl);
244:
245: Method read = helperClass.getMethod("read",
246: new Class[] { org.omg.CORBA.portable.InputStream.class });
247:
248: return (UserException) read.invoke(null, new Object[] { input });
249: }
250: catch (MARSHAL mex)
251: {
252:
253: throw mex;
254: }
255: catch (Exception ex)
256: {
257: ex.printStackTrace();
258: return null;
259: }
260: }
261:
262:
268: public static String toHelperName(String IDL)
269: {
270: String s = IDL;
271: int a = s.indexOf(':') + 1;
272: int b = s.lastIndexOf(':');
273:
274: s = IDL.substring(a, b);
275:
276: if (s.startsWith(OMG_PREFIX))
277: s = JAVA_PREFIX + s.substring(OMG_PREFIX.length());
278:
279: return s.replace('/', '.') + "Helper";
280: }
281:
282:
288: public static void writeSystemException(OutputStream output,
289: SystemException ex)
290: {
291: String exIDL = getRepositoryId(ex.getClass());
292: output.write_string(exIDL);
293: output.write_ulong(ex.minor);
294: CompletionStatusHelper.write(output, ex.completed);
295: }
296:
297:
303: protected static String toClassName(String prefix, String IDL)
304: {
305: String s = IDL;
306: int a = s.indexOf(':') + 1;
307: int b = s.lastIndexOf(':');
308:
309: s = IDL.substring(a, b);
310:
311: if (s.startsWith(OMG_PREFIX))
312: s = prefix + s.substring(OMG_PREFIX.length());
313:
314: return s.replace('/', '.');
315: }
316:
317:
326: public static Class Idl2class(String IDL)
327: {
328: synchronized (m_classes)
329: {
330: Class c = (Class) m_classes.get(IDL);
331:
332: if (c != null)
333: return c;
334: else
335: {
336: String s = IDL;
337: int a = s.indexOf(':') + 1;
338: int b = s.lastIndexOf(':');
339:
340: s = IDL.substring(a, b);
341:
342: if (s.startsWith(OMG_PREFIX))
343: s = JAVA_PREFIX + s.substring(OMG_PREFIX.length());
344:
345: String cn = s.replace('/', '.');
346:
347: try
348: {
349: c = forName(cn);
350: m_classes.put(IDL, c);
351: return c;
352: }
353: catch (ClassNotFoundException ex)
354: {
355: return null;
356: }
357: }
358: }
359: }
360:
361:
372: public static java.lang.Object Idl2Object(String IDL)
373: {
374: Class cx = Idl2class(IDL);
375:
376: try
377: {
378: if (cx != null)
379: return cx.newInstance();
380: else
381: return null;
382: }
383: catch (Exception ex)
384: {
385: return null;
386: }
387: }
388:
389:
399: public static synchronized String getRepositoryId(Class cx)
400: {
401: String name = (String) m_names.get(cx);
402: if (name != null)
403: return name;
404:
405: String cn = cx.getName();
406: if (!(IDLEntity.class.isAssignableFrom(cx)
407: || ValueBase.class.isAssignableFrom(cx) || SystemException.class.isAssignableFrom(cx)))
408: {
409:
410: name = Util.createValueHandler().getRMIRepositoryID(cx);
411: }
412: else
413: {
414: if (cn.startsWith(JAVA_PREFIX))
415: cn = OMG_PREFIX
416: + cn.substring(JAVA_PREFIX.length()).replace('.', '/');
417: else if (cn.startsWith(CLASSPATH_PREFIX))
418: cn = OMG_PREFIX
419: + cn.substring(CLASSPATH_PREFIX.length()).replace('.', '/');
420:
421: name = "IDL:" + cn + ":1.0";
422: }
423: m_names.put(cx, name);
424: return name;
425: }
426:
427:
439: public static boolean insertWithHelper(Any into, Object object)
440: {
441: try
442: {
443: String helperClassName = object.getClass().getName() + "Helper";
444: Class helperClass = forName(helperClassName);
445:
446: Method insert = helperClass.getMethod("insert", new Class[] {
447: Any.class, object.getClass() });
448:
449: insert.invoke(null, new Object[] { into, object });
450:
451: return true;
452: }
453: catch (Exception exc)
454: {
455:
456: return false;
457: }
458: }
459:
460:
463: public static boolean insertSysException(Any into, SystemException exception)
464: {
465: try
466: {
467: BufferedCdrOutput output = new BufferedCdrOutput();
468:
469: String m_exception_id = getRepositoryId(exception.getClass());
470: output.write_string(m_exception_id);
471: output.write_ulong(exception.minor);
472: CompletionStatusHelper.write(output, exception.completed);
473:
474: String name = getDefaultName(m_exception_id);
475:
476: GeneralHolder h = new GeneralHolder(output);
477:
478: into.insert_Streamable(h);
479:
480: RecordTypeCode r = new RecordTypeCode(TCKind.tk_except);
481: r.setId(m_exception_id);
482: r.setName(name);
483: into.type(r);
484:
485: return true;
486: }
487: catch (Exception ex)
488: {
489: ex.printStackTrace();
490: return false;
491: }
492: }
493:
494:
497: public static String getDefaultName(String idl)
498: {
499: int f1 = idl.lastIndexOf("/");
500: int p1 = (f1 < 0) ? 0 : f1;
501: int p2 = idl.indexOf(":", p1);
502: if (p2 < 0)
503: p2 = idl.length();
504:
505: String name = idl.substring(f1 + 1, p2);
506: return name;
507: }
508:
509:
513: public static void insertException(Any into, Throwable exception)
514: {
515: boolean ok = false;
516: if (exception instanceof SystemException)
517: ok = insertSysException(into, (SystemException) exception);
518: else if (exception instanceof UserException)
519: ok = insertWithHelper(into, exception);
520:
521: if (!ok)
522: ok = insertSysException(into, new UNKNOWN());
523: if (!ok)
524: throw new InternalError("Exception wrapping broken");
525: }
526:
527:
530: public static Class findHelper(String idl)
531: {
532: synchronized (m_helpers)
533: {
534: Class c = (Class) m_helpers.get(idl);
535: if (c != null)
536: return c;
537: try
538: {
539: String helper = toHelperName(idl);
540: c = forName(helper);
541:
542: m_helpers.put(idl, c);
543: return c;
544: }
545: catch (Exception ex)
546: {
547: return null;
548: }
549: }
550: }
551:
552:
558: public static Class forName(String className) throws ClassNotFoundException
559: {
560: try
561: {
562: return Class.forName(className, true,
563: Thread.currentThread().getContextClassLoader());
564: }
565: catch (ClassNotFoundException nex)
566: {
567:
571:
572:
573:
574:
575:
576:
577:
578:
579:
580:
581: ClassLoader cl = ClassLoader.getSystemClassLoader();
582: try
583: {
584: if (cl != null)
585: return Class.forName(className, true, cl);
586: }
587: catch (ClassNotFoundException nex2)
588: {
589:
590: }
591:
592:
593: }
594: throw new ClassNotFoundException(className);
595: }
596: }