Exemplo n.º 1
0
public abstract class InputStreamHook extends ObjectInputStream {
  // These should be visible in all the nested classes
  static final OMGSystemException omgWrapper = OMGSystemException.get(CORBALogDomains.RPC_ENCODING);

  static final UtilSystemException utilWrapper =
      UtilSystemException.get(CORBALogDomains.RPC_ENCODING);

  private class HookGetFields extends ObjectInputStream.GetField {
    private Map fields = null;

    HookGetFields(Map fields) {
      this.fields = fields;
    }

    /**
     * Get the ObjectStreamClass that describes the fields in the stream.
     *
     * <p>REVISIT! This doesn't work since we have our own ObjectStreamClass.
     */
    public java.io.ObjectStreamClass getObjectStreamClass() {
      return null;
    }

    /** Return true if the named field is defaulted and has no value in this stream. */
    public boolean defaulted(String name) throws IOException, IllegalArgumentException {
      return (!fields.containsKey(name));
    }

    /** Get the value of the named boolean field from the persistent field. */
    public boolean get(String name, boolean defvalue) throws IOException, IllegalArgumentException {
      if (defaulted(name)) return defvalue;
      else return ((Boolean) fields.get(name)).booleanValue();
    }

    /** Get the value of the named char field from the persistent fields. */
    public char get(String name, char defvalue) throws IOException, IllegalArgumentException {
      if (defaulted(name)) return defvalue;
      else return ((Character) fields.get(name)).charValue();
    }

    /** Get the value of the named byte field from the persistent fields. */
    public byte get(String name, byte defvalue) throws IOException, IllegalArgumentException {
      if (defaulted(name)) return defvalue;
      else return ((Byte) fields.get(name)).byteValue();
    }

    /** Get the value of the named short field from the persistent fields. */
    public short get(String name, short defvalue) throws IOException, IllegalArgumentException {
      if (defaulted(name)) return defvalue;
      else return ((Short) fields.get(name)).shortValue();
    }

    /** Get the value of the named int field from the persistent fields. */
    public int get(String name, int defvalue) throws IOException, IllegalArgumentException {
      if (defaulted(name)) return defvalue;
      else return ((Integer) fields.get(name)).intValue();
    }

    /** Get the value of the named long field from the persistent fields. */
    public long get(String name, long defvalue) throws IOException, IllegalArgumentException {
      if (defaulted(name)) return defvalue;
      else return ((Long) fields.get(name)).longValue();
    }

    /** Get the value of the named float field from the persistent fields. */
    public float get(String name, float defvalue) throws IOException, IllegalArgumentException {
      if (defaulted(name)) return defvalue;
      else return ((Float) fields.get(name)).floatValue();
    }

    /** Get the value of the named double field from the persistent field. */
    public double get(String name, double defvalue) throws IOException, IllegalArgumentException {
      if (defaulted(name)) return defvalue;
      else return ((Double) fields.get(name)).doubleValue();
    }

    /** Get the value of the named Object field from the persistent field. */
    public Object get(String name, Object defvalue) throws IOException, IllegalArgumentException {
      if (defaulted(name)) return defvalue;
      else return fields.get(name);
    }

    public String toString() {
      return fields.toString();
    }
  }

  public InputStreamHook() throws IOException {
    super();
  }

  public void defaultReadObject() throws IOException, ClassNotFoundException, NotActiveException {
    readObjectState.beginDefaultReadObject(this);

    defaultReadObjectDelegate();

    readObjectState.endDefaultReadObject(this);
  }

  public abstract void defaultReadObjectDelegate();

  abstract void readFields(java.util.Map fieldToValueMap)
      throws java.io.InvalidClassException, java.io.StreamCorruptedException,
          ClassNotFoundException, java.io.IOException;

  // See java.io.ObjectInputStream.GetField
  // Remember that this is equivalent to defaultReadObject
  // in RMI-IIOP
  public ObjectInputStream.GetField readFields()
      throws IOException, ClassNotFoundException, NotActiveException {

    HashMap fieldValueMap = new HashMap();

    // We were treating readFields same as defaultReadObject. It is
    // incorrect if the state is readOptionalData. If this line
    // is uncommented, it will throw a stream corrupted exception.
    // _REVISIT_: The ideal fix would be to add a new state. In
    // writeObject user may do one of the following
    // 1. Call defaultWriteObject()
    // 2. Put out optional fields
    // 3. Call writeFields
    // We have the state defined for (1) and (2) but not for (3), so
    // we should ideally introduce a new state for 3 and have the
    // beginDefaultReadObject do nothing.
    // readObjectState.beginDefaultReadObject(this);

    readFields(fieldValueMap);

    readObjectState.endDefaultReadObject(this);

    return new HookGetFields(fieldValueMap);
  }

  // The following is a State pattern implementation of what
  // should be done when the sender's Serializable has a
  // writeObject method.  This was especially necessary for
  // RMI-IIOP stream format version 2.  Please see the
  // state diagrams in the docs directory of the workspace.
  //
  // On the reader's side, the main factors are whether or not
  // we have a readObject method and whether or not the
  // sender wrote default data

  protected void setState(ReadObjectState newState) {
    readObjectState = newState;
  }

  protected abstract byte getStreamFormatVersion();

  protected abstract org.omg.CORBA_2_3.portable.InputStream getOrbStream();

  // Description of possible actions
  protected static class ReadObjectState {
    public void beginUnmarshalCustomValue(
        InputStreamHook stream, boolean calledDefaultWriteObject, boolean hasReadObject)
        throws IOException {}

    public void endUnmarshalCustomValue(InputStreamHook stream) throws IOException {}

    public void beginDefaultReadObject(InputStreamHook stream) throws IOException {}

    public void endDefaultReadObject(InputStreamHook stream) throws IOException {}

    public void readData(InputStreamHook stream) throws IOException {}
  }

  protected ReadObjectState readObjectState = DEFAULT_STATE;

  protected static final ReadObjectState DEFAULT_STATE = new DefaultState();
  protected static final ReadObjectState IN_READ_OBJECT_OPT_DATA =
      new InReadObjectOptionalDataState();
  protected static final ReadObjectState IN_READ_OBJECT_NO_MORE_OPT_DATA =
      new InReadObjectNoMoreOptionalDataState();
  protected static final ReadObjectState IN_READ_OBJECT_DEFAULTS_SENT =
      new InReadObjectDefaultsSentState();
  protected static final ReadObjectState NO_READ_OBJECT_DEFAULTS_SENT =
      new NoReadObjectDefaultsSentState();

  protected static final ReadObjectState IN_READ_OBJECT_REMOTE_NOT_CUSTOM_MARSHALED =
      new InReadObjectRemoteDidNotUseWriteObjectState();
  protected static final ReadObjectState IN_READ_OBJECT_PAST_DEFAULTS_REMOTE_NOT_CUSTOM =
      new InReadObjectPastDefaultsRemoteDidNotUseWOState();

  protected static class DefaultState extends ReadObjectState {

    public void beginUnmarshalCustomValue(
        InputStreamHook stream, boolean calledDefaultWriteObject, boolean hasReadObject)
        throws IOException {

      if (hasReadObject) {
        if (calledDefaultWriteObject) stream.setState(IN_READ_OBJECT_DEFAULTS_SENT);
        else {
          try {
            if (stream.getStreamFormatVersion() == 2)
              ((ValueInputStream) stream.getOrbStream()).start_value();
          } catch (Exception e) {
            // This will happen for Big Integer which uses
            // writeFields in it's writeObject. We should be past
            // start_value by now.
            // NOTE: If we don't log any exception here we should
            // be fine. If there is an error, it will be caught
            // while reading the optional data.

          }
          stream.setState(IN_READ_OBJECT_OPT_DATA);
        }
      } else {
        if (calledDefaultWriteObject) stream.setState(NO_READ_OBJECT_DEFAULTS_SENT);
        else
          // XXX I18N and logging needed.
          throw new StreamCorruptedException("No default data sent");
      }
    }
  }

  // REVISIT.  If a readObject exits here without reading
  // default data, we won't skip it.  This could be done automatically
  // as in line 1492 in IIOPInputStream.
  protected static class InReadObjectRemoteDidNotUseWriteObjectState extends ReadObjectState {

    public void beginUnmarshalCustomValue(
        InputStreamHook stream, boolean calledDefaultWriteObject, boolean hasReadObject) {
      throw utilWrapper.badBeginUnmarshalCustomValue();
    }

    public void endDefaultReadObject(InputStreamHook stream) {
      stream.setState(IN_READ_OBJECT_PAST_DEFAULTS_REMOTE_NOT_CUSTOM);
    }

    public void readData(InputStreamHook stream) {
      stream.throwOptionalDataIncompatibleException();
    }
  }

  protected static class InReadObjectPastDefaultsRemoteDidNotUseWOState extends ReadObjectState {

    public void beginUnmarshalCustomValue(
        InputStreamHook stream, boolean calledDefaultWriteObject, boolean hasReadObject) {
      throw utilWrapper.badBeginUnmarshalCustomValue();
    }

    public void beginDefaultReadObject(InputStreamHook stream) throws IOException {
      // XXX I18N and logging needed.
      throw new StreamCorruptedException("Default data already read");
    }

    public void readData(InputStreamHook stream) {
      stream.throwOptionalDataIncompatibleException();
    }
  }

  protected void throwOptionalDataIncompatibleException() {
    throw omgWrapper.rmiiiopOptionalDataIncompatible2();
  }

  protected static class InReadObjectDefaultsSentState extends ReadObjectState {

    public void beginUnmarshalCustomValue(
        InputStreamHook stream, boolean calledDefaultWriteObject, boolean hasReadObject) {
      // This should never happen.
      throw utilWrapper.badBeginUnmarshalCustomValue();
    }

    public void endUnmarshalCustomValue(InputStreamHook stream) {

      // In stream format version 2, we can skip over
      // the optional data this way.  In stream format version 1,
      // we will probably wind up with an error if we're
      // unmarshaling a superclass.
      if (stream.getStreamFormatVersion() == 2) {
        ((ValueInputStream) stream.getOrbStream()).start_value();
        ((ValueInputStream) stream.getOrbStream()).end_value();
      }

      stream.setState(DEFAULT_STATE);
    }

    public void endDefaultReadObject(InputStreamHook stream) throws IOException {

      // Read the fake valuetype header in stream format version 2
      if (stream.getStreamFormatVersion() == 2)
        ((ValueInputStream) stream.getOrbStream()).start_value();

      stream.setState(IN_READ_OBJECT_OPT_DATA);
    }

    public void readData(InputStreamHook stream) throws IOException {
      org.omg.CORBA.ORB orb = stream.getOrbStream().orb();
      if ((orb == null) || !(orb instanceof com.sun.corba.se.spi.orb.ORB)) {
        throw new StreamCorruptedException("Default data must be read first");
      }
      ORBVersion clientOrbVersion = ((com.sun.corba.se.spi.orb.ORB) orb).getORBVersion();

      // Fix Date interop bug. For older versions of the ORB don't do
      // anything for readData(). Before this used to throw
      // StreamCorruptedException for older versions of the ORB where
      // calledDefaultWriteObject always returns true.
      if ((ORBVersionFactory.getPEORB().compareTo(clientOrbVersion) <= 0)
          || (clientOrbVersion.equals(ORBVersionFactory.getFOREIGN()))) {
        // XXX I18N and logging needed.
        throw new StreamCorruptedException("Default data must be read first");
      }
    }
  }

  protected static class InReadObjectOptionalDataState extends ReadObjectState {

    public void beginUnmarshalCustomValue(
        InputStreamHook stream, boolean calledDefaultWriteObject, boolean hasReadObject) {
      // This should never happen.
      throw utilWrapper.badBeginUnmarshalCustomValue();
    }

    public void endUnmarshalCustomValue(InputStreamHook stream) throws IOException {
      if (stream.getStreamFormatVersion() == 2) {
        ((ValueInputStream) stream.getOrbStream()).end_value();
      }
      stream.setState(DEFAULT_STATE);
    }

    public void beginDefaultReadObject(InputStreamHook stream) throws IOException {
      // XXX I18N and logging needed.
      throw new StreamCorruptedException("Default data not sent or already read/passed");
    }
  }

  protected static class InReadObjectNoMoreOptionalDataState extends InReadObjectOptionalDataState {

    public void readData(InputStreamHook stream) throws IOException {
      stream.throwOptionalDataIncompatibleException();
    }
  }

  protected static class NoReadObjectDefaultsSentState extends ReadObjectState {
    public void endUnmarshalCustomValue(InputStreamHook stream) throws IOException {
      // Code should read default fields before calling this

      if (stream.getStreamFormatVersion() == 2) {
        ((ValueInputStream) stream.getOrbStream()).start_value();
        ((ValueInputStream) stream.getOrbStream()).end_value();
      }

      stream.setState(DEFAULT_STATE);
    }
  }
}
Exemplo n.º 2
0
  /**
   * 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);
  }
Exemplo n.º 3
0
 public void beginUnmarshalCustomValue(
     InputStreamHook stream, boolean calledDefaultWriteObject, boolean hasReadObject) {
   // This should never happen.
   throw utilWrapper.badBeginUnmarshalCustomValue();
 }
Exemplo n.º 4
0
/** 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.
   *
   * <p>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.
   *
   * <p>Returns null if it wasn't possible to create the TypeCode (means it wasn't our ORB or Any
   * implementation).
   *
   * <p>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.
   *
   * <p>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</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) {
          // ignore
        } 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()) {
          keepAlive.quit();
          keepAlive = null;
        }
      } else {
        throw new java.rmi.NoSuchObjectException("Tie not found");
      }
    }
  }

  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, ClassLoader loader)
      throws ClassNotFoundException {
    return JDKBridge.loadClass(className, remoteCodebase, loader);
  }

  /**
   * The <tt>isLocal</tt> method has the same semantics as the ObjectImpl._is_local method, except
   * that it can throw a RemoteException. (no it doesn't but the spec says it should.)
   *
   * <p>The <tt>_is_local()</tt> method is provided so that stubs may determine if a particular
   * object is implemented by a local servant and hence local invocation APIs may be used.
   *
   * @param stub the stub to test.
   * @return The <tt>_is_local()</tt> method returns true if the servant incarnating the object is
   *     located in the same process as the stub and they both share the same ORB instance. The
   *     <tt>_is_local()</tt> method returns false otherwise. The default behavior of
   *     <tt>_is_local()</tt> is to return false.
   * @throws RemoteException The Java to IDL specification does to specify the conditions that cause
   *     a RemoteException to be thrown.
   */
  public boolean isLocal(javax.rmi.CORBA.Stub stub) throws RemoteException {
    boolean result = false;

    try {
      org.omg.CORBA.portable.Delegate delegate = stub._get_delegate();
      if (delegate instanceof CorbaClientDelegate) {
        // For the Sun ORB
        CorbaClientDelegate cdel = (CorbaClientDelegate) delegate;
        ContactInfoList cil = cdel.getContactInfoList();
        if (cil instanceof CorbaContactInfoList) {
          CorbaContactInfoList ccil = (CorbaContactInfoList) cil;
          LocalClientRequestDispatcher lcs = ccil.getLocalClientRequestDispatcher();
          result = lcs.useLocalInvocation(null);
        }
      } else {
        // For a non-Sun ORB
        result = delegate.is_local(stub);
      }
    } catch (SystemException e) {
      throw javax.rmi.CORBA.Util.mapSystemException(e);
    }

    return result;
  }

  /**
   * Wraps an exception thrown by an implementation method. It returns the corresponding client-side
   * exception.
   *
   * @param orig the exception to wrap.
   * @return the wrapped exception.
   */
  public RemoteException wrapException(Throwable orig) {
    if (orig instanceof SystemException) {
      return mapSystemException((SystemException) orig);
    }

    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;
    }

    if (orig instanceof Exception)
      return new UnexpectedException(orig.toString(), (Exception) orig);
    else return new UnexpectedException(orig.toString());
  }

  /**
   * Copies or connects an array of objects. Used by local stubs to copy any number of actual
   * parameters, preserving sharing across parameters as necessary to support RMI semantics.
   *
   * @param obj the objects to copy or connect.
   * @param orb the ORB.
   * @return the copied or connected objects.
   * @exception RemoteException if any object could not be copied or connected.
   */
  public Object[] copyObjects(Object[] obj, org.omg.CORBA.ORB orb) throws RemoteException {
    if (obj == null)
      // Bug fix for 5018613: JCK test expects copyObjects to throw
      // NPE when obj==null.  This is actually not in the spec, since
      // obj is not really an RMI-IDL data type, but we follow our
      // test here, and force this error to be thrown.
      throw new NullPointerException();

    Class compType = obj.getClass().getComponentType();
    if (Remote.class.isAssignableFrom(compType) && !compType.isInterface()) {
      // obj is an array of remote impl types.  This
      // causes problems with stream copier, so we copy
      // it over to an array of Remotes instead.
      Remote[] result = new Remote[obj.length];
      System.arraycopy((Object) obj, 0, (Object) result, 0, obj.length);
      return (Object[]) copyObject(result, orb);
    } else return (Object[]) copyObject(obj, orb);
  }

  /**
   * Copies or connects an object. Used by local stubs to copy an actual parameter, result object,
   * or exception.
   *
   * @param obj the object to copy.
   * @param orb the ORB.
   * @return the copy or connected object.
   * @exception RemoteException if the object could not be copied or connected.
   */
  public Object copyObject(Object obj, org.omg.CORBA.ORB orb) throws RemoteException {
    if (orb instanceof ORB) {
      ORB lorb = (ORB) orb;

      try {
        try {
          // This gets the copier for the current invocation, which was
          // previously set by preinvoke.
          return lorb.peekInvocationInfo().getCopierFactory().make().copy(obj);
        } catch (java.util.EmptyStackException exc) {
          // copyObject was invoked outside of an invocation, probably by
          // a test.  Get the default copier from the ORB.
          // XXX should we just make the default copier available directly
          // and avoid constructing one on each call?
          CopierManager cm = lorb.getCopierManager();
          ObjectCopier copier = cm.getDefaultObjectCopierFactory().make();
          return copier.copy(obj);
        }
      } catch (ReflectiveCopyException exc) {
        RemoteException rexc = new RemoteException();
        rexc.initCause(exc);
        throw rexc;
      }
    } else {
      org.omg.CORBA_2_3.portable.OutputStream out =
          (org.omg.CORBA_2_3.portable.OutputStream) orb.create_output_stream();
      out.write_value((Serializable) obj);
      org.omg.CORBA_2_3.portable.InputStream in =
          (org.omg.CORBA_2_3.portable.InputStream) out.create_input_stream();
      return in.read_value();
    }
  }
}