KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > dream > message > codec > MessageCodecOptimizedStreamImpl


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):
23  */

24
25 package org.objectweb.dream.message.codec;
26
27 import java.io.DataInput JavaDoc;
28 import java.io.DataOutput JavaDoc;
29 import java.io.Externalizable JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.ObjectInput JavaDoc;
32 import java.io.ObjectOutput JavaDoc;
33 import java.util.Iterator JavaDoc;
34
35 import org.objectweb.dream.message.Chunk;
36 import org.objectweb.dream.message.ChunkAlreadyExistsException;
37 import org.objectweb.dream.message.ChunkType;
38 import org.objectweb.dream.message.ExtensibleMessage;
39 import org.objectweb.dream.message.Message;
40 import org.objectweb.dream.message.MessageNC;
41 import org.objectweb.dream.message.MessageType;
42 import org.objectweb.dream.message.MessageTypeImpl;
43 import org.objectweb.dream.message.MessageTypeNCImpl;
44 import org.objectweb.dream.message.manager.MessageManager;
45 import org.objectweb.dream.pool.ObjectPool;
46 import org.objectweb.dream.pool.Recyclable;
47 import org.objectweb.dream.util.Error;
48 import org.objectweb.fractal.api.NoSuchInterfaceException;
49 import org.objectweb.fractal.api.control.IllegalBindingException;
50 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
51
52 /**
53  * Optimized implementation of message codec for extensible message. <br>
54  * Requirements :
55  * <ul>
56  * <li>Decoded messages (ie. newly created message returned by the message
57  * manager) must implement {@link ExtensibleMessage}</li>
58  * <li>Messages must provide correct message types (ie. the
59  * {@link Message#getMessageType() }method must be implemented)</li>
60  * <li>Every chunks must implements interface {@link Externalizable}</li>
61  * <li>Decoded chunks (ie. newly created chunks returned by the message
62  * manager) must implements {@link Chunk }interface</li>
63  * <li>Chunk types must implements {@link Externalizable}</li>
64  * </ul>
65  */

66 public class MessageCodecOptimizedStreamImpl
67     extends
68       AbstractMessageCodecObjectStream
69 {
70   // ---------------------------------------------------------------------------
71
// Client interface
72
// ---------------------------------------------------------------------------
73

74   /** The name of the {@link ChunkType }pool client interface name */
75   public static final String JavaDoc CHUNK_TYPE_POOL_ITF_NAME = "chunk-type-pool";
76   protected ObjectPool chunkTypePoolItf = null;
77
78   // ---------------------------------------------------------------------------
79
// Implementation of the AbstractMessageCodecDataStream abstract methods
80
// ---------------------------------------------------------------------------
81

82   /**
83    * @see AbstractMessageCodecDataStream#doDecodeSingleMessage(DataInput)
84    */

85   protected Message doDecodeSingleMessage(DataInput JavaDoc input) throws IOException JavaDoc
86   {
87     ObjectInput JavaDoc objectInput = (ObjectInput JavaDoc) input;
88     ExtensibleMessage message;
89     boolean messageNC = objectInput.readBoolean();
90     if (messageNC)
91     {
92       message = (ExtensibleMessage) messageManagerItf
93           .createMessage(MessageTypeNCImpl.EMPTY_MESSAGE_TYPE_NC);
94     }
95     else
96     {
97       message = (ExtensibleMessage) messageManagerItf
98           .createMessage(MessageTypeImpl.EMPTY_MESSAGE_TYPE);
99     }
100
101     ChunkType chunkType = (ChunkType) chunkTypePoolItf.newInstance();
102     boolean hasMoreChunk = objectInput.readBoolean();
103     try
104     {
105       while (hasMoreChunk)
106       {
107         String JavaDoc chunkName = objectInput.readUTF();
108         ((Externalizable JavaDoc) chunkType).readExternal(objectInput);
109         Externalizable JavaDoc chunk = (Externalizable JavaDoc) messageManagerItf
110             .createChunk(chunkType);
111         chunk.readExternal(objectInput);
112         message.addChunk(chunkName, ((Chunk) chunk).getType(), chunk);
113         hasMoreChunk = objectInput.readBoolean();
114       }
115     }
116     catch (ClassNotFoundException JavaDoc e)
117     {
118       throw new IOException JavaDoc("Unable to decode message : " + e.getMessage());
119     }
120     catch (ChunkAlreadyExistsException e)
121     {
122       // cannot happen
123
Error.bug(logger, e);
124     }
125     finally
126     {
127       chunkTypePoolItf.recycleInstance((Recyclable) chunkType);
128     }
129     message.setMessageManagerId(messageManagerItf.getMessageManagerId());
130     return message;
131   }
132
133   /**
134    * @see AbstractMessageCodecDataStream#doEncodeSingleMessage(Message,
135    * DataOutput)
136    */

137   protected void doEncodeSingleMessage(Message message, DataOutput JavaDoc output)
138       throws IOException JavaDoc
139   {
140     ObjectOutput JavaDoc objectOutput = (ObjectOutput JavaDoc) output;
141     if (message instanceof MessageNC)
142     {
143       objectOutput.writeBoolean(true);
144     }
145     else
146     {
147       objectOutput.writeBoolean(false);
148     }
149     MessageType type = message.getMessageType();
150     Iterator JavaDoc iterator = type.getChunkNamesIterator();
151     while (iterator.hasNext())
152     {
153       String JavaDoc chunkName = (String JavaDoc) iterator.next();
154       ChunkType chunkType = type.getChunkType(chunkName);
155       Object JavaDoc chunk = message.getChunk(chunkName);
156       objectOutput.writeBoolean(true);
157       objectOutput.writeUTF(chunkName);
158       ((Externalizable JavaDoc) chunkType).writeExternal(objectOutput);
159       ((Externalizable JavaDoc) chunk).writeExternal(objectOutput);
160     }
161     objectOutput.writeBoolean(false);
162   }
163
164   // ---------------------------------------------------------------------------
165
// Implementation of BindingController interface
166
// ---------------------------------------------------------------------------
167

168   /**
169    * @see org.objectweb.fractal.api.control.BindingController#listFc()
170    */

171   public String JavaDoc[] listFc()
172   {
173     return new String JavaDoc[]{MessageManager.ITF_NAME, CHUNK_TYPE_POOL_ITF_NAME};
174   }
175
176   /**
177    * @see org.objectweb.fractal.api.control.BindingController#bindFc(String,
178    * Object)
179    */

180   public synchronized void bindFc(String JavaDoc clientItfName, Object JavaDoc serverItf)
181       throws NoSuchInterfaceException, IllegalBindingException,
182       IllegalLifeCycleException
183   {
184     super.bindFc(clientItfName, serverItf);
185     if (clientItfName.equals(CHUNK_TYPE_POOL_ITF_NAME))
186     {
187       chunkTypePoolItf = (ObjectPool) serverItf;
188     }
189   }
190
191 }
Popular Tags