1 31 package org.objectweb.proactive.core.body; 32 33 34 47 import org.apache.log4j.Logger; 48 import org.objectweb.proactive.Active; 49 import org.objectweb.proactive.Body; 50 import org.objectweb.proactive.EndActive; 51 import org.objectweb.proactive.InitActive; 52 import org.objectweb.proactive.RunActive; 53 import org.objectweb.proactive.core.body.migration.MigratableBody; 54 import org.objectweb.proactive.core.mop.ConstructorCall; 55 import org.objectweb.proactive.core.mop.ConstructorCallExecutionFailedException; 56 57 58 public class ActiveBody extends MigratableBody implements Runnable , 59 java.io.Serializable { 60 protected static Logger logger = Logger.getLogger(ActiveBody.class.getName()); 61 62 private transient InitActive initActive; private RunActive runActive; 73 private EndActive endActive; 74 75 79 82 public ActiveBody() { 83 } 84 85 88 public ActiveBody(ConstructorCall c, String nodeURL, Active activity, 89 MetaObjectFactory factory, String jobID) 90 throws java.lang.reflect.InvocationTargetException , 91 ConstructorCallExecutionFailedException { 92 super(c.execute(), nodeURL, factory, jobID); 94 95 Object reifiedObject = localBodyStrategy.getReifiedObject(); 97 if ((activity != null) && activity instanceof InitActive) { 98 initActive = (InitActive) activity; 99 } else if (reifiedObject instanceof InitActive) { 100 initActive = (InitActive) reifiedObject; 101 } 102 103 if ((activity != null) && activity instanceof RunActive) { 105 runActive = (RunActive) activity; 106 } else if (reifiedObject instanceof RunActive) { 107 runActive = (RunActive) reifiedObject; 108 } else { 109 runActive = new FIFORunActive(); 110 } 111 112 if ((activity != null) && activity instanceof EndActive) { 114 endActive = (EndActive) activity; 115 } else if (reifiedObject instanceof EndActive) { 116 endActive = (EndActive) reifiedObject; 117 } else { 118 endActive = null; 119 } 120 121 startBody(); 122 } 123 124 131 135 public void run() { 136 activityStarted(); 137 if (initActive != null) { 139 initActive.initActivity(this); 140 initActive = null; } 142 143 try { 145 runActive.runActivity(this); 146 if (isActive()) { 148 while (!(localBodyStrategy.getRequestQueue().isEmpty())) { 150 serve(localBodyStrategy.getRequestQueue().removeOldest()); 151 } 152 } 153 } catch (Exception e) { 154 155 logger.error("Exception occured in runActivity method of body " + 156 toString() + ". Now terminating the body"); 157 158 e.printStackTrace(); 159 terminate(); 160 } finally { 161 if (isActive()) { 162 activityStopped(); 163 } 164 165 if ((!hasJustMigrated) && (endActive != null)) { 167 endActive.endActivity(this); 168 } 169 } 170 } 171 172 176 179 protected void startBody() { 180 if (logger.isDebugEnabled()) { 181 logger.debug("Starting Body"); 182 } 183 Thread t = new Thread (this, 184 shortClassName(getName()) + " on " + getNodeURL()); 185 t.start(); 186 } 187 188 191 protected void activityStopped() { 192 super.activityStopped(); 193 runActive = null; 194 } 195 196 private static String shortClassName(String fqn) { 200 int n = fqn.lastIndexOf('.'); 201 if ((n == -1) || (n == (fqn.length() - 1))) { 202 return fqn; 203 } 204 return fqn.substring(n + 1); 205 } 206 207 private void writeObject(java.io.ObjectOutputStream out) 208 throws java.io.IOException { 209 if (logger.isDebugEnabled()) { 210 logger.debug("out = " + out); 211 } 212 out.defaultWriteObject(); 213 } 214 215 private void readObject(java.io.ObjectInputStream in) 216 throws java.io.IOException , ClassNotFoundException { 217 if (logger.isDebugEnabled()) { 218 logger.debug("in = " + in); 219 } 220 in.defaultReadObject(); 221 startBody(); 222 } 223 224 private class FIFORunActive implements RunActive, java.io.Serializable { 228 public void runActivity(Body body) { 229 while (isActive()) { 230 serve(localBodyStrategy.getRequestQueue().blockingRemoveOldest()); 231 } 232 } 233 } 234 } 235 | Popular Tags |