aoqi@0: /* aoqi@0: * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: /* aoqi@0: * Licensed Materials - Property of IBM aoqi@0: * RMI-IIOP v1.0 aoqi@0: * Copyright IBM Corp. 1998 1999 All Rights Reserved aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: package javax.rmi; aoqi@0: aoqi@0: import java.lang.reflect.Method ; aoqi@0: aoqi@0: import org.omg.CORBA.INITIALIZE; aoqi@0: import javax.rmi.CORBA.Util; aoqi@0: aoqi@0: import java.rmi.RemoteException; aoqi@0: import java.rmi.NoSuchObjectException; aoqi@0: import java.rmi.Remote; aoqi@0: import java.io.File; aoqi@0: import java.io.FileInputStream; aoqi@0: import java.util.Properties; aoqi@0: import java.net.MalformedURLException ; aoqi@0: import java.security.AccessController; aoqi@0: import java.security.PrivilegedAction; aoqi@0: import java.rmi.server.RMIClassLoader; aoqi@0: aoqi@0: import com.sun.corba.se.impl.orbutil.GetPropertyAction; aoqi@0: aoqi@0: /** aoqi@0: * Server implementation objects may either inherit from aoqi@0: * javax.rmi.PortableRemoteObject or they may implement a remote interface aoqi@0: * and then use the exportObject method to register themselves as a server object. aoqi@0: * The toStub method takes a server implementation and returns a stub that aoqi@0: * can be used to access that server object. aoqi@0: * The connect method makes a Remote object ready for remote communication. aoqi@0: * The unexportObject method is used to deregister a server object, allowing it to become aoqi@0: * available for garbage collection. aoqi@0: * The narrow method takes an object reference or abstract interface type and aoqi@0: * attempts to narrow it to conform to aoqi@0: * the given interface. If the operation is successful the result will be an aoqi@0: * object of the specified type, otherwise an exception will be thrown. aoqi@0: */ aoqi@0: public class PortableRemoteObject { aoqi@0: aoqi@0: private static final javax.rmi.CORBA.PortableRemoteObjectDelegate proDelegate; aoqi@0: aoqi@0: private static final String PortableRemoteObjectClassKey = aoqi@0: "javax.rmi.CORBA.PortableRemoteObjectClass"; aoqi@0: aoqi@0: static { aoqi@0: proDelegate = (javax.rmi.CORBA.PortableRemoteObjectDelegate) aoqi@0: createDelegate(PortableRemoteObjectClassKey); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Initializes the object by calling exportObject(this). aoqi@0: * @exception RemoteException if export fails. aoqi@0: */ aoqi@0: protected PortableRemoteObject() throws RemoteException { aoqi@0: if (proDelegate != null) { aoqi@0: PortableRemoteObject.exportObject((Remote)this); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Makes a server object ready to receive remote calls. Note aoqi@0: * that subclasses of PortableRemoteObject do not need to call this aoqi@0: * method, as it is called by the constructor. aoqi@0: * @param obj the server object to export. aoqi@0: * @exception RemoteException if export fails. aoqi@0: */ aoqi@0: public static void exportObject(Remote obj) aoqi@0: throws RemoteException { aoqi@0: aoqi@0: // Let the delegate do everything, including error handling. aoqi@0: if (proDelegate != null) { aoqi@0: proDelegate.exportObject(obj); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns a stub for the given server object. aoqi@0: * @param obj the server object for which a stub is required. Must either be a subclass aoqi@0: * of PortableRemoteObject or have been previously the target of a call to aoqi@0: * {@link #exportObject}. aoqi@0: * @return the most derived stub for the object. aoqi@0: * @exception NoSuchObjectException if a stub cannot be located for the given server object. aoqi@0: */ aoqi@0: public static Remote toStub (Remote obj) aoqi@0: throws NoSuchObjectException { aoqi@0: aoqi@0: if (proDelegate != null) { aoqi@0: return proDelegate.toStub(obj); aoqi@0: } aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Deregisters a server object from the runtime, allowing the object to become aoqi@0: * available for garbage collection. aoqi@0: * @param obj the object to unexport. aoqi@0: * @exception NoSuchObjectException if the remote object is not aoqi@0: * currently exported. aoqi@0: */ aoqi@0: public static void unexportObject(Remote obj) aoqi@0: throws NoSuchObjectException { aoqi@0: aoqi@0: if (proDelegate != null) { aoqi@0: proDelegate.unexportObject(obj); aoqi@0: } aoqi@0: aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Checks to ensure that an object of a remote or abstract interface type aoqi@0: * can be cast to a desired type. aoqi@0: * @param narrowFrom the object to check. aoqi@0: * @param narrowTo the desired type. aoqi@0: * @return an object which can be cast to the desired type. aoqi@0: * @throws ClassCastException if narrowFrom cannot be cast to narrowTo. aoqi@0: */ aoqi@0: public static java.lang.Object narrow ( java.lang.Object narrowFrom, aoqi@0: java.lang.Class narrowTo) aoqi@0: throws ClassCastException { aoqi@0: aoqi@0: if (proDelegate != null) { aoqi@0: return proDelegate.narrow(narrowFrom, narrowTo); aoqi@0: } aoqi@0: return null; aoqi@0: aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Makes a Remote object ready for remote communication. This normally aoqi@0: * happens implicitly when the object is sent or received as an argument aoqi@0: * on a remote method call, but in some circumstances it is useful to aoqi@0: * perform this action by making an explicit call. See the aoqi@0: * {@link javax.rmi.CORBA.Stub#connect} method for more information. aoqi@0: * @param target the object to connect. aoqi@0: * @param source a previously connected object. aoqi@0: * @throws RemoteException if source is not connected aoqi@0: * or if target is already connected to a different ORB than aoqi@0: * source. aoqi@0: */ aoqi@0: public static void connect (Remote target, Remote source) aoqi@0: throws RemoteException { aoqi@0: aoqi@0: if (proDelegate != null) { aoqi@0: proDelegate.connect(target, source); aoqi@0: } aoqi@0: aoqi@0: } aoqi@0: aoqi@0: // Same code as in javax.rmi.CORBA.Util. Can not be shared because they aoqi@0: // are in different packages and the visibility needs to be package for aoqi@0: // security reasons. If you know a better solution how to share this code aoqi@0: // then remove it from here. aoqi@0: private static Object createDelegate(String classKey) { aoqi@0: String className = (String) aoqi@0: AccessController.doPrivileged(new GetPropertyAction(classKey)); aoqi@0: if (className == null) { aoqi@0: Properties props = getORBPropertiesFile(); aoqi@0: if (props != null) { aoqi@0: className = props.getProperty(classKey); aoqi@0: } aoqi@0: } aoqi@0: if (className == null) { aoqi@0: return new com.sun.corba.se.impl.javax.rmi.PortableRemoteObject(); aoqi@0: } aoqi@0: aoqi@0: try { aoqi@0: return (Object) loadDelegateClass(className).newInstance(); aoqi@0: } catch (ClassNotFoundException ex) { aoqi@0: INITIALIZE exc = new INITIALIZE( "Cannot instantiate " + className); aoqi@0: exc.initCause( ex ) ; aoqi@0: throw exc ; aoqi@0: } catch (Exception ex) { aoqi@0: INITIALIZE exc = new INITIALIZE( "Error while instantiating" + className); aoqi@0: exc.initCause( ex ) ; aoqi@0: throw exc ; aoqi@0: } aoqi@0: aoqi@0: } aoqi@0: aoqi@0: private static Class loadDelegateClass( String className ) throws ClassNotFoundException aoqi@0: { aoqi@0: try { aoqi@0: ClassLoader loader = Thread.currentThread().getContextClassLoader(); aoqi@0: return Class.forName(className, false, loader); aoqi@0: } catch (ClassNotFoundException e) { aoqi@0: // ignore, then try RMIClassLoader aoqi@0: } aoqi@0: aoqi@0: try { aoqi@0: return RMIClassLoader.loadClass(className); aoqi@0: } catch (MalformedURLException e) { aoqi@0: String msg = "Could not load " + className + ": " + e.toString(); aoqi@0: ClassNotFoundException exc = new ClassNotFoundException( msg ) ; aoqi@0: throw exc ; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Load the orb.properties file. aoqi@0: */ aoqi@0: private static Properties getORBPropertiesFile () { aoqi@0: return (Properties) AccessController.doPrivileged(new GetORBPropertiesFileAction()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: class GetORBPropertiesFileAction implements PrivilegedAction { aoqi@0: private boolean debug = false ; aoqi@0: aoqi@0: public GetORBPropertiesFileAction () { aoqi@0: } aoqi@0: aoqi@0: private String getSystemProperty(final String name) { aoqi@0: // This will not throw a SecurityException because this aoqi@0: // class was loaded from rt.jar using the bootstrap classloader. aoqi@0: String propValue = (String) AccessController.doPrivileged( aoqi@0: new PrivilegedAction() { aoqi@0: public java.lang.Object run() { aoqi@0: return System.getProperty(name); aoqi@0: } aoqi@0: } aoqi@0: ); aoqi@0: aoqi@0: return propValue; aoqi@0: } aoqi@0: aoqi@0: private void getPropertiesFromFile( Properties props, String fileName ) aoqi@0: { aoqi@0: try { aoqi@0: File file = new File( fileName ) ; aoqi@0: if (!file.exists()) aoqi@0: return ; aoqi@0: aoqi@0: FileInputStream in = new FileInputStream( file ) ; aoqi@0: aoqi@0: try { aoqi@0: props.load( in ) ; aoqi@0: } finally { aoqi@0: in.close() ; aoqi@0: } aoqi@0: } catch (Exception exc) { aoqi@0: if (debug) aoqi@0: System.out.println( "ORB properties file " + fileName + aoqi@0: " not found: " + exc) ; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public Object run() aoqi@0: { aoqi@0: Properties defaults = new Properties() ; aoqi@0: aoqi@0: String javaHome = getSystemProperty( "java.home" ) ; aoqi@0: String fileName = javaHome + File.separator + "lib" + File.separator + aoqi@0: "orb.properties" ; aoqi@0: aoqi@0: getPropertiesFromFile( defaults, fileName ) ; aoqi@0: aoqi@0: Properties results = new Properties( defaults ) ; aoqi@0: aoqi@0: String userHome = getSystemProperty( "user.home" ) ; aoqi@0: fileName = userHome + File.separator + "orb.properties" ; aoqi@0: aoqi@0: getPropertiesFromFile( results, fileName ) ; aoqi@0: return results ; aoqi@0: } aoqi@0: }