KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > rmi > multi > JacORBPRODelegate


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

25 package org.objectweb.carol.rmi.multi;
26
27 import java.rmi.NoSuchObjectException JavaDoc;
28 import java.rmi.Remote JavaDoc;
29 import java.rmi.RemoteException JavaDoc;
30
31 import javax.rmi.CORBA.PortableRemoteObjectDelegate JavaDoc;
32
33 import org.objectweb.carol.util.configuration.TraceCarol;
34
35
36 /**
37  * TODO : Use the same class for all JVM (maybe Classpath implementation ?)
38  * @author Florent Benoit
39  */

40 public class JacORBPRODelegate implements PortableRemoteObjectDelegate JavaDoc {
41
42     /**
43      * Used by Sun 1.4 and Bea 1.4
44      */

45     private static final String JavaDoc SUN_JDK14_CLASS = "com.sun.corba.se.internal.javax.rmi.PortableRemoteObject";
46
47     /**
48      * Used by IBM 1.4 JDK
49      */

50     private static final String JavaDoc IBM_JDK14_CLASS = "com.ibm.CORBA.iiop.PortableRemoteObject";
51
52     /**
53      * Used by Sun 5.0 JDK
54      */

55     private static final String JavaDoc SUN_JDK50_CLASS = "com.sun.corba.se.impl.javax.rmi.PortableRemoteObject";
56
57     /**
58      * Default JacORB class is the other classes cannot be loaded
59      */

60     private static final String JavaDoc DEFAULT_JACORB_CLASS = "org.jacorb.orb.rmi.PortableRemoteObjectDelegateImpl";
61
62
63     /**
64      * List of classes by order (try first, if not present, try next and etc.)
65      */

66     private static final String JavaDoc[] DELEGATE_CLASSES = new String JavaDoc[] {SUN_JDK14_CLASS, IBM_JDK14_CLASS, SUN_JDK50_CLASS };
67
68
69     /**
70      * internal delegate object
71      */

72     private static PortableRemoteObjectDelegate JavaDoc delegate = null;
73
74     /**
75      * Makes a server object ready to receive remote calls. Note
76      * that subclasses of PortableRemoteObject do not need to call this
77      * method, as it is called by the constructor.
78      * @param obj the server object to export.
79      * @exception RemoteException if export fails.
80      */

81     public void exportObject(Remote JavaDoc obj) throws RemoteException JavaDoc {
82         getDelegate().exportObject(obj);
83
84     }
85
86     /**
87      * Deregisters a server object from the runtime, allowing the object to become
88      * available for garbage collection.
89      * @param obj the object to unexport.
90      * @exception NoSuchObjectException if the remote object is not
91      * currently exported.
92      */

93     public void unexportObject(Remote JavaDoc obj) throws NoSuchObjectException JavaDoc {
94         getDelegate().unexportObject(obj);
95
96     }
97
98     /**
99      * Returns a stub for the given server object.
100      * @param obj the server object for which a stub is required. Must either be a subclass
101      * of PortableRemoteObject or have been previously the target of a call to
102      * {@link #exportObject}.
103      * @return the most derived stub for the object.
104      * @exception NoSuchObjectException if a stub cannot be located for the given server object.
105      */

106     public Remote JavaDoc toStub(Remote JavaDoc obj) throws NoSuchObjectException JavaDoc {
107         return getDelegate().toStub(obj);
108     }
109
110     /**
111      * Makes a Remote object ready for remote communication. This normally
112      * happens implicitly when the object is sent or received as an argument
113      * on a remote method call, but in some circumstances it is useful to
114      * perform this action by making an explicit call. See the
115      * {@link Stub#connect} method for more information.
116      * @param target the object to connect.
117      * @param source a previously connected object.
118      * @throws RemoteException if <code>source</code> is not connected
119      * or if <code>target</code> is already connected to a different ORB than
120      * <code>source</code>.
121      */

122     public void connect(Remote JavaDoc target, Remote JavaDoc source) throws RemoteException JavaDoc {
123         getDelegate().connect(target, source);
124
125     }
126
127     /**
128      * Checks to ensure that an object of a remote or abstract interface type
129      * can be cast to a desired type.
130      * @param narrowFrom the object to check.
131      * @param narrowTo the desired type.
132      * @return an object which can be cast to the desired type.
133      * @throws ClassCastException if narrowFrom cannot be cast to narrowTo.
134      */

135     public Object JavaDoc narrow(Object JavaDoc narrowFrom, Class JavaDoc narrowTo) throws ClassCastException JavaDoc {
136         return getDelegate().narrow(narrowFrom, narrowTo);
137     }
138
139
140     /**
141      * Instantiate the delegate object by trying the different JVM classes
142      * or use the default class if none is found.
143      * Once the class is loaded, return always the same object.
144      * @return the delegate portable remote object
145      */

146     private PortableRemoteObjectDelegate JavaDoc getDelegate() {
147         // delegate already loaded
148
if (delegate != null) {
149             return delegate;
150         }
151
152         Class JavaDoc clazz = null;
153         // use thread classloader
154
ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
155
156         // Need to find one class present in current JVM
157

158         boolean classFound = false;
159         int i = 0;
160         while (!classFound && i < DELEGATE_CLASSES.length) {
161             // get the classname
162
String JavaDoc cls = DELEGATE_CLASSES[i];
163
164             // debug trace
165
if (TraceCarol.isDebugCarol()) {
166                 TraceCarol.debugCarol("Trying with class '" + cls + "'.");
167             }
168
169             try {
170                 // Try to load the class
171
clazz = cl.loadClass(cls);
172                 if (TraceCarol.isDebugCarol()) {
173                     TraceCarol.debugCarol("Class found, Use as prodelegate class : '" + cls + "'.");
174                 }
175                 // class is available, the class is found
176
classFound = true;
177             } catch (ClassNotFoundException JavaDoc cnfesun) {
178                 // class not available, try with next one if available
179
if (TraceCarol.isDebugCarol()) {
180                     TraceCarol.debugCarol("Class '" + cls + "' not available.");
181                 }
182             }
183
184             // increment item
185
i++;
186         }
187
188         // No class was found, use default one
189
if (!classFound) {
190             try {
191                 clazz = cl.loadClass(DEFAULT_JACORB_CLASS);
192             } catch (ClassNotFoundException JavaDoc cnfejacorb) {
193                 throw new IllegalArgumentException JavaDoc("Could not load default class '" + DEFAULT_JACORB_CLASS + "' :" + cnfejacorb.getMessage());
194             }
195             TraceCarol.infoCarol("Using default Jacorb delegate class and not the JVM class as JVM class was not found. It may fail in some cases.");
196         }
197
198
199         // new instance of the object
200
Object JavaDoc o = null;
201         try {
202             o = clazz.newInstance();
203         } catch (IllegalAccessException JavaDoc iae) {
204             throw new IllegalArgumentException JavaDoc("Cannot make instance of class : '" + clazz + "' : " + iae.getMessage());
205         } catch (InstantiationException JavaDoc ie) {
206             throw new IllegalArgumentException JavaDoc("Cannot make instance of class : '" + clazz + "' : " + ie.getMessage());
207         }
208
209         // then cast as PortableRemoteObjectDelegate
210
if (o instanceof PortableRemoteObjectDelegate JavaDoc) {
211             delegate = (PortableRemoteObjectDelegate JavaDoc) o;
212             return delegate;
213         } else {
214             throw new IllegalArgumentException JavaDoc("Object '" + o + "' is not an instance of PortableRemoteObjectDelegate");
215         }
216
217     }
218
219
220 }
Popular Tags