1 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 { 50 protected UniqueID bodyID; 57 58 public AbstractBodyProxy() { 62 } 63 64 AbstractBodyProxy(UniqueID bodyID) { 65 this.bodyID = bodyID; 66 } 67 68 public UniqueID getBodyID() { 75 return bodyID; 76 } 77 78 82 97 public Object reify(MethodCall methodCall) 98 throws Throwable { 99 if (methodCall.getName().equals("equals")) { 100 Object arg = methodCall.getParameter(0); 102 if (MOP.isReifiedObject(arg)) { 103 AbstractBodyProxy bodyProxy = (AbstractBodyProxy)((StubObject)arg).getProxy(); 104 return new Boolean (bodyID.equals(bodyProxy.bodyID)); 105 } else { 106 return new Boolean (false); 107 } 108 } 109 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 t) { 124 if (t instanceof RuntimeException ) { 125 throw (RuntimeException )t; 126 } else if (t instanceof Error ) { 127 throw (Error )t; 128 } else { 129 Class [] declaredExceptions = methodCall.getReifiedMethod().getExceptionTypes(); 131 for (int i = 0; i < declaredExceptions.length; i++) { 132 Class exceptionClass = declaredExceptions[i]; 133 if (exceptionClass.isAssignableFrom(t.getClass())) { 134 throw t; 135 } 136 } 137 throw new ProActiveRuntimeException(t); 139 } 140 } 141 } 142 143 146 147 protected void reifyAsOneWay(MethodCall methodCall) throws MethodCallExecutionFailedException, RenegotiateSessionException { 148 try { 149 sendRequest(methodCall, null); 150 } catch (java.io.IOException e) { 151 throw new MethodCallExecutionFailedException("Exception occured in reifyAsOneWay while sending request for methodcall ="+methodCall.getName(), e); 152 } 153 } 154 155 156 protected Object reifyAsAsynchronous(MethodCall methodCall) throws MethodCallExecutionFailedException, RenegotiateSessionException { 157 StubObject futureobject; 158 try { 160 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 e) { 166 throw new MethodCallExecutionFailedException("Exception occured in reifyAsAsynchronous while creating future for methodcall ="+methodCall.getName(), e); 167 } 168 169 FutureProxy fp = (FutureProxy)(futureobject.getProxy()); 171 fp.setCreatorID(bodyID); 172 173 try { 175 sendRequest(methodCall, (Future)futureobject.getProxy()); 176 } catch (java.io.IOException e) { 177 throw new MethodCallExecutionFailedException("Exception occured in reifyAsAsynchronous while sending request for methodcall ="+methodCall.getName(), e); 178 } 179 return futureobject; 181 } 182 183 184 protected Object reifyAsSynchronous(MethodCall methodCall) throws Throwable , MethodCallExecutionFailedException , RenegotiateSessionException{ 185 Future f = FutureProxy.getFutureProxy(); 187 f.setCreatorID(bodyID); 188 191 try { 192 sendRequest(methodCall, f); 193 } catch (java.io.IOException e) { 194 throw new MethodCallExecutionFailedException("Exception occured in reifyAsSynchronous while sending request for methodcall ="+methodCall.getName(), e); 195 } 196 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 , RenegotiateSessionException; 207 208 protected abstract void sendRequest(MethodCall methodCall, Future future, Body sourceBody) throws java.io.IOException , RenegotiateSessionException; 209 210 } | Popular Tags |