📄 utility.java
字号:
/* * @(#)Utility.java 1.42 05/11/17 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. *//* * Licensed Materials - Property of IBM * RMI-IIOP v1.0 * Copyright IBM Corp. 1998 1999 All Rights Reserved * * US Government Users Restricted Rights - Use, duplication or * disclosure restricted by GSA ADP Schedule Contract with IBM Corp. */package com.sun.corba.se.impl.util;import org.omg.CORBA.SystemException;import org.omg.CORBA.CompletionStatus;import org.omg.CORBA.BAD_OPERATION;import org.omg.CORBA.BAD_INV_ORDER;import org.omg.CORBA.BAD_PARAM;import org.omg.CORBA.ORB;import org.omg.CORBA.Any;import org.omg.CORBA.TypeCode;import org.omg.CORBA.Principal;import org.omg.CORBA.portable.InputStream;import org.omg.CORBA.portable.OutputStream;import org.omg.CORBA.portable.BoxedValueHelper;import org.omg.CORBA.portable.ValueFactory;import org.omg.CORBA.portable.Streamable;import org.omg.CORBA.portable.Delegate;import java.util.Hashtable;import java.util.NoSuchElementException;import java.rmi.Remote;import java.rmi.NoSuchObjectException;import java.rmi.RemoteException;import java.rmi.server.RemoteStub;import javax.rmi.PortableRemoteObject;import javax.rmi.CORBA.Stub;import javax.rmi.CORBA.Tie;import javax.rmi.CORBA.Util;import java.io.Serializable;import java.io.File;import java.io.FileInputStream;import org.omg.PortableServer.POA;import com.sun.org.omg.SendingContext.CodeBase;import com.sun.corba.se.spi.logging.CORBALogDomains ;import com.sun.corba.se.spi.presentation.rmi.PresentationManager;import com.sun.corba.se.spi.presentation.rmi.StubAdapter ;import com.sun.corba.se.impl.logging.UtilSystemException ;import com.sun.corba.se.impl.logging.OMGSystemException ;/** * Handy class full of static functions. */public final class Utility { public static final String STUB_PREFIX = "_"; public static final String RMI_STUB_SUFFIX = "_Stub"; public static final String DYNAMIC_STUB_SUFFIX = "_DynamicStub" ; public static final String IDL_STUB_SUFFIX = "Stub"; public static final String TIE_SUFIX = "_Tie"; private static IdentityHashtable tieCache = new IdentityHashtable(); private static IdentityHashtable tieToStubCache = new IdentityHashtable(); private static IdentityHashtable stubToTieCache = new IdentityHashtable(); private static Object CACHE_MISS = new Object(); private static UtilSystemException wrapper = UtilSystemException.get( CORBALogDomains.UTIL ) ; private static OMGSystemException omgWrapper = OMGSystemException.get( CORBALogDomains.UTIL ) ; /** * Ensure that stubs, ties, and implementation objects * are 'connected' to the runtime. Converts implementation * objects to a type suitable for sending on the wire. * @param obj the object to connect. * @param orb the ORB to connect to if obj is exported to IIOP. * @param convertToStub true if implementation types should be * converted to Stubs rather than just org.omg.CORBA.Object. * @return the connected object. * @exception NoSuchObjectException if obj is an implementation * which has not been exported. */ public static Object autoConnect(Object obj, ORB orb, boolean convertToStub) { if (obj == null) { return obj; } if (StubAdapter.isStub(obj)) { try { StubAdapter.getDelegate(obj) ; } catch (BAD_OPERATION okay) { try { StubAdapter.connect( obj, orb ) ; } catch (RemoteException e) { // The stub could not be connected because it // has an invalid IOR... throw wrapper.objectNotConnected( e, obj.getClass().getName() ) ; } } return obj; } if (obj instanceof Remote) { Remote remoteObj = (Remote)obj; Tie theTie = Util.getTie(remoteObj); if (theTie != null) { try { theTie.orb(); } catch (SystemException okay) { theTie.orb(orb); } if (convertToStub) { Object result = loadStub(theTie,null,null,true); if (result != null) { return result; } else { throw wrapper.couldNotLoadStub(obj.getClass().getName()); } } else { return StubAdapter.activateTie( theTie ); } } else { // This is an implementation object which has not been // exported to IIOP OR is a JRMP stub or implementation // object which cannot be marshalled into an ORB stream... throw wrapper.objectNotExported( obj.getClass().getName() ) ; } } // Didn't need to do anything, just return the input... return obj; } /* * Get a new instance of an RMI-IIOP Tie for the * given server object. */ public static Tie loadTie(Remote obj) { Tie result = null; Class objClass = obj.getClass(); // Have we tried to find this guy before? synchronized (tieCache) { Object it = tieCache.get(obj); if (it == null) { // No, so try it... try { // First try the classname... result = loadTie(objClass); // If we don't have a valid tie at this point, // walk up the parent chain until we either // load a tie or encounter PortableRemoteObject // or java.lang.Object... while (result == null && (objClass = objClass.getSuperclass()) != null && objClass != PortableRemoteObject.class && objClass != Object.class) { result = loadTie(objClass); } } catch (Exception ex) { wrapper.loadTieFailed( ex, objClass.getName() ) ; } // Did we get it? if (result == null) { // Nope, so cache that fact... tieCache.put(obj,CACHE_MISS); } else { // Yes, so cache it... tieCache.put(obj,result); } } else { // Yes, return a new instance or fail again if // it was a miss last time... if (it != CACHE_MISS) { try { result = (Tie) it.getClass().newInstance(); } catch (Exception e) { } } } } return result; } /* * Load an RMI-IIOP Tie */ private static Tie loadTie(Class theClass) { return com.sun.corba.se.spi.orb.ORB.getStubFactoryFactory(). getTie( theClass ) ; } /* * Clear the stub/tie caches. Intended for use by * test code. */ public static void clearCaches() { synchronized (tieToStubCache) { tieToStubCache.clear(); } synchronized (tieCache) { tieCache.clear(); } synchronized (stubToTieCache) { stubToTieCache.clear(); } } /* * Load a class and check that it is assignable to a given type. * @param className the class name. * @param remoteCodebase the codebase to use. May be null. * @param loader the class loader of last resort. May be null. * @param expectedType the expected type. May be null. * @return the loaded class. */ static Class loadClassOfType(String className, String remoteCodebase, ClassLoader loader, Class expectedType, ClassLoader expectedTypeClassLoader) throws ClassNotFoundException { Class loadedClass = null; try { //Sequence finding of the stubs according to spec try{ //If-else is put here for speed up of J2EE. //According to the OMG spec, the if clause is not dead code. //It can occur if some compiler has allowed generation //into org.omg.stub hierarchy for non-offending //classes. This will encourage people to //produce non-offending class stubs in their own hierarchy. if (!PackagePrefixChecker.hasOffendingPrefix( PackagePrefixChecker.withoutPackagePrefix(className))){ loadedClass = Util.loadClass( PackagePrefixChecker.withoutPackagePrefix(className), remoteCodebase, loader); } else { loadedClass = Util.loadClass(className, remoteCodebase, loader); } } catch (ClassNotFoundException cnfe) { loadedClass = Util.loadClass(className, remoteCodebase, loader); } if (expectedType == null) return loadedClass; } catch (ClassNotFoundException cnfe) { if (expectedType == null) throw cnfe; } // If no class was loaded, or if the loaded class is not of the // correct type, make a further attempt to load the correct class // using the classloader of the expected type. // _REVISIT_ Is this step necessary, or should the Util,loadClass // algorithm always produce a valid class if the setup is correct? // Does the OMG standard algorithm need to be changed to include // this step? if (loadedClass == null || !expectedType.isAssignableFrom(loadedClass)){ if (expectedType.getClassLoader() != expectedTypeClassLoader) throw new IllegalArgumentException( "expectedTypeClassLoader not class loader of " + "expected Type."); if (expectedTypeClassLoader != null) loadedClass = expectedTypeClassLoader.loadClass(className); else { ClassLoader cl = Thread.currentThread().getContextClassLoader(); if (cl == null) cl = ClassLoader.getSystemClassLoader(); loadedClass = cl.loadClass(className); } } return loadedClass; } /* * Load a class and check that it is compatible with a given type. * @param className the class name. * @param remoteCodebase the codebase to use. May be null. * @param loadingContext the loading context. May be null. * @param relatedType the related type. May be null. * @return the loaded class. */ public static Class loadClassForClass (String className, String remoteCodebase, ClassLoader loader, Class relatedType,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -