00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 package org.objectweb.proactive.core.body;
00032
00033
00047 import org.apache.log4j.Logger;
00048 import org.objectweb.proactive.Active;
00049 import org.objectweb.proactive.Body;
00050 import org.objectweb.proactive.EndActive;
00051 import org.objectweb.proactive.InitActive;
00052 import org.objectweb.proactive.RunActive;
00053 import org.objectweb.proactive.Service;
00054 import org.objectweb.proactive.core.component.body.ComponentActivity;
00055 import org.objectweb.proactive.core.component.body.ComponentBodyImpl;
00056 import org.objectweb.proactive.core.mop.ConstructorCall;
00057 import org.objectweb.proactive.core.mop.ConstructorCallExecutionFailedException;
00058 import org.objectweb.proactive.core.util.log.Loggers;
00059 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00060
00061
00062 public class ActiveBody extends ComponentBodyImpl implements Runnable,
00063 java.io.Serializable {
00064 protected static Logger logger = ProActiveLogger.getLogger(Loggers.BODY);
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075 private transient InitActive initActive;
00076 private RunActive runActive;
00077 private EndActive endActive;
00078
00079
00080
00081
00082
00086 public ActiveBody() {
00087 }
00088
00092 public ActiveBody(ConstructorCall c, String nodeURL, Active activity,
00093 MetaObjectFactory factory, String jobID)
00094 throws java.lang.reflect.InvocationTargetException,
00095 ConstructorCallExecutionFailedException {
00096
00097 super(c.execute(), nodeURL, activity, factory, jobID);
00098
00099 Object reifiedObject = localBodyStrategy.getReifiedObject();
00100
00101
00102
00103 if (getProActiveComponentImpl() != null) {
00104 activity = new ComponentActivity(activity, reifiedObject);
00105 }
00106
00107
00108 if ((activity != null) && activity instanceof InitActive) {
00109 initActive = (InitActive) activity;
00110 } else if (reifiedObject instanceof InitActive) {
00111 initActive = (InitActive) reifiedObject;
00112 }
00113
00114
00115 if ((activity != null) && activity instanceof RunActive) {
00116 runActive = (RunActive) activity;
00117 } else if (reifiedObject instanceof RunActive) {
00118 runActive = (RunActive) reifiedObject;
00119 } else {
00120 runActive = new FIFORunActive();
00121 }
00122
00123
00124 if ((activity != null) && activity instanceof EndActive) {
00125 endActive = (EndActive) activity;
00126 } else if (reifiedObject instanceof EndActive) {
00127 endActive = (EndActive) reifiedObject;
00128 } else {
00129 endActive = null;
00130 }
00131
00132 startBody();
00133 }
00134
00135
00136
00137
00138
00139
00140
00141
00146 public void run() {
00147 activityStarted();
00148
00149 if (initActive != null) {
00150 initActive.initActivity(this);
00151 initActive = null;
00152 }
00153
00154
00155 try {
00156 runActive.runActivity(this);
00157
00158 if (isActive()) {
00159
00160 while (!(localBodyStrategy.getRequestQueue().isEmpty())) {
00161 serve(localBodyStrategy.getRequestQueue().removeOldest());
00162 }
00163 }
00164 } catch (Exception e) {
00165 logger.error("Exception occured in runActivity method of body " +
00166 toString() + ". Now terminating the body");
00167
00168 e.printStackTrace();
00169 terminate();
00170 } finally {
00171
00172 if ((!hasJustMigrated) && (endActive != null)) {
00173 endActive.endActivity(this);
00174 }
00175
00176 if (isActive()) {
00177 activityStopped();
00178 }
00179 }
00180 }
00181
00182
00183
00184
00185
00189 public void startBody() {
00190 if (logger.isDebugEnabled()) {
00191 logger.debug("Starting Body");
00192 }
00193
00194
00195 String inc = (this.ftmanager != null) ? ("" + this.ftmanager) : ("");
00196 Thread t = new Thread(this,
00197 shortClassName(getName()) + " on " + getNodeURL() + inc);
00198 t.start();
00199 }
00200
00204 protected void activityStopped() {
00205 super.activityStopped();
00206 runActive = null;
00207 }
00208
00209
00210
00211
00212 private static String shortClassName(String fqn) {
00213 int n = fqn.lastIndexOf('.');
00214 if ((n == -1) || (n == (fqn.length() - 1))) {
00215 return fqn;
00216 }
00217 return fqn.substring(n + 1);
00218 }
00219
00220 private void writeObject(java.io.ObjectOutputStream out)
00221 throws java.io.IOException {
00222 if (logger.isDebugEnabled()) {
00223 logger.debug("out = " + out);
00224 }
00225 out.defaultWriteObject();
00226 }
00227
00228 private void readObject(java.io.ObjectInputStream in)
00229 throws java.io.IOException, ClassNotFoundException {
00230 if (logger.isDebugEnabled()) {
00231 logger.debug("in = " + in);
00232 }
00233 in.defaultReadObject();
00234
00235
00236 if (this.ftmanager != null) {
00237 if (!this.ftmanager.isACheckpoint()) {
00238 startBody();
00239 }
00240 } else {
00241 startBody();
00242 }
00243 }
00244
00245
00246
00247
00248 private class FIFORunActive implements RunActive, java.io.Serializable {
00249 public void runActivity(Body body) {
00250 new Service(body).fifoServing();
00251 }
00252 }
00253 }