src/share/classes/javax/rmi/PortableRemoteObject.java

Thu, 31 Aug 2017 18:10:36 +0800

author
aoqi
date
Thu, 31 Aug 2017 18:10:36 +0800
changeset 748
6845b95cba6b
parent 553
5ca1b4c282b8
parent 0
7ef37b2cdcad
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25 /*
aoqi@0 26 * Licensed Materials - Property of IBM
aoqi@0 27 * RMI-IIOP v1.0
aoqi@0 28 * Copyright IBM Corp. 1998 1999 All Rights Reserved
aoqi@0 29 *
aoqi@0 30 */
aoqi@0 31
aoqi@0 32 package javax.rmi;
aoqi@0 33
aoqi@0 34 import java.lang.reflect.Method ;
aoqi@0 35
aoqi@0 36 import org.omg.CORBA.INITIALIZE;
aoqi@0 37 import javax.rmi.CORBA.Util;
aoqi@0 38
aoqi@0 39 import java.rmi.RemoteException;
aoqi@0 40 import java.rmi.NoSuchObjectException;
aoqi@0 41 import java.rmi.Remote;
aoqi@0 42 import java.io.File;
aoqi@0 43 import java.io.FileInputStream;
aoqi@0 44 import java.util.Properties;
aoqi@0 45 import java.net.MalformedURLException ;
aoqi@0 46 import java.security.AccessController;
aoqi@0 47 import java.security.PrivilegedAction;
aoqi@0 48 import java.rmi.server.RMIClassLoader;
aoqi@0 49
aoqi@0 50 import com.sun.corba.se.impl.orbutil.GetPropertyAction;
aoqi@0 51
aoqi@0 52 /**
aoqi@0 53 * Server implementation objects may either inherit from
aoqi@0 54 * javax.rmi.PortableRemoteObject or they may implement a remote interface
aoqi@0 55 * and then use the exportObject method to register themselves as a server object.
aoqi@0 56 * The toStub method takes a server implementation and returns a stub that
aoqi@0 57 * can be used to access that server object.
aoqi@0 58 * The connect method makes a Remote object ready for remote communication.
aoqi@0 59 * The unexportObject method is used to deregister a server object, allowing it to become
aoqi@0 60 * available for garbage collection.
aoqi@0 61 * The narrow method takes an object reference or abstract interface type and
aoqi@0 62 * attempts to narrow it to conform to
aoqi@0 63 * the given interface. If the operation is successful the result will be an
aoqi@0 64 * object of the specified type, otherwise an exception will be thrown.
aoqi@0 65 */
aoqi@0 66 public class PortableRemoteObject {
aoqi@0 67
aoqi@0 68 private static final javax.rmi.CORBA.PortableRemoteObjectDelegate proDelegate;
aoqi@0 69
aoqi@0 70 private static final String PortableRemoteObjectClassKey =
aoqi@0 71 "javax.rmi.CORBA.PortableRemoteObjectClass";
aoqi@0 72
aoqi@0 73 static {
aoqi@0 74 proDelegate = (javax.rmi.CORBA.PortableRemoteObjectDelegate)
aoqi@0 75 createDelegate(PortableRemoteObjectClassKey);
aoqi@0 76 }
aoqi@0 77
aoqi@0 78 /**
aoqi@0 79 * Initializes the object by calling <code>exportObject(this)</code>.
aoqi@0 80 * @exception RemoteException if export fails.
aoqi@0 81 */
aoqi@0 82 protected PortableRemoteObject() throws RemoteException {
aoqi@0 83 if (proDelegate != null) {
aoqi@0 84 PortableRemoteObject.exportObject((Remote)this);
aoqi@0 85 }
aoqi@0 86 }
aoqi@0 87
aoqi@0 88 /**
aoqi@0 89 * Makes a server object ready to receive remote calls. Note
aoqi@0 90 * that subclasses of PortableRemoteObject do not need to call this
aoqi@0 91 * method, as it is called by the constructor.
aoqi@0 92 * @param obj the server object to export.
aoqi@0 93 * @exception RemoteException if export fails.
aoqi@0 94 */
aoqi@0 95 public static void exportObject(Remote obj)
aoqi@0 96 throws RemoteException {
aoqi@0 97
aoqi@0 98 // Let the delegate do everything, including error handling.
aoqi@0 99 if (proDelegate != null) {
aoqi@0 100 proDelegate.exportObject(obj);
aoqi@0 101 }
aoqi@0 102 }
aoqi@0 103
aoqi@0 104 /**
aoqi@0 105 * Returns a stub for the given server object.
aoqi@0 106 * @param obj the server object for which a stub is required. Must either be a subclass
aoqi@0 107 * of PortableRemoteObject or have been previously the target of a call to
aoqi@0 108 * {@link #exportObject}.
aoqi@0 109 * @return the most derived stub for the object.
aoqi@0 110 * @exception NoSuchObjectException if a stub cannot be located for the given server object.
aoqi@0 111 */
aoqi@0 112 public static Remote toStub (Remote obj)
aoqi@0 113 throws NoSuchObjectException {
aoqi@0 114
aoqi@0 115 if (proDelegate != null) {
aoqi@0 116 return proDelegate.toStub(obj);
aoqi@0 117 }
aoqi@0 118 return null;
aoqi@0 119 }
aoqi@0 120
aoqi@0 121 /**
aoqi@0 122 * Deregisters a server object from the runtime, allowing the object to become
aoqi@0 123 * available for garbage collection.
aoqi@0 124 * @param obj the object to unexport.
aoqi@0 125 * @exception NoSuchObjectException if the remote object is not
aoqi@0 126 * currently exported.
aoqi@0 127 */
aoqi@0 128 public static void unexportObject(Remote obj)
aoqi@0 129 throws NoSuchObjectException {
aoqi@0 130
aoqi@0 131 if (proDelegate != null) {
aoqi@0 132 proDelegate.unexportObject(obj);
aoqi@0 133 }
aoqi@0 134
aoqi@0 135 }
aoqi@0 136
aoqi@0 137 /**
aoqi@0 138 * Checks to ensure that an object of a remote or abstract interface type
aoqi@0 139 * can be cast to a desired type.
aoqi@0 140 * @param narrowFrom the object to check.
aoqi@0 141 * @param narrowTo the desired type.
aoqi@0 142 * @return an object which can be cast to the desired type.
aoqi@0 143 * @throws ClassCastException if narrowFrom cannot be cast to narrowTo.
aoqi@0 144 */
aoqi@0 145 public static java.lang.Object narrow ( java.lang.Object narrowFrom,
aoqi@0 146 java.lang.Class narrowTo)
aoqi@0 147 throws ClassCastException {
aoqi@0 148
aoqi@0 149 if (proDelegate != null) {
aoqi@0 150 return proDelegate.narrow(narrowFrom, narrowTo);
aoqi@0 151 }
aoqi@0 152 return null;
aoqi@0 153
aoqi@0 154 }
aoqi@0 155
aoqi@0 156 /**
aoqi@0 157 * Makes a Remote object ready for remote communication. This normally
aoqi@0 158 * happens implicitly when the object is sent or received as an argument
aoqi@0 159 * on a remote method call, but in some circumstances it is useful to
aoqi@0 160 * perform this action by making an explicit call. See the
aoqi@0 161 * {@link javax.rmi.CORBA.Stub#connect} method for more information.
aoqi@0 162 * @param target the object to connect.
aoqi@0 163 * @param source a previously connected object.
aoqi@0 164 * @throws RemoteException if <code>source</code> is not connected
aoqi@0 165 * or if <code>target</code> is already connected to a different ORB than
aoqi@0 166 * <code>source</code>.
aoqi@0 167 */
aoqi@0 168 public static void connect (Remote target, Remote source)
aoqi@0 169 throws RemoteException {
aoqi@0 170
aoqi@0 171 if (proDelegate != null) {
aoqi@0 172 proDelegate.connect(target, source);
aoqi@0 173 }
aoqi@0 174
aoqi@0 175 }
aoqi@0 176
aoqi@0 177 // Same code as in javax.rmi.CORBA.Util. Can not be shared because they
aoqi@0 178 // are in different packages and the visibility needs to be package for
aoqi@0 179 // security reasons. If you know a better solution how to share this code
aoqi@0 180 // then remove it from here.
aoqi@0 181 private static Object createDelegate(String classKey) {
aoqi@0 182 String className = (String)
aoqi@0 183 AccessController.doPrivileged(new GetPropertyAction(classKey));
aoqi@0 184 if (className == null) {
aoqi@0 185 Properties props = getORBPropertiesFile();
aoqi@0 186 if (props != null) {
aoqi@0 187 className = props.getProperty(classKey);
aoqi@0 188 }
aoqi@0 189 }
aoqi@0 190 if (className == null) {
aoqi@0 191 return new com.sun.corba.se.impl.javax.rmi.PortableRemoteObject();
aoqi@0 192 }
aoqi@0 193
aoqi@0 194 try {
aoqi@0 195 return (Object) loadDelegateClass(className).newInstance();
aoqi@0 196 } catch (ClassNotFoundException ex) {
aoqi@0 197 INITIALIZE exc = new INITIALIZE( "Cannot instantiate " + className);
aoqi@0 198 exc.initCause( ex ) ;
aoqi@0 199 throw exc ;
aoqi@0 200 } catch (Exception ex) {
aoqi@0 201 INITIALIZE exc = new INITIALIZE( "Error while instantiating" + className);
aoqi@0 202 exc.initCause( ex ) ;
aoqi@0 203 throw exc ;
aoqi@0 204 }
aoqi@0 205
aoqi@0 206 }
aoqi@0 207
aoqi@0 208 private static Class loadDelegateClass( String className ) throws ClassNotFoundException
aoqi@0 209 {
aoqi@0 210 try {
aoqi@0 211 ClassLoader loader = Thread.currentThread().getContextClassLoader();
aoqi@0 212 return Class.forName(className, false, loader);
aoqi@0 213 } catch (ClassNotFoundException e) {
aoqi@0 214 // ignore, then try RMIClassLoader
aoqi@0 215 }
aoqi@0 216
aoqi@0 217 try {
aoqi@0 218 return RMIClassLoader.loadClass(className);
aoqi@0 219 } catch (MalformedURLException e) {
aoqi@0 220 String msg = "Could not load " + className + ": " + e.toString();
aoqi@0 221 ClassNotFoundException exc = new ClassNotFoundException( msg ) ;
aoqi@0 222 throw exc ;
aoqi@0 223 }
aoqi@0 224 }
aoqi@0 225
aoqi@0 226 /**
aoqi@0 227 * Load the orb.properties file.
aoqi@0 228 */
aoqi@0 229 private static Properties getORBPropertiesFile () {
aoqi@0 230 return (Properties) AccessController.doPrivileged(new GetORBPropertiesFileAction());
aoqi@0 231 }
aoqi@0 232 }
aoqi@0 233
aoqi@0 234 class GetORBPropertiesFileAction implements PrivilegedAction {
aoqi@0 235 private boolean debug = false ;
aoqi@0 236
aoqi@0 237 public GetORBPropertiesFileAction () {
aoqi@0 238 }
aoqi@0 239
aoqi@0 240 private String getSystemProperty(final String name) {
aoqi@0 241 // This will not throw a SecurityException because this
aoqi@0 242 // class was loaded from rt.jar using the bootstrap classloader.
aoqi@0 243 String propValue = (String) AccessController.doPrivileged(
aoqi@0 244 new PrivilegedAction() {
aoqi@0 245 public java.lang.Object run() {
aoqi@0 246 return System.getProperty(name);
aoqi@0 247 }
aoqi@0 248 }
aoqi@0 249 );
aoqi@0 250
aoqi@0 251 return propValue;
aoqi@0 252 }
aoqi@0 253
aoqi@0 254 private void getPropertiesFromFile( Properties props, String fileName )
aoqi@0 255 {
aoqi@0 256 try {
aoqi@0 257 File file = new File( fileName ) ;
aoqi@0 258 if (!file.exists())
aoqi@0 259 return ;
aoqi@0 260
aoqi@0 261 FileInputStream in = new FileInputStream( file ) ;
aoqi@0 262
aoqi@0 263 try {
aoqi@0 264 props.load( in ) ;
aoqi@0 265 } finally {
aoqi@0 266 in.close() ;
aoqi@0 267 }
aoqi@0 268 } catch (Exception exc) {
aoqi@0 269 if (debug)
aoqi@0 270 System.out.println( "ORB properties file " + fileName +
aoqi@0 271 " not found: " + exc) ;
aoqi@0 272 }
aoqi@0 273 }
aoqi@0 274
aoqi@0 275 public Object run()
aoqi@0 276 {
aoqi@0 277 Properties defaults = new Properties() ;
aoqi@0 278
aoqi@0 279 String javaHome = getSystemProperty( "java.home" ) ;
aoqi@0 280 String fileName = javaHome + File.separator + "lib" + File.separator +
aoqi@0 281 "orb.properties" ;
aoqi@0 282
aoqi@0 283 getPropertiesFromFile( defaults, fileName ) ;
aoqi@0 284
aoqi@0 285 Properties results = new Properties( defaults ) ;
aoqi@0 286
aoqi@0 287 String userHome = getSystemProperty( "user.home" ) ;
aoqi@0 288 fileName = userHome + File.separator + "orb.properties" ;
aoqi@0 289
aoqi@0 290 getPropertiesFromFile( results, fileName ) ;
aoqi@0 291 return results ;
aoqi@0 292 }
aoqi@0 293 }

mercurial