src/share/classes/javax/rmi/CORBA/Stub.java

Thu, 17 Jun 2010 16:27:56 -0700

author
mikejwre
date
Thu, 17 Jun 2010 16:27:56 -0700
changeset 171
95db968660e7
parent 158
91006f157c46
child 533
52ad44f9a3ec
permissions
-rw-r--r--

Added tag jdk7-b98 for changeset 3b99409057e4

duke@1 1 /*
ohair@158 2 * Copyright (c) 1998, 2004, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
ohair@158 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@158 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
ohair@158 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@158 22 * or visit www.oracle.com if you need additional information or have any
ohair@158 23 * questions.
duke@1 24 */
duke@1 25
duke@1 26 /*
duke@1 27 * Licensed Materials - Property of IBM
duke@1 28 * RMI-IIOP v1.0
duke@1 29 * Copyright IBM Corp. 1998 1999 All Rights Reserved
duke@1 30 *
duke@1 31 */
duke@1 32
duke@1 33 package javax.rmi.CORBA;
duke@1 34
duke@1 35 import org.omg.CORBA.ORB;
duke@1 36 import org.omg.CORBA.INITIALIZE;
duke@1 37 import org.omg.CORBA_2_3.portable.ObjectImpl;
duke@1 38
duke@1 39 import java.io.IOException;
duke@1 40 import java.rmi.RemoteException;
duke@1 41 import java.io.File;
duke@1 42 import java.io.FileInputStream;
duke@1 43 import java.net.MalformedURLException ;
duke@1 44 import java.security.AccessController;
duke@1 45 import java.security.PrivilegedAction;
duke@1 46 import java.util.Properties;
duke@1 47 import java.rmi.server.RMIClassLoader;
duke@1 48
duke@1 49 import com.sun.corba.se.impl.orbutil.GetPropertyAction;
duke@1 50
duke@1 51
duke@1 52 /**
duke@1 53 * Base class from which all RMI-IIOP stubs must inherit.
duke@1 54 */
duke@1 55 public abstract class Stub extends ObjectImpl
duke@1 56 implements java.io.Serializable {
duke@1 57
duke@1 58 private static final long serialVersionUID = 1087775603798577179L;
duke@1 59
duke@1 60 // This can only be set at object construction time (no sync necessary).
duke@1 61 private transient StubDelegate stubDelegate = null;
duke@1 62 private static Class stubDelegateClass = null;
duke@1 63 private static final String StubClassKey = "javax.rmi.CORBA.StubClass";
duke@1 64 private static final String defaultStubImplName = "com.sun.corba.se.impl.javax.rmi.CORBA.StubDelegateImpl";
duke@1 65
duke@1 66 static {
duke@1 67 Object stubDelegateInstance = (Object) createDelegateIfSpecified(StubClassKey, defaultStubImplName);
duke@1 68 if (stubDelegateInstance != null)
duke@1 69 stubDelegateClass = stubDelegateInstance.getClass();
duke@1 70
duke@1 71 }
duke@1 72
duke@1 73
duke@1 74 /**
duke@1 75 * Returns a hash code value for the object which is the same for all stubs
duke@1 76 * that represent the same remote object.
duke@1 77 * @return the hash code value.
duke@1 78 */
duke@1 79 public int hashCode() {
duke@1 80
duke@1 81 if (stubDelegate == null) {
duke@1 82 setDefaultDelegate();
duke@1 83 }
duke@1 84
duke@1 85 if (stubDelegate != null) {
duke@1 86 return stubDelegate.hashCode(this);
duke@1 87 }
duke@1 88
duke@1 89 return 0;
duke@1 90 }
duke@1 91
duke@1 92 /**
duke@1 93 * Compares two stubs for equality. Returns <code>true</code> when used to compare stubs
duke@1 94 * that represent the same remote object, and <code>false</code> otherwise.
duke@1 95 * @param obj the reference object with which to compare.
duke@1 96 * @return <code>true</code> if this object is the same as the <code>obj</code>
duke@1 97 * argument; <code>false</code> otherwise.
duke@1 98 */
duke@1 99 public boolean equals(java.lang.Object obj) {
duke@1 100
duke@1 101 if (stubDelegate == null) {
duke@1 102 setDefaultDelegate();
duke@1 103 }
duke@1 104
duke@1 105 if (stubDelegate != null) {
duke@1 106 return stubDelegate.equals(this, obj);
duke@1 107 }
duke@1 108
duke@1 109 return false;
duke@1 110 }
duke@1 111
duke@1 112 /**
duke@1 113 * Returns a string representation of this stub. Returns the same string
duke@1 114 * for all stubs that represent the same remote object.
duke@1 115 * @return a string representation of this stub.
duke@1 116 */
duke@1 117 public String toString() {
duke@1 118
duke@1 119
duke@1 120 if (stubDelegate == null) {
duke@1 121 setDefaultDelegate();
duke@1 122 }
duke@1 123
duke@1 124 String ior;
duke@1 125 if (stubDelegate != null) {
duke@1 126 ior = stubDelegate.toString(this);
duke@1 127 if (ior == null) {
duke@1 128 return super.toString();
duke@1 129 } else {
duke@1 130 return ior;
duke@1 131 }
duke@1 132 }
duke@1 133 return super.toString();
duke@1 134 }
duke@1 135
duke@1 136 /**
duke@1 137 * Connects this stub to an ORB. Required after the stub is deserialized
duke@1 138 * but not after it is demarshalled by an ORB stream. If an unconnected
duke@1 139 * stub is passed to an ORB stream for marshalling, it is implicitly
duke@1 140 * connected to that ORB. Application code should not call this method
duke@1 141 * directly, but should call the portable wrapper method
duke@1 142 * {@link javax.rmi.PortableRemoteObject#connect}.
duke@1 143 * @param orb the ORB to connect to.
duke@1 144 * @exception RemoteException if the stub is already connected to a different
duke@1 145 * ORB, or if the stub does not represent an exported remote or local object.
duke@1 146 */
duke@1 147 public void connect(ORB orb) throws RemoteException {
duke@1 148
duke@1 149 if (stubDelegate == null) {
duke@1 150 setDefaultDelegate();
duke@1 151 }
duke@1 152
duke@1 153 if (stubDelegate != null) {
duke@1 154 stubDelegate.connect(this, orb);
duke@1 155 }
duke@1 156
duke@1 157 }
duke@1 158
duke@1 159 /**
duke@1 160 * Serialization method to restore the IOR state.
duke@1 161 */
duke@1 162 private void readObject(java.io.ObjectInputStream stream)
duke@1 163 throws IOException, ClassNotFoundException {
duke@1 164
duke@1 165 if (stubDelegate == null) {
duke@1 166 setDefaultDelegate();
duke@1 167 }
duke@1 168
duke@1 169 if (stubDelegate != null) {
duke@1 170 stubDelegate.readObject(this, stream);
duke@1 171 }
duke@1 172
duke@1 173 }
duke@1 174
duke@1 175 /**
duke@1 176 * Serialization method to save the IOR state.
duke@1 177 * @serialData The length of the IOR type ID (int), followed by the IOR type ID
duke@1 178 * (byte array encoded using ISO8859-1), followed by the number of IOR profiles
duke@1 179 * (int), followed by the IOR profiles. Each IOR profile is written as a
duke@1 180 * profile tag (int), followed by the length of the profile data (int), followed
duke@1 181 * by the profile data (byte array).
duke@1 182 */
duke@1 183 private void writeObject(java.io.ObjectOutputStream stream) throws IOException {
duke@1 184
duke@1 185 if (stubDelegate == null) {
duke@1 186 setDefaultDelegate();
duke@1 187 }
duke@1 188
duke@1 189 if (stubDelegate != null) {
duke@1 190 stubDelegate.writeObject(this, stream);
duke@1 191 }
duke@1 192 }
duke@1 193
duke@1 194 private void setDefaultDelegate() {
duke@1 195 if (stubDelegateClass != null) {
duke@1 196 try {
duke@1 197 stubDelegate = (javax.rmi.CORBA.StubDelegate) stubDelegateClass.newInstance();
duke@1 198 } catch (Exception ex) {
duke@1 199 // what kind of exception to throw
duke@1 200 // delegate not set therefore it is null and will return default
duke@1 201 // values
duke@1 202 }
duke@1 203 }
duke@1 204 }
duke@1 205
duke@1 206 // Same code as in PortableRemoteObject. Can not be shared because they
duke@1 207 // are in different packages and the visibility needs to be package for
duke@1 208 // security reasons. If you know a better solution how to share this code
duke@1 209 // then remove it from PortableRemoteObject. Also in Util.java
duke@1 210 private static Object createDelegateIfSpecified(String classKey, String defaultClassName) {
duke@1 211 String className = (String)
duke@1 212 AccessController.doPrivileged(new GetPropertyAction(classKey));
duke@1 213 if (className == null) {
duke@1 214 Properties props = getORBPropertiesFile();
duke@1 215 if (props != null) {
duke@1 216 className = props.getProperty(classKey);
duke@1 217 }
duke@1 218 }
duke@1 219
duke@1 220 if (className == null) {
duke@1 221 className = defaultClassName;
duke@1 222 }
duke@1 223
duke@1 224 try {
duke@1 225 return loadDelegateClass(className).newInstance();
duke@1 226 } catch (ClassNotFoundException ex) {
duke@1 227 INITIALIZE exc = new INITIALIZE( "Cannot instantiate " + className);
duke@1 228 exc.initCause( ex ) ;
duke@1 229 throw exc ;
duke@1 230 } catch (Exception ex) {
duke@1 231 INITIALIZE exc = new INITIALIZE( "Error while instantiating" + className);
duke@1 232 exc.initCause( ex ) ;
duke@1 233 throw exc ;
duke@1 234 }
duke@1 235
duke@1 236 }
duke@1 237
duke@1 238 private static Class loadDelegateClass( String className ) throws ClassNotFoundException
duke@1 239 {
duke@1 240 try {
duke@1 241 ClassLoader loader = Thread.currentThread().getContextClassLoader();
duke@1 242 return Class.forName(className, false, loader);
duke@1 243 } catch (ClassNotFoundException e) {
duke@1 244 // ignore, then try RMIClassLoader
duke@1 245 }
duke@1 246
duke@1 247 try {
duke@1 248 return RMIClassLoader.loadClass(className);
duke@1 249 } catch (MalformedURLException e) {
duke@1 250 String msg = "Could not load " + className + ": " + e.toString();
duke@1 251 ClassNotFoundException exc = new ClassNotFoundException( msg ) ;
duke@1 252 throw exc ;
duke@1 253 }
duke@1 254 }
duke@1 255
duke@1 256 /**
duke@1 257 * Load the orb.properties file.
duke@1 258 */
duke@1 259 private static Properties getORBPropertiesFile () {
duke@1 260 return (Properties) AccessController.doPrivileged(new GetORBPropertiesFileAction());
duke@1 261 }
duke@1 262
duke@1 263 }

mercurial