duke@1: /* ohair@158: * Copyright (c) 1998, 2004, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as ohair@158: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@158: * by Oracle in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@158: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@158: * or visit www.oracle.com if you need additional information or have any ohair@158: * questions. duke@1: */ duke@1: duke@1: /* duke@1: * Licensed Materials - Property of IBM duke@1: * RMI-IIOP v1.0 duke@1: * Copyright IBM Corp. 1998 1999 All Rights Reserved duke@1: * duke@1: */ duke@1: duke@1: package javax.rmi.CORBA; duke@1: duke@1: import org.omg.CORBA.ORB; duke@1: import org.omg.CORBA.INITIALIZE; duke@1: import org.omg.CORBA_2_3.portable.ObjectImpl; duke@1: duke@1: import java.io.IOException; duke@1: import java.rmi.RemoteException; duke@1: import java.io.File; duke@1: import java.io.FileInputStream; duke@1: import java.net.MalformedURLException ; duke@1: import java.security.AccessController; duke@1: import java.security.PrivilegedAction; duke@1: import java.util.Properties; duke@1: import java.rmi.server.RMIClassLoader; duke@1: duke@1: import com.sun.corba.se.impl.orbutil.GetPropertyAction; duke@1: duke@1: duke@1: /** duke@1: * Base class from which all RMI-IIOP stubs must inherit. duke@1: */ duke@1: public abstract class Stub extends ObjectImpl duke@1: implements java.io.Serializable { duke@1: duke@1: private static final long serialVersionUID = 1087775603798577179L; duke@1: duke@1: // This can only be set at object construction time (no sync necessary). duke@1: private transient StubDelegate stubDelegate = null; duke@1: private static Class stubDelegateClass = null; duke@1: private static final String StubClassKey = "javax.rmi.CORBA.StubClass"; duke@1: duke@1: static { alanb@533: Object stubDelegateInstance = createDelegate(StubClassKey); duke@1: if (stubDelegateInstance != null) duke@1: stubDelegateClass = stubDelegateInstance.getClass(); duke@1: } duke@1: duke@1: duke@1: /** duke@1: * Returns a hash code value for the object which is the same for all stubs duke@1: * that represent the same remote object. duke@1: * @return the hash code value. duke@1: */ duke@1: public int hashCode() { duke@1: duke@1: if (stubDelegate == null) { duke@1: setDefaultDelegate(); duke@1: } duke@1: duke@1: if (stubDelegate != null) { duke@1: return stubDelegate.hashCode(this); duke@1: } duke@1: duke@1: return 0; duke@1: } duke@1: duke@1: /** duke@1: * Compares two stubs for equality. Returns true when used to compare stubs duke@1: * that represent the same remote object, and false otherwise. duke@1: * @param obj the reference object with which to compare. duke@1: * @return true if this object is the same as the obj duke@1: * argument; false otherwise. duke@1: */ duke@1: public boolean equals(java.lang.Object obj) { duke@1: duke@1: if (stubDelegate == null) { duke@1: setDefaultDelegate(); duke@1: } duke@1: duke@1: if (stubDelegate != null) { duke@1: return stubDelegate.equals(this, obj); duke@1: } duke@1: duke@1: return false; duke@1: } duke@1: duke@1: /** duke@1: * Returns a string representation of this stub. Returns the same string duke@1: * for all stubs that represent the same remote object. duke@1: * @return a string representation of this stub. duke@1: */ duke@1: public String toString() { duke@1: duke@1: duke@1: if (stubDelegate == null) { duke@1: setDefaultDelegate(); duke@1: } duke@1: duke@1: String ior; duke@1: if (stubDelegate != null) { duke@1: ior = stubDelegate.toString(this); duke@1: if (ior == null) { duke@1: return super.toString(); duke@1: } else { duke@1: return ior; duke@1: } duke@1: } duke@1: return super.toString(); duke@1: } duke@1: duke@1: /** duke@1: * Connects this stub to an ORB. Required after the stub is deserialized duke@1: * but not after it is demarshalled by an ORB stream. If an unconnected duke@1: * stub is passed to an ORB stream for marshalling, it is implicitly duke@1: * connected to that ORB. Application code should not call this method duke@1: * directly, but should call the portable wrapper method duke@1: * {@link javax.rmi.PortableRemoteObject#connect}. duke@1: * @param orb the ORB to connect to. duke@1: * @exception RemoteException if the stub is already connected to a different duke@1: * ORB, or if the stub does not represent an exported remote or local object. duke@1: */ duke@1: public void connect(ORB orb) throws RemoteException { duke@1: duke@1: if (stubDelegate == null) { duke@1: setDefaultDelegate(); duke@1: } duke@1: duke@1: if (stubDelegate != null) { duke@1: stubDelegate.connect(this, orb); duke@1: } duke@1: duke@1: } duke@1: duke@1: /** duke@1: * Serialization method to restore the IOR state. duke@1: */ duke@1: private void readObject(java.io.ObjectInputStream stream) duke@1: throws IOException, ClassNotFoundException { duke@1: duke@1: if (stubDelegate == null) { duke@1: setDefaultDelegate(); duke@1: } duke@1: duke@1: if (stubDelegate != null) { duke@1: stubDelegate.readObject(this, stream); duke@1: } duke@1: duke@1: } duke@1: duke@1: /** duke@1: * Serialization method to save the IOR state. duke@1: * @serialData The length of the IOR type ID (int), followed by the IOR type ID duke@1: * (byte array encoded using ISO8859-1), followed by the number of IOR profiles duke@1: * (int), followed by the IOR profiles. Each IOR profile is written as a duke@1: * profile tag (int), followed by the length of the profile data (int), followed duke@1: * by the profile data (byte array). duke@1: */ duke@1: private void writeObject(java.io.ObjectOutputStream stream) throws IOException { duke@1: duke@1: if (stubDelegate == null) { duke@1: setDefaultDelegate(); duke@1: } duke@1: duke@1: if (stubDelegate != null) { duke@1: stubDelegate.writeObject(this, stream); duke@1: } duke@1: } duke@1: duke@1: private void setDefaultDelegate() { duke@1: if (stubDelegateClass != null) { duke@1: try { duke@1: stubDelegate = (javax.rmi.CORBA.StubDelegate) stubDelegateClass.newInstance(); duke@1: } catch (Exception ex) { duke@1: // what kind of exception to throw duke@1: // delegate not set therefore it is null and will return default duke@1: // values duke@1: } duke@1: } duke@1: } duke@1: duke@1: // Same code as in PortableRemoteObject. Can not be shared because they duke@1: // are in different packages and the visibility needs to be package for duke@1: // security reasons. If you know a better solution how to share this code duke@1: // then remove it from PortableRemoteObject. Also in Util.java alanb@533: private static Object createDelegate(String classKey) { duke@1: String className = (String) duke@1: AccessController.doPrivileged(new GetPropertyAction(classKey)); duke@1: if (className == null) { duke@1: Properties props = getORBPropertiesFile(); duke@1: if (props != null) { duke@1: className = props.getProperty(classKey); duke@1: } duke@1: } duke@1: duke@1: if (className == null) { alanb@533: return new com.sun.corba.se.impl.javax.rmi.CORBA.StubDelegateImpl(); duke@1: } duke@1: duke@1: try { duke@1: return loadDelegateClass(className).newInstance(); duke@1: } catch (ClassNotFoundException ex) { duke@1: INITIALIZE exc = new INITIALIZE( "Cannot instantiate " + className); duke@1: exc.initCause( ex ) ; duke@1: throw exc ; duke@1: } catch (Exception ex) { duke@1: INITIALIZE exc = new INITIALIZE( "Error while instantiating" + className); duke@1: exc.initCause( ex ) ; duke@1: throw exc ; duke@1: } duke@1: duke@1: } duke@1: duke@1: private static Class loadDelegateClass( String className ) throws ClassNotFoundException duke@1: { duke@1: try { duke@1: ClassLoader loader = Thread.currentThread().getContextClassLoader(); duke@1: return Class.forName(className, false, loader); duke@1: } catch (ClassNotFoundException e) { duke@1: // ignore, then try RMIClassLoader duke@1: } duke@1: duke@1: try { duke@1: return RMIClassLoader.loadClass(className); duke@1: } catch (MalformedURLException e) { duke@1: String msg = "Could not load " + className + ": " + e.toString(); duke@1: ClassNotFoundException exc = new ClassNotFoundException( msg ) ; duke@1: throw exc ; duke@1: } duke@1: } duke@1: duke@1: /** duke@1: * Load the orb.properties file. duke@1: */ duke@1: private static Properties getORBPropertiesFile () { duke@1: return (Properties) AccessController.doPrivileged(new GetORBPropertiesFileAction()); duke@1: } duke@1: duke@1: }