KickJava   Java API By Example, From Geeks To Geeks.

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


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.objectweb.proactive.Body;
34 import org.objectweb.proactive.core.Constants;
35 import org.objectweb.proactive.core.ProActiveRuntimeException;
36 import org.objectweb.proactive.core.UniqueID;
37 import org.objectweb.proactive.core.body.future.Future;
38 import org.objectweb.proactive.core.body.future.FutureProxy;
39 import org.objectweb.proactive.core.mop.MOP;
40 import org.objectweb.proactive.core.mop.MOPException;
41 import org.objectweb.proactive.core.mop.MethodCall;
42 import org.objectweb.proactive.core.mop.MethodCallExecutionFailedException;
43 import org.objectweb.proactive.core.mop.StubObject;
44 import org.objectweb.proactive.ext.security.RenegotiateSessionException;
45
46
47 public abstract class AbstractBodyProxy extends AbstractProxy
48     implements BodyProxy,
49                java.io.Serializable JavaDoc {
50     //
51
// -- STATIC MEMBERS -----------------------------------------------
52
//
53
//
54
// -- PROTECTED MEMBERS -----------------------------------------------
55
//
56
protected UniqueID bodyID;
57
58     //
59
// -- CONSTRUCTORS -----------------------------------------------
60
//
61
public AbstractBodyProxy() {
62     }
63
64     AbstractBodyProxy(UniqueID bodyID) {
65         this.bodyID = bodyID;
66     }
67
68     //
69
// -- PUBLIC METHODS -----------------------------------------------
70
//
71
//
72
// -- implements BodyProxy -----------------------------------------------
73
//
74
public UniqueID getBodyID() {
75         return bodyID;
76     }
77
78     //
79
// -- implements Proxy -----------------------------------------------
80
//
81

82     /**
83    * Performs operations on the Call object created by the stub, thus changing
84    * the semantics of message-passing to asynchronous message-passing with
85    * future objects
86    *
87    *
88    * The semantics of message-passing implemented by this proxy class
89    * may be definied as follows :<UL>
90    * <LI>Asynchronous message-passing
91    * <LI>Creation of future objects where possible (which leads to
92    * wait-by-necessity).
93    * <LI>Synchronous, blocking calls where futures are not available.
94    * <LI>The Call <code>methodCall</code> is passed to the skeleton for execution.
95    * </UL>
96    */

97     public Object JavaDoc reify(MethodCall methodCall)
98                  throws Throwable JavaDoc {
99         if (methodCall.getName().equals("equals")) {
100             //there is only one argument to this method
101
Object JavaDoc arg = methodCall.getParameter(0);
102             if (MOP.isReifiedObject(arg)) {
103                 AbstractBodyProxy bodyProxy = (AbstractBodyProxy)((StubObject)arg).getProxy();
104                 return new Boolean JavaDoc(bodyID.equals(bodyProxy.bodyID));
105             } else {
106                 return new Boolean JavaDoc(false);
107             }
108         }
109         // Now gives the MethodCall object to the body
110
try {
111             if (isOneWayCall(methodCall)) {
112                 reifyAsOneWay(methodCall);
113                 return null;
114             }
115             if (isAsynchronousCall(methodCall)) {
116                 return reifyAsAsynchronous(methodCall);
117             }
118             return reifyAsSynchronous(methodCall);
119         } catch (MethodCallExecutionFailedException e) {
120             throw new ProActiveRuntimeException(e.getMessage(),
121                                                 e.getTargetException());
122         }
123          catch (Throwable JavaDoc t) {
124             if (t instanceof RuntimeException JavaDoc) {
125                 throw (RuntimeException JavaDoc)t;
126             } else if (t instanceof Error JavaDoc) {
127                 throw (Error JavaDoc)t;
128             } else {
129                 // check now which exception can be safely thrown
130
Class JavaDoc[] declaredExceptions = methodCall.getReifiedMethod().getExceptionTypes();
131                 for (int i = 0; i < declaredExceptions.length; i++) {
132                     Class JavaDoc exceptionClass = declaredExceptions[i];
133                     if (exceptionClass.isAssignableFrom(t.getClass())) {
134                         throw t;
135                     }
136                 }
137                 // Here we should extend the behavior to accept exception Handler
138
throw new ProActiveRuntimeException(t);
139             }
140         }
141     }
142
143     /**
144    *
145    */

146
147   protected void reifyAsOneWay(MethodCall methodCall) throws MethodCallExecutionFailedException, RenegotiateSessionException {
148     try {
149       sendRequest(methodCall, null);
150     } catch (java.io.IOException JavaDoc e) {
151       throw new MethodCallExecutionFailedException("Exception occured in reifyAsOneWay while sending request for methodcall ="+methodCall.getName(), e);
152     }
153   }
154
155
156   protected Object JavaDoc reifyAsAsynchronous(MethodCall methodCall) throws MethodCallExecutionFailedException, RenegotiateSessionException {
157     StubObject futureobject;
158     // Creates a stub + FutureProxy for representing the result
159
try {
160       //futureobject = (StubObject)MOP.newInstance(methodCall.getReifiedMethod().getReturnType().getName(), null, Constants.DEFAULT_FUTURE_PROXY_CLASS_NAME, null);
161
futureobject = (StubObject)MOP.newInstance(methodCall.getReifiedMethod().getReturnType(), null, Constants.DEFAULT_FUTURE_PROXY_CLASS_NAME, null);
162      
163     } catch (MOPException e) {
164       throw new MethodCallExecutionFailedException("Exception occured in reifyAsAsynchronous while creating future for methodcall ="+methodCall.getName(), e);
165     } catch (ClassNotFoundException JavaDoc e) {
166       throw new MethodCallExecutionFailedException("Exception occured in reifyAsAsynchronous while creating future for methodcall ="+methodCall.getName(), e);
167     }
168    
169     // Set the id of the body creator in the created future
170
FutureProxy fp = (FutureProxy)(futureobject.getProxy());
171     fp.setCreatorID(bodyID);
172    
173     // Send the request
174
try {
175       sendRequest(methodCall, (Future)futureobject.getProxy());
176     } catch (java.io.IOException JavaDoc e) {
177       throw new MethodCallExecutionFailedException("Exception occured in reifyAsAsynchronous while sending request for methodcall ="+methodCall.getName(), e);
178     }
179     // And return the future object
180
return futureobject;
181   }
182
183
184   protected Object JavaDoc reifyAsSynchronous(MethodCall methodCall) throws Throwable JavaDoc, MethodCallExecutionFailedException , RenegotiateSessionException{
185     // Setting methodCall.res to null means that we do not use the future mechanism
186
Future f = FutureProxy.getFutureProxy();
187     f.setCreatorID(bodyID);
188     // Set it as the 'thing' to send results to methodCall.res = f;
189
// Send the request
190

191     try {
192       sendRequest(methodCall, f);
193     } catch (java.io.IOException JavaDoc e) {
194       throw new MethodCallExecutionFailedException("Exception occured in reifyAsSynchronous while sending request for methodcall ="+methodCall.getName(), e);
195     }
196     // Returns the result
197
if (f.getRaisedException() != null) {
198       throw f.getRaisedException();
199     } else {
200       return f.getResult();
201     }
202   }
203
204
205
206     protected abstract void sendRequest(MethodCall methodCall, Future future) throws java.io.IOException JavaDoc, RenegotiateSessionException;
207     
208     protected abstract void sendRequest(MethodCall methodCall, Future future, Body sourceBody) throws java.io.IOException JavaDoc, RenegotiateSessionException;
209
210 }
Popular Tags