|
Java example source code file (Util.java)
The Util.java Java example source code/* * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* * Licensed Materials - Property of IBM * RMI-IIOP v1.0 * Copyright IBM Corp. 1998 1999 All Rights Reserved * */ package com.sun.corba.se.impl.javax.rmi.CORBA; // Util (sed marker, don't remove!) import java.rmi.RemoteException; import java.rmi.UnexpectedException; import java.rmi.MarshalException; import java.rmi.server.RMIClassLoader; import java.util.Hashtable; import java.util.Enumeration; import java.util.Properties; import java.util.Map; import java.util.WeakHashMap; import java.io.Serializable; import java.io.NotSerializableException; import java.lang.reflect.Constructor; import javax.rmi.CORBA.ValueHandler; import javax.rmi.CORBA.Tie; import java.security.AccessController; import java.security.PrivilegedAction; import java.rmi.MarshalException; import java.rmi.NoSuchObjectException; import java.rmi.AccessException; import java.rmi.Remote; import java.rmi.ServerError; import java.rmi.ServerException; import java.rmi.ServerRuntimeException; import javax.transaction.TransactionRequiredException; import javax.transaction.TransactionRolledbackException; import javax.transaction.InvalidTransactionException; import org.omg.CORBA.SystemException; import org.omg.CORBA.Any; import org.omg.CORBA.TypeCode; import org.omg.CORBA.COMM_FAILURE; import org.omg.CORBA.BAD_PARAM; import org.omg.CORBA.INV_OBJREF; import org.omg.CORBA.NO_PERMISSION; import org.omg.CORBA.MARSHAL; import org.omg.CORBA.OBJECT_NOT_EXIST; import org.omg.CORBA.TRANSACTION_REQUIRED; import org.omg.CORBA.TRANSACTION_ROLLEDBACK; import org.omg.CORBA.INVALID_TRANSACTION; import org.omg.CORBA.BAD_OPERATION; import org.omg.CORBA.ACTIVITY_REQUIRED; import org.omg.CORBA.ACTIVITY_COMPLETED; import org.omg.CORBA.INVALID_ACTIVITY; import org.omg.CORBA.CompletionStatus; import org.omg.CORBA.TCKind; import org.omg.CORBA.portable.UnknownException; import org.omg.CORBA.portable.InputStream; import org.omg.CORBA.portable.OutputStream; // This class must be able to function with non-Sun ORBs. // This means that any of the following com.sun.corba classes // must only occur in contexts that also handle the non-Sun case. import com.sun.corba.se.pept.transport.ContactInfoList ; import com.sun.corba.se.spi.orb.ORB; import com.sun.corba.se.spi.orb.ORBVersionFactory; import com.sun.corba.se.spi.protocol.CorbaClientDelegate; import com.sun.corba.se.spi.transport.CorbaContactInfoList ; import com.sun.corba.se.spi.protocol.LocalClientRequestDispatcher ; import com.sun.corba.se.spi.copyobject.ReflectiveCopyException ; import com.sun.corba.se.spi.copyobject.CopierManager ; import com.sun.corba.se.spi.copyobject.ObjectCopierFactory ; import com.sun.corba.se.spi.copyobject.ObjectCopier ; import com.sun.corba.se.impl.io.ValueHandlerImpl; import com.sun.corba.se.impl.orbutil.ORBConstants; import com.sun.corba.se.impl.orbutil.ORBUtility; import com.sun.corba.se.impl.logging.OMGSystemException; import com.sun.corba.se.impl.util.Utility; import com.sun.corba.se.impl.util.IdentityHashtable; import com.sun.corba.se.impl.util.JDKBridge; import com.sun.corba.se.impl.logging.UtilSystemException; import com.sun.corba.se.spi.logging.CORBALogDomains; import sun.corba.SharedSecrets; /** * Provides utility methods that can be used by stubs and ties to * perform common operations. */ public class Util implements javax.rmi.CORBA.UtilDelegate { // Runs as long as there are exportedServants private static KeepAlive keepAlive = null; // Maps targets to ties. private static IdentityHashtable exportedServants = new IdentityHashtable(); private static final ValueHandlerImpl valueHandlerSingleton = SharedSecrets.getJavaCorbaAccess().newValueHandlerImpl(); private UtilSystemException utilWrapper = UtilSystemException.get( CORBALogDomains.RPC_ENCODING); private static Util instance = null; public Util() { setInstance(this); } private static void setInstance( Util util ) { assert instance == null : "Instance already defined"; instance = util; } public static Util getInstance() { return instance; } public static boolean isInstanceDefined() { return instance != null; } // Used by TOAFactory.shutdown to unexport all targets for this // particular ORB. This happens during ORB shutdown. public void unregisterTargetsForORB(org.omg.CORBA.ORB orb) { for (Enumeration e = exportedServants.keys(); e.hasMoreElements(); ) { java.lang.Object key = e.nextElement(); Remote target = (Remote)(key instanceof Tie ? ((Tie)key).getTarget() : key); // Bug 4476347: BAD_OPERATION is thrown if the ties delegate isn't set. // We can ignore this because it means the tie is not connected to an ORB. try { if (orb == getTie(target).orb()) { try { unexportObject(target); } catch( java.rmi.NoSuchObjectException ex ) { // We neglect this exception if at all if it is // raised. It is not harmful. } } } catch (BAD_OPERATION bad) { /* Ignore */ } } } /** * 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); } /** * Writes any java.lang.Object as a CORBA any. * @param out the stream in which to write the any. * @param obj the object to write as an any. */ public void writeAny( org.omg.CORBA.portable.OutputStream out, java.lang.Object obj) { org.omg.CORBA.ORB orb = out.orb(); // Create Any Any any = orb.create_any(); // Make sure we have a connected object... java.lang.Object newObj = Utility.autoConnect(obj,orb,false); if (newObj instanceof org.omg.CORBA.Object) { any.insert_Object((org.omg.CORBA.Object)newObj); } else { if (newObj == null) { // Handle the null case, including backwards // compatibility issues any.insert_Value(null, createTypeCodeForNull(orb)); } else { if (newObj instanceof Serializable) { // If they're our Any and ORB implementations, // we may want to do type code related versioning. TypeCode tc = createTypeCode((Serializable)newObj, any, orb); if (tc == null) any.insert_Value((Serializable)newObj); else any.insert_Value((Serializable)newObj, tc); } else if (newObj instanceof Remote) { ORBUtility.throwNotSerializableForCorba(newObj.getClass().getName()); } else { ORBUtility.throwNotSerializableForCorba(newObj.getClass().getName()); } } } out.write_any(any); } /** * When using our own ORB and Any implementations, we need to get * the ORB version and create the type code appropriately. This is * to overcome a bug in which the JDK 1.3.x ORBs used a tk_char * rather than a tk_wchar to describe a Java char field. * * This only works in RMI-IIOP with Util.writeAny since we actually * know what ORB and stream we're writing with when we insert * the value. * * Returns null if it wasn't possible to create the TypeCode (means * it wasn't our ORB or Any implementation). * * This does not handle null objs. */ private TypeCode createTypeCode(Serializable obj, org.omg.CORBA.Any any, org.omg.CORBA.ORB orb) { if (any instanceof com.sun.corba.se.impl.corba.AnyImpl && orb instanceof ORB) { com.sun.corba.se.impl.corba.AnyImpl anyImpl = (com.sun.corba.se.impl.corba.AnyImpl)any; ORB ourORB = (ORB)orb; return anyImpl.createTypeCodeForClass(obj.getClass(), ourORB); } else return null; } /** * This is used to create the TypeCode for a null reference. * It also handles backwards compatibility with JDK 1.3.x. * * This method will not return null. */ private TypeCode createTypeCodeForNull(org.omg.CORBA.ORB orb) { if (orb instanceof ORB) { ORB ourORB = (ORB)orb; // Preserve backwards compatibility with Kestrel and Ladybird // by not fully implementing interop issue resolution 3857, // and returning a null TypeCode with a tk_value TCKind. // If we're not talking to Kestrel or Ladybird, fall through // to the abstract interface case (also used for foreign ORBs). if (!ORBVersionFactory.getFOREIGN().equals(ourORB.getORBVersion()) && ORBVersionFactory.getNEWER().compareTo(ourORB.getORBVersion()) > 0) { return orb.get_primitive_tc(TCKind.tk_value); } } // Use tk_abstract_interface as detailed in the resolution // REVISIT: Define this in IDL and get the ID in generated code String abstractBaseID = "IDL:omg.org/CORBA/AbstractBase:1.0"; return orb.create_abstract_interface_tc(abstractBaseID, ""); } /** * Reads a java.lang.Object as a CORBA any. * @param in the stream from which to read the any. * @return the object read from the stream. */ public Object readAny(InputStream in) { Any any = in.read_any(); if ( any.type().kind().value() == TCKind._tk_objref ) return any.extract_Object (); else return any.extract_Value(); } /** * Writes a java.lang.Object as a CORBA Object. If <code>obj is * an exported RMI-IIOP server object, the tie is found * and wired to <code>obj, then written to Other Java examples (source code examples)Here is a short list of links related to this Java Util.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.