Tue, 22 Oct 2013 11:40:27 +0100
8021257: com.sun.corba.se.** should be on restricted package list
Reviewed-by: chegar, coffeys, smarks
Contributed-by: alan.bateman@oralce.com, mark.sheppard@oracle.com
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 | |
duke@1 | 65 | static { |
alanb@533 | 66 | Object stubDelegateInstance = createDelegate(StubClassKey); |
duke@1 | 67 | if (stubDelegateInstance != null) |
duke@1 | 68 | stubDelegateClass = stubDelegateInstance.getClass(); |
duke@1 | 69 | } |
duke@1 | 70 | |
duke@1 | 71 | |
duke@1 | 72 | /** |
duke@1 | 73 | * Returns a hash code value for the object which is the same for all stubs |
duke@1 | 74 | * that represent the same remote object. |
duke@1 | 75 | * @return the hash code value. |
duke@1 | 76 | */ |
duke@1 | 77 | public int hashCode() { |
duke@1 | 78 | |
duke@1 | 79 | if (stubDelegate == null) { |
duke@1 | 80 | setDefaultDelegate(); |
duke@1 | 81 | } |
duke@1 | 82 | |
duke@1 | 83 | if (stubDelegate != null) { |
duke@1 | 84 | return stubDelegate.hashCode(this); |
duke@1 | 85 | } |
duke@1 | 86 | |
duke@1 | 87 | return 0; |
duke@1 | 88 | } |
duke@1 | 89 | |
duke@1 | 90 | /** |
duke@1 | 91 | * Compares two stubs for equality. Returns <code>true</code> when used to compare stubs |
duke@1 | 92 | * that represent the same remote object, and <code>false</code> otherwise. |
duke@1 | 93 | * @param obj the reference object with which to compare. |
duke@1 | 94 | * @return <code>true</code> if this object is the same as the <code>obj</code> |
duke@1 | 95 | * argument; <code>false</code> otherwise. |
duke@1 | 96 | */ |
duke@1 | 97 | public boolean equals(java.lang.Object obj) { |
duke@1 | 98 | |
duke@1 | 99 | if (stubDelegate == null) { |
duke@1 | 100 | setDefaultDelegate(); |
duke@1 | 101 | } |
duke@1 | 102 | |
duke@1 | 103 | if (stubDelegate != null) { |
duke@1 | 104 | return stubDelegate.equals(this, obj); |
duke@1 | 105 | } |
duke@1 | 106 | |
duke@1 | 107 | return false; |
duke@1 | 108 | } |
duke@1 | 109 | |
duke@1 | 110 | /** |
duke@1 | 111 | * Returns a string representation of this stub. Returns the same string |
duke@1 | 112 | * for all stubs that represent the same remote object. |
duke@1 | 113 | * @return a string representation of this stub. |
duke@1 | 114 | */ |
duke@1 | 115 | public String toString() { |
duke@1 | 116 | |
duke@1 | 117 | |
duke@1 | 118 | if (stubDelegate == null) { |
duke@1 | 119 | setDefaultDelegate(); |
duke@1 | 120 | } |
duke@1 | 121 | |
duke@1 | 122 | String ior; |
duke@1 | 123 | if (stubDelegate != null) { |
duke@1 | 124 | ior = stubDelegate.toString(this); |
duke@1 | 125 | if (ior == null) { |
duke@1 | 126 | return super.toString(); |
duke@1 | 127 | } else { |
duke@1 | 128 | return ior; |
duke@1 | 129 | } |
duke@1 | 130 | } |
duke@1 | 131 | return super.toString(); |
duke@1 | 132 | } |
duke@1 | 133 | |
duke@1 | 134 | /** |
duke@1 | 135 | * Connects this stub to an ORB. Required after the stub is deserialized |
duke@1 | 136 | * but not after it is demarshalled by an ORB stream. If an unconnected |
duke@1 | 137 | * stub is passed to an ORB stream for marshalling, it is implicitly |
duke@1 | 138 | * connected to that ORB. Application code should not call this method |
duke@1 | 139 | * directly, but should call the portable wrapper method |
duke@1 | 140 | * {@link javax.rmi.PortableRemoteObject#connect}. |
duke@1 | 141 | * @param orb the ORB to connect to. |
duke@1 | 142 | * @exception RemoteException if the stub is already connected to a different |
duke@1 | 143 | * ORB, or if the stub does not represent an exported remote or local object. |
duke@1 | 144 | */ |
duke@1 | 145 | public void connect(ORB orb) throws RemoteException { |
duke@1 | 146 | |
duke@1 | 147 | if (stubDelegate == null) { |
duke@1 | 148 | setDefaultDelegate(); |
duke@1 | 149 | } |
duke@1 | 150 | |
duke@1 | 151 | if (stubDelegate != null) { |
duke@1 | 152 | stubDelegate.connect(this, orb); |
duke@1 | 153 | } |
duke@1 | 154 | |
duke@1 | 155 | } |
duke@1 | 156 | |
duke@1 | 157 | /** |
duke@1 | 158 | * Serialization method to restore the IOR state. |
duke@1 | 159 | */ |
duke@1 | 160 | private void readObject(java.io.ObjectInputStream stream) |
duke@1 | 161 | throws IOException, ClassNotFoundException { |
duke@1 | 162 | |
duke@1 | 163 | if (stubDelegate == null) { |
duke@1 | 164 | setDefaultDelegate(); |
duke@1 | 165 | } |
duke@1 | 166 | |
duke@1 | 167 | if (stubDelegate != null) { |
duke@1 | 168 | stubDelegate.readObject(this, stream); |
duke@1 | 169 | } |
duke@1 | 170 | |
duke@1 | 171 | } |
duke@1 | 172 | |
duke@1 | 173 | /** |
duke@1 | 174 | * Serialization method to save the IOR state. |
duke@1 | 175 | * @serialData The length of the IOR type ID (int), followed by the IOR type ID |
duke@1 | 176 | * (byte array encoded using ISO8859-1), followed by the number of IOR profiles |
duke@1 | 177 | * (int), followed by the IOR profiles. Each IOR profile is written as a |
duke@1 | 178 | * profile tag (int), followed by the length of the profile data (int), followed |
duke@1 | 179 | * by the profile data (byte array). |
duke@1 | 180 | */ |
duke@1 | 181 | private void writeObject(java.io.ObjectOutputStream stream) throws IOException { |
duke@1 | 182 | |
duke@1 | 183 | if (stubDelegate == null) { |
duke@1 | 184 | setDefaultDelegate(); |
duke@1 | 185 | } |
duke@1 | 186 | |
duke@1 | 187 | if (stubDelegate != null) { |
duke@1 | 188 | stubDelegate.writeObject(this, stream); |
duke@1 | 189 | } |
duke@1 | 190 | } |
duke@1 | 191 | |
duke@1 | 192 | private void setDefaultDelegate() { |
duke@1 | 193 | if (stubDelegateClass != null) { |
duke@1 | 194 | try { |
duke@1 | 195 | stubDelegate = (javax.rmi.CORBA.StubDelegate) stubDelegateClass.newInstance(); |
duke@1 | 196 | } catch (Exception ex) { |
duke@1 | 197 | // what kind of exception to throw |
duke@1 | 198 | // delegate not set therefore it is null and will return default |
duke@1 | 199 | // values |
duke@1 | 200 | } |
duke@1 | 201 | } |
duke@1 | 202 | } |
duke@1 | 203 | |
duke@1 | 204 | // Same code as in PortableRemoteObject. Can not be shared because they |
duke@1 | 205 | // are in different packages and the visibility needs to be package for |
duke@1 | 206 | // security reasons. If you know a better solution how to share this code |
duke@1 | 207 | // then remove it from PortableRemoteObject. Also in Util.java |
alanb@533 | 208 | private static Object createDelegate(String classKey) { |
duke@1 | 209 | String className = (String) |
duke@1 | 210 | AccessController.doPrivileged(new GetPropertyAction(classKey)); |
duke@1 | 211 | if (className == null) { |
duke@1 | 212 | Properties props = getORBPropertiesFile(); |
duke@1 | 213 | if (props != null) { |
duke@1 | 214 | className = props.getProperty(classKey); |
duke@1 | 215 | } |
duke@1 | 216 | } |
duke@1 | 217 | |
duke@1 | 218 | if (className == null) { |
alanb@533 | 219 | return new com.sun.corba.se.impl.javax.rmi.CORBA.StubDelegateImpl(); |
duke@1 | 220 | } |
duke@1 | 221 | |
duke@1 | 222 | try { |
duke@1 | 223 | return loadDelegateClass(className).newInstance(); |
duke@1 | 224 | } catch (ClassNotFoundException ex) { |
duke@1 | 225 | INITIALIZE exc = new INITIALIZE( "Cannot instantiate " + className); |
duke@1 | 226 | exc.initCause( ex ) ; |
duke@1 | 227 | throw exc ; |
duke@1 | 228 | } catch (Exception ex) { |
duke@1 | 229 | INITIALIZE exc = new INITIALIZE( "Error while instantiating" + className); |
duke@1 | 230 | exc.initCause( ex ) ; |
duke@1 | 231 | throw exc ; |
duke@1 | 232 | } |
duke@1 | 233 | |
duke@1 | 234 | } |
duke@1 | 235 | |
duke@1 | 236 | private static Class loadDelegateClass( String className ) throws ClassNotFoundException |
duke@1 | 237 | { |
duke@1 | 238 | try { |
duke@1 | 239 | ClassLoader loader = Thread.currentThread().getContextClassLoader(); |
duke@1 | 240 | return Class.forName(className, false, loader); |
duke@1 | 241 | } catch (ClassNotFoundException e) { |
duke@1 | 242 | // ignore, then try RMIClassLoader |
duke@1 | 243 | } |
duke@1 | 244 | |
duke@1 | 245 | try { |
duke@1 | 246 | return RMIClassLoader.loadClass(className); |
duke@1 | 247 | } catch (MalformedURLException e) { |
duke@1 | 248 | String msg = "Could not load " + className + ": " + e.toString(); |
duke@1 | 249 | ClassNotFoundException exc = new ClassNotFoundException( msg ) ; |
duke@1 | 250 | throw exc ; |
duke@1 | 251 | } |
duke@1 | 252 | } |
duke@1 | 253 | |
duke@1 | 254 | /** |
duke@1 | 255 | * Load the orb.properties file. |
duke@1 | 256 | */ |
duke@1 | 257 | private static Properties getORBPropertiesFile () { |
duke@1 | 258 | return (Properties) AccessController.doPrivileged(new GetORBPropertiesFileAction()); |
duke@1 | 259 | } |
duke@1 | 260 | |
duke@1 | 261 | } |