KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * Copyright (C) 2002,2005 - INRIA (www.inria.fr)
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: JeremiePRODelegate.java,v 1.18 2005/04/07 15:07:08 benoitf Exp $
26  * --------------------------------------------------------------------------
27  */

28 package org.objectweb.carol.rmi.multi;
29
30 import java.lang.reflect.InvocationTargetException JavaDoc;
31 import java.lang.reflect.Method JavaDoc;
32 import java.rmi.NoSuchObjectException JavaDoc;
33 import java.rmi.Remote JavaDoc;
34 import java.rmi.RemoteException JavaDoc;
35 import java.util.Properties JavaDoc;
36
37 import javax.rmi.CORBA.PortableRemoteObjectDelegate JavaDoc;
38
39 import org.objectweb.carol.rmi.exception.NoSuchObjectExceptionHelper;
40 import org.objectweb.carol.rmi.exception.RemoteExceptionHelper;
41 import org.objectweb.carol.rmi.util.PortNumber;
42 import org.objectweb.carol.util.configuration.CarolDefaultValues;
43 import org.objectweb.carol.util.configuration.ConfigurationRepository;
44
45 /**
46  * class <code>JeremiePRODelegate</code> for the mapping between Jeremie
47  * UnicastRemoteObject and PortableRemoteObject
48  * @author Guillaume Riviere
49  * @author Florent Benoit (exception rethrow)
50  */

51 public class JeremiePRODelegate implements PortableRemoteObjectDelegate JavaDoc {
52
53     /**
54      * Extension of Jeremie Stubs
55      */

56     private static final String JavaDoc JEREMIE_STUB_EXTENSION = "_OWStub";
57
58     /**
59      * port number
60      */

61     private int port;
62
63     /**
64      * UnicastRemoteObjectClass
65      */

66     private static final String JavaDoc JEREMIE_UNICAST_CLASS = "org.objectweb.jeremie.binding.moa.UnicastRemoteObject";
67
68     /**
69      * Instance object for this UnicastRemoteObject
70      */

71     private static Class JavaDoc unicastClass = null;
72
73     /**
74      * Number of protocols
75      */

76     private static int nbProtocols = 0;
77
78     /**
79      * Empty constructor for instanciate this class
80      * @throws ClassNotFoundException if the class JEREMIE_UNICAST_CLASS cannot be loaded
81      */

82     public JeremiePRODelegate() throws ClassNotFoundException JavaDoc {
83         // class for name
84
unicastClass = Thread.currentThread().getContextClassLoader().loadClass(JEREMIE_UNICAST_CLASS);
85         Properties JavaDoc prop = ConfigurationRepository.getProperties();
86         if (prop != null) {
87             String JavaDoc propertyName = CarolDefaultValues.SERVER_JEREMIE_PORT;
88             this.port = PortNumber.strToint(prop.getProperty(propertyName, "0"), propertyName);
89         }
90     }
91
92     /**
93      * Makes a server object ready to receive remote calls. Note
94      * that subclasses of PortableRemoteObject do not need to call this
95      * method, as it is called by the constructor.
96      * @param obj the server object to export.
97      * @exception RemoteException if export fails.
98      */

99     public void exportObject(Remote JavaDoc obj) throws RemoteException JavaDoc {
100         if (!containsJeremieStub(obj)) {
101             return;
102         }
103         try {
104             Method JavaDoc exportO = unicastClass.getMethod("exportObject", new Class JavaDoc[] {Remote JavaDoc.class, Integer.TYPE});
105             exportO.invoke(unicastClass, (new Object JavaDoc[] {obj, new Integer JavaDoc(port)}));
106         } catch (InvocationTargetException JavaDoc e) {
107             throw RemoteExceptionHelper.create(e.getTargetException());
108         } catch (Exception JavaDoc e) {
109             throw new RemoteException JavaDoc("exportObject() method fails on object '" + obj + "'", e);
110         }
111     }
112
113     /**
114      * Deregisters a server object from the runtime, allowing the object to become
115      * available for garbage collection.
116      * @param obj the object to unexport.
117      * @exception NoSuchObjectException if the remote object is not
118      * currently exported.
119      */

120     public void unexportObject(Remote JavaDoc obj) throws NoSuchObjectException JavaDoc {
121         if (!containsJeremieStub(obj)) {
122             return;
123         }
124         try {
125             Method JavaDoc unexportO = unicastClass.getMethod("unexportObject", new Class JavaDoc[] {Remote JavaDoc.class, Boolean.TYPE});
126             unexportO.invoke(unicastClass, (new Object JavaDoc[] {obj, Boolean.TRUE}));
127         } catch (InvocationTargetException JavaDoc e) {
128             throw NoSuchObjectExceptionHelper.create(e.getTargetException());
129         } catch (Exception JavaDoc e) {
130             throw NoSuchObjectExceptionHelper.create("unexportObject() method fails on object '" + obj + "'", e);
131         }
132     }
133
134     /**
135      * Makes a Remote object ready for remote communication. This normally
136      * happens implicitly when the object is sent or received as an argument
137      * on a remote method call, but in some circumstances it is useful to
138      * perform this action by making an explicit call. See the
139      * {@link Stub#connect} method for more information.
140      * @param target the object to connect.
141      * @param source a previously connected object.
142      * @throws RemoteException if <code>source</code> is not connected
143      * or if <code>target</code> is already connected to a different ORB than
144      * <code>source</code>.
145      */

146     public void connect(Remote JavaDoc target, Remote JavaDoc source) throws RemoteException JavaDoc {
147         // do nothing
148
}
149
150     /**
151      * Checks to ensure that an object of a remote or abstract interface type
152      * can be cast to a desired type.
153      * @param narrowFrom the object to check.
154      * @param narrowTo the desired type.
155      * @return an object which can be cast to the desired type.
156      * @throws ClassCastException if narrowFrom cannot be cast to narrowTo.
157      */

158     public Object JavaDoc narrow(Object JavaDoc narrowFrom, Class JavaDoc narrowTo) throws ClassCastException JavaDoc {
159         if (narrowTo.isAssignableFrom(narrowFrom.getClass())) {
160             return narrowFrom;
161         } else {
162             throw new ClassCastException JavaDoc("Cannot cast '" + narrowFrom.getClass().getName() + "' in '" + narrowTo.getName() + "'.");
163         }
164     }
165
166     /**
167      * Returns a stub for the given server object.
168      * @param obj the server object for which a stub is required. Must either be a subclass
169      * of PortableRemoteObject or have been previously the target of a call to
170      * {@link #exportObject}.
171      * @return the most derived stub for the object.
172      * @exception NoSuchObjectException if a stub cannot be located for the given server object.
173      */

174     public Remote JavaDoc toStub(Remote JavaDoc obj) throws NoSuchObjectException JavaDoc {
175         try {
176             Method JavaDoc exportO = unicastClass.getMethod("toStub", new Class JavaDoc[] {Remote JavaDoc.class});
177             return (Remote JavaDoc) exportO.invoke(unicastClass, (new Object JavaDoc[] {obj}));
178         } catch (InvocationTargetException JavaDoc e) {
179             throw NoSuchObjectExceptionHelper.create(e.getTargetException());
180         } catch (Exception JavaDoc e) {
181             throw NoSuchObjectExceptionHelper.create("toStub() method fails on object '" + obj + "'", e);
182         }
183     }
184
185     /**
186      * Check if the remote object contains the Jeremie stub in the case of
187      * multiprotocols. It can happen that a remote object calls the methods of
188      * this delegate object, For example if we try to bind a iiop object in an
189      * iiop context, this delegate object is called anyway.
190      * @param r remote object
191      * @return true if the jeremie stubs are found, else false
192      */

193     private boolean containsJeremieStub(Remote JavaDoc r) {
194
195         if (nbProtocols == 0) {
196             // need to init number of protocols
197
nbProtocols = ConfigurationRepository.getActiveConfigurationsNumber();
198         }
199
200         // No check on single protocol
201
if (nbProtocols == 1) {
202             return true;
203         }
204
205         // Construct name of a jeremie stub of a remote object
206
String JavaDoc stubName = r.getClass().getName() + JEREMIE_STUB_EXTENSION;
207
208         // Convert . into / as we check the resource name
209
String JavaDoc resourceName = stubName.replace('.', '/');
210
211         // suffix it by the classname
212
resourceName += ".class";
213
214         // return true if the resource is found
215
return (Thread.currentThread().getContextClassLoader().getResource(resourceName) != null);
216
217     }
218
219 }
Popular Tags