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