org/objectweb/proactive/core/component/body/ComponentActivity.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.component.body;
00032 
00033 import java.io.Serializable;
00034 
00035 import org.apache.log4j.Logger;
00036 import org.objectweb.fractal.api.NoSuchInterfaceException;
00037 import org.objectweb.fractal.api.control.LifeCycleController;
00038 import org.objectweb.fractal.util.Fractal;
00039 import org.objectweb.proactive.Active;
00040 import org.objectweb.proactive.Body;
00041 import org.objectweb.proactive.EndActive;
00042 import org.objectweb.proactive.InitActive;
00043 import org.objectweb.proactive.RunActive;
00044 import org.objectweb.proactive.Service;
00045 import org.objectweb.proactive.core.body.ActiveBody;
00046 import org.objectweb.proactive.core.util.log.Loggers;
00047 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00048 
00049 
00059 public class ComponentActivity implements RunActive, InitActive, EndActive, Serializable {
00060     private static Logger logger = ProActiveLogger.getLogger(Loggers.COMPONENTS_ACTIVITY);
00061     private transient InitActive componentInitActive; // used only once
00062     private RunActive componentRunActive;
00063     private EndActive componentEndActive;
00064     private ActiveBody activeBody;
00065     private InitActive functionalInitActive;
00066     private RunActive functionalRunActive;
00067     private EndActive functionalEndActive;
00068 
00069     public ComponentActivity() {
00070         // default component activity
00071         componentInitActive = null;
00072         componentRunActive = this;
00073         componentEndActive = null;
00074 
00075         functionalInitActive = new DefaultInitActive();
00076         functionalRunActive = new ComponentFIFORunActive();
00077         functionalEndActive = new DefaultEndActive();
00078     }
00079 
00080     public ComponentActivity(Active activity, Object reifiedObject) {
00081         // ComponentInitActive
00082         if ((activity != null) && activity instanceof ComponentInitActive) {
00083             componentInitActive = new ComponentInitActiveWrapper((ComponentInitActive) activity);
00084         } else if (reifiedObject instanceof ComponentInitActive) {
00085             componentInitActive = new ComponentInitActiveWrapper((ComponentInitActive) reifiedObject);
00086         } else {
00087             componentInitActive = null;
00088         }
00089 
00090         // ComponentRunActive
00091         if ((activity != null) && activity instanceof ComponentRunActive) {
00092             componentRunActive = new ComponentRunActiveWrapper((ComponentRunActive) activity);
00093         } else if (reifiedObject instanceof ComponentRunActive) {
00094             componentRunActive = new ComponentRunActiveWrapper((ComponentRunActive) reifiedObject);
00095         } else {
00096             componentRunActive = this;
00097         }
00098 
00099         // ComponentEndActive
00100         if ((activity != null) && activity instanceof ComponentEndActive) {
00101             componentEndActive = new ComponentEndActiveWrapper((ComponentEndActive) activity);
00102         } else if (reifiedObject instanceof ComponentEndActive) {
00103             componentEndActive = new ComponentEndActiveWrapper((ComponentEndActive) reifiedObject);
00104         } else {
00105             componentEndActive = null;
00106         }
00107 
00108         if ((activity != null) && activity instanceof InitActive) {
00109             functionalInitActive = (InitActive) activity;
00110         } else if (reifiedObject instanceof InitActive) {
00111             functionalInitActive = (InitActive) reifiedObject;
00112         } else {
00113             functionalInitActive = new DefaultInitActive();
00114         }
00115 
00116         // RunActive
00117         if ((activity != null) && activity instanceof RunActive) {
00118             functionalRunActive = (RunActive) activity;
00119         } else if (reifiedObject instanceof RunActive) {
00120             functionalRunActive = (RunActive) reifiedObject;
00121         } else {
00122             functionalRunActive = new ComponentFIFORunActive();
00123         }
00124 
00125         // EndActive
00126         if ((activity != null) && activity instanceof EndActive) {
00127             functionalEndActive = (EndActive) activity;
00128         } else if (reifiedObject instanceof EndActive) {
00129             functionalEndActive = (EndActive) reifiedObject;
00130         } else {
00131             functionalEndActive = new DefaultEndActive();
00132         }
00133     }
00134 
00145     public void runActivity(Body body) {
00146         if ((componentRunActive != null) && (componentRunActive != this)) {
00147             componentRunActive.runActivity(body);
00148         } else {
00149             // this is the default activity of the active object
00150             // the activity of the component has been initialized and started, now
00151             // what we have to do is to manage the life cycle, i.e. start and stop the
00152             // activity
00153             // that can be redefined on the reified object.
00154             try {
00155                 Service componentService = new Service(body);
00156                 NFRequestFilterImpl nfRequestFilter = new NFRequestFilterImpl();
00157                 while (body.isActive()) {
00158                     ComponentBody componentBody = (ComponentBody) body;
00159                     while (LifeCycleController.STOPPED.equals(
00160                                 Fractal.getLifeCycleController(
00161                                     componentBody.getProActiveComponentImpl())
00162                                            .getFcState()) ) {
00163                         componentService.blockingServeOldest(nfRequestFilter);
00164                         if (!body.isActive()) {
00165                                 // in case of a migration 
00166                                 break;
00167                         }
00168                     }
00169                     if (!body.isActive()) {
00170                         // in case of a migration 
00171                         break;
00172                     }
00173 
00174                     // 3.1. init object Activity
00175                     // life cycle started : starting activity of the object
00176                     if (functionalInitActive != null) {
00177                         functionalInitActive.initActivity(activeBody);
00178                         //functionalInitActive = null; // we won't do it again
00179                     }
00180 
00181                     ((ComponentBody) body).startingFunctionalActivity();
00182                     // 3.2 while object activity
00183                     // componentServe (includes filter on priority)
00184                     functionalRunActive.runActivity(body);
00185                     ((ComponentBody) body).finishedFunctionalActivity();
00186                     if (functionalEndActive != null) {
00187                         functionalEndActive.endActivity(body);
00188                     }
00189                 }
00190             } catch (NoSuchInterfaceException e) {
00191                 logger.error(
00192                     "could not retreive an interface, probably the life cycle controller of this component; terminating the component. Error message is : " +
00193                     e.getMessage());
00194             }
00195         }
00196     }
00197 
00198     /*
00199      * @see org.objectweb.proactive.InitActive#initActivity(org.objectweb.proactive.Body)
00200      */
00201     public void initActivity(Body body) {
00202         if (componentInitActive != null) {
00203             componentInitActive.initActivity(body);
00204         } else {
00205             if (logger.isDebugEnabled()) {
00206                 logger.debug(
00207                     "initializing component activity ... (component will be active but not yet started)");
00208             }
00209         }
00210     }
00211 
00212     /*
00213      * @see org.objectweb.proactive.EndActive#endActivity(org.objectweb.proactive.Body)
00214      */
00215     public void endActivity(Body body) {
00216         if (componentEndActive != null) {
00217             componentEndActive.endActivity(body);
00218         } else {
00219             if (logger.isDebugEnabled()) {
00220                 logger.debug(
00221                     "ending component activity ... (object is still active)");
00222             }
00223         }
00224     }
00225 
00226     private class ComponentFIFORunActive implements RunActive, Serializable {
00227         public void runActivity(Body body) {
00228             new Service(body).fifoServing();
00229         }
00230     }
00231 
00232     private class DefaultInitActive implements InitActive, Serializable {
00233         public void initActivity(Body body) {
00234             if (logger.isDebugEnabled()) {
00235                 logger.debug(
00236                     "initializing default functional activity of the component");
00237             }
00238         }
00239     }
00240 
00241     private class DefaultEndActive implements EndActive, Serializable {
00242         public void endActivity(Body body) {
00243             if (logger.isDebugEnabled()) {
00244                 logger.debug(
00245                     "ending default functional activity of this component");
00246             }
00247         }
00248     }
00249 
00250     private class ComponentInitActiveWrapper implements InitActive,
00251         Serializable {
00252         private ComponentInitActive wrappedComponentInitActive;
00253 
00254         public ComponentInitActiveWrapper(
00255             ComponentInitActive componentInitActive) {
00256             wrappedComponentInitActive = componentInitActive;
00257         }
00258 
00259         public void initActivity(Body body) {
00260             wrappedComponentInitActive.initComponentActivity(body);
00261         }
00262     }
00263 
00264     private class ComponentRunActiveWrapper implements RunActive, Serializable {
00265         private ComponentRunActive wrappedComponentRunActive;
00266 
00267         public ComponentRunActiveWrapper(ComponentRunActive componentRunActive) {
00268             wrappedComponentRunActive = componentRunActive;
00269         }
00270 
00271         public void runActivity(Body body) {
00272             wrappedComponentRunActive.runComponentActivity(body);
00273         }
00274     }
00275 
00276     private class ComponentEndActiveWrapper implements EndActive, Serializable {
00277         private ComponentEndActive wrappedComponentEndActive;
00278 
00279         public ComponentEndActiveWrapper(ComponentEndActive componentEndActive) {
00280             wrappedComponentEndActive = componentEndActive;
00281         }
00282 
00283         public void endActivity(Body body) {
00284             wrappedComponentEndActive.endComponentActivity(body);
00285         }
00286     }
00287 }

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