KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > javax > rmi > PortableRemoteObject


1 /*
2  * @(#)PortableRemoteObject.java 1.13 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 com.sun.corba.se.impl.javax.rmi;
17
18 import java.lang.reflect.Method JavaDoc ;
19
20 import javax.rmi.CORBA.Tie 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
27 import java.util.Properties JavaDoc;
28
29 import org.omg.CORBA.ORB JavaDoc;
30 import org.omg.CORBA.portable.Delegate JavaDoc;
31 import org.omg.CORBA.SystemException JavaDoc;
32
33 import java.rmi.server.UnicastRemoteObject JavaDoc;
34 import java.rmi.server.RemoteStub JavaDoc;
35 import java.rmi.server.ExportException JavaDoc;
36
37 import java.net.URL JavaDoc;
38
39 import com.sun.corba.se.impl.util.JDKBridge;
40 import com.sun.corba.se.impl.util.Utility;
41 import com.sun.corba.se.impl.util.RepositoryId;
42
43 import com.sun.corba.se.spi.presentation.rmi.StubAdapter;
44
45 import java.security.AccessController JavaDoc;
46 import com.sun.corba.se.impl.orbutil.GetPropertyAction;
47
48 /**
49  * Server implementation objects may either inherit from
50  * javax.rmi.PortableRemoteObject or they may implement a remote interface
51  * and then use the exportObject method to register themselves as a server object.
52  * The toStub method takes a server implementation and returns a stub that
53  * can be used to access that server object.
54  * The connect method makes a Remote object ready for remote communication.
55  * The unexportObject method is used to deregister a server object, allowing it to become
56  * available for garbage collection.
57  * The narrow method takes an object reference or abstract interface type and
58  * attempts to narrow it to conform to
59  * the given interface. If the operation is successful the result will be an
60  * object of the specified type, otherwise an exception will be thrown.
61  */

62 public class PortableRemoteObject
63     implements javax.rmi.CORBA.PortableRemoteObjectDelegate JavaDoc {
64
65     /**
66      * Makes a server object ready to receive remote calls. Note
67      * that subclasses of PortableRemoteObject do not need to call this
68      * method, as it is called by the constructor.
69      * @param obj the server object to export.
70      * @exception RemoteException if export fails.
71      */

72     public void exportObject(Remote JavaDoc obj)
73     throws RemoteException JavaDoc {
74
75         if (obj == null) {
76             throw new NullPointerException JavaDoc("invalid argument");
77         }
78     
79         // Has this object already been exported to IIOP?
80

81         if (Util.getTie(obj) != null) {
82         
83             // Yes, so this is an error...
84

85             throw new ExportException JavaDoc (obj.getClass().getName() + " already exported");
86         }
87         
88         // Can we load a Tie?
89

90         Tie JavaDoc theTie = Utility.loadTie(obj);
91         
92         if (theTie != null) {
93         
94             // Yes, so export it to IIOP...
95

96             Util.registerTarget(theTie,obj);
97             
98         } else {
99             
100             // No, so export to JRMP. If this is called twice for the
101
// same object, it will throw an ExportException...
102

103         UnicastRemoteObject.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 Remote JavaDoc toStub (Remote JavaDoc obj)
116     throws NoSuchObjectException JavaDoc
117     {
118         Remote JavaDoc result = null;
119         if (obj == null) {
120             throw new NullPointerException JavaDoc("invalid argument");
121         }
122         
123         // If the class is already an IIOP stub then return it.
124
if (StubAdapter.isStub( obj )) {
125             return obj;
126         }
127         
128         // If the class is already a JRMP stub then return it.
129
if (obj instanceof java.rmi.server.RemoteStub JavaDoc) {
130             return obj;
131         }
132             
133         // Has it been exported to IIOP?
134
Tie JavaDoc theTie = Util.getTie(obj);
135         
136         if (theTie != null) {
137             result = Utility.loadStub(theTie,null,null,true);
138         } else {
139             if (Utility.loadTie(obj) == null) {
140                 result = java.rmi.server.RemoteObject.toStub(obj);
141             }
142         }
143         
144         if (result == null) {
145             throw new NoSuchObjectException JavaDoc("object not exported");
146         }
147         
148         return result;
149     }
150
151     /**
152      * Deregisters a server object from the runtime, allowing the object to become
153      * available for garbage collection.
154      * @param obj the object to unexport.
155      * @exception NoSuchObjectException if the remote object is not
156      * currently exported.
157      */

158     public void unexportObject(Remote JavaDoc obj)
159     throws NoSuchObjectException JavaDoc {
160         
161         if (obj == null) {
162             throw new NullPointerException JavaDoc("invalid argument");
163         }
164         
165         if (StubAdapter.isStub(obj) ||
166             obj instanceof java.rmi.server.RemoteStub JavaDoc) {
167             throw new NoSuchObjectException JavaDoc(
168         "Can only unexport a server object.");
169         }
170         
171         Tie JavaDoc theTie = Util.getTie(obj);
172         if (theTie != null) {
173         Util.unexportObject(obj);
174     } else {
175         if (Utility.loadTie(obj) == null) {
176                 UnicastRemoteObject.unexportObject(obj,true);
177             } else {
178                 throw new NoSuchObjectException JavaDoc("Object not exported.");
179             }
180     }
181     }
182
183     /**
184      * Checks to ensure that an object of a remote or abstract interface type
185      * can be cast to a desired type.
186      * @param narrowFrom the object to check.
187      * @param narrowTo the desired type.
188      * @return an object which can be cast to the desired type.
189      * @throws ClassCastException if narrowFrom cannot be cast to narrowTo.
190      */

191     public java.lang.Object JavaDoc narrow ( java.lang.Object JavaDoc narrowFrom,
192     java.lang.Class JavaDoc narrowTo) throws ClassCastException JavaDoc
193     {
194         java.lang.Object JavaDoc result = null;
195
196         if (narrowFrom == null)
197             return null;
198
199         if (narrowTo == null)
200             throw new NullPointerException JavaDoc("invalid argument");
201
202         try {
203             if (narrowTo.isAssignableFrom(narrowFrom.getClass()))
204                 return narrowFrom;
205
206         // Is narrowTo an interface that might be
207
// implemented by a servant running on iiop?
208
if (narrowTo.isInterface() &&
209         narrowTo != java.io.Serializable JavaDoc.class &&
210         narrowTo != java.io.Externalizable JavaDoc.class) {
211     
212         org.omg.CORBA.Object JavaDoc narrowObj
213             = (org.omg.CORBA.Object JavaDoc) narrowFrom;
214         
215         // Create an id from the narrowTo type...
216
String JavaDoc id = RepositoryId.createForAnyType(narrowTo);
217         
218         if (narrowObj._is_a(id)) {
219             return Utility.loadStub(narrowObj,narrowTo);
220         } else {
221             throw new ClassCastException JavaDoc( "Object is not of remote type " +
222             narrowTo.getName() ) ;
223         }
224         } else {
225         throw new ClassCastException JavaDoc( "Class " + narrowTo.getName() +
226             " is not a valid remote interface" ) ;
227         }
228         } catch(Exception JavaDoc error) {
229         ClassCastException JavaDoc cce = new ClassCastException JavaDoc() ;
230         cce.initCause( error ) ;
231         throw cce ;
232         }
233     }
234  
235     /**
236      * Makes a Remote object ready for remote communication. This normally
237      * happens implicitly when the object is sent or received as an argument
238      * on a remote method call, but in some circumstances it is useful to
239      * perform this action by making an explicit call. See the
240      * {@link Stub#connect} method for more information.
241      * @param target the object to connect.
242      * @param source a previously connected object.
243      * @throws RemoteException if <code>source</code> is not connected
244      * or if <code>target</code> is already connected to a different ORB than
245      * <code>source</code>.
246      */

247     public void connect (Remote JavaDoc target, Remote JavaDoc source)
248     throws RemoteException JavaDoc
249     {
250         if (target == null || source == null) {
251             throw new NullPointerException JavaDoc("invalid argument");
252         }
253  
254         ORB JavaDoc orb = null;
255         try {
256         if (StubAdapter.isStub( source )) {
257         orb = StubAdapter.getORB( source ) ;
258             } else {
259                 // Is this a servant that was exported to iiop?
260
Tie JavaDoc tie = Util.getTie(source);
261                 if (tie == null) {
262             /* loadTie always succeeds for dynamic RMI-IIOP
263                     // No, can we get a tie for it? If not,
264                     // assume that source is a JRMP object...
265                     if (Utility.loadTie(source) != null) {
266                         // Yes, so it is an iiop object which
267                         // has not been exported...
268                         throw new RemoteException(
269                 "'source' object not exported");
270                     }
271             */

272                 } else {
273                     orb = tie.orb();
274                 }
275             }
276         } catch (SystemException JavaDoc e) {
277             throw new RemoteException JavaDoc("'source' object not connected", e );
278         }
279
280         boolean targetIsIIOP = false ;
281         Tie JavaDoc targetTie = null;
282         if (StubAdapter.isStub(target)) {
283             targetIsIIOP = true;
284         } else {
285             targetTie = Util.getTie(target);
286             if (targetTie != null) {
287                 targetIsIIOP = true;
288             } else {
289         /* loadTie always succeeds for dynamic RMI-IIOP
290                 if (Utility.loadTie(target) != null) {
291                     throw new RemoteException("'target' servant not exported");
292                 }
293         */

294             }
295         }
296
297         if (!targetIsIIOP) {
298             // Yes. Do we have an ORB from the source object?
299
// If not, we're done - there is nothing to do to
300
// connect a JRMP object. If so, it is an error because
301
// the caller mixed JRMP and IIOP...
302
if (orb != null) {
303                 throw new RemoteException JavaDoc(
304             "'source' object exported to IIOP, 'target' is JRMP");
305             }
306         } else {
307             // The target object is IIOP. Make sure we have a
308
// valid ORB from the source object...
309
if (orb == null) {
310                 throw new RemoteException JavaDoc(
311             "'source' object is JRMP, 'target' is IIOP");
312             }
313             
314             // And, finally, connect it up...
315
try {
316                 if (targetTie != null) {
317                     // Is the tie already connected?
318
try {
319                         ORB JavaDoc existingOrb = targetTie.orb();
320                         
321                         // Yes. Is it the same orb?
322
if (existingOrb == orb) {
323                             
324                             // Yes, so nothing to do...
325
return;
326                         } else {
327                             // No, so this is an error...
328
throw new RemoteException JavaDoc(
329                 "'target' object was already connected");
330                         }
331                     } catch (SystemException JavaDoc e) {}
332                     
333                     // No, so do it...
334
targetTie.orb(orb);
335                 } else {
336             StubAdapter.connect( target, orb ) ;
337                 }
338             } catch (SystemException JavaDoc e) {
339                 
340                 // The stub or tie was already connected...
341
throw new RemoteException JavaDoc(
342             "'target' object was already connected", e );
343             }
344         }
345     }
346 }
347
Popular Tags