KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > body > LocalBodyStore


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

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 /**
41  * <i><font size="-1" color="#FF0000">**For internal use only** </font></i><br>
42  * <p>
43  * This class store all active bodies known in the current JVM. The class is a singleton
44  * in a given JVM. It also associates each active thread with its matching body.
45  * </p>
46  *
47  * @author ProActive Team
48  * @version 1.0, 2001/10/23
49  * @since ProActive 0.9
50  * @see Body
51  * @see UniqueID
52  *
53  */

54 public class LocalBodyStore {
55     //
56
// -- STATIC MEMBERS -----------------------------------------------
57
//
58
static Logger logger = Logger.getLogger(LocalBodyStore.class.getName());
59     
60     private static LocalBodyStore instance = new LocalBodyStore();
61
62     //
63
// -- PRIVATE MEMBERS -----------------------------------------------
64
//
65

66     /**
67      * This table maps all known active Bodies in this JVM with their UniqueID
68      * From one UniqueID it is possible to get the corresponding body if
69      * it belongs to this JVM
70      */

71     private BodyMap localBodyMap = new BodyMap();
72
73     /**
74      * This table maps all known HalfBodies in this JVM with their UniqueID
75      * From one UniqueID it is possible to get the corresponding halfbody if
76      * it belongs to this JVM
77      */

78     private BodyMap localHalfBodyMap = new BodyMap();
79
80     /**
81      * Static object that manages the registration of listeners and the sending of
82      * events
83      */

84     private BodyEventProducerImpl bodyEventProducer = new BodyEventProducerImpl();
85     private ThreadLocal JavaDoc bodyPerThread = new ThreadLocal JavaDoc();
86     private MetaObjectFactory halfBodyMetaObjectFactory = null ;
87
88     //
89
// -- CONSTRUCTORS -----------------------------------------------
90
//
91

92     /**
93      * Creates a new AbstractBody.
94      * Used for serialization.
95      */

96     private LocalBodyStore() {
97     }
98
99     //
100
// -- STATIC METHODS -----------------------------------------------
101
//
102
public static LocalBodyStore getInstance() {
103         return instance;
104     }
105
106     //
107
// -- PUBLIC METHODS -----------------------------------------------
108
//
109
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     /**
122      * Returns the body associated with the thread calling the method. If no body is associated with the
123      * calling thread, an HalfBody is created to manage the futures.
124      * @return the body associated to the active object whose active thread is calling this method.
125      */

126     public Body getCurrentThreadBody() {
127         AbstractBody body = (AbstractBody) bodyPerThread.get();
128
129         if (body == null) {
130             // If we cannot find the body from the current thread we assume that the current thread
131
// is not the one from an active object. Therefore in this case we create an HalfBody
132
// that handle the futures
133
body = HalfBody.getHalfBody(this.getHalfBodyMetaObjectFactory());
134             bodyPerThread.set(body);
135             registerHalfBody(body);
136         }
137
138         return body;
139     }
140
141     /**
142      * Associates the body with the thread calling the method.
143      * @param the body to associate to the active thread that calls this method.
144      */

145     public void setCurrentThreadBody(Body body) {
146         bodyPerThread.set(body);
147     }
148
149     /**
150      * Returns the body belonging to this JVM whose ID is the one specified.
151      * Returns null if a body with such an id is not found in this jvm
152      * @param bodyID the ID to look for
153      * @return the body with matching id or null
154      */

155     public Body getLocalBody(UniqueID bodyID) {
156         return (Body) localBodyMap.getBody(bodyID);
157     }
158
159     /**
160      * Returns the halfbody belonging to this JVM whose ID is the one specified.
161      * Returns null if a halfbody with such an id is not found in this jvm
162      * @param bodyID the ID to look for
163      * @return the halfbody with matching id or null
164      */

165     public Body getLocalHalfBody(UniqueID bodyID) {
166         return (Body) localHalfBodyMap.getBody(bodyID);
167     }
168
169     /**
170      * Returns all local Bodies in a new BodyMap
171      * @return all local Bodies in a new BodyMap
172      */

173     public BodyMap getLocalBodies() {
174         return (BodyMap) localBodyMap.clone();
175     }
176
177     /**
178      * Returns all local HalfBodies in a new BodyMap
179      * @return all local HalfBodies in a new BodyMap
180      */

181     public BodyMap getLocalHalfBodies() {
182         return (BodyMap) localHalfBodyMap.clone();
183     }
184
185     /**
186      * Adds a listener of body events. The listener is notified every time a body
187      * (active or not) is registered or unregistered in this JVM.
188      * @param listener the listener of body events to add
189      */

190     public void addBodyEventListener(BodyEventListener listener) {
191         bodyEventProducer.addBodyEventListener(listener);
192     }
193
194
195     /**
196      * Removes a listener of body events.
197      * @param listener the listener of body events to remove
198      */

199     public void removeBodyEventListener(BodyEventListener listener) {
200         bodyEventProducer.removeBodyEventListener(listener);
201     }
202
203     //
204
// -- FRIENDLY METHODS -----------------------------------------------
205
//
206
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         //bodyEventProducer.fireBodyCreated(body);
223
}
224
225     void unregisterHalfBody(AbstractBody body) {
226         localHalfBodyMap.removeBody(body.bodyID);
227         //bodyEventProducer.fireBodyRemoved(body);
228
}
229 }
230
Popular Tags