KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > rmi > PortableRemoteObject


1 /*
2  * @(#)PortableRemoteObject.java 1.32 04/06/21
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 /*
8  * Licensed Materials - Property of IBM
9  * RMI-IIOP v1.0
10  * Copyright IBM Corp. 1998 1999 All Rights Reserved
11  *
12  * US Government Users Restricted Rights - Use, duplication or
13  * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
14  */

15
16 package javax.rmi;
17
18 import java.lang.reflect.Method JavaDoc ;
19
20 import org.omg.CORBA.INITIALIZE JavaDoc;
21 import javax.rmi.CORBA.Util JavaDoc;
22
23 import java.rmi.RemoteException JavaDoc;
24 import java.rmi.NoSuchObjectException JavaDoc;
25 import java.rmi.Remote JavaDoc;
26 import java.io.File JavaDoc;
27 import java.io.FileInputStream JavaDoc;
28 import java.util.Properties JavaDoc;
29 import java.net.MalformedURLException JavaDoc ;
30 import java.security.AccessController JavaDoc;
31 import java.security.PrivilegedAction JavaDoc;
32 import java.rmi.server.RMIClassLoader JavaDoc;
33
34 import com.sun.corba.se.impl.orbutil.GetPropertyAction;
35
36 /**
37  * Server implementation objects may either inherit from
38  * javax.rmi.PortableRemoteObject or they may implement a remote interface
39  * and then use the exportObject method to register themselves as a server object.
40  * The toStub method takes a server implementation and returns a stub that
41  * can be used to access that server object.
42  * The connect method makes a Remote object ready for remote communication.
43  * The unexportObject method is used to deregister a server object, allowing it to become
44  * available for garbage collection.
45  * The narrow method takes an object reference or abstract interface type and
46  * attempts to narrow it to conform to
47  * the given interface. If the operation is successful the result will be an
48  * object of the specified type, otherwise an exception will be thrown.
49  */

50 public class PortableRemoteObject {
51
52     private static javax.rmi.CORBA.PortableRemoteObjectDelegate JavaDoc proDelegate = null;
53
54     private static final String JavaDoc PortableRemoteObjectClassKey =
55             "javax.rmi.CORBA.PortableRemoteObjectClass";
56
57     private static final String JavaDoc defaultPortableRemoteObjectImplName =
58             "com.sun.corba.se.impl.javax.rmi.PortableRemoteObject";
59
60     static {
61         proDelegate = (javax.rmi.CORBA.PortableRemoteObjectDelegate JavaDoc)
62             createDelegateIfSpecified(PortableRemoteObjectClassKey);
63     }
64
65     /**
66      * Initializes the object by calling <code>exportObject(this)</code>.
67      * @exception RemoteException if export fails.
68      */

69     protected PortableRemoteObject() throws RemoteException JavaDoc {
70     if (proDelegate != null) {
71             PortableRemoteObject.exportObject((Remote JavaDoc)this);
72     }
73     }
74     
75     /**
76      * Makes a server object ready to receive remote calls. Note
77      * that subclasses of PortableRemoteObject do not need to call this
78      * method, as it is called by the constructor.
79      * @param obj the server object to export.
80      * @exception RemoteException if export fails.
81      */

82     public static void exportObject(Remote JavaDoc obj)
83     throws RemoteException JavaDoc {
84
85     // Let the delegate do everything, including error handling.
86
if (proDelegate != null) {
87         proDelegate.exportObject(obj);
88     }
89     }
90
91     /**
92      * Returns a stub for the given server object.
93      * @param obj the server object for which a stub is required. Must either be a subclass
94      * of PortableRemoteObject or have been previously the target of a call to
95      * {@link #exportObject}.
96      * @return the most derived stub for the object.
97      * @exception NoSuchObjectException if a stub cannot be located for the given server object.
98      */

99     public static Remote JavaDoc toStub (Remote JavaDoc obj)
100     throws NoSuchObjectException JavaDoc {
101        
102     if (proDelegate != null) {
103         return proDelegate.toStub(obj);
104     }
105     return null;
106     }
107
108     /**
109      * Deregisters a server object from the runtime, allowing the object to become
110      * available for garbage collection.
111      * @param obj the object to unexport.
112      * @exception NoSuchObjectException if the remote object is not
113      * currently exported.
114      */

115     public static void unexportObject(Remote JavaDoc obj)
116     throws NoSuchObjectException JavaDoc {
117         
118     if (proDelegate != null) {
119         proDelegate.unexportObject(obj);
120     }
121         
122     }
123
124     /**
125      * Checks to ensure that an object of a remote or abstract interface type
126      * can be cast to a desired type.
127      * @param narrowFrom the object to check.
128      * @param narrowTo the desired type.
129      * @return an object which can be cast to the desired type.
130      * @throws ClassCastException if narrowFrom cannot be cast to narrowTo.
131      */

132     public static java.lang.Object JavaDoc narrow ( java.lang.Object JavaDoc narrowFrom,
133                                             java.lang.Class JavaDoc narrowTo)
134     throws ClassCastException JavaDoc {
135
136     if (proDelegate != null) {
137         return proDelegate.narrow(narrowFrom, narrowTo);
138     }
139     return null;
140            
141     }
142  
143     /**
144      * Makes a Remote object ready for remote communication. This normally
145      * happens implicitly when the object is sent or received as an argument
146      * on a remote method call, but in some circumstances it is useful to
147      * perform this action by making an explicit call. See the
148      * {@link Stub#connect} method for more information.
149      * @param target the object to connect.
150      * @param source a previously connected object.
151      * @throws RemoteException if <code>source</code> is not connected
152      * or if <code>target</code> is already connected to a different ORB than
153      * <code>source</code>.
154      */

155     public static void connect (Remote JavaDoc target, Remote JavaDoc source)
156     throws RemoteException JavaDoc {
157     
158     if (proDelegate != null) {
159         proDelegate.connect(target, source);
160     }
161  
162     }
163
164     // Same code as in javax.rmi.CORBA.Util. Can not be shared because they
165
// are in different packages and the visibility needs to be package for
166
// security reasons. If you know a better solution how to share this code
167
// then remove it from here.
168
private static Object JavaDoc createDelegateIfSpecified(String JavaDoc classKey) {
169         String JavaDoc className = (String JavaDoc)
170             AccessController.doPrivileged(new GetPropertyAction(classKey));
171         if (className == null) {
172             Properties JavaDoc props = getORBPropertiesFile();
173             if (props != null) {
174                 className = props.getProperty(classKey);
175             }
176         }
177     if (className == null) {
178         className = defaultPortableRemoteObjectImplName;
179     }
180
181         try {
182             return (Object JavaDoc) loadDelegateClass(className).newInstance();
183         } catch (ClassNotFoundException JavaDoc ex) {
184         INITIALIZE JavaDoc exc = new INITIALIZE JavaDoc( "Cannot instantiate " + className);
185         exc.initCause( ex ) ;
186         throw exc ;
187         } catch (Exception JavaDoc ex) {
188         INITIALIZE JavaDoc exc = new INITIALIZE JavaDoc( "Error while instantiating" + className);
189         exc.initCause( ex ) ;
190         throw exc ;
191         }
192
193     }
194
195     private static Class JavaDoc loadDelegateClass( String JavaDoc className ) throws ClassNotFoundException JavaDoc
196     {
197     try {
198         ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
199         return Class.forName(className, false, loader);
200     } catch (ClassNotFoundException JavaDoc e) {
201         // ignore, then try RMIClassLoader
202
}
203
204     try {
205         return RMIClassLoader.loadClass(className);
206     } catch (MalformedURLException JavaDoc e) {
207         String JavaDoc msg = "Could not load " + className + ": " + e.toString();
208         ClassNotFoundException JavaDoc exc = new ClassNotFoundException JavaDoc( msg ) ;
209         throw exc ;
210     }
211     }
212
213     /**
214      * Load the orb.properties file.
215      */

216     private static Properties JavaDoc getORBPropertiesFile () {
217         return (Properties JavaDoc) AccessController.doPrivileged(new GetORBPropertiesFileAction());
218     }
219 }
220
221 class GetORBPropertiesFileAction implements PrivilegedAction JavaDoc {
222     private boolean debug = false ;
223
224     public GetORBPropertiesFileAction () {
225     }
226
227     private String JavaDoc getSystemProperty(final String JavaDoc name) {
228     // This will not throw a SecurityException because this
229
// class was loaded from rt.jar using the bootstrap classloader.
230
String JavaDoc propValue = (String JavaDoc) AccessController.doPrivileged(
231         new PrivilegedAction JavaDoc() {
232         public java.lang.Object JavaDoc run() {
233                 return System.getProperty(name);
234             }
235             }
236     );
237
238     return propValue;
239     }
240
241     private void getPropertiesFromFile( Properties JavaDoc props, String JavaDoc fileName )
242     {
243         try {
244         File JavaDoc file = new File JavaDoc( fileName ) ;
245         if (!file.exists())
246         return ;
247
248             FileInputStream JavaDoc in = new FileInputStream JavaDoc( file ) ;
249         
250         try {
251         props.load( in ) ;
252         } finally {
253         in.close() ;
254         }
255         } catch (Exception JavaDoc exc) {
256             if (debug)
257                 System.out.println( "ORB properties file " + fileName +
258             " not found: " + exc) ;
259         }
260     }
261
262     public Object JavaDoc run()
263     {
264         Properties JavaDoc defaults = new Properties JavaDoc() ;
265
266     String JavaDoc javaHome = getSystemProperty( "java.home" ) ;
267     String JavaDoc fileName = javaHome + File.separator + "lib" + File.separator +
268         "orb.properties" ;
269
270     getPropertiesFromFile( defaults, fileName ) ;
271
272     Properties JavaDoc results = new Properties JavaDoc( defaults ) ;
273
274         String JavaDoc userHome = getSystemProperty( "user.home" ) ;
275         fileName = userHome + File.separator + "orb.properties" ;
276
277     getPropertiesFromFile( results, fileName ) ;
278     return results ;
279     }
280 }
281
282
283
Popular Tags