KickJava   Java API By Example, From Geeks To Geeks.

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


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.objectweb.proactive.Body;
34 import org.objectweb.proactive.core.UniqueID;
35 import org.objectweb.proactive.core.event.AbstractEventProducer;
36 import org.objectweb.proactive.core.event.BodyEvent;
37 import org.objectweb.proactive.core.event.BodyEventListener;
38 import org.objectweb.proactive.core.event.ProActiveEvent;
39 import org.objectweb.proactive.core.event.ProActiveListener;
40
41
42 /**
43  * <i><font size="-1" color="#FF0000">**For internal use only** </font></i><br>
44  * <p>
45  * This class is a Map between UniqueID and either remote or local bodies.
46  * It accepts event listeners interested in BodyEvent.
47  * Body event are produced whenever a body is added or removed from
48  * the collection.
49  * </p><p>
50  * In case of serialization of a object of this class, all reference to local bodies will
51  * get serialized as reference of remote body. Local bodies are never serialized from
52  * this container.
53  * </p>
54  *
55  * @author ProActive Team
56  * @version 1.1, 2001/12/23
57  * @since ProActive 0.9
58  */

59 public class BodyMap extends AbstractEventProducer implements Cloneable JavaDoc,
60     java.io.Externalizable JavaDoc {
61     //
62
// -- PRIVATE MEMBER -----------------------------------------------
63
//
64
private java.util.HashMap JavaDoc idToBodyMap;
65
66     //
67
// -- CONSTRUCTORS -----------------------------------------------
68
//
69
public BodyMap() {
70         idToBodyMap = new java.util.HashMap JavaDoc();
71     }
72
73     //
74
// -- PUBLIC METHODS -----------------------------------------------
75
//
76

77     /**
78      * add the set (id, node) in the idToBodyMap
79      * block if it already exists until it is removed
80      */

81     public synchronized void putBody(UniqueID id, UniversalBody b) {
82         while (idToBodyMap.get(id) != null) {
83             try {
84                 wait();
85             } catch (InterruptedException JavaDoc e) {
86                 e.printStackTrace();
87             }
88         }
89         idToBodyMap.put(id, b);
90         if (hasListeners()) {
91             notifyAllListeners(new BodyEvent(b, BodyEvent.BODY_CREATED));
92         }
93     }
94
95     /**
96      * add the set (id, node) in the idToBodyMap
97      * erase any previous entry
98      */

99     public synchronized void updateBody(UniqueID id, UniversalBody b) {
100         idToBodyMap.put(id, b);
101         if (hasListeners()) {
102             notifyAllListeners(new BodyEvent(b, BodyEvent.BODY_CREATED));
103         }
104     }
105
106     public synchronized void removeBody(UniqueID id) {
107         UniversalBody b = (UniversalBody) idToBodyMap.remove(id);
108         notifyAll();
109         if ((b != null) && hasListeners()) {
110             notifyAllListeners(new BodyEvent(b, BodyEvent.BODY_DESTROYED));
111         }
112     }
113
114     public synchronized int size() {
115         return idToBodyMap.size();
116     }
117
118     public synchronized UniversalBody getBody(UniqueID id) {
119         return (UniversalBody) idToBodyMap.get(id);
120     }
121
122     public synchronized boolean containsBody(UniqueID id) {
123         return idToBodyMap.containsKey(id);
124     }
125
126     public java.util.Iterator JavaDoc bodiesIterator() {
127         return idToBodyMap.values().iterator();
128     }
129
130     public synchronized String JavaDoc toString() {
131         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
132         sb.append(" -- BodyMap ------- \n");
133         java.util.Set JavaDoc entrySet = idToBodyMap.entrySet();
134         java.util.Iterator JavaDoc iterator = entrySet.iterator();
135         while (iterator.hasNext()) {
136             java.util.Map.Entry entry = (java.util.Map.Entry) iterator.next();
137             sb.append(entry.getKey()).append(" body = ")
138               .append(entry.getValue()).append("\n");
139         }
140         return sb.toString();
141     }
142
143     //
144
// -- implements Cloneable -----------------------------------------------
145
//
146
public Object JavaDoc clone() {
147         BodyMap newLocationTable = new BodyMap();
148         newLocationTable.idToBodyMap = (java.util.HashMap JavaDoc) idToBodyMap.clone();
149         return newLocationTable;
150     }
151
152     //
153
// -- methods for BodyEventProducer -----------------------------------------------
154
//
155
public void addBodyEventListener(BodyEventListener listener) {
156         addListener(listener);
157     }
158
159     public void removeBodyEventListener(BodyEventListener listener) {
160         removeListener(listener);
161     }
162
163     //
164
// -- implements Externalizable -----------------------------------------------
165
//
166

167     /**
168      * The object implements the readExternal method to restore its contents by calling the methods
169      * of DataInput for primitive types and readObject for objects, strings and arrays.
170      */

171     public synchronized void readExternal(java.io.ObjectInput JavaDoc in)
172         throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc {
173         int size = in.readInt();
174         for (int i = 0; i < size; i++) {
175             UniqueID id = (UniqueID) in.readObject();
176             UniversalBody remoteBody = (UniversalBody) in.readObject();
177             idToBodyMap.put(id, remoteBody);
178         }
179     }
180
181     /**
182      * The object implements the writeExternal method to save its contents by calling the methods
183      * of DataOutput for its primitive values or calling the writeObject method of ObjectOutput
184      * for objects, strings, and arrays.
185      */

186     public synchronized void writeExternal(java.io.ObjectOutput JavaDoc out)
187         throws java.io.IOException JavaDoc {
188         int size = idToBodyMap.size();
189         out.writeInt(size);
190         java.util.Set JavaDoc entrySet = idToBodyMap.entrySet();
191         java.util.Iterator JavaDoc iterator = entrySet.iterator();
192         while (iterator.hasNext()) {
193             java.util.Map.Entry entry = (java.util.Map.Entry) iterator.next();
194             out.writeObject(entry.getKey());
195             Object JavaDoc value = entry.getValue();
196             if (value instanceof Body) {
197                 out.writeObject(((Body) value).getRemoteAdapter());
198             } else {
199                 out.writeObject(value);
200             }
201         }
202     }
203
204     //
205
// -- PROTECTED METHODS -----------------------------------------------
206
//
207
protected void notifyOneListener(ProActiveListener listener,
208         ProActiveEvent event) {
209         BodyEvent bodyEvent = (BodyEvent) event;
210         switch (bodyEvent.getType()) {
211         case BodyEvent.BODY_CREATED:
212             ((BodyEventListener) listener).bodyCreated(bodyEvent);
213             break;
214         case BodyEvent.BODY_DESTROYED:
215             ((BodyEventListener) listener).bodyDestroyed(bodyEvent);
216             break;
217         }
218     }
219 }
220
Popular Tags