KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > component > smartclient > spi > ProDelegate


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
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:$
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.component.smartclient.spi;
27
28 import java.rmi.NoSuchObjectException JavaDoc;
29 import java.rmi.Remote JavaDoc;
30 import java.rmi.RemoteException JavaDoc;
31 import java.util.logging.Level JavaDoc;
32 import java.util.logging.Logger JavaDoc;
33
34 import javax.rmi.CORBA.PortableRemoteObjectDelegate JavaDoc;
35
36 /**
37  * Implementation of the prodelegate class that will redirect to the carol class
38  * by using the correct classloader mechanism.
39  * @author Florent Benoit
40  */

41 public class ProDelegate implements PortableRemoteObjectDelegate JavaDoc {
42
43     /**
44      * Use the JDK logger (to avoid any dependency).
45      */

46     private static Logger JavaDoc logger = Logger.getLogger(ProDelegate.class.getName());
47
48     /**
49      * Carol ProDelegate class.
50      */

51     private static final String JavaDoc CAROL_PRO_DELEGATE = "org.objectweb.carol.rmi.multi.MultiPRODelegate";
52
53     /**
54      * Reference to the classloader to use. This classloader will be set by the
55      * EasyBeans smartclient factory.
56      */

57     private static ClassLoader JavaDoc classLoader;
58
59     /**
60      * Wrapped reference of the carol PortableRemoteObjectDelegate class.
61      */

62     private PortableRemoteObjectDelegate JavaDoc wrapped = null;
63
64     /**
65      * Gets the Carol wrapping class.
66      */

67     public ProDelegate() {
68         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
69         Thread.currentThread().setContextClassLoader(classLoader);
70         try {
71             Class JavaDoc clazz = null;
72             try {
73                 clazz = classLoader.loadClass(CAROL_PRO_DELEGATE);
74             } catch (ClassNotFoundException JavaDoc e) {
75                 throw new IllegalStateException JavaDoc("Cannot find the '" + CAROL_PRO_DELEGATE + "' class.", e);
76             }
77             try {
78                 wrapped = (PortableRemoteObjectDelegate JavaDoc) clazz.newInstance();
79             } catch (InstantiationException JavaDoc e) {
80                 throw new IllegalStateException JavaDoc("Cannot build an instance of the '" + CAROL_PRO_DELEGATE + "' class.", e);
81             } catch (IllegalAccessException JavaDoc e) {
82                 throw new IllegalStateException JavaDoc("Cannot build an instance of the '" + CAROL_PRO_DELEGATE + "' class.", e);
83             }
84         } finally {
85             Thread.currentThread().setContextClassLoader(old);
86         }
87     }
88
89     /**
90      * Check if there is a classloader.
91      */

92     private void check() {
93         if (classLoader == null) {
94             throw new IllegalStateException JavaDoc("No classloader was set previously. Invalid call.");
95         }
96
97     }
98
99     /**
100      * Makes a Remote object ready for remote communication. This normally
101      * happens implicitly when the object is sent or received as an argument on
102      * a remote method call, but in some circumstances it is useful to perform
103      * this action by making an explicit call.
104      * @param target the object to connect.
105      * @param source a previously connected object.
106      * @throws RemoteException if <code>source</code> is not connected or if
107      * <code>target</code> is already connected to a different ORB
108      * than <code>source</code>.
109      */

110     public void connect(final Remote JavaDoc target, final Remote JavaDoc source) throws RemoteException JavaDoc {
111         check();
112         if (logger.isLoggable(Level.FINE)) {
113             logger.log(Level.FINE, "connect '" + target + "' with '" + source + "'.");
114         }
115         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
116         Thread.currentThread().setContextClassLoader(classLoader);
117         try {
118             wrapped.connect(target, source);
119         } finally {
120             Thread.currentThread().setContextClassLoader(old);
121         }
122
123     }
124
125     /**
126      * Makes a server object ready to receive remote calls. Note that subclasses
127      * of PortableRemoteObject do not need to call this method, as it is called
128      * by the constructor.
129      * @param obj the server object to export.
130      * @exception RemoteException if export fails.
131      */

132     public void exportObject(final Remote JavaDoc obj) throws RemoteException JavaDoc {
133         check();
134         if (logger.isLoggable(Level.FINE)) {
135             logger.log(Level.FINE, "exportObject '" + obj + "'.");
136         }
137         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
138         Thread.currentThread().setContextClassLoader(classLoader);
139         try {
140             wrapped.exportObject(obj);
141         } finally {
142             Thread.currentThread().setContextClassLoader(old);
143         }
144
145     }
146
147     /**
148      * Checks to ensure that an object of a remote or abstract interface type
149      * can be cast to a desired type.
150      * @param narrowFrom the object to check.
151      * @param narrowTo the desired type.
152      * @return an object which can be cast to the desired type.
153      * @throws ClassCastException if narrowFrom cannot be cast to narrowTo.
154      */

155     public Object JavaDoc narrow(final Object JavaDoc narrowFrom, final Class JavaDoc narrowTo) throws ClassCastException JavaDoc {
156         check();
157         if (logger.isLoggable(Level.FINE)) {
158             logger.log(Level.FINE, "Narrowing '" + narrowFrom + "' to '" + narrowTo + "'.");
159         }
160         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
161         Thread.currentThread().setContextClassLoader(classLoader);
162         try {
163             return wrapped.narrow(narrowFrom, narrowTo);
164         } finally {
165             Thread.currentThread().setContextClassLoader(old);
166         }
167     }
168
169     /**
170      * Returns a stub for the given server object.
171      * @param obj the server object for which a stub is required. Must either be
172      * a subclass of PortableRemoteObject or have been previously the
173      * target of a call to {@link #exportObject}.
174      * @return the most derived stub for the object.
175      * @exception NoSuchObjectException if a stub cannot be located for the
176      * given server object.
177      */

178     public Remote JavaDoc toStub(final Remote JavaDoc obj) throws NoSuchObjectException JavaDoc {
179         check();
180         if (logger.isLoggable(Level.FINE)) {
181             logger.log(Level.FINE, "toStub '" + obj + "'.");
182         }
183         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
184         Thread.currentThread().setContextClassLoader(classLoader);
185         try {
186             return wrapped.toStub(obj);
187         } finally {
188             Thread.currentThread().setContextClassLoader(old);
189         }
190     }
191
192     /**
193      * Deregisters a server object from the runtime, allowing the object to
194      * become available for garbage collection.
195      * @param obj the object to unexport.
196      * @exception NoSuchObjectException if the remote object is not currently
197      * exported.
198      */

199     public void unexportObject(final Remote JavaDoc obj) throws NoSuchObjectException JavaDoc {
200         check();
201         if (logger.isLoggable(Level.FINE)) {
202             logger.log(Level.FINE, "unexportObject '" + obj + "'.");
203         }
204         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
205         Thread.currentThread().setContextClassLoader(classLoader);
206         try {
207             wrapped.unexportObject(obj);
208         } finally {
209             Thread.currentThread().setContextClassLoader(old);
210         }
211     }
212
213     /**
214      * Sets the ClassLoader to use.
215      * @param cl the ClassLoader to use.
216      */

217     public static void setClassLoader(final ClassLoader JavaDoc cl) {
218         classLoader = cl;
219     }
220
221 }
222
Popular Tags