org/objectweb/proactive/core/body/BodyMap.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 java.util.Hashtable;
00034 
00035 import org.objectweb.proactive.Body;
00036 import org.objectweb.proactive.core.UniqueID;
00037 import org.objectweb.proactive.core.event.AbstractEventProducer;
00038 import org.objectweb.proactive.core.event.BodyEvent;
00039 import org.objectweb.proactive.core.event.BodyEventListener;
00040 import org.objectweb.proactive.core.event.ProActiveEvent;
00041 import org.objectweb.proactive.core.event.ProActiveListener;
00042 
00043 
00061 public class BodyMap extends AbstractEventProducer implements Cloneable,
00062     java.io.Externalizable {
00063     //
00064     // -- PRIVATE MEMBER -----------------------------------------------
00065     //
00066     private Hashtable<UniqueID, UniversalBody> idToBodyMap;
00067 
00068     //
00069     // -- CONSTRUCTORS -----------------------------------------------
00070     //
00071     public BodyMap() {
00072         idToBodyMap = new Hashtable<UniqueID, UniversalBody>();
00073     }
00074 
00075     //
00076     // -- PUBLIC METHODS -----------------------------------------------
00077     // 
00078 
00083     public synchronized void putBody(UniqueID id, UniversalBody b) {
00084         while (idToBodyMap.get(id) != null) {
00085             try {
00086                 wait();
00087             } catch (InterruptedException e) {
00088                 e.printStackTrace();
00089             }
00090         }
00091         idToBodyMap.put(id, b);
00092 
00093         if (hasListeners()) {
00094             notifyAllListeners(new BodyEvent(b, BodyEvent.BODY_CREATED));
00095         }
00096     }
00097 
00102     public synchronized void updateBody(UniqueID id, UniversalBody b) {
00103         //remove old reference
00104         if (idToBodyMap.get(id) != null) {
00105             idToBodyMap.remove(id);
00106         }
00107 
00108         //add new reference
00109         idToBodyMap.put(id, b);
00110 
00111         if (hasListeners()) {
00112             notifyAllListeners(new BodyEvent(b, BodyEvent.BODY_CREATED));
00113         }
00114     }
00115 
00116     public synchronized void removeBody(UniqueID id) {
00117         UniversalBody b = idToBodyMap.remove(id);
00118         notifyAll();
00119 
00120         if ((b != null) && hasListeners()) {
00121             notifyAllListeners(new BodyEvent(b, BodyEvent.BODY_DESTROYED));
00122         }
00123     }
00124 
00125     public synchronized int size() {
00126         int val = idToBodyMap.size();
00127 
00128         return val;
00129     }
00130 
00131     public synchronized UniversalBody getBody(UniqueID id) {
00132         Object o = null;
00133         if (id != null) {
00134             o = idToBodyMap.get(id);
00135         }
00136 
00137         return (UniversalBody) o;
00138     }
00139 
00140     public synchronized boolean containsBody(UniqueID id) {
00141         return idToBodyMap.containsKey(id);
00142     }
00143 
00144     public java.util.Iterator<UniversalBody> bodiesIterator() {
00145         return idToBodyMap.values().iterator();
00146     }
00147 
00148     public synchronized String toString() {
00149         StringBuffer sb = new StringBuffer();
00150 
00151         sb.append(" -- BodyMap ------- \n");
00152 
00153         java.util.Set entrySet = idToBodyMap.entrySet();
00154         java.util.Iterator iterator = entrySet.iterator();
00155 
00156         while (iterator.hasNext()) {
00157             java.util.Map.Entry entry = (java.util.Map.Entry) iterator.next();
00158             sb.append(entry.getKey()).append("  body = ")
00159               .append(entry.getValue()).append("\n");
00160         }
00161 
00162         return sb.toString();
00163     }
00164 
00165     //
00166     // -- implements Cloneable -----------------------------------------------
00167     //
00168     public Object clone() {
00169         BodyMap newLocationTable = new BodyMap();
00170         newLocationTable.idToBodyMap = (Hashtable<UniqueID, UniversalBody>)idToBodyMap.clone();
00171 
00172         return newLocationTable;
00173     }
00174 
00175     //
00176     // -- methods for BodyEventProducer -----------------------------------------------
00177     //
00178     public void addBodyEventListener(BodyEventListener listener) {
00179         addListener(listener);
00180     }
00181 
00182     public void removeBodyEventListener(BodyEventListener listener) {
00183         removeListener(listener);
00184     }
00185 
00186     //
00187     // -- implements Externalizable -----------------------------------------------
00188     //
00189 
00194     public synchronized void readExternal(java.io.ObjectInput in)
00195         throws java.io.IOException, ClassNotFoundException {
00196         int size = in.readInt();
00197 
00198         for (int i = 0; i < size; i++) {
00199             UniqueID id = (UniqueID) in.readObject();
00200             UniversalBody remoteBody = (UniversalBody) in.readObject();
00201             idToBodyMap.put(id, remoteBody);
00202         }
00203     }
00204 
00210     public synchronized void writeExternal(java.io.ObjectOutput out)
00211         throws java.io.IOException {
00212         int size = idToBodyMap.size();
00213         out.writeInt(size);
00214 
00215         java.util.Set entrySet = idToBodyMap.entrySet();
00216         java.util.Iterator iterator = entrySet.iterator();
00217 
00218         while (iterator.hasNext()) {
00219             java.util.Map.Entry entry = (java.util.Map.Entry) iterator.next();
00220             out.writeObject(entry.getKey());
00221 
00222             Object value = entry.getValue();
00223 
00224             if (value instanceof Body) {
00225                 out.writeObject(((Body) value).getRemoteAdapter());
00226             } else {
00227                 out.writeObject(value);
00228             }
00229         }
00230     }
00231 
00232     //
00233     // -- PROTECTED METHODS -----------------------------------------------
00234     //
00235     protected void notifyOneListener(ProActiveListener listener,
00236         ProActiveEvent event) {
00237         BodyEvent bodyEvent = (BodyEvent) event;
00238 
00239         switch (bodyEvent.getType()) {
00240         case BodyEvent.BODY_CREATED:
00241             ((BodyEventListener) listener).bodyCreated(bodyEvent);
00242             break;
00243         case BodyEvent.BODY_DESTROYED:
00244             ((BodyEventListener) listener).bodyDestroyed(bodyEvent);
00245             break;
00246         }
00247     }
00248 }

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