Thu, 17 Jun 2010 16:27:56 -0700
Added tag jdk7-b98 for changeset 3b99409057e4
1 /*
2 * Copyright (c) 1998, 2004, Oracle and/or its affiliates. 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 /*
27 * Licensed Materials - Property of IBM
28 * RMI-IIOP v1.0
29 * Copyright IBM Corp. 1998 1999 All Rights Reserved
30 *
31 */
33 package javax.rmi.CORBA;
35 import org.omg.CORBA.ORB;
36 import org.omg.CORBA.INITIALIZE;
37 import org.omg.CORBA_2_3.portable.ObjectImpl;
39 import java.io.IOException;
40 import java.rmi.RemoteException;
41 import java.io.File;
42 import java.io.FileInputStream;
43 import java.net.MalformedURLException ;
44 import java.security.AccessController;
45 import java.security.PrivilegedAction;
46 import java.util.Properties;
47 import java.rmi.server.RMIClassLoader;
49 import com.sun.corba.se.impl.orbutil.GetPropertyAction;
52 /**
53 * Base class from which all RMI-IIOP stubs must inherit.
54 */
55 public abstract class Stub extends ObjectImpl
56 implements java.io.Serializable {
58 private static final long serialVersionUID = 1087775603798577179L;
60 // This can only be set at object construction time (no sync necessary).
61 private transient StubDelegate stubDelegate = null;
62 private static Class stubDelegateClass = null;
63 private static final String StubClassKey = "javax.rmi.CORBA.StubClass";
64 private static final String defaultStubImplName = "com.sun.corba.se.impl.javax.rmi.CORBA.StubDelegateImpl";
66 static {
67 Object stubDelegateInstance = (Object) createDelegateIfSpecified(StubClassKey, defaultStubImplName);
68 if (stubDelegateInstance != null)
69 stubDelegateClass = stubDelegateInstance.getClass();
71 }
74 /**
75 * Returns a hash code value for the object which is the same for all stubs
76 * that represent the same remote object.
77 * @return the hash code value.
78 */
79 public int hashCode() {
81 if (stubDelegate == null) {
82 setDefaultDelegate();
83 }
85 if (stubDelegate != null) {
86 return stubDelegate.hashCode(this);
87 }
89 return 0;
90 }
92 /**
93 * Compares two stubs for equality. Returns <code>true</code> when used to compare stubs
94 * that represent the same remote object, and <code>false</code> otherwise.
95 * @param obj the reference object with which to compare.
96 * @return <code>true</code> if this object is the same as the <code>obj</code>
97 * argument; <code>false</code> otherwise.
98 */
99 public boolean equals(java.lang.Object obj) {
101 if (stubDelegate == null) {
102 setDefaultDelegate();
103 }
105 if (stubDelegate != null) {
106 return stubDelegate.equals(this, obj);
107 }
109 return false;
110 }
112 /**
113 * Returns a string representation of this stub. Returns the same string
114 * for all stubs that represent the same remote object.
115 * @return a string representation of this stub.
116 */
117 public String toString() {
120 if (stubDelegate == null) {
121 setDefaultDelegate();
122 }
124 String ior;
125 if (stubDelegate != null) {
126 ior = stubDelegate.toString(this);
127 if (ior == null) {
128 return super.toString();
129 } else {
130 return ior;
131 }
132 }
133 return super.toString();
134 }
136 /**
137 * Connects this stub to an ORB. Required after the stub is deserialized
138 * but not after it is demarshalled by an ORB stream. If an unconnected
139 * stub is passed to an ORB stream for marshalling, it is implicitly
140 * connected to that ORB. Application code should not call this method
141 * directly, but should call the portable wrapper method
142 * {@link javax.rmi.PortableRemoteObject#connect}.
143 * @param orb the ORB to connect to.
144 * @exception RemoteException if the stub is already connected to a different
145 * ORB, or if the stub does not represent an exported remote or local object.
146 */
147 public void connect(ORB orb) throws RemoteException {
149 if (stubDelegate == null) {
150 setDefaultDelegate();
151 }
153 if (stubDelegate != null) {
154 stubDelegate.connect(this, orb);
155 }
157 }
159 /**
160 * Serialization method to restore the IOR state.
161 */
162 private void readObject(java.io.ObjectInputStream stream)
163 throws IOException, ClassNotFoundException {
165 if (stubDelegate == null) {
166 setDefaultDelegate();
167 }
169 if (stubDelegate != null) {
170 stubDelegate.readObject(this, stream);
171 }
173 }
175 /**
176 * Serialization method to save the IOR state.
177 * @serialData The length of the IOR type ID (int), followed by the IOR type ID
178 * (byte array encoded using ISO8859-1), followed by the number of IOR profiles
179 * (int), followed by the IOR profiles. Each IOR profile is written as a
180 * profile tag (int), followed by the length of the profile data (int), followed
181 * by the profile data (byte array).
182 */
183 private void writeObject(java.io.ObjectOutputStream stream) throws IOException {
185 if (stubDelegate == null) {
186 setDefaultDelegate();
187 }
189 if (stubDelegate != null) {
190 stubDelegate.writeObject(this, stream);
191 }
192 }
194 private void setDefaultDelegate() {
195 if (stubDelegateClass != null) {
196 try {
197 stubDelegate = (javax.rmi.CORBA.StubDelegate) stubDelegateClass.newInstance();
198 } catch (Exception ex) {
199 // what kind of exception to throw
200 // delegate not set therefore it is null and will return default
201 // values
202 }
203 }
204 }
206 // Same code as in PortableRemoteObject. Can not be shared because they
207 // are in different packages and the visibility needs to be package for
208 // security reasons. If you know a better solution how to share this code
209 // then remove it from PortableRemoteObject. Also in Util.java
210 private static Object createDelegateIfSpecified(String classKey, String defaultClassName) {
211 String className = (String)
212 AccessController.doPrivileged(new GetPropertyAction(classKey));
213 if (className == null) {
214 Properties props = getORBPropertiesFile();
215 if (props != null) {
216 className = props.getProperty(classKey);
217 }
218 }
220 if (className == null) {
221 className = defaultClassName;
222 }
224 try {
225 return loadDelegateClass(className).newInstance();
226 } catch (ClassNotFoundException ex) {
227 INITIALIZE exc = new INITIALIZE( "Cannot instantiate " + className);
228 exc.initCause( ex ) ;
229 throw exc ;
230 } catch (Exception ex) {
231 INITIALIZE exc = new INITIALIZE( "Error while instantiating" + className);
232 exc.initCause( ex ) ;
233 throw exc ;
234 }
236 }
238 private static Class loadDelegateClass( String className ) throws ClassNotFoundException
239 {
240 try {
241 ClassLoader loader = Thread.currentThread().getContextClassLoader();
242 return Class.forName(className, false, loader);
243 } catch (ClassNotFoundException e) {
244 // ignore, then try RMIClassLoader
245 }
247 try {
248 return RMIClassLoader.loadClass(className);
249 } catch (MalformedURLException e) {
250 String msg = "Could not load " + className + ": " + e.toString();
251 ClassNotFoundException exc = new ClassNotFoundException( msg ) ;
252 throw exc ;
253 }
254 }
256 /**
257 * Load the orb.properties file.
258 */
259 private static Properties getORBPropertiesFile () {
260 return (Properties) AccessController.doPrivileged(new GetORBPropertiesFileAction());
261 }
263 }