1 24 25 package org.objectweb.dream.message; 26 27 import java.io.Externalizable ; 28 import java.io.IOException ; 29 import java.io.ObjectInput ; 30 import java.io.ObjectOutput ; 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.Map ; 34 35 import org.objectweb.dream.util.EmptyIterator; 36 import org.objectweb.dream.util.EmptyStringArray; 37 import org.objectweb.dream.util.Util; 38 39 43 public abstract class AbstractExtensibleMessage 44 implements 45 ExtensibleMessage, 46 MessageType, 47 MessageReferenceCounter, 48 EmptyStringArray, 49 Externalizable 50 { 51 52 53 protected transient short messageManagerId; 54 55 protected transient short referenceCounter = 1; 56 57 58 protected Map chunks = new HashMap (); 59 60 protected Map chunkTypes = new HashMap (); 61 62 63 protected transient String [] chunkNames = null; 64 65 69 72 public Object getChunk(String name) 73 { 74 return chunks.get(name); 75 } 76 77 80 public MessageType getMessageType() 81 { 82 return this; 83 } 84 85 88 public void transfertChunkStates(Message newInstance) 89 { 90 Iterator iter = chunks.entrySet().iterator(); 91 while (iter.hasNext()) 92 { 93 Map.Entry e = (Map.Entry ) iter.next(); 94 ((Chunk) e.getValue()).transfertState((Chunk) newInstance 95 .getChunk((String ) e.getKey())); 96 } 97 } 98 99 102 public short getMessageManagerId() 103 { 104 return messageManagerId; 105 } 106 107 110 public void setMessageManagerId(short id) 111 { 112 messageManagerId = id; 113 } 114 115 119 122 public void recycle() 123 { 124 if (!chunks.isEmpty()) 125 { 126 chunks.clear(); 127 chunkTypes.clear(); 128 } 129 } 130 131 135 138 public void addChunk(String name, ChunkType chkType, Object chunk) 139 throws ChunkAlreadyExistsException 140 { 141 Object previousChunk = chunks.put(name, chunk); 142 if (previousChunk != null) 143 { 144 chunks.put(name, previousChunk); 145 throw new ChunkAlreadyExistsException(); 146 } 147 chunkTypes.put(name, chkType); 148 chunkNames = null; 149 } 150 151 154 public Object removeChunk(String name) 155 { 156 if (chunkTypes.remove(name) != null) 157 { 158 chunkNames = null; 159 } 160 return chunks.remove(name); 161 } 162 163 167 170 public String [] getChunkNames() 171 { 172 if (chunkNames == null) 173 { 174 if (chunks.isEmpty()) 175 { 176 chunkNames = EMPTY_STRING_ARRAY; 177 } 178 else 179 { 180 chunkNames = (String []) chunks.keySet().toArray(EMPTY_STRING_ARRAY); 181 } 182 } 183 return chunkNames; 184 } 185 186 189 public Iterator getChunkNamesIterator() 190 { 191 if (chunks.isEmpty()) 192 { 193 return EmptyIterator.INSTANCE; 194 } 195 return chunks.keySet().iterator(); 196 } 197 198 201 public ChunkType getChunkType(String name) 202 { 203 return (ChunkType) chunkTypes.get(name); 204 } 205 206 209 public boolean isEmpty() 210 { 211 return chunks.isEmpty(); 212 } 213 214 218 221 public void incrementReferenceCounter() 222 { 223 referenceCounter++; 224 } 225 226 229 public boolean decrementReferenceCounter() 230 { 231 synchronized (this) 232 { 233 referenceCounter--; 234 return (referenceCounter == 0); 235 } 236 } 237 238 242 protected void readChunksState(ObjectInput in) throws IOException , 243 ClassNotFoundException 244 { 245 int size = in.readInt(); 246 chunks = new HashMap (size); 247 chunkTypes = new HashMap (size); 248 for (int i = 0; i < size; i++) 249 { 250 String chunkName = in.readUTF(); 251 Object chunk = Util.readObject(in); 252 Object chunkType = Util.readObject(in); 253 chunks.put(chunkName, chunk); 254 chunkTypes.put(chunkName, chunkType); 255 } 256 } 257 258 protected void writeChunksState(ObjectOutput out) throws IOException 259 { 260 out.writeInt(chunks.size()); 261 Iterator iter = getChunkNamesIterator(); 262 while (iter.hasNext()) 263 { 264 String chunkName = (String ) iter.next(); 265 Object chunk = chunks.get(chunkName); 266 Object chunkType = chunkTypes.get(chunkName); 267 out.writeUTF(chunkName); 268 Util.writeObject(out, chunk); 269 Util.writeObject(out, chunkType); 270 } 271 } 272 } | Popular Tags |