KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > rmi > CORBA > Stub


1 /*
2  * @(#)Stub.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 /*
9  * Licensed Materials - Property of IBM
10  * RMI-IIOP v1.0
11  * Copyright IBM Corp. 1998 1999 All Rights Reserved
12  *
13  * US Government Users Restricted Rights - Use, duplication or
14  * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
15  */

16
17 package javax.rmi.CORBA;
18
19 import org.omg.CORBA.ORB JavaDoc;
20 import org.omg.CORBA.INITIALIZE JavaDoc;
21 import org.omg.CORBA_2_3.portable.ObjectImpl JavaDoc;
22
23 import java.io.IOException JavaDoc;
24 import java.rmi.RemoteException JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.net.MalformedURLException JavaDoc ;
28 import java.security.AccessController JavaDoc;
29 import java.security.PrivilegedAction JavaDoc;
30 import java.util.Properties JavaDoc;
31 import java.rmi.server.RMIClassLoader JavaDoc;
32
33 import com.sun.corba.se.impl.orbutil.GetPropertyAction;
34
35
36 /**
37  * Base class from which all RMI-IIOP stubs must inherit.
38  */

39 public abstract class Stub extends ObjectImpl JavaDoc
40     implements java.io.Serializable JavaDoc {
41
42     private static final long serialVersionUID = 1087775603798577179L;
43
44     // This can only be set at object construction time (no sync necessary).
45
private transient StubDelegate JavaDoc stubDelegate = null;
46     private static Class JavaDoc stubDelegateClass = null;
47     private static final String JavaDoc StubClassKey = "javax.rmi.CORBA.StubClass";
48     private static final String JavaDoc defaultStubImplName = "com.sun.corba.se.impl.javax.rmi.CORBA.StubDelegateImpl";
49
50     static {
51         Object JavaDoc stubDelegateInstance = (Object JavaDoc) createDelegateIfSpecified(StubClassKey, defaultStubImplName);
52     if (stubDelegateInstance != null)
53         stubDelegateClass = stubDelegateInstance.getClass();
54     
55     }
56
57
58     /**
59      * Returns a hash code value for the object which is the same for all stubs
60      * that represent the same remote object.
61      * @return the hash code value.
62      */

63     public int hashCode() {
64
65     if (stubDelegate == null) {
66         setDefaultDelegate();
67     }
68
69     if (stubDelegate != null) {
70         return stubDelegate.hashCode(this);
71     }
72
73     return 0;
74     }
75
76     /**
77      * Compares two stubs for equality. Returns <code>true</code> when used to compare stubs
78      * that represent the same remote object, and <code>false</code> otherwise.
79      * @param obj the reference object with which to compare.
80      * @return <code>true</code> if this object is the same as the <code>obj</code>
81      * argument; <code>false</code> otherwise.
82      */

83     public boolean equals(java.lang.Object JavaDoc obj) {
84
85     if (stubDelegate == null) {
86         setDefaultDelegate();
87     }
88
89     if (stubDelegate != null) {
90         return stubDelegate.equals(this, obj);
91     }
92
93         return false;
94     }
95
96     /**
97      * Returns a string representation of this stub. Returns the same string
98      * for all stubs that represent the same remote object.
99      * @return a string representation of this stub.
100      */

101     public String JavaDoc toString() {
102
103
104     if (stubDelegate == null) {
105         setDefaultDelegate();
106     }
107
108     String JavaDoc ior;
109     if (stubDelegate != null) {
110         ior = stubDelegate.toString(this);
111         if (ior == null) {
112             return super.toString();
113         } else {
114             return ior;
115         }
116     }
117         return super.toString();
118     }
119     
120     /**
121      * Connects this stub to an ORB. Required after the stub is deserialized
122      * but not after it is demarshalled by an ORB stream. If an unconnected
123      * stub is passed to an ORB stream for marshalling, it is implicitly
124      * connected to that ORB. Application code should not call this method
125      * directly, but should call the portable wrapper method
126      * {@link javax.rmi.PortableRemoteObject#connect}.
127      * @param orb the ORB to connect to.
128      * @exception RemoteException if the stub is already connected to a different
129      * ORB, or if the stub does not represent an exported remote or local object.
130      */

131     public void connect(ORB JavaDoc orb) throws RemoteException JavaDoc {
132         
133     if (stubDelegate == null) {
134         setDefaultDelegate();
135     }
136
137     if (stubDelegate != null) {
138         stubDelegate.connect(this, orb);
139     }
140
141     }
142
143     /**
144      * Serialization method to restore the IOR state.
145      */

146     private void readObject(java.io.ObjectInputStream JavaDoc stream)
147         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
148
149     if (stubDelegate == null) {
150         setDefaultDelegate();
151     }
152
153     if (stubDelegate != null) {
154         stubDelegate.readObject(this, stream);
155     }
156
157     }
158
159     /**
160      * Serialization method to save the IOR state.
161      * @serialData The length of the IOR type ID (int), followed by the IOR type ID
162      * (byte array encoded using ISO8859-1), followed by the number of IOR profiles
163      * (int), followed by the IOR profiles. Each IOR profile is written as a
164      * profile tag (int), followed by the length of the profile data (int), followed
165      * by the profile data (byte array).
166      */

167     private void writeObject(java.io.ObjectOutputStream JavaDoc stream) throws IOException JavaDoc {
168
169     if (stubDelegate == null) {
170         setDefaultDelegate();
171     }
172
173     if (stubDelegate != null) {
174         stubDelegate.writeObject(this, stream);
175     }
176     }
177
178     private void setDefaultDelegate() {
179         if (stubDelegateClass != null) {
180             try {
181                  stubDelegate = (javax.rmi.CORBA.StubDelegate JavaDoc) stubDelegateClass.newInstance();
182         } catch (Exception JavaDoc ex) {
183         // what kind of exception to throw
184
// delegate not set therefore it is null and will return default
185
// values
186
}
187     }
188     }
189
190     // Same code as in PortableRemoteObject. Can not be shared because they
191
// are in different packages and the visibility needs to be package for
192
// security reasons. If you know a better solution how to share this code
193
// then remove it from PortableRemoteObject. Also in Util.java
194
private static Object JavaDoc createDelegateIfSpecified(String JavaDoc classKey, String JavaDoc defaultClassName) {
195         String JavaDoc className = (String JavaDoc)
196             AccessController.doPrivileged(new GetPropertyAction(classKey));
197         if (className == null) {
198             Properties JavaDoc props = getORBPropertiesFile();
199             if (props != null) {
200                 className = props.getProperty(classKey);
201             }
202         }
203
204     if (className == null) {
205         className = defaultClassName;
206     }
207
208         try {
209             return loadDelegateClass(className).newInstance();
210         } catch (ClassNotFoundException JavaDoc ex) {
211         INITIALIZE JavaDoc exc = new INITIALIZE JavaDoc( "Cannot instantiate " + className);
212         exc.initCause( ex ) ;
213         throw exc ;
214         } catch (Exception JavaDoc ex) {
215         INITIALIZE JavaDoc exc = new INITIALIZE JavaDoc( "Error while instantiating" + className);
216         exc.initCause( ex ) ;
217         throw exc ;
218         }
219
220     }
221
222     private static Class JavaDoc loadDelegateClass( String JavaDoc className ) throws ClassNotFoundException JavaDoc
223     {
224     try {
225         ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
226         return Class.forName(className, false, loader);
227     } catch (ClassNotFoundException JavaDoc e) {
228         // ignore, then try RMIClassLoader
229
}
230
231     try {
232         return RMIClassLoader.loadClass(className);
233     } catch (MalformedURLException JavaDoc e) {
234         String JavaDoc msg = "Could not load " + className + ": " + e.toString();
235         ClassNotFoundException JavaDoc exc = new ClassNotFoundException JavaDoc( msg ) ;
236         throw exc ;
237     }
238     }
239
240     /**
241      * Load the orb.properties file.
242      */

243     private static Properties JavaDoc getORBPropertiesFile () {
244         return (Properties JavaDoc) AccessController.doPrivileged(new GetORBPropertiesFileAction JavaDoc());
245     }
246
247 }
248
Popular Tags