org/objectweb/proactive/core/body/ActiveBody.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
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     // -- STATIC MEMBERS -----------------------------------------------
00068     //
00069     //
00070     // -- PROTECTED MEMBERS -----------------------------------------------
00071     //
00072     //
00073     // -- PRIVATE MEMBERS -----------------------------------------------
00074     //
00075     private transient InitActive initActive; // used only once when active object is started first time
00076     private RunActive runActive;
00077     private EndActive endActive;
00078 
00079     //
00080     // -- CONSTRUCTORS -----------------------------------------------
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         // Creates the reified object
00097         super(c.execute(), nodeURL, activity, factory, jobID);
00098 
00099         Object reifiedObject = localBodyStrategy.getReifiedObject();
00100 
00101         // when building a component, encapsulate the functional activity
00102         // TODO_M read some flag before doing this?
00103         if (getProActiveComponentImpl() != null) {
00104             activity = new ComponentActivity(activity, reifiedObject);
00105         }
00106 
00107         // InitActive
00108         if ((activity != null) && activity instanceof InitActive) {
00109             initActive = (InitActive) activity;
00110         } else if (reifiedObject instanceof InitActive) {
00111             initActive = (InitActive) reifiedObject;
00112         }
00113 
00114         // RunActive
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         // EndActive
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     // -- PUBLIC METHODS -----------------------------------------------
00137     //
00138     //
00139     // -- implements Runnable -----------------------------------------------
00140     //
00141 
00146     public void run() {
00147         activityStarted();
00148         // execute the initialization if needed. Only once
00149         if (initActive != null) {
00150             initActive.initActivity(this);
00151             initActive = null; // we won't do it again
00152         }
00153 
00154         // run the activity of the body
00155         try {
00156             runActive.runActivity(this);
00157             // the body terminate its activity
00158             if (isActive()) {
00159                 // serve remaining requests if non dead
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             // execute the end of activity if not after migration
00172             if ((!hasJustMigrated) && (endActive != null)) {
00173                 endActive.endActivity(this);
00174             }
00175 
00176             if (isActive()) {
00177                 activityStopped();
00178             }
00179         }
00180     }
00181 
00182     //
00183     // -- PROTECTED METHODS -----------------------------------------------
00184     //
00185 
00189     public void startBody() {
00190         if (logger.isDebugEnabled()) {
00191             logger.debug("Starting Body");
00192         }
00193 
00194         // get the incarnation number if ft is enable
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     // -- PRIVATE METHODS -----------------------------------------------
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         // FAULT-TOLERANCE: if this is a recovering checkpoint, 
00235         // activity will be started in ProActiveRuntimeImpl.receiveCheckpoint()
00236         if (this.ftmanager != null) {
00237             if (!this.ftmanager.isACheckpoint()) {
00238                 startBody();
00239             }
00240         } else {
00241             startBody();
00242         }
00243     }
00244 
00245     //
00246     // -- INNER CLASSES -----------------------------------------------
00247     //
00248     private class FIFORunActive implements RunActive, java.io.Serializable {
00249         public void runActivity(Body body) {
00250             new Service(body).fifoServing();
00251         }
00252     }
00253 }

Generated on Mon Jan 22 15:16:05 2007 for ProActive by  doxygen 1.5.1