1 |
/* ExceptionCreator.java -- |
/* ObjectCreator.java -- |
2 |
Copyright (C) 2005 Free Software Foundation, Inc. |
Copyright (C) 2005 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
38 |
|
|
39 |
package gnu.CORBA; |
package gnu.CORBA; |
40 |
|
|
41 |
|
import gnu.CORBA.CDR.cdrBufOutput; |
42 |
|
|
43 |
|
import org.omg.CORBA.Any; |
44 |
import org.omg.CORBA.CompletionStatus; |
import org.omg.CORBA.CompletionStatus; |
45 |
import org.omg.CORBA.CompletionStatusHelper; |
import org.omg.CORBA.CompletionStatusHelper; |
46 |
import org.omg.CORBA.MARSHAL; |
import org.omg.CORBA.MARSHAL; |
47 |
|
import org.omg.CORBA.StructMember; |
48 |
import org.omg.CORBA.SystemException; |
import org.omg.CORBA.SystemException; |
49 |
|
import org.omg.CORBA.TCKind; |
50 |
import org.omg.CORBA.UNKNOWN; |
import org.omg.CORBA.UNKNOWN; |
51 |
import org.omg.CORBA.UserException; |
import org.omg.CORBA.UserException; |
52 |
import org.omg.CORBA.portable.InputStream; |
import org.omg.CORBA.portable.InputStream; |
56 |
import java.lang.reflect.Method; |
import java.lang.reflect.Method; |
57 |
|
|
58 |
/** |
/** |
59 |
* Creates java objects from the agreed IDL names for the simple |
* Creates java objects from the agreed IDL names for the simple case when the |
60 |
* case when the CORBA object is directly mapped into the locally |
* CORBA object is directly mapped into the locally defined java class. |
|
* defined java class. |
|
61 |
* |
* |
62 |
* @author Audrius Meskauskas (AudriusA@Bioinformatics.org) |
* @author Audrius Meskauskas (AudriusA@Bioinformatics.org) |
63 |
*/ |
*/ |
74 |
public static final String JAVA_PREFIX = "org.omg."; |
public static final String JAVA_PREFIX = "org.omg."; |
75 |
|
|
76 |
/** |
/** |
77 |
* The prefix for classes that are placed instide the |
* The prefix for classes that are placed instide the gnu.CORBA namespace. |
|
* gnu.CORBA namespace. |
|
78 |
*/ |
*/ |
79 |
public static final String CLASSPATH_PREFIX = "gnu.CORBA."; |
public static final String CLASSPATH_PREFIX = "gnu.CORBA."; |
80 |
|
|
81 |
/** |
/** |
82 |
* Try to instantiate an object with the given IDL name. |
* Try to instantiate an object with the given IDL name. The object must be |
83 |
* The object must be mapped to the local java class. |
* mapped to the local java class. The omg.org domain must be mapped into the |
84 |
* The omg.org domain must be mapped into the object in either |
* object in either org/omg or gnu/CORBA namespace. |
|
* org/omg or gnu/CORBA namespace. |
|
85 |
* |
* |
86 |
* @param IDL name |
* @param IDL name |
87 |
* @return instantiated object instance or null if no such |
* @return instantiated object instance or null if no such available. |
|
* available. |
|
88 |
*/ |
*/ |
89 |
public static java.lang.Object createObject(String idl, String suffix) |
public static java.lang.Object createObject(String idl, String suffix) |
90 |
{ |
{ |
110 |
/** |
/** |
111 |
* Create the system exception with the given idl name. |
* Create the system exception with the given idl name. |
112 |
* |
* |
113 |
* @param idl the exception IDL name, must match the syntax |
* @param idl the exception IDL name, must match the syntax "IDL:<class/name>:1.0". |
|
* "IDL:<class/name>:1.0". |
|
114 |
* @param minor the exception minor code. |
* @param minor the exception minor code. |
115 |
* @param completed the exception completion status. |
* @param completed the exception completion status. |
116 |
* |
* |
117 |
* @return the created exception. |
* @return the created exception. |
118 |
*/ |
*/ |
119 |
public static SystemException createSystemException(String idl, int minor, |
public static SystemException createSystemException(String idl, int minor, |
120 |
CompletionStatus completed |
CompletionStatus completed |
121 |
) |
) |
122 |
{ |
{ |
123 |
try |
try |
124 |
{ |
{ |
127 |
|
|
128 |
Constructor constructor = |
Constructor constructor = |
129 |
exClass.getConstructor(new Class[] |
exClass.getConstructor(new Class[] |
130 |
{ |
{ |
131 |
String.class, int.class, |
String.class, int.class, CompletionStatus.class |
132 |
CompletionStatus.class |
} |
133 |
} |
); |
|
); |
|
134 |
|
|
135 |
Object exception = |
Object exception = |
136 |
constructor.newInstance(new Object[] |
constructor.newInstance(new Object[] |
137 |
{ |
{ |
138 |
" Remote exception " + idl + ", minor " + |
" Remote exception " + idl + ", minor " + minor + ", " + |
139 |
minor + ", " + completed + ".", |
completed + ".", new Integer(minor), completed |
140 |
new Integer(minor), completed |
} |
141 |
} |
); |
|
); |
|
142 |
|
|
143 |
return (SystemException) exception; |
return (SystemException) exception; |
144 |
} |
} |
151 |
|
|
152 |
/** |
/** |
153 |
* Read the system exception from the given stream. |
* Read the system exception from the given stream. |
154 |
|
* |
155 |
* @param input the CDR stream to read from. |
* @param input the CDR stream to read from. |
156 |
* @return the exception that has been stored in the stream |
* @return the exception that has been stored in the stream (IDL name, minor |
157 |
* (IDL name, minor code and completion status). |
* code and completion status). |
158 |
*/ |
*/ |
159 |
public static SystemException readSystemException(InputStream input) |
public static SystemException readSystemException(InputStream input) |
160 |
{ |
{ |
169 |
} |
} |
170 |
|
|
171 |
/** |
/** |
172 |
* Reads the user exception, having the given Id, from the |
* Reads the user exception, having the given Id, from the input stream. The |
173 |
* input stream. The id is expected to be in the form like |
* id is expected to be in the form like |
174 |
* 'IDL:test/org/omg/CORBA/ORB/communication/ourUserException:1.0' |
* 'IDL:test/org/omg/CORBA/ORB/communication/ourUserException:1.0' |
175 |
* |
* |
176 |
* @param idl the exception idl name. |
* @param idl the exception idl name. |
188 |
|
|
189 |
Method read = |
Method read = |
190 |
helperClass.getMethod("read", |
helperClass.getMethod("read", |
191 |
new Class[] |
new Class[] { org.omg.CORBA.portable.InputStream.class } |
192 |
{ |
); |
|
org.omg.CORBA.portable.InputStream.class |
|
|
} |
|
|
); |
|
193 |
|
|
194 |
return (UserException) read.invoke(null, new Object[] { input }); |
return (UserException) read.invoke(null, new Object[] { input }); |
195 |
} |
} |
232 |
* @param ex an exception to write. |
* @param ex an exception to write. |
233 |
*/ |
*/ |
234 |
public static void writeSystemException(OutputStream output, |
public static void writeSystemException(OutputStream output, |
235 |
SystemException ex |
SystemException ex |
236 |
) |
) |
237 |
{ |
{ |
238 |
String exIDL = toIDL(ex.getClass().getName()); |
String exIDL = toIDL(ex.getClass().getName()); |
239 |
output.write_string(exIDL); |
output.write_string(exIDL); |
262 |
} |
} |
263 |
|
|
264 |
/** |
/** |
265 |
* Converts the given IDL name to class name and tries to load the |
* Converts the given IDL name to class name and tries to load the matching |
266 |
* matching class. The OMG prefix (omg.org) is replaced by |
* class. The OMG prefix (omg.org) is replaced by the java prefix org.omg. No |
267 |
* the java prefix org.omg. No other prefixes are added. |
* other prefixes are added. |
268 |
* |
* |
269 |
* @param IDL the idl name. |
* @param IDL the idl name. |
270 |
* |
* |
271 |
* TODO Cache the returned classes, avoiding these string manipulations |
* TODO Cache the returned classes, avoiding these string manipulations each |
272 |
* each time the conversion is required. |
* time the conversion is required. |
273 |
* |
* |
274 |
* @return the matching class or null if no such is available. |
* @return the matching class or null if no such is available. |
275 |
*/ |
*/ |
297 |
} |
} |
298 |
|
|
299 |
/** |
/** |
300 |
* Converts the given IDL name to class name, tries to load the |
* Converts the given IDL name to class name, tries to load the matching class |
301 |
* matching class and create an object instance with parameterless |
* and create an object instance with parameterless constructor. The OMG |
302 |
* constructor. The OMG prefix (omg.org) is replaced by |
* prefix (omg.org) is replaced by the java prefix org.omg. No other prefixes |
303 |
* the java prefix org.omg. No other prefixes are added. |
* are added. |
304 |
* |
* |
305 |
* @param IDL the idl name. |
* @param IDL the idl name. |
306 |
* |
* |
337 |
cn = OMG_PREFIX + cn.substring(JAVA_PREFIX.length()).replace('.', '/'); |
cn = OMG_PREFIX + cn.substring(JAVA_PREFIX.length()).replace('.', '/'); |
338 |
else if (cn.startsWith(CLASSPATH_PREFIX)) |
else if (cn.startsWith(CLASSPATH_PREFIX)) |
339 |
cn = |
cn = |
340 |
OMG_PREFIX + cn.substring(CLASSPATH_PREFIX.length()).replace('.', '/'); |
OMG_PREFIX + |
341 |
|
cn.substring(CLASSPATH_PREFIX.length()).replace('.', '/'); |
342 |
|
|
343 |
return "IDL:" + cn + ":1.0"; |
return "IDL:" + cn + ":1.0"; |
344 |
} |
} |
345 |
|
|
346 |
|
/** |
347 |
|
* Insert the passed parameter into the given Any, assuming that the helper |
348 |
|
* class is available. The helper class must have the "Helper" suffix and be |
349 |
|
* in the same package as the class of the object being inserted. |
350 |
|
* |
351 |
|
* @param into the target to insert. |
352 |
|
* |
353 |
|
* @param object the object to insert. It can be any object as far as the |
354 |
|
* corresponding helper is provided. |
355 |
|
* |
356 |
|
* @return true on success, false otherwise. |
357 |
|
*/ |
358 |
|
public static boolean insertWithHelper(Any into, Object object) |
359 |
|
{ |
360 |
|
try |
361 |
|
{ |
362 |
|
String helperClassName = object.getClass().getName() + "Helper"; |
363 |
|
Class helperClass = Class.forName(helperClassName); |
364 |
|
|
365 |
|
Method insert = |
366 |
|
helperClass.getMethod("insert", |
367 |
|
new Class[] { Any.class, object.getClass() } |
368 |
|
); |
369 |
|
|
370 |
|
insert.invoke(null, new Object[] { into, object }); |
371 |
|
|
372 |
|
return true; |
373 |
|
} |
374 |
|
catch (Exception exc) |
375 |
|
{ |
376 |
|
// Failed due some reason. |
377 |
|
return false; |
378 |
|
} |
379 |
|
} |
380 |
|
|
381 |
|
/** |
382 |
|
* Insert the system exception into the given Any. |
383 |
|
*/ |
384 |
|
public static boolean insertSysException(Any into, SystemException exception) |
385 |
|
{ |
386 |
|
try |
387 |
|
{ |
388 |
|
cdrBufOutput output = new cdrBufOutput(); |
389 |
|
|
390 |
|
String m_exception_id = toIDL(exception.getClass().getName()); |
391 |
|
output.write_string(m_exception_id); |
392 |
|
output.write_ulong(exception.minor); |
393 |
|
CompletionStatusHelper.write(output, exception.completed); |
394 |
|
|
395 |
|
String name = getDefaultName(m_exception_id); |
396 |
|
|
397 |
|
universalHolder h = new universalHolder(output); |
398 |
|
|
399 |
|
into.insert_Streamable(h); |
400 |
|
|
401 |
|
recordTypeCode r = new recordTypeCode(TCKind.tk_except); |
402 |
|
r.setId(m_exception_id); |
403 |
|
r.setName(name); |
404 |
|
into.type(r); |
405 |
|
|
406 |
|
return true; |
407 |
|
} |
408 |
|
catch (Exception ex) |
409 |
|
{ |
410 |
|
ex.printStackTrace(); |
411 |
|
return false; |
412 |
|
} |
413 |
|
} |
414 |
|
|
415 |
|
/** |
416 |
|
* Get the type name from the IDL string. |
417 |
|
*/ |
418 |
|
public static String getDefaultName(String idl) |
419 |
|
{ |
420 |
|
int f1 = idl.lastIndexOf("/"); |
421 |
|
int p1 = (f1 < 0) ? 0 : f1; |
422 |
|
int p2 = idl.indexOf(":", p1); |
423 |
|
if (p2 < 0) |
424 |
|
p2 = idl.length(); |
425 |
|
|
426 |
|
String name = idl.substring(f1 + 1, p2); |
427 |
|
return name; |
428 |
|
} |
429 |
|
|
430 |
|
/** |
431 |
|
* Insert this exception into the given Any. On failure, insert the UNKNOWN |
432 |
|
* exception. |
433 |
|
*/ |
434 |
|
public static void insertException(Any into, Throwable exception) |
435 |
|
{ |
436 |
|
boolean ok = false; |
437 |
|
if (exception instanceof SystemException) |
438 |
|
ok = insertSysException(into, (SystemException) exception); |
439 |
|
else if (exception instanceof UserException) |
440 |
|
ok = insertWithHelper(into, exception); |
441 |
|
|
442 |
|
if (!ok) |
443 |
|
ok = insertSysException(into, new UNKNOWN()); |
444 |
|
if (!ok) |
445 |
|
throw new InternalError("Exception wrapping broken"); |
446 |
|
} |
447 |
} |
} |