KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > dream > message > AbstractExtensibleMessage


1 /**
2  * Dream
3  * Copyright (C) 2003-2004 INRIA Rhone-Alpes
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact : dream@objectweb.org
20  *
21  * Initial developer(s): Matthieu Leclercq
22  * Contributor(s): Vivien Quema
23  */

24
25 package org.objectweb.dream.message;
26
27 import java.io.Externalizable JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.ObjectInput JavaDoc;
30 import java.io.ObjectOutput JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Map JavaDoc;
34
35 import org.objectweb.dream.util.EmptyIterator;
36 import org.objectweb.dream.util.EmptyStringArray;
37 import org.objectweb.dream.util.Util;
38
39 /**
40  * Abstract implementation of the <code>ExtensibleMessage</code> interface.
41  * Methods managing sub messages are not implemented.
42  */

43 public abstract class AbstractExtensibleMessage
44     implements
45       ExtensibleMessage,
46       MessageType,
47       MessageReferenceCounter,
48       EmptyStringArray,
49       Externalizable JavaDoc
50 {
51
52   /** The id of the message manager that created this chunk. */
53   protected transient short messageManagerId;
54   /** The number of reference to this message. */
55   protected transient short referenceCounter = 1;
56
57   /** Contains (name, chunk object) pairs. */
58   protected Map JavaDoc chunks = new HashMap JavaDoc();
59   /** Contains (name, chunk type) pairs. */
60   protected Map JavaDoc chunkTypes = new HashMap JavaDoc();
61
62   /** The chunk names */
63   protected transient String JavaDoc[] chunkNames = null;
64
65   // ---------------------------------------------------------------------------
66
// Implementation of the Message interface
67
// ---------------------------------------------------------------------------
68

69   /**
70    * @see Message#getChunk(String)
71    */

72   public Object JavaDoc getChunk(String JavaDoc name)
73   {
74     return chunks.get(name);
75   }
76
77   /**
78    * @see Message#getMessageType()
79    */

80   public MessageType getMessageType()
81   {
82     return this;
83   }
84
85   /**
86    * @see Message#transfertChunkStates(Message)
87    */

88   public void transfertChunkStates(Message newInstance)
89   {
90     Iterator JavaDoc iter = chunks.entrySet().iterator();
91     while (iter.hasNext())
92     {
93       Map.Entry JavaDoc e = (Map.Entry JavaDoc) iter.next();
94       ((Chunk) e.getValue()).transfertState((Chunk) newInstance
95           .getChunk((String JavaDoc) e.getKey()));
96     }
97   }
98
99   /**
100    * @see Message#getMessageManagerId()
101    */

102   public short getMessageManagerId()
103   {
104     return messageManagerId;
105   }
106
107   /**
108    * @see Message#setMessageManagerId(short)
109    */

110   public void setMessageManagerId(short id)
111   {
112     messageManagerId = id;
113   }
114
115   // ---------------------------------------------------------------------------
116
// Implementation of the Recyclable interface
117
// ---------------------------------------------------------------------------
118

119   /**
120    * @see org.objectweb.dream.pool.Recyclable#recycle()
121    */

122   public void recycle()
123   {
124     if (!chunks.isEmpty())
125     {
126       chunks.clear();
127       chunkTypes.clear();
128     }
129   }
130
131   // ---------------------------------------------------------------------------
132
// Implementation of the ExtensibleMessage interface
133
// ---------------------------------------------------------------------------
134

135   /**
136    * @see ExtensibleMessage#addChunk(String, ChunkType, Object)
137    */

138   public void addChunk(String JavaDoc name, ChunkType chkType, Object JavaDoc chunk)
139       throws ChunkAlreadyExistsException
140   {
141     Object JavaDoc 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   /**
152    * @see ExtensibleMessage#removeChunk(String)
153    */

154   public Object JavaDoc removeChunk(String JavaDoc name)
155   {
156     if (chunkTypes.remove(name) != null)
157     {
158       chunkNames = null;
159     }
160     return chunks.remove(name);
161   }
162
163   // ---------------------------------------------------------------------------
164
// Implementation of the MessageType interface
165
// ---------------------------------------------------------------------------
166

167   /**
168    * @see MessageType#getChunkNames()
169    */

170   public String JavaDoc[] getChunkNames()
171   {
172     if (chunkNames == null)
173     {
174       if (chunks.isEmpty())
175       {
176         chunkNames = EMPTY_STRING_ARRAY;
177       }
178       else
179       {
180         chunkNames = (String JavaDoc[]) chunks.keySet().toArray(EMPTY_STRING_ARRAY);
181       }
182     }
183     return chunkNames;
184   }
185
186   /**
187    * @see MessageType#getChunkNamesIterator()
188    */

189   public Iterator JavaDoc getChunkNamesIterator()
190   {
191     if (chunks.isEmpty())
192     {
193       return EmptyIterator.INSTANCE;
194     }
195     return chunks.keySet().iterator();
196   }
197
198   /**
199    * @see MessageType#getChunkType(String)
200    */

201   public ChunkType getChunkType(String JavaDoc name)
202   {
203     return (ChunkType) chunkTypes.get(name);
204   }
205
206   /**
207    * @see MessageType#isEmpty()
208    */

209   public boolean isEmpty()
210   {
211     return chunks.isEmpty();
212   }
213
214   // ---------------------------------------------------------------------------
215
// Implementation of the MessageReferenceCounter interface
216
// ---------------------------------------------------------------------------
217

218   /**
219    * @see MessageReferenceCounter#incrementReferenceCounter()
220    */

221   public void incrementReferenceCounter()
222   {
223     referenceCounter++;
224   }
225
226   /**
227    * @see MessageReferenceCounter#decrementReferenceCounter()
228    */

229   public boolean decrementReferenceCounter()
230   {
231     synchronized (this)
232     {
233       referenceCounter--;
234       return (referenceCounter == 0);
235     }
236   }
237
238   // ---------------------------------------------------------------------------
239
// Serialisation methods
240
// ---------------------------------------------------------------------------
241

242   protected void readChunksState(ObjectInput JavaDoc in) throws IOException JavaDoc,
243       ClassNotFoundException JavaDoc
244   {
245     int size = in.readInt();
246     chunks = new HashMap JavaDoc(size);
247     chunkTypes = new HashMap JavaDoc(size);
248     for (int i = 0; i < size; i++)
249     {
250       String JavaDoc chunkName = in.readUTF();
251       Object JavaDoc chunk = Util.readObject(in);
252       Object JavaDoc chunkType = Util.readObject(in);
253       chunks.put(chunkName, chunk);
254       chunkTypes.put(chunkName, chunkType);
255     }
256   }
257
258   protected void writeChunksState(ObjectOutput JavaDoc out) throws IOException JavaDoc
259   {
260     out.writeInt(chunks.size());
261     Iterator JavaDoc iter = getChunkNamesIterator();
262     while (iter.hasNext())
263     {
264       String JavaDoc chunkName = (String JavaDoc) iter.next();
265       Object JavaDoc chunk = chunks.get(chunkName);
266       Object JavaDoc chunkType = chunkTypes.get(chunkName);
267       out.writeUTF(chunkName);
268       Util.writeObject(out, chunk);
269       Util.writeObject(out, chunkType);
270     }
271   }
272 }
Popular Tags