1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/classes/javax/rmi/PortableRemoteObject.java Wed Apr 27 01:21:28 2016 +0800 1.3 @@ -0,0 +1,293 @@ 1.4 +/* 1.5 + * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 +/* 1.29 + * Licensed Materials - Property of IBM 1.30 + * RMI-IIOP v1.0 1.31 + * Copyright IBM Corp. 1998 1999 All Rights Reserved 1.32 + * 1.33 + */ 1.34 + 1.35 +package javax.rmi; 1.36 + 1.37 +import java.lang.reflect.Method ; 1.38 + 1.39 +import org.omg.CORBA.INITIALIZE; 1.40 +import javax.rmi.CORBA.Util; 1.41 + 1.42 +import java.rmi.RemoteException; 1.43 +import java.rmi.NoSuchObjectException; 1.44 +import java.rmi.Remote; 1.45 +import java.io.File; 1.46 +import java.io.FileInputStream; 1.47 +import java.util.Properties; 1.48 +import java.net.MalformedURLException ; 1.49 +import java.security.AccessController; 1.50 +import java.security.PrivilegedAction; 1.51 +import java.rmi.server.RMIClassLoader; 1.52 + 1.53 +import com.sun.corba.se.impl.orbutil.GetPropertyAction; 1.54 + 1.55 +/** 1.56 + * Server implementation objects may either inherit from 1.57 + * javax.rmi.PortableRemoteObject or they may implement a remote interface 1.58 + * and then use the exportObject method to register themselves as a server object. 1.59 + * The toStub method takes a server implementation and returns a stub that 1.60 + * can be used to access that server object. 1.61 + * The connect method makes a Remote object ready for remote communication. 1.62 + * The unexportObject method is used to deregister a server object, allowing it to become 1.63 + * available for garbage collection. 1.64 + * The narrow method takes an object reference or abstract interface type and 1.65 + * attempts to narrow it to conform to 1.66 + * the given interface. If the operation is successful the result will be an 1.67 + * object of the specified type, otherwise an exception will be thrown. 1.68 + */ 1.69 +public class PortableRemoteObject { 1.70 + 1.71 + private static final javax.rmi.CORBA.PortableRemoteObjectDelegate proDelegate; 1.72 + 1.73 + private static final String PortableRemoteObjectClassKey = 1.74 + "javax.rmi.CORBA.PortableRemoteObjectClass"; 1.75 + 1.76 + static { 1.77 + proDelegate = (javax.rmi.CORBA.PortableRemoteObjectDelegate) 1.78 + createDelegate(PortableRemoteObjectClassKey); 1.79 + } 1.80 + 1.81 + /** 1.82 + * Initializes the object by calling <code>exportObject(this)</code>. 1.83 + * @exception RemoteException if export fails. 1.84 + */ 1.85 + protected PortableRemoteObject() throws RemoteException { 1.86 + if (proDelegate != null) { 1.87 + PortableRemoteObject.exportObject((Remote)this); 1.88 + } 1.89 + } 1.90 + 1.91 + /** 1.92 + * Makes a server object ready to receive remote calls. Note 1.93 + * that subclasses of PortableRemoteObject do not need to call this 1.94 + * method, as it is called by the constructor. 1.95 + * @param obj the server object to export. 1.96 + * @exception RemoteException if export fails. 1.97 + */ 1.98 + public static void exportObject(Remote obj) 1.99 + throws RemoteException { 1.100 + 1.101 + // Let the delegate do everything, including error handling. 1.102 + if (proDelegate != null) { 1.103 + proDelegate.exportObject(obj); 1.104 + } 1.105 + } 1.106 + 1.107 + /** 1.108 + * Returns a stub for the given server object. 1.109 + * @param obj the server object for which a stub is required. Must either be a subclass 1.110 + * of PortableRemoteObject or have been previously the target of a call to 1.111 + * {@link #exportObject}. 1.112 + * @return the most derived stub for the object. 1.113 + * @exception NoSuchObjectException if a stub cannot be located for the given server object. 1.114 + */ 1.115 + public static Remote toStub (Remote obj) 1.116 + throws NoSuchObjectException { 1.117 + 1.118 + if (proDelegate != null) { 1.119 + return proDelegate.toStub(obj); 1.120 + } 1.121 + return null; 1.122 + } 1.123 + 1.124 + /** 1.125 + * Deregisters a server object from the runtime, allowing the object to become 1.126 + * available for garbage collection. 1.127 + * @param obj the object to unexport. 1.128 + * @exception NoSuchObjectException if the remote object is not 1.129 + * currently exported. 1.130 + */ 1.131 + public static void unexportObject(Remote obj) 1.132 + throws NoSuchObjectException { 1.133 + 1.134 + if (proDelegate != null) { 1.135 + proDelegate.unexportObject(obj); 1.136 + } 1.137 + 1.138 + } 1.139 + 1.140 + /** 1.141 + * Checks to ensure that an object of a remote or abstract interface type 1.142 + * can be cast to a desired type. 1.143 + * @param narrowFrom the object to check. 1.144 + * @param narrowTo the desired type. 1.145 + * @return an object which can be cast to the desired type. 1.146 + * @throws ClassCastException if narrowFrom cannot be cast to narrowTo. 1.147 + */ 1.148 + public static java.lang.Object narrow ( java.lang.Object narrowFrom, 1.149 + java.lang.Class narrowTo) 1.150 + throws ClassCastException { 1.151 + 1.152 + if (proDelegate != null) { 1.153 + return proDelegate.narrow(narrowFrom, narrowTo); 1.154 + } 1.155 + return null; 1.156 + 1.157 + } 1.158 + 1.159 + /** 1.160 + * Makes a Remote object ready for remote communication. This normally 1.161 + * happens implicitly when the object is sent or received as an argument 1.162 + * on a remote method call, but in some circumstances it is useful to 1.163 + * perform this action by making an explicit call. See the 1.164 + * {@link javax.rmi.CORBA.Stub#connect} method for more information. 1.165 + * @param target the object to connect. 1.166 + * @param source a previously connected object. 1.167 + * @throws RemoteException if <code>source</code> is not connected 1.168 + * or if <code>target</code> is already connected to a different ORB than 1.169 + * <code>source</code>. 1.170 + */ 1.171 + public static void connect (Remote target, Remote source) 1.172 + throws RemoteException { 1.173 + 1.174 + if (proDelegate != null) { 1.175 + proDelegate.connect(target, source); 1.176 + } 1.177 + 1.178 + } 1.179 + 1.180 + // Same code as in javax.rmi.CORBA.Util. Can not be shared because they 1.181 + // are in different packages and the visibility needs to be package for 1.182 + // security reasons. If you know a better solution how to share this code 1.183 + // then remove it from here. 1.184 + private static Object createDelegate(String classKey) { 1.185 + String className = (String) 1.186 + AccessController.doPrivileged(new GetPropertyAction(classKey)); 1.187 + if (className == null) { 1.188 + Properties props = getORBPropertiesFile(); 1.189 + if (props != null) { 1.190 + className = props.getProperty(classKey); 1.191 + } 1.192 + } 1.193 + if (className == null) { 1.194 + return new com.sun.corba.se.impl.javax.rmi.PortableRemoteObject(); 1.195 + } 1.196 + 1.197 + try { 1.198 + return (Object) loadDelegateClass(className).newInstance(); 1.199 + } catch (ClassNotFoundException ex) { 1.200 + INITIALIZE exc = new INITIALIZE( "Cannot instantiate " + className); 1.201 + exc.initCause( ex ) ; 1.202 + throw exc ; 1.203 + } catch (Exception ex) { 1.204 + INITIALIZE exc = new INITIALIZE( "Error while instantiating" + className); 1.205 + exc.initCause( ex ) ; 1.206 + throw exc ; 1.207 + } 1.208 + 1.209 + } 1.210 + 1.211 + private static Class loadDelegateClass( String className ) throws ClassNotFoundException 1.212 + { 1.213 + try { 1.214 + ClassLoader loader = Thread.currentThread().getContextClassLoader(); 1.215 + return Class.forName(className, false, loader); 1.216 + } catch (ClassNotFoundException e) { 1.217 + // ignore, then try RMIClassLoader 1.218 + } 1.219 + 1.220 + try { 1.221 + return RMIClassLoader.loadClass(className); 1.222 + } catch (MalformedURLException e) { 1.223 + String msg = "Could not load " + className + ": " + e.toString(); 1.224 + ClassNotFoundException exc = new ClassNotFoundException( msg ) ; 1.225 + throw exc ; 1.226 + } 1.227 + } 1.228 + 1.229 + /** 1.230 + * Load the orb.properties file. 1.231 + */ 1.232 + private static Properties getORBPropertiesFile () { 1.233 + return (Properties) AccessController.doPrivileged(new GetORBPropertiesFileAction()); 1.234 + } 1.235 +} 1.236 + 1.237 +class GetORBPropertiesFileAction implements PrivilegedAction { 1.238 + private boolean debug = false ; 1.239 + 1.240 + public GetORBPropertiesFileAction () { 1.241 + } 1.242 + 1.243 + private String getSystemProperty(final String name) { 1.244 + // This will not throw a SecurityException because this 1.245 + // class was loaded from rt.jar using the bootstrap classloader. 1.246 + String propValue = (String) AccessController.doPrivileged( 1.247 + new PrivilegedAction() { 1.248 + public java.lang.Object run() { 1.249 + return System.getProperty(name); 1.250 + } 1.251 + } 1.252 + ); 1.253 + 1.254 + return propValue; 1.255 + } 1.256 + 1.257 + private void getPropertiesFromFile( Properties props, String fileName ) 1.258 + { 1.259 + try { 1.260 + File file = new File( fileName ) ; 1.261 + if (!file.exists()) 1.262 + return ; 1.263 + 1.264 + FileInputStream in = new FileInputStream( file ) ; 1.265 + 1.266 + try { 1.267 + props.load( in ) ; 1.268 + } finally { 1.269 + in.close() ; 1.270 + } 1.271 + } catch (Exception exc) { 1.272 + if (debug) 1.273 + System.out.println( "ORB properties file " + fileName + 1.274 + " not found: " + exc) ; 1.275 + } 1.276 + } 1.277 + 1.278 + public Object run() 1.279 + { 1.280 + Properties defaults = new Properties() ; 1.281 + 1.282 + String javaHome = getSystemProperty( "java.home" ) ; 1.283 + String fileName = javaHome + File.separator + "lib" + File.separator + 1.284 + "orb.properties" ; 1.285 + 1.286 + getPropertiesFromFile( defaults, fileName ) ; 1.287 + 1.288 + Properties results = new Properties( defaults ) ; 1.289 + 1.290 + String userHome = getSystemProperty( "user.home" ) ; 1.291 + fileName = userHome + File.separator + "orb.properties" ; 1.292 + 1.293 + getPropertiesFromFile( results, fileName ) ; 1.294 + return results ; 1.295 + } 1.296 +}