KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 import org.objectweb.dream.AbstractComponent;
33 import org.objectweb.dream.message.ExtensibleMessage;
34 import org.objectweb.dream.message.ExtensibleMessageNC;
35 import org.objectweb.dream.message.Message;
36 import org.objectweb.dream.message.MessageAlreadyExistException;
37 import org.objectweb.dream.message.MessageNC;
38 import org.objectweb.dream.message.MessageTypeNC;
39 import org.objectweb.dream.message.manager.MessageManager;
40 import org.objectweb.dream.util.Error;
41 import org.objectweb.fractal.api.NoSuchInterfaceException;
42 import org.objectweb.fractal.api.control.IllegalBindingException;
43 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
44
45 /**
46  * Abstract implementation of a message codec using data input/output to
47  * decode/encode messages. This class implements
48  */

49 public abstract class AbstractMessageCodecDataStream extends AbstractComponent
50     implements
51       MessageCodec
52 {
53
54   protected MessageManager messageManagerItf;
55
56   // ---------------------------------------------------------------------------
57
// Encoding methods
58
// ---------------------------------------------------------------------------
59

60   /**
61    * Decode recursively a message and its sub messages (if any). This method use
62    * the {@link #doDecodeSingleMessage(DataInput) }method to decode a single
63    * message.
64    */

65   protected Message doDecode(DataInput JavaDoc input) throws IOException JavaDoc
66   {
67     Message message = doDecodeSingleMessage(input);
68     if (message instanceof MessageNC)
69     {
70       boolean hasSubMessage = input.readBoolean();
71       if (hasSubMessage)
72       {
73         if (!(message instanceof ExtensibleMessage))
74         {
75           throw new IOException JavaDoc(
76               "Invalid decoded message, must implement ExtensibleMessage.");
77         }
78         // read unnamed sub message.
79
ExtensibleMessage extensibleMessage = (ExtensibleMessage) message;
80         while (hasSubMessage)
81         {
82           Message subMessage = doDecode(input);
83           extensibleMessage.addSubMessage(subMessage);
84           hasSubMessage = input.readBoolean();
85         }
86       }
87
88       hasSubMessage = input.readBoolean();
89       if (hasSubMessage)
90       {
91         if (!(message instanceof ExtensibleMessageNC))
92         {
93           throw new IOException JavaDoc(
94               "Invalid decoded message, must implements ExtensibleMessageNC.");
95         }
96         // read named sub message.
97
ExtensibleMessageNC extensibleMessageNC = (ExtensibleMessageNC) message;
98         while (hasSubMessage)
99         {
100           String JavaDoc subMessageName = input.readUTF();
101           Message subMessage = doDecode(input);
102           try
103           {
104             extensibleMessageNC.addSubMessage(subMessageName, subMessage);
105           }
106           catch (MessageAlreadyExistException e)
107           {
108             Error.bug(logger, e);
109           }
110           hasSubMessage = input.readBoolean();
111         }
112       }
113     }
114     else
115     {
116       boolean hasSubMessage = input.readBoolean();
117       if (hasSubMessage)
118       {
119         if (!(message instanceof ExtensibleMessage))
120         {
121           throw new IOException JavaDoc(
122               "Invalid decoded message, must implement ExtensibleMessage.");
123         }
124         ExtensibleMessage extensibleMessage = (ExtensibleMessage) message;
125         while (hasSubMessage)
126         {
127           Message subMessage = doDecode(input);
128           extensibleMessage.addSubMessage(subMessage);
129           hasSubMessage = input.readBoolean();
130         }
131       }
132     }
133     return message;
134   }
135
136   /**
137    * Abstract method used by {@link #doDecode(DataInput) }to decode a single
138    * message. This method decode only chunks of message.
139    *
140    * @param input the input from which message is decoded.
141    * @return a message.
142    * @throws IOException if an error occurs.
143    */

144   protected abstract Message doDecodeSingleMessage(DataInput JavaDoc input)
145       throws IOException JavaDoc;
146
147   /**
148    * Encode recursively a message and its sub messages (if any). This method use
149    * the {@link #doEncodeSingleMessage(Message, DataOutput) }method to encode a
150    * single message.
151    */

152   protected void doEncode(Message message, DataOutput JavaDoc output)
153       throws IOException JavaDoc
154   {
155     doEncodeSingleMessage(message, output);
156     if (message instanceof MessageNC)
157     {
158       MessageNC messageNC = (MessageNC) message;
159       Iterator JavaDoc iterator = messageNC.getUnnamedSubMessageIterator();
160       while (iterator.hasNext())
161       {
162         output.writeBoolean(true);
163         Message subMessage = (Message) iterator.next();
164         doEncode(subMessage, output);
165       }
166       output.writeBoolean(false);
167       MessageTypeNC messageTypeNC = (MessageTypeNC) messageNC.getMessageType();
168       iterator = messageTypeNC.getSubMessageNamesIterator();
169       while (iterator.hasNext())
170       {
171         output.writeBoolean(true);
172         String JavaDoc subMessageName = (String JavaDoc) iterator.next();
173         output.writeUTF(subMessageName);
174         doEncode(messageNC.getSubMessage(subMessageName), output);
175       }
176       output.writeBoolean(false);
177     }
178     else
179     {
180       Iterator JavaDoc iterator = message.getSubMessageIterator();
181       while (iterator.hasNext())
182       {
183         output.writeBoolean(true);
184         Message subMessage = (Message) iterator.next();
185         doEncode(subMessage, output);
186       }
187       output.writeBoolean(false);
188     }
189   }
190
191   /**
192    * Abstract method used by {@link #doEncode(Message, DataOutput) }to encode a
193    * single message. This method encode only chunks of message.
194    *
195    * @param message a message to encode.
196    * @param output the output to use to encode the message.
197    * @throws IOException if an error occurs.
198    */

199   protected abstract void doEncodeSingleMessage(Message message,
200       DataOutput JavaDoc output) throws IOException JavaDoc;
201
202   // ---------------------------------------------------------------------------
203
// Implementation of BindingController interface
204
// ---------------------------------------------------------------------------
205

206   /**
207    * @see org.objectweb.fractal.api.control.BindingController#listFc()
208    */

209   public String JavaDoc[] listFc()
210   {
211     return new String JavaDoc[]{MessageManager.ITF_NAME};
212   }
213
214   /**
215    * @see org.objectweb.fractal.api.control.BindingController#bindFc(String,
216    * Object)
217    */

218   public synchronized void bindFc(String JavaDoc clientItfName, Object JavaDoc serverItf)
219       throws NoSuchInterfaceException, IllegalBindingException,
220       IllegalLifeCycleException
221   {
222     if (clientItfName.equals(MessageManager.ITF_NAME))
223     {
224       messageManagerItf = (MessageManager) serverItf;
225     }
226     super.bindFc(clientItfName, serverItf);
227   }
228
229 }
Popular Tags