KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > body > proxy > UniversalBodyProxy


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31 package org.objectweb.proactive.core.body.proxy;
32
33 import org.apache.log4j.Logger;
34
35 import org.objectweb.proactive.Active;
36 import org.objectweb.proactive.Body;
37 import org.objectweb.proactive.core.Constants;
38 import org.objectweb.proactive.core.ProActiveException;
39 import org.objectweb.proactive.core.body.LocalBodyStore;
40 import org.objectweb.proactive.core.body.MetaObjectFactory;
41 import org.objectweb.proactive.core.body.UniversalBody;
42 import org.objectweb.proactive.core.body.future.Future;
43 import org.objectweb.proactive.core.mop.ConstructorCall;
44 import org.objectweb.proactive.core.mop.ConstructorCallExecutionFailedException;
45 import org.objectweb.proactive.core.mop.ConstructorCallImpl;
46 import org.objectweb.proactive.core.mop.MethodCall;
47 import org.objectweb.proactive.core.node.Node;
48 import org.objectweb.proactive.core.node.NodeException;
49 import org.objectweb.proactive.core.node.NodeFactory;
50 import org.objectweb.proactive.core.runtime.ProActiveRuntime;
51 import org.objectweb.proactive.core.runtime.ProActiveRuntimeImpl;
52 import org.objectweb.proactive.ext.security.RenegotiateSessionException;
53
54 import java.lang.reflect.Constructor JavaDoc;
55 import java.lang.reflect.InvocationTargetException JavaDoc;
56
57
58 public class UniversalBodyProxy extends AbstractBodyProxy
59     implements java.io.Serializable JavaDoc {
60     protected static Logger logger = Logger.getLogger(UniversalBodyProxy.class.getName());
61
62     // note that we do not want to serialize this member but rather handle
63
// the serialization by ourselve
64
protected transient UniversalBody universalBody;
65     protected transient boolean isLocal;
66
67     //
68
// -- CONSTRUCTORS -----------------------------------------------
69
//
70

71     /**
72      * Empty, no args constructor
73      */

74     public UniversalBodyProxy() {
75     }
76
77     /**
78      * Instantiates an object of class BodyProxy, creates a body object
79      * (referenced either via the instance variable <code>localBody</code>
80      * or <code>remoteBody</code>) and passes the ConstructorCall
81      * object <code>c</code> to the body, which will then handle
82      * the creation of the reified object (That's it !).
83      * parameter contains either :
84      * &lt;Node, Active, MetaObjectFactory>
85      * or
86      * &lt;UniversalBody>
87      */

88     public UniversalBodyProxy(ConstructorCall constructorCall,
89         Object JavaDoc[] parameters) throws ProActiveException {
90         Object JavaDoc p0 = parameters[0];
91
92         // Determines whether the body is local or remote
93
if (p0 instanceof UniversalBody) {
94             // This is simple connection to an existant local body
95
this.universalBody = (UniversalBody) p0;
96             this.bodyID = universalBody.getID();
97             isLocal = LocalBodyStore.getInstance().getLocalBody(bodyID) != null;
98             if (logger.isDebugEnabled()) {
99                 //logger.debug("UniversalBodyProxy created from UniversalBody bodyID="+bodyID+" isLocal="+isLocal);
100
}
101         } else {
102             // instantiate the body locally or remotely
103
Class JavaDoc bodyClass = Constants.DEFAULT_BODY_CLASS;
104             Node node = (Node) p0;
105
106             //added lines--------------------------
107
//ProActiveRuntime part = node.getProActiveRuntime();
108
//added lines----------------------------
109
Active activity = (Active) parameters[1];
110             MetaObjectFactory factory = (MetaObjectFactory) parameters[2];
111             String JavaDoc jobID = (String JavaDoc)parameters[3];
112             Class JavaDoc[] argsClass = new Class JavaDoc[] {
113                     ConstructorCall.class, String JavaDoc.class, Active.class,
114                     MetaObjectFactory.class, String JavaDoc.class
115                 };
116             Object JavaDoc[] args = new Object JavaDoc[] {
117                     constructorCall, node.getNodeInformation().getURL(),
118                     activity, factory, jobID
119                 };
120
121             //added lines--------------------------
122
//Object[] args = new Object[] { constructorCall, node.getNodeInformation().getURL(), activity, factory };
123
//added lines--------------------------
124
ConstructorCall bodyConstructorCall = buildBodyConstructorCall(bodyClass,
125                     argsClass, args);
126             if (NodeFactory.isNodeLocal(node)) {
127                 // the node is local
128
//added line -------------------------
129
//if (RuntimeFactory.isRuntimeLocal(part)){
130
//added line -------------------------
131
this.universalBody = createLocalBody(bodyConstructorCall,
132                         constructorCall, node);
133                 isLocal = true;
134             } else {
135                 this.universalBody = createRemoteBody(bodyConstructorCall, node);
136                 //added line -------------------------
137
//this.universalBody = createRemoteBody(bodyConstructorCall, part , node);
138
//added line -------------------------
139
isLocal = false;
140             }
141             this.bodyID = universalBody.getID();
142             if (logger.isDebugEnabled()) {
143                 //logger.debug("UniversalBodyProxy created from constructorCall bodyID="+bodyID+" isLocal="+isLocal);
144
}
145         }
146     }
147
148     //
149
// -- PUBLIC METHODS -----------------------------------------------
150
//
151
public boolean equals(Object JavaDoc o) {
152         if (!(o instanceof UniversalBodyProxy)) {
153             return false;
154         }
155
156         UniversalBodyProxy proxy = (UniversalBodyProxy) o;
157         return universalBody.equals(proxy.universalBody);
158     }
159
160     public int hashCode() {
161         return universalBody.hashCode();
162     }
163
164     //
165
// -- implements BodyProxy interface -----------------------------------------------
166
//
167
public UniversalBody getBody() {
168         return universalBody;
169     }
170
171     //
172
// -- PROTECTED METHODS -----------------------------------------------
173
//
174
protected UniversalBody createLocalBody(
175         ConstructorCall bodyConstructorCall,
176         ConstructorCall reifiedObjectConstructorCall, Node node)
177         throws ProActiveException {
178         try {
179             reifiedObjectConstructorCall.makeDeepCopyOfArguments();
180
181             //The node is local, so is the proActiveRuntime
182
// acessing it direclty avoids to get a copy of the body
183
ProActiveRuntime part = ProActiveRuntimeImpl.getProActiveRuntime();
184
185             //return (UniversalBody) bodyConstructorCall.execute();
186
//---------------------added lines--------------------------
187
// if (logger.isDebugEnabled()) {
188
// logger.debug("LocalBodyProxy created using " + body + " from ConstructorCall");
189
// }
190
return part.createBody(node.getNodeInformation().getName(),
191                 bodyConstructorCall, true);
192             //---------------------added lines------------------------------
193
} catch (ConstructorCallExecutionFailedException e) {
194             throw new ProActiveException(e);
195         } catch (InvocationTargetException JavaDoc e) {
196             throw new ProActiveException(e.getTargetException());
197         } catch (java.io.IOException JavaDoc e) {
198             throw new ProActiveException("Error in the copy of the arguments of the constructor",
199                 e);
200         }
201     }
202
203     protected UniversalBody createRemoteBody(
204         ConstructorCall bodyConstructorCall, Node node)
205         throws ProActiveException {
206         try {
207             ProActiveRuntime part = node.getProActiveRuntime();
208
209             //if (logger.isDebugEnabled()) {
210
// //logger.debug("UniversalBodyProxy.createRemoteBody bodyClass="+bodyClass+" node="+node);
211
//}
212
//return node.createBody(bodyConstructorCall);
213
//--------------added lines
214
if (logger.isDebugEnabled()) {
215                 logger.debug("RemoteBodyProxy created bodyID=" + bodyID +
216                     " from ConstructorCall");
217             }
218             return part.createBody(node.getNodeInformation().getName(),
219                 bodyConstructorCall, false);
220             //--------------added lines
221
} catch (ConstructorCallExecutionFailedException e) {
222             throw new ProActiveException(e);
223         } catch (java.lang.reflect.InvocationTargetException JavaDoc e) {
224             throw new ProActiveException(e);
225         } catch (NodeException e) {
226             throw new ProActiveException(e);
227         }
228     }
229
230     protected void sendRequest(MethodCall methodCall, Future future)
231         throws java.io.IOException JavaDoc , RenegotiateSessionException{
232         // Determines the body that is at the root of the subsystem from which the
233
// call was sent.
234
// It is always true that the body that issued the request (and not the body
235
// that is the target of the call) and this BodyProxy are in the same
236
// address space because being a local representative for something remote
237
// is what the proxy is all about. This is why we know that the table that
238
// can be accessed by using a static methode has this information.
239
sendRequest(methodCall, future,
240             LocalBodyStore.getInstance().getCurrentThreadBody());
241     }
242
243     protected void sendRequest(MethodCall methodCall, Future future,
244         Body sourceBody) throws java.io.IOException JavaDoc, RenegotiateSessionException {
245         // Now we check whether the reference to the remoteBody has changed i.e the body has migrated
246
// Maybe we could use some optimisation here
247
UniversalBody newBody = sourceBody.checkNewLocation(universalBody.getID());
248         if (newBody != null) {
249             universalBody = newBody;
250             isLocal = LocalBodyStore.getInstance().getLocalBody(bodyID) != null;
251         }
252         sourceBody.getFuturePool().registerDestination(universalBody);
253         if (isLocal) {
254             // Replaces the effective arguments with a deep copy
255
// Only do this if the body is local
256
// For remote bodies, this is automatically handled by the RMI stub
257
methodCall.makeDeepCopyOfArguments();
258         }
259         sendRequestInternal(methodCall, future, sourceBody);
260         sourceBody.getFuturePool().removeDestination();
261     }
262
263     protected void sendRequestInternal(MethodCall methodCall, Future future,
264         Body sourceBody) throws java.io.IOException JavaDoc, RenegotiateSessionException {
265         sourceBody.sendRequest(methodCall, future, universalBody);
266     }
267
268     //
269
// -- PRIVATE METHODS -----------------------------------------------
270
//
271
private ConstructorCall buildBodyConstructorCall(Class JavaDoc bodyClass,
272         Class JavaDoc[] argsClass, Object JavaDoc[] args) throws ProActiveException {
273         // Determines the constructor of the body object: it is the constructor that
274
// has only one argument, this argument being of type ConstructorCall
275
try {
276             Constructor JavaDoc cstr = bodyClass.getConstructor(argsClass);
277
278             // A word of explanation: here we have two nested ConstructorCall objects:
279
// 'bodyConstructorCall' is the reification of the construction of the body,
280
// which contains another ConstructorCall object that represents the reification
281
// of the construction of the reified object itself.
282
return new ConstructorCallImpl(cstr, args);
283         } catch (NoSuchMethodException JavaDoc e) {
284             throw new ProActiveException("Class " + bodyClass.getName() +
285                 " has no constructor matching ", e);
286         }
287     }
288
289     public boolean isLocal() {
290         return this.isLocal;
291     }
292
293
294     //
295
// -- SERIALIZATION -----------------------------------------------
296
//
297
private void writeObject(java.io.ObjectOutputStream JavaDoc out)
298         throws java.io.IOException JavaDoc {
299         out.writeObject(universalBody.getRemoteAdapter());
300     }
301
302     private void readObject(java.io.ObjectInputStream JavaDoc in)
303         throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc {
304         Body localBody = LocalBodyStore.getInstance().getLocalBody(bodyID);
305         if (logger.isDebugEnabled()) {
306             logger.debug(" XXXXX UniversalBodyProxy XXXXX ");
307         }
308
309         // Thread.dumpStack();
310
if (logger.isDebugEnabled()) {
311             logger.debug("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
312             logger.debug("Local body is " + localBody);
313         }
314         if (localBody != null) {
315             // the body is local
316
universalBody = localBody;
317             in.readObject();
318             isLocal = true;
319         } else {
320             // the body is not local
321
universalBody = (UniversalBody) in.readObject();
322             isLocal = false;
323         }
324         if (logger.isDebugEnabled()) {
325             logger.debug("universalBody is " + universalBody);
326         }
327     }
328 }
329
Popular Tags