1 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 59 public class BodyMap extends AbstractEventProducer implements Cloneable , 60 java.io.Externalizable { 61 private java.util.HashMap idToBodyMap; 65 66 public BodyMap() { 70 idToBodyMap = new java.util.HashMap (); 71 } 72 73 77 81 public synchronized void putBody(UniqueID id, UniversalBody b) { 82 while (idToBodyMap.get(id) != null) { 83 try { 84 wait(); 85 } catch (InterruptedException 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 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 bodiesIterator() { 127 return idToBodyMap.values().iterator(); 128 } 129 130 public synchronized String toString() { 131 StringBuffer sb = new StringBuffer (); 132 sb.append(" -- BodyMap ------- \n"); 133 java.util.Set entrySet = idToBodyMap.entrySet(); 134 java.util.Iterator 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 public Object clone() { 147 BodyMap newLocationTable = new BodyMap(); 148 newLocationTable.idToBodyMap = (java.util.HashMap ) idToBodyMap.clone(); 149 return newLocationTable; 150 } 151 152 public void addBodyEventListener(BodyEventListener listener) { 156 addListener(listener); 157 } 158 159 public void removeBodyEventListener(BodyEventListener listener) { 160 removeListener(listener); 161 } 162 163 167 171 public synchronized void readExternal(java.io.ObjectInput in) 172 throws java.io.IOException , ClassNotFoundException { 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 186 public synchronized void writeExternal(java.io.ObjectOutput out) 187 throws java.io.IOException { 188 int size = idToBodyMap.size(); 189 out.writeInt(size); 190 java.util.Set entrySet = idToBodyMap.entrySet(); 191 java.util.Iterator 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 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 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 |