/** * Static method for reading a CORBA standard exception from a stream. * * @param strm The InputStream to use for unmarshaling. */ public static SystemException readSystemException(InputStream strm) { try { String name = classNameOf(strm.read_string()); SystemException ex = (SystemException) ORBClassLoader.loadClass(name).newInstance(); ex.minor = strm.read_long(); ex.completed = CompletionStatus.from_int(strm.read_long()); return ex; } catch (Exception ex) { throw wrapper.unknownSysex(CompletionStatus.COMPLETED_MAYBE, ex); } }
/** * Static method for writing a CORBA standard exception to a stream. * * @param strm The OutputStream to use for marshaling. */ public static void writeSystemException(SystemException ex, OutputStream strm) { String s; s = repositoryIdOf(ex.getClass().getName()); strm.write_string(s); strm.write_long(ex.minor); strm.write_long(ex.completed.value()); }
/** * Static method for writing a CORBA standard exception to an Any. * * @param any The Any to write the SystemException into. */ public static void insertSystemException(SystemException ex, Any any) { OutputStream out = any.create_output_stream(); ORB orb = (ORB) (out.orb()); String name = ex.getClass().getName(); String repID = ORBUtility.repositoryIdOf(name); out.write_string(repID); out.write_long(ex.minor); out.write_long(ex.completed.value()); any.read_value(out.create_input_stream(), getSystemExceptionTypeCode(orb, repID, name)); }
@Test public void test() { org.omg.CosTransactions.Status status = Status.StatusUnknown; tranobject_i localObject = null; demosync sync = null; ORB myORB = null; RootOA myOA = null; try { myORB = ORB.getInstance("test"); myOA = OA.getRootOA(myORB); myORB.initORB(new String[] {}, null); myOA.initOA(); ORBManager.setORB(myORB); ORBManager.setPOA(myOA); Control myControl = null; org.omg.CosTransactions.Current current = OTSManager.get_current(); Coordinator coord = null; sync = new demosync(); localObject = new tranobject_i(); current.begin(); myControl = current.get_control(); coord = myControl.get_coordinator(); coord.register_resource(localObject.getReference()); coord.register_synchronization(sync.getReference()); try { current.commit(true); } catch (TRANSACTION_ROLLEDBACK e1) { System.out.println("Transaction rolledback"); } try { status = coord.get_status(); } catch (SystemException ex) { // assume reference no longer valid! status = Status.StatusUnknown; } } catch (UserException e1) { fail("Caught UserException: " + e1); e1.printStackTrace(); } catch (SystemException e2) { fail("Caught SystemException: " + e2); e2.printStackTrace(); } System.out.print( "Final action status: " + com.arjuna.ats.jts.utils.Utility.stringStatus(status)); System.out.println("\nTest completed successfully."); myOA.shutdownObject(sync); myOA.shutdownObject(localObject); myOA.destroy(); myORB.shutdown(); }
/** * Maps a SystemException to a RemoteException. * * @param ex the SystemException to map. * @return the mapped exception. */ public RemoteException mapSystemException(SystemException ex) { if (ex instanceof UnknownException) { Throwable orig = ((UnknownException) ex).originalEx; if (orig instanceof Error) { return new ServerError("Error occurred in server thread", (Error) orig); } else if (orig instanceof RemoteException) { return new ServerException("RemoteException occurred in server thread", (Exception) orig); } else if (orig instanceof RuntimeException) { throw (RuntimeException) orig; } } // Build the message string... String name = ex.getClass().getName(); String corbaName = name.substring(name.lastIndexOf('.') + 1); String status; switch (ex.completed.value()) { case CompletionStatus._COMPLETED_YES: status = "Yes"; break; case CompletionStatus._COMPLETED_NO: status = "No"; break; case CompletionStatus._COMPLETED_MAYBE: default: status = "Maybe"; break; } String message = "CORBA " + corbaName + " " + ex.minor + " " + status; // Now map to the correct RemoteException type... if (ex instanceof COMM_FAILURE) { return new MarshalException(message, ex); } else if (ex instanceof INV_OBJREF) { RemoteException newEx = new NoSuchObjectException(message); newEx.detail = ex; return newEx; } else if (ex instanceof NO_PERMISSION) { return new AccessException(message, ex); } else if (ex instanceof MARSHAL) { return new MarshalException(message, ex); } else if (ex instanceof OBJECT_NOT_EXIST) { RemoteException newEx = new NoSuchObjectException(message); newEx.detail = ex; return newEx; } else if (ex instanceof TRANSACTION_REQUIRED) { RemoteException newEx = new TransactionRequiredException(message); newEx.detail = ex; return newEx; } else if (ex instanceof TRANSACTION_ROLLEDBACK) { RemoteException newEx = new TransactionRolledbackException(message); newEx.detail = ex; return newEx; } else if (ex instanceof INVALID_TRANSACTION) { RemoteException newEx = new InvalidTransactionException(message); newEx.detail = ex; return newEx; } else if (ex instanceof BAD_PARAM) { Exception inner = ex; // Pre-Merlin Sun ORBs used the incorrect minor code for // this case. See Java to IDL ptc-00-01-08 1.4.8. if (ex.minor == ORBConstants.LEGACY_SUN_NOT_SERIALIZABLE || ex.minor == OMGSystemException.NOT_SERIALIZABLE) { if (ex.getMessage() != null) inner = new NotSerializableException(ex.getMessage()); else inner = new NotSerializableException(); inner.initCause(ex); } return new MarshalException(message, inner); } else if (ex instanceof ACTIVITY_REQUIRED) { try { Class<?> cl = SharedSecrets.getJavaCorbaAccess() .loadClass("javax.activity.ActivityRequiredException"); Class[] params = new Class[2]; params[0] = java.lang.String.class; params[1] = java.lang.Throwable.class; Constructor cr = cl.getConstructor(params); Object[] args = new Object[2]; args[0] = message; args[1] = ex; return (RemoteException) cr.newInstance(args); } catch (Throwable e) { utilWrapper.classNotFound(e, "javax.activity.ActivityRequiredException"); } } else if (ex instanceof ACTIVITY_COMPLETED) { try { Class<?> cl = SharedSecrets.getJavaCorbaAccess() .loadClass("javax.activity.ActivityCompletedException"); Class[] params = new Class[2]; params[0] = java.lang.String.class; params[1] = java.lang.Throwable.class; Constructor cr = cl.getConstructor(params); Object[] args = new Object[2]; args[0] = message; args[1] = ex; return (RemoteException) cr.newInstance(args); } catch (Throwable e) { utilWrapper.classNotFound(e, "javax.activity.ActivityCompletedException"); } } else if (ex instanceof INVALID_ACTIVITY) { try { Class<?> cl = SharedSecrets.getJavaCorbaAccess().loadClass("javax.activity.InvalidActivityException"); Class[] params = new Class[2]; params[0] = java.lang.String.class; params[1] = java.lang.Throwable.class; Constructor cr = cl.getConstructor(params); Object[] args = new Object[2]; args[0] = message; args[1] = ex; return (RemoteException) cr.newInstance(args); } catch (Throwable e) { utilWrapper.classNotFound(e, "javax.activity.InvalidActivityException"); } } // Just map to a generic RemoteException... return new RemoteException(message, ex); }
@Test public void test() { ORB myORB = null; RootOA myOA = null; try { myORB = ORB.getInstance("test"); myOA = OA.getRootOA(myORB); myORB.initORB(new String[] {}, null); myOA.initOA(); ORBManager.setORB(myORB); ORBManager.setPOA(myOA); TransactionFactoryImple theOTS = new TransactionFactoryImple(); Control myControl; grid_i localGrid = new grid_i(100, 100); int h, w, v; myControl = theOTS.create(0); assertNotNull(myControl); h = localGrid.height(); w = localGrid.width(); localGrid.set(2, 4, 123, myControl); v = localGrid.get(2, 4, myControl); // no problem setting and getting the elememt: System.out.println("grid[2,4] is " + v); assertEquals(123, v); Terminator handle = myControl.get_terminator(); try { if (handle != null) { handle.commit(false); } else System.err.println("Error - no transaction terminator!"); } catch (Exception ex) { System.out.println("Test error! Caught: " + ex); } ORBManager.getPOA().shutdownObject(theOTS); ORBManager.getPOA().shutdownObject(localGrid); } catch (UserException e) { fail("Caught UserException: " + e); e.printStackTrace(); } catch (SystemException e) { fail("Caught SystemException: " + e); e.printStackTrace(); } System.out.println("\nWill now try different thread terminating transaction.\n"); try { org.omg.CosTransactions.Current current = OTSManager.get_current(); System.out.println("Starting new transaction."); current.begin(); Control tc = current.get_control(); if (tc != null) { System.out.println("Creating new thread."); TransactionalThread tranThread = new TransactionalThread(tc); System.out.println("Waiting for thread to terminate transaction.\n"); tranThread.start(); while (!tranThread.finished()) Thread.yield(); System.out.println("\nCreator will now attempt to rollback transaction. Should fail."); try { current.rollback(); fail("Error - managed to rollback transaction!"); } catch (NoTransaction e1) { System.out.println("Correct termination - caught: " + e1); } catch (INVALID_TRANSACTION e2) { System.out.println("Correct termination - caught: " + e2); } catch (Exception e3) { fail("Wrong termination - caught unexpected exception: " + e3); e3.printStackTrace(); } System.out.println("Test completed successfully."); } else System.err.println("Error - null transaction control!"); } catch (Exception e) { System.out.println("Caught unexpected exception: " + e); } myOA.destroy(); myORB.shutdown(); }