org/objectweb/proactive/core/body/LocalBodyStore.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 import org.apache.log4j.Logger;
00034 import org.objectweb.proactive.Body;
00035 import org.objectweb.proactive.core.UniqueID;
00036 import org.objectweb.proactive.core.event.BodyEventListener;
00037 import org.objectweb.proactive.core.event.BodyEventProducerImpl;
00038 import org.objectweb.proactive.core.util.log.Loggers;
00039 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00040 
00041 
00056 public class LocalBodyStore {
00057     //
00058     // -- STATIC MEMBERS -----------------------------------------------
00059     //
00060     static Logger logger = ProActiveLogger.getLogger(Loggers.BODY);
00061     private static LocalBodyStore instance = new LocalBodyStore();
00062 
00063     //
00064     // -- PRIVATE MEMBERS -----------------------------------------------
00065     //
00066 
00072     private BodyMap localBodyMap = new BodyMap();
00073 
00079     private BodyMap localHalfBodyMap = new BodyMap();
00080 
00089     private BodyMap localForwarderMap = new BodyMap();
00090 
00095     private BodyEventProducerImpl bodyEventProducer = new BodyEventProducerImpl();
00096     private ThreadLocal<Body> bodyPerThread = new ThreadLocal<Body>();
00097     private MetaObjectFactory halfBodyMetaObjectFactory = null;
00098 
00100     private boolean exitOnEmpty = false;
00101 
00102     //
00103     // -- CONSTRUCTORS -----------------------------------------------
00104     //
00105 
00110     private LocalBodyStore() {
00111     }
00112 
00113     //
00114     // -- STATIC METHODS -----------------------------------------------
00115     //
00116     public static LocalBodyStore getInstance() {
00117         return instance;
00118     }
00119 
00120     //
00121     // -- PUBLIC METHODS -----------------------------------------------
00122     //
00123     public synchronized MetaObjectFactory getHalfBodyMetaObjectFactory() {
00124         if (this.halfBodyMetaObjectFactory == null) {
00125             halfBodyMetaObjectFactory = ProActiveMetaObjectFactory.newInstance();
00126         }
00127         return halfBodyMetaObjectFactory;
00128     }
00129 
00130     public synchronized void setHalfBodyMetaObjectFactory(
00131         MetaObjectFactory factory) {
00132         halfBodyMetaObjectFactory = factory;
00133     }
00134 
00140     public Body getCurrentThreadBody() {
00141         AbstractBody body = (AbstractBody) bodyPerThread.get();
00142         if (body == null) {
00143             // If we cannot find the body from the current thread we assume that the current thread
00144             // is not the one from an active object. Therefore in this case we create an HalfBody
00145             // that handle the futures
00146             body = HalfBody.getHalfBody(this.getHalfBodyMetaObjectFactory());
00147             bodyPerThread.set(body);
00148             registerHalfBody(body);
00149         }
00150 
00151         return body;
00152     }
00153 
00158     public void setCurrentThreadBody(Body body) {
00159         bodyPerThread.set(body);
00160     }
00161 
00168     public Body getLocalBody(UniqueID bodyID) {
00169         return (Body) localBodyMap.getBody(bodyID);
00170     }
00171 
00178     public Body getLocalHalfBody(UniqueID bodyID) {
00179         return (Body) localHalfBodyMap.getBody(bodyID);
00180     }
00181 
00188     public Body getForwarder(UniqueID bodyID) {
00189         return (Body) localForwarderMap.getBody(bodyID);
00190     }
00191 
00196     public BodyMap getLocalBodies() {
00197         return (BodyMap) localBodyMap.clone();
00198     }
00199 
00204     public BodyMap getLocalHalfBodies() {
00205         return (BodyMap) localHalfBodyMap.clone();
00206     }
00207 
00213     public void addBodyEventListener(BodyEventListener listener) {
00214         bodyEventProducer.addBodyEventListener(listener);
00215     }
00216 
00221     public void removeBodyEventListener(BodyEventListener listener) {
00222         bodyEventProducer.removeBodyEventListener(listener);
00223     }
00224 
00225     private void checkExitOnEmpty() {
00226         if (this.exitOnEmpty && this.localBodyMap.size() == 0) {
00227                 System.exit(0);
00228         }
00229     }
00230     
00236     public void enableExitOnEmpty() {
00237         this.exitOnEmpty = true;
00238         checkExitOnEmpty();
00239     }
00240 
00241     //
00242     // -- FRIENDLY METHODS -----------------------------------------------
00243     //
00244     void registerBody(AbstractBody body) {
00245         if (localBodyMap.getBody(body.getID()) != null) {
00246             logger.warn("Body already registered in the body map");
00247         }
00248         localBodyMap.putBody(body.bodyID, body);
00249         bodyEventProducer.fireBodyCreated(body);
00250     }
00251 
00252     void unregisterBody(AbstractBody body) {
00253         localBodyMap.removeBody(body.bodyID);
00254         bodyEventProducer.fireBodyRemoved(body);
00255         checkExitOnEmpty();
00256     }
00257 
00258     void registerHalfBody(AbstractBody body) {
00259         localHalfBodyMap.putBody(body.bodyID, body);
00260 
00261         //bodyEventProducer.fireBodyCreated(body);
00262     }
00263 
00264     void unregisterHalfBody(AbstractBody body) {
00265         localHalfBodyMap.removeBody(body.bodyID);
00266         //bodyEventProducer.fireBodyRemoved(body);
00267     }
00268 
00269     public void registerForwarder(AbstractBody body) {
00270         if (localForwarderMap.getBody(body.bodyID) != null) {
00271             logger.debug("Forwarder already registered in the body map");
00272             localForwarderMap.removeBody(body.bodyID);
00273         }
00274         localForwarderMap.putBody(body.bodyID, body);
00275     }
00276 
00277     public void unregisterForwarder(AbstractBody body) {
00278         localForwarderMap.removeBody(body.bodyID);
00279     }
00280 }

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