KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > util > delegate > UtilDelegateImpl


1 /**
2  * Copyright (C) 2005 - ObjectWeb (http://www.objectweb.org)
3  *
4  * CAROL: Common Architecture for RMI ObjectWeb Layer
5  *
6  * This library is developed inside the ObjectWeb Consortium,
7  * http://www.objectweb.org
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22  * USA
23  *
24  * --------------------------------------------------------------------------
25  * $Id: UtilDelegateImpl.java,v 1.2 2005/04/20 16:36:16 benoitf Exp $
26  * --------------------------------------------------------------------------
27  */

28
29 package org.objectweb.carol.util.delegate;
30
31 import java.rmi.NoSuchObjectException JavaDoc;
32 import java.rmi.Remote JavaDoc;
33 import java.rmi.RemoteException JavaDoc;
34 import java.util.Arrays JavaDoc;
35 import java.util.logging.Level JavaDoc;
36 import java.util.logging.Logger JavaDoc;
37
38 import javax.rmi.CORBA.Stub JavaDoc;
39 import javax.rmi.CORBA.Tie JavaDoc;
40 import javax.rmi.CORBA.UtilDelegate JavaDoc;
41 import javax.rmi.CORBA.ValueHandler JavaDoc;
42
43 import org.omg.CORBA.ORB JavaDoc;
44 import org.omg.CORBA.SystemException JavaDoc;
45 import org.omg.CORBA.portable.InputStream JavaDoc;
46 import org.omg.CORBA.portable.OutputStream JavaDoc;
47
48
49 /**
50  * This class smoothes out incompatibilities between JDKs provided by different
51  * vendors.
52  * @author Vadim Nasardinov (vadimn@redhat.com)
53  * @since 2005-04-12
54  */

55 public final class UtilDelegateImpl implements UtilDelegate JavaDoc {
56
57     /**
58      * Logger
59      */

60     private static Logger JavaDoc logger = Logger.getLogger(UtilDelegateImpl.class.getName());
61
62     /**
63      * Wrapper on UtilDelegate implementation of the JVM used at runtime
64      */

65     private static UtilDelegate JavaDoc proxied = null;
66
67     /**
68      * Default constructor.<br>
69      * Build a proxied object which is map to the JVM implementation of
70      * UtilDelegate interface.
71      */

72     public UtilDelegateImpl() {
73         String JavaDoc[] vendorDelegates = new String JavaDoc[] {
74                 "com.sun.corba.se.internal.POA.ShutdownUtilDelegate", // Sun/BEA JDK 1.4
75
"com.sun.corba.se.impl.javax.rmi.CORBA.Util", // Sun JDK 1.5
76
"com.ibm.CORBA.iiop.UtilDelegateImpl"}; // IBM JDK 1.4
77

78         Class JavaDoc clz = null;
79         for (int ii = 0; ii < vendorDelegates.length; ii++) {
80             try {
81                 clz = Class.forName(vendorDelegates[ii]);
82                 break;
83             } catch (ClassNotFoundException JavaDoc ex) {
84                 if (logger.isLoggable(Level.FINEST)) {
85                     logger.log(Level.FINEST, "The class '" + vendorDelegates[ii] + "' was not found.", ex);
86                 }
87             }
88         }
89
90         if (clz == null) {
91             throw new RuntimeException JavaDoc("Couldn't load any of these: " + Arrays.asList(vendorDelegates));
92         }
93
94         try {
95             proxied = (UtilDelegate JavaDoc) clz.newInstance();
96         } catch (InstantiationException JavaDoc ex) {
97             throw new RuntimeException JavaDoc(clz.getName(), ex);
98         } catch (IllegalAccessException JavaDoc ex) {
99             throw new RuntimeException JavaDoc(clz.getName() + " does not have a public constructor", ex);
100         }
101     }
102
103     /**
104      * Copies or connects an object. Used by local stubs to copy an actual
105      * parameter, result object, or exception.
106      * @param obj the object to copy.
107      * @param orb the ORB.
108      * @return the copy or connected object.
109      * @exception RemoteException if the object could not be copied or
110      * connected.
111      */

112     public Object JavaDoc copyObject(Object JavaDoc obj, ORB JavaDoc orb) throws RemoteException JavaDoc {
113         return proxied.copyObject(obj, orb);
114     }
115
116     /**
117      * Copies or connects an array of objects. Used by local stubs to copy any
118      * number of actual parameters, preserving sharing across parameters as
119      * necessary to support RMI semantics.
120      * @param objs the objects to copy or connect.
121      * @param orb the ORB.
122      * @return the copied or connected objects.
123      * @exception RemoteException if any object could not be copied or
124      * connected.
125      */

126     public Object JavaDoc[] copyObjects(Object JavaDoc[] objs, ORB JavaDoc orb) throws RemoteException JavaDoc {
127         return proxied.copyObjects(objs, orb);
128     }
129
130     /**
131      * Returns a singleton instance of a class that implements the interface.
132      * @return a class which implements the ValueHandler interface.
133      */

134     public ValueHandler JavaDoc createValueHandler() {
135         return proxied.createValueHandler();
136     }
137
138     /**
139      * Returns the codebase, if any, for the given class.
140      * @param clz the class to get a codebase for.
141      * @return a space-separated list of URLs, or null.
142      */

143     public String JavaDoc getCodebase(Class JavaDoc clz) {
144         return proxied.getCodebase(clz);
145     }
146
147     /**
148      * Returns the tie (if any) for a given target object.
149      * @param target the given target from which we want the tie
150      * @return the tie or null if no tie is registered for the given target.
151      */

152     public Tie JavaDoc getTie(Remote JavaDoc target) {
153         return proxied.getTie(target);
154     }
155
156     /**
157      * The <tt>_is_local()</tt> method is provided so that stubs may determine
158      * if a particular object is implemented by a local servant and hence local
159      * invocation APIs may be used.
160      * @param stub the stub to test.
161      * @return true if the servant incarnating the object is located in the same
162      * process as the stub and they both share the same ORB instance.
163      * The <tt>_is_local()</tt> method returns false otherwise. The
164      * default behavior of <tt>_is_local()</tt> is to return false.
165      * @throws RemoteException The Java to IDL specification does not specify
166      * the conditions that cause a <tt>RemoteException</tt> to be
167      * thrown.
168      */

169     public boolean isLocal(Stub JavaDoc stub) throws RemoteException JavaDoc {
170         return proxied.isLocal(stub);
171     }
172
173     /**
174      * Returns a class instance for the specified class. This provides an
175      * implementation of {@link Util#loadClass(String,String,ClassLoader)} that
176      * eliminates an incompatibility between Sun's and IBM's interpretation of
177      * Section 1.4.6 <em>Locating Stubs and Ties</em> of <a
178      * HREF="http://www.omg.org/cgi-bin/doc?formal/03-09-04">Java to IDL
179      * Language Mapping, v1.3</a>.
180      * @param className the name of the class.
181      * @param remoteCodebase a space-separated list of URLs at which the class
182      * might be found. May be null.
183      * @param loader a <tt>ClassLoader</tt> that may be used to load the class
184      * if all other methods fail.
185      * @return the <code>Class</code> object representing the loaded class.
186      * @exception ClassNotFoundException if class cannot be loaded.
187      */

188     public Class JavaDoc loadClass(String JavaDoc className, String JavaDoc remoteCodebase, ClassLoader JavaDoc loader) throws ClassNotFoundException JavaDoc {
189
190         final ClassNotFoundException JavaDoc cnfe;
191         try {
192             return proxied.loadClass(className, remoteCodebase, loader);
193         } catch (ClassNotFoundException JavaDoc ex) {
194             cnfe = ex;
195         }
196
197         if (className.endsWith("Tie")) {
198             final String JavaDoc prefix = "org.omg.stub.";
199             final String JavaDoc mangledName;
200             if (className.startsWith(prefix)) {
201                 // IBM interprets Section 1.4.6 to mean that ties should NOT be
202
// prefixed with "org.omg.stub."
203
mangledName = className.substring(prefix.length());
204             } else {
205                 // Sun hasn't read Section 1.4.6. Its IIOP implementation
206
// prefixes ties with "org.omg.stub." the same way that stubs
207
// are prefixed.
208
mangledName = prefix + className;
209             }
210             return proxied.loadClass(mangledName, remoteCodebase, loader);
211         }
212         throw cnfe;
213     }
214
215     /**
216      * Maps a SystemException to a RemoteException.
217      * @param ex the SystemException to map.
218      * @return the mapped exception.
219      */

220     public RemoteException JavaDoc mapSystemException(SystemException JavaDoc ex) {
221         return proxied.mapSystemException(ex);
222     }
223
224     /**
225      * Reads a java.lang.Object as a CORBA any.
226      * @param in the stream from which to read the any.
227      * @return the object read from the stream.
228      */

229     public Object JavaDoc readAny(InputStream JavaDoc in) {
230         return proxied.readAny(in);
231     }
232
233     /**
234      * Registers a target for a tie. Adds the tie to an internal table and calls
235      * {@link Tie#setTarget} on the tie object.
236      * @param tie the tie to register.
237      * @param target the target for the tie.
238      */

239     public void registerTarget(Tie JavaDoc tie, Remote JavaDoc target) {
240         proxied.registerTarget(tie, target);
241     }
242
243     /**
244      * Removes the associated tie from an internal table and calls {@link
245      * Tie#deactivate} to deactivate the object.
246      * @param target the object to unexport.
247      * @throws NoSuchObjectException if the object cannot be unexported
248      */

249     public void unexportObject(Remote JavaDoc target) throws NoSuchObjectException JavaDoc {
250         proxied.unexportObject(target);
251     }
252
253     /**
254      * Wraps an exception thrown by an implementation method. It returns the
255      * corresponding client-side exception.
256      * @param orig the exception to wrap.
257      * @return the wrapped exception.
258      */

259     public RemoteException JavaDoc wrapException(Throwable JavaDoc orig) {
260         return proxied.wrapException(orig);
261     }
262
263     /**
264      * Writes a java.lang.Object as either a value or a CORBA Object. If
265      * <code>obj</code> is a value object or a stub object, it is written to
266      * <code>out.write_abstract_interface(java.lang.Object)</code>. If
267      * <code>obj</code> is an exported RMI-IIOP server object, the tie is
268      * found and wired to <code>obj</code>, then written to
269      * <code>out.write_abstract_interface(java.lang.Object)</code>.
270      * @param out the stream in which to write the object.
271      * @param obj the object to write.
272      */

273     public void writeAbstractObject(OutputStream JavaDoc out, Object JavaDoc obj) {
274         proxied.writeAbstractObject(out, obj);
275     }
276
277     /**
278      * Writes any java.lang.Object as a CORBA any.
279      * @param out the stream in which to write the any.
280      * @param obj the object to write as an any.
281      */

282     public void writeAny(OutputStream JavaDoc out, Object JavaDoc obj) {
283         proxied.writeAny(out, obj);
284     }
285
286     /**
287      * Writes a java.lang.Object as a CORBA Object. If <code>obj</code> is an
288      * exported RMI-IIOP server object, the tie is found and wired to
289      * <code>obj</code>, then written to
290      * <code>out.write_Object(org.omg.CORBA.Object)</code>. If
291      * <code>obj</code> is a CORBA Object, it is written to
292      * <code>out.write_Object(org.omg.CORBA.Object)</code>.
293      * @param out the stream in which to write the object.
294      * @param obj the object to write.
295      */

296     public void writeRemoteObject(OutputStream JavaDoc out, Object JavaDoc obj) {
297         proxied.writeRemoteObject(out, obj);
298     }
299 }
300
Popular Tags