1 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 ; 55 import java.lang.reflect.InvocationTargetException ; 56 57 58 public class UniversalBodyProxy extends AbstractBodyProxy 59 implements java.io.Serializable { 60 protected static Logger logger = Logger.getLogger(UniversalBodyProxy.class.getName()); 61 62 protected transient UniversalBody universalBody; 65 protected transient boolean isLocal; 66 67 71 74 public UniversalBodyProxy() { 75 } 76 77 88 public UniversalBodyProxy(ConstructorCall constructorCall, 89 Object [] parameters) throws ProActiveException { 90 Object p0 = parameters[0]; 91 92 if (p0 instanceof UniversalBody) { 94 this.universalBody = (UniversalBody) p0; 96 this.bodyID = universalBody.getID(); 97 isLocal = LocalBodyStore.getInstance().getLocalBody(bodyID) != null; 98 if (logger.isDebugEnabled()) { 99 } 101 } else { 102 Class bodyClass = Constants.DEFAULT_BODY_CLASS; 104 Node node = (Node) p0; 105 106 Active activity = (Active) parameters[1]; 110 MetaObjectFactory factory = (MetaObjectFactory) parameters[2]; 111 String jobID = (String )parameters[3]; 112 Class [] argsClass = new Class [] { 113 ConstructorCall.class, String .class, Active.class, 114 MetaObjectFactory.class, String .class 115 }; 116 Object [] args = new Object [] { 117 constructorCall, node.getNodeInformation().getURL(), 118 activity, factory, jobID 119 }; 120 121 ConstructorCall bodyConstructorCall = buildBodyConstructorCall(bodyClass, 125 argsClass, args); 126 if (NodeFactory.isNodeLocal(node)) { 127 this.universalBody = createLocalBody(bodyConstructorCall, 132 constructorCall, node); 133 isLocal = true; 134 } else { 135 this.universalBody = createRemoteBody(bodyConstructorCall, node); 136 isLocal = false; 140 } 141 this.bodyID = universalBody.getID(); 142 if (logger.isDebugEnabled()) { 143 } 145 } 146 } 147 148 public boolean equals(Object 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 public UniversalBody getBody() { 168 return universalBody; 169 } 170 171 protected UniversalBody createLocalBody( 175 ConstructorCall bodyConstructorCall, 176 ConstructorCall reifiedObjectConstructorCall, Node node) 177 throws ProActiveException { 178 try { 179 reifiedObjectConstructorCall.makeDeepCopyOfArguments(); 180 181 ProActiveRuntime part = ProActiveRuntimeImpl.getProActiveRuntime(); 184 185 return part.createBody(node.getNodeInformation().getName(), 191 bodyConstructorCall, true); 192 } catch (ConstructorCallExecutionFailedException e) { 194 throw new ProActiveException(e); 195 } catch (InvocationTargetException e) { 196 throw new ProActiveException(e.getTargetException()); 197 } catch (java.io.IOException 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()) { 215 logger.debug("RemoteBodyProxy created bodyID=" + bodyID + 216 " from ConstructorCall"); 217 } 218 return part.createBody(node.getNodeInformation().getName(), 219 bodyConstructorCall, false); 220 } catch (ConstructorCallExecutionFailedException e) { 222 throw new ProActiveException(e); 223 } catch (java.lang.reflect.InvocationTargetException 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 , RenegotiateSessionException{ 232 sendRequest(methodCall, future, 240 LocalBodyStore.getInstance().getCurrentThreadBody()); 241 } 242 243 protected void sendRequest(MethodCall methodCall, Future future, 244 Body sourceBody) throws java.io.IOException , RenegotiateSessionException { 245 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 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 , RenegotiateSessionException { 265 sourceBody.sendRequest(methodCall, future, universalBody); 266 } 267 268 private ConstructorCall buildBodyConstructorCall(Class bodyClass, 272 Class [] argsClass, Object [] args) throws ProActiveException { 273 try { 276 Constructor cstr = bodyClass.getConstructor(argsClass); 277 278 return new ConstructorCallImpl(cstr, args); 283 } catch (NoSuchMethodException 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 private void writeObject(java.io.ObjectOutputStream out) 298 throws java.io.IOException { 299 out.writeObject(universalBody.getRemoteAdapter()); 300 } 301 302 private void readObject(java.io.ObjectInputStream in) 303 throws java.io.IOException , ClassNotFoundException { 304 Body localBody = LocalBodyStore.getInstance().getLocalBody(bodyID); 305 if (logger.isDebugEnabled()) { 306 logger.debug(" XXXXX UniversalBodyProxy XXXXX "); 307 } 308 309 if (logger.isDebugEnabled()) { 311 logger.debug("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); 312 logger.debug("Local body is " + localBody); 313 } 314 if (localBody != null) { 315 universalBody = localBody; 317 in.readObject(); 318 isLocal = true; 319 } else { 320 universalBody = (UniversalBody) in.readObject(); 322 isLocal = false; 323 } 324 if (logger.isDebugEnabled()) { 325 logger.debug("universalBody is " + universalBody); 326 } 327 } 328 } 329 | Popular Tags |