1 31 package org.objectweb.proactive.core.body; 32 33 import org.apache.log4j.Logger; 34 import org.objectweb.proactive.Body; 35 import org.objectweb.proactive.core.UniqueID; 36 import org.objectweb.proactive.core.event.BodyEventListener; 37 import org.objectweb.proactive.core.event.BodyEventProducerImpl; 38 39 40 54 public class LocalBodyStore { 55 static Logger logger = Logger.getLogger(LocalBodyStore.class.getName()); 59 60 private static LocalBodyStore instance = new LocalBodyStore(); 61 62 66 71 private BodyMap localBodyMap = new BodyMap(); 72 73 78 private BodyMap localHalfBodyMap = new BodyMap(); 79 80 84 private BodyEventProducerImpl bodyEventProducer = new BodyEventProducerImpl(); 85 private ThreadLocal bodyPerThread = new ThreadLocal (); 86 private MetaObjectFactory halfBodyMetaObjectFactory = null ; 87 88 92 96 private LocalBodyStore() { 97 } 98 99 public static LocalBodyStore getInstance() { 103 return instance; 104 } 105 106 public synchronized MetaObjectFactory getHalfBodyMetaObjectFactory() { 110 111 if (this.halfBodyMetaObjectFactory == null) { 112 halfBodyMetaObjectFactory = ProActiveMetaObjectFactory.newInstance(); 113 } 114 return halfBodyMetaObjectFactory; 115 } 116 117 public synchronized void setHalfBodyMetaObjectFactory(MetaObjectFactory factory) { 118 halfBodyMetaObjectFactory = factory; 119 } 120 121 126 public Body getCurrentThreadBody() { 127 AbstractBody body = (AbstractBody) bodyPerThread.get(); 128 129 if (body == null) { 130 body = HalfBody.getHalfBody(this.getHalfBodyMetaObjectFactory()); 134 bodyPerThread.set(body); 135 registerHalfBody(body); 136 } 137 138 return body; 139 } 140 141 145 public void setCurrentThreadBody(Body body) { 146 bodyPerThread.set(body); 147 } 148 149 155 public Body getLocalBody(UniqueID bodyID) { 156 return (Body) localBodyMap.getBody(bodyID); 157 } 158 159 165 public Body getLocalHalfBody(UniqueID bodyID) { 166 return (Body) localHalfBodyMap.getBody(bodyID); 167 } 168 169 173 public BodyMap getLocalBodies() { 174 return (BodyMap) localBodyMap.clone(); 175 } 176 177 181 public BodyMap getLocalHalfBodies() { 182 return (BodyMap) localHalfBodyMap.clone(); 183 } 184 185 190 public void addBodyEventListener(BodyEventListener listener) { 191 bodyEventProducer.addBodyEventListener(listener); 192 } 193 194 195 199 public void removeBodyEventListener(BodyEventListener listener) { 200 bodyEventProducer.removeBodyEventListener(listener); 201 } 202 203 void registerBody(AbstractBody body) { 207 if (localBodyMap.getBody(body.getID()) != null) { 208 logger.warn("WARNING Body already registered in the body map"); 209 } 210 localBodyMap.putBody(body.bodyID, body); 211 bodyEventProducer.fireBodyCreated(body); 212 } 213 214 void unregisterBody(AbstractBody body) { 215 localBodyMap.removeBody(body.bodyID); 216 bodyEventProducer.fireBodyRemoved(body); 217 } 218 219 void registerHalfBody(AbstractBody body) { 220 localHalfBodyMap.putBody(body.bodyID, body); 221 222 } 224 225 void unregisterHalfBody(AbstractBody body) { 226 localHalfBodyMap.removeBody(body.bodyID); 227 } 229 } 230 | Popular Tags |