util.java
来自「java jdk 1.4的源码」· Java 代码 · 共 601 行 · 第 1/2 页
JAVA
601 行
/* * @(#)Util.java 1.27 03/01/23 * * Copyright 2003 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.internal.javax.rmi.CORBA; // Util (sed marker, don't remove!)import java.rmi.RemoteException;import javax.rmi.CORBA.ValueHandler;import org.omg.CORBA.ORB;import org.omg.CORBA.SystemException;import org.omg.CORBA.Any;import org.omg.CORBA.portable.InputStream;import org.omg.CORBA.portable.OutputStream;import org.omg.CORBA.portable.ObjectImpl;import org.omg.CORBA.TCKind;import javax.rmi.CORBA.Tie;import java.util.Hashtable;import java.util.Enumeration;import java.io.Serializable;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.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.CompletionStatus;import org.omg.CORBA.portable.UnknownException;import com.sun.corba.se.internal.io.ValueHandlerImpl;import com.sun.corba.se.internal.util.Utility;import com.sun.corba.se.internal.util.IdentityHashtable;import com.sun.corba.se.internal.util.JDKBridge;import java.io.NotSerializableException;import java.rmi.UnexpectedException;import java.rmi.MarshalException;import java.rmi.server.RMIClassLoader;import java.security.AccessController;import java.security.PrivilegedAction;import java.util.Properties;/** * Provides utility methods that can be used by stubs and ties to * perform common operations. */public abstract 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 ValueHandlerImpl valueHandlerSingleton = new ValueHandlerImpl(); public abstract RemoteException mapSystemException(SystemException ex); public abstract void writeAny(OutputStream out, Object obj); /** * 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) { //d11638 Read the Any 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</code> is * an exported RMI-IIOP server object, the tie is found * and wired to <code>obj</code>, then written to <code>out.write_Object(org.omg.CORBA.Object)</code>. * If <code>obj</code> is a CORBA Object, it is written to * <code>out.write_Object(org.omg.CORBA.Object)</code>. * @param out the stream in which to write the object. * @param obj the object to write. */ public void writeRemoteObject(OutputStream out, java.lang.Object obj) { // Make sure we have a connected object, then // write it out... Object newObj = Utility.autoConnect(obj,out.orb(),false); out.write_Object((org.omg.CORBA.Object)newObj); } /** * Writes a java.lang.Object as either a value or a CORBA Object. * If <code>obj</code> is a value object or a stub object, it is written to * <code>out.write_abstract_interface(java.lang.Object)</code>. If <code>obj</code> is an exported * RMI-IIOP server object, the tie is found and wired to <code>obj</code>, * then written to <code>out.write_abstract_interface(java.lang.Object)</code>. * @param out the stream in which to write the object. * @param obj the object to write. */ public void writeAbstractObject(OutputStream out, java.lang.Object obj) { // Make sure we have a connected object, then // write it out... Object newObj = Utility.autoConnect(obj,out.orb(),false); ((org.omg.CORBA_2_3.portable.OutputStream)out).write_abstract_interface(newObj); } /** * Registers a target for a tie. Adds the tie to an internal table and calls * {@link Tie#setTarget} on the tie object. * @param tie the tie to register. * @param target the target for the tie. */ public void registerTarget(javax.rmi.CORBA.Tie tie, java.rmi.Remote target) { synchronized (exportedServants) { // Do we already have this target registered? if (lookupTie(target) == null) { // No, so register it and set the target... exportedServants.put(target,tie); tie.setTarget(target); // Do we need to instantiate our keep-alive thread? if (keepAlive == null) { // Yes. Instantiate our keep-alive thread and start // it up... keepAlive = (KeepAlive)AccessController.doPrivileged(new PrivilegedAction() { public java.lang.Object run() { return new KeepAlive(); } }); keepAlive.start(); } } } } /** * Removes the associated tie from an internal table and calls {@link Tie#deactivate} * to deactivate the object. * @param target the object to unexport. */ public void unexportObject(java.rmi.Remote target) throws java.rmi.NoSuchObjectException { synchronized (exportedServants) { Tie cachedTie = lookupTie(target); if (cachedTie != null) { exportedServants.remove(target); Utility.purgeStubForTie(cachedTie); Utility.purgeTieAndServant(cachedTie); try { cleanUpTie(cachedTie); } catch (BAD_OPERATION e) { } catch (org.omg.CORBA.OBJ_ADAPTER e) { // This can happen when the target was never associated with a POA. // We can safely ignore this case. } // Is it time to shut down our keep alive thread? if (exportedServants.isEmpty()) { // Yep, so shut it down... keepAlive.quit(); keepAlive = null; } } else { throw new java.rmi.NoSuchObjectException("Tie not found" ); } } } protected void unregisterTargetsForORB(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 */ } } } // Needed to be overridden by subclass in the POA package protected void cleanUpTie(Tie cachedTie) throws java.rmi.NoSuchObjectException { cachedTie.setTarget(null); cachedTie.deactivate(); } /** * Returns the tie (if any) for a given target object. * @return the tie or null if no tie is registered for the given target. */ public Tie getTie (Remote target) { synchronized (exportedServants) { return lookupTie(target); } } /** * An unsynchronized version of getTie() for internal use. */ private static Tie lookupTie (Remote target) { Tie result = (Tie)exportedServants.get(target); if (result == null && target instanceof Tie) { if (exportedServants.contains(target)) { result = (Tie)target; } } return result; } /** * Returns a singleton instance of a class that implements the * {@link ValueHandler} interface. * @return a class which implements the ValueHandler interface. */ public ValueHandler createValueHandler() { return valueHandlerSingleton; } /** * Returns the codebase, if any, for the given class. * @param clz the class to get a codebase for. * @return a space-separated list of URLs, or null. */ public String getCodebase(java.lang.Class clz) { return RMIClassLoader.getClassAnnotation(clz); } /** * Returns a class instance for the specified class. * @param className the name of the class. * @param remoteCodebase a space-separated list of URLs at which * the class might be found. May be null. * @param loadingContext a class whose ClassLoader may be used to * load the class if all other methods fail. * @return the <code>Class</code> object representing the loaded class. * @exception ClassNotFoundException if class cannot be loaded. */ public Class loadClass(String className, String remoteCodebase,
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?