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