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