KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > shared > client > AbstractJmsMessage


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2006 ScalAgent Distributed Technologies
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.1 of the License, or 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
18  * USA.
19  *
20  * Initial developer(s): ScalAgent Distributed Technologies
21  * Contributor(s):
22  */

23 package org.objectweb.joram.shared.client;
24
25 import java.io.Externalizable JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.io.ObjectInput JavaDoc;
29 import java.io.ObjectOutput JavaDoc;
30 import java.io.ByteArrayInputStream JavaDoc;
31 import java.io.ByteArrayOutputStream JavaDoc;
32 import java.io.IOException JavaDoc;
33
34 import java.util.Hashtable JavaDoc;
35
36 import org.objectweb.joram.shared.stream.Streamable;
37 import org.objectweb.joram.shared.stream.StreamUtil;
38
39 import fr.dyade.aaa.util.Debug;
40 import org.objectweb.util.monolog.api.BasicLevel;
41 import org.objectweb.util.monolog.api.Logger;
42
43 /**
44  * An <code>AbstractJmsMessage</code> is a message exchanged between a
45  * Joram client and its proxy.
46  */

47 public abstract class AbstractJmsMessage implements Externalizable JavaDoc, Streamable {
48   public static Logger logger = Debug.getLogger(AbstractJmsMessage.class.getName());
49
50   protected final static int NULL_CLASS_ID = -1;
51
52   protected final static int CNX_CONNECT_REQUEST = 0;
53   protected final static int CNX_CONNECT_REPLY = 1;
54   protected final static int CNX_START_REQUEST = 2;
55   protected final static int CNX_STOP_REQUEST = 3;
56   protected final static int CNX_CLOSE_REQUEST = 4;
57   protected final static int CNX_CLOSE_REPLY = 5;
58   protected final static int PRODUCER_MESSAGES = 6;
59   protected final static int CONSUMER_RECEIVE_REQUEST = 7; // TODO
60
protected final static int CONSUMER_MESSAGES = 8; // TODO
61
protected final static int CONSUMER_SUB_REQUEST = 9;
62   protected final static int CONSUMER_UNSUB_REQUEST = 10;
63   protected final static int CONSUMER_ACK_REQUEST = 11; // TODO
64
protected final static int CONSUMER_DENY_REQUEST = 12; // TODO
65
protected final static int SESS_ACK_REQUEST = 13;
66   protected final static int SESS_DENY_REQUEST = 14;
67   protected final static int MOM_EXCEPTION_REPLY = 15;
68   protected final static int SERVER_REPLY = 16; // TODO
69

70   protected final static int ACTIVATE_CONSUMER_REQUEST = 17;
71   protected final static int COMMIT_REQUEST = 18;
72   protected final static int CONSUMER_CLOSE_SUB_REQUEST = 19;
73   protected final static int CONSUMER_SET_LIST_REQUEST = 20;
74   protected final static int CONSUMER_UNSET_LIST_REQUEST = 21;
75   protected final static int GET_ADMIN_TOPIC_REPLY = 22;
76   protected final static int GET_ADMIN_TOPIC_REQUEST = 23;
77   protected final static int JMS_REQUEST_GROUP = 24;
78   protected final static int PING_REQUEST = 25;
79   protected final static int QBROWSE_REPLY = 26;
80   protected final static int QBROWSE_REQUEST = 27;
81   protected final static int SESS_CREATE_TDREPLY = 28;
82   protected final static int SESS_CREATE_TQREQUEST = 29;
83   protected final static int SESS_CREATE_TTREQUEST = 30;
84   protected final static int TEMP_DEST_DELETE_REQUEST = 31;
85   protected final static int XA_CNX_COMMIT = 32;
86   protected final static int XA_CNX_PREPARE = 33;
87   protected final static int XA_CNX_RECOVER_REPLY = 34;
88   protected final static int XA_CNX_RECOVER_REQUEST = 35;
89   protected final static int XA_CNX_ROLLBACK = 36;
90
91   protected int classid;
92
93   protected static final String JavaDoc[] classnames = {
94     "org.objectweb.joram.shared.client.CnxConnectRequest",
95     "org.objectweb.joram.shared.client.CnxConnectReply",
96     "org.objectweb.joram.shared.client.CnxStartRequest",
97     "org.objectweb.joram.shared.client.CnxStopRequest",
98     "org.objectweb.joram.shared.client.CnxCloseRequest",
99     "org.objectweb.joram.shared.client.CnxCloseReply",
100     "org.objectweb.joram.shared.client.ProducerMessages",
101     "org.objectweb.joram.shared.client.ConsumerReceiveRequest",
102     "org.objectweb.joram.shared.client.ConsumerMessages",
103     "org.objectweb.joram.shared.client.ConsumerSubRequest",
104     "org.objectweb.joram.shared.client.ConsumerUnsubRequest",
105     "org.objectweb.joram.shared.client.ConsumerAckRequest",
106     "org.objectweb.joram.shared.client.ConsumerDenyRequest",
107     "org.objectweb.joram.shared.client.SessAckRequest",
108     "org.objectweb.joram.shared.client.SessDenyRequest",
109     "org.objectweb.joram.shared.client.MomExceptionReply",
110     "org.objectweb.joram.shared.client.ServerReply",
111
112     "org.objectweb.joram.shared.client.ActivateConsumerRequest",
113     "org.objectweb.joram.shared.client.CommitRequest",
114     "org.objectweb.joram.shared.client.ConsumerCloseSubRequest",
115     "org.objectweb.joram.shared.client.ConsumerSetListRequest",
116     "org.objectweb.joram.shared.client.ConsumerUnsetListRequest",
117     "org.objectweb.joram.shared.client.GetAdminTopicReply",
118     "org.objectweb.joram.shared.client.GetAdminTopicRequest",
119     "org.objectweb.joram.shared.client.JmsRequestGroup",
120     "org.objectweb.joram.shared.client.PingRequest",
121     "org.objectweb.joram.shared.client.QBrowseReply",
122     "org.objectweb.joram.shared.client.QBrowseRequest",
123     "org.objectweb.joram.shared.client.SessCreateTDReply",
124     "org.objectweb.joram.shared.client.SessCreateTQRequest",
125     "org.objectweb.joram.shared.client.SessCreateTTRequest",
126     "org.objectweb.joram.shared.client.TempDestDeleteRequest",
127     "org.objectweb.joram.shared.client.XACnxCommit",
128     "org.objectweb.joram.shared.client.XACnxPrepare",
129     "org.objectweb.joram.shared.client.XACnxRecoverReply",
130     "org.objectweb.joram.shared.client.XACnxRecoverRequest",
131     "org.objectweb.joram.shared.client.XACnxRollback"
132   };
133
134   protected abstract int getClassId();
135
136   /**
137    * Constructs an <code>AbstractJmsMessage</code>.
138    */

139   public AbstractJmsMessage() {
140     classid = getClassId();
141   }
142
143   /** ***** ***** ***** ***** ***** ***** ***** *****
144    * Interface needed for soap serialization
145    * ***** ***** ***** ***** ***** ***** ***** ***** */

146
147   /**
148    *
149    * @exception IOException
150    */

151   public Hashtable JavaDoc soapCode() throws IOException JavaDoc {
152     Hashtable JavaDoc h = new Hashtable JavaDoc();
153     h.put("classname", getClass().getName());
154
155     ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
156     writeTo(baos);
157     baos.flush();
158     h.put("bytecontent", baos.toByteArray());
159     baos.close();
160
161     return h;
162   }
163
164   /**
165    *
166    * @exception ClassNotFound
167    * @exception InstantiationException
168    * @exception IllegalAccessException
169    * @exception IOException
170    */

171   public static Object JavaDoc soapDecode(Hashtable JavaDoc h) throws Exception JavaDoc {
172     AbstractJmsMessage msg = null;
173     ByteArrayInputStream JavaDoc bais = null;
174
175     try {
176       String JavaDoc classname = (String JavaDoc) h.get("classname");
177       msg = (AbstractJmsMessage) Class.forName(classname).newInstance();
178       byte[] content = (byte[]) h.get("bytecontent");
179       bais = new ByteArrayInputStream JavaDoc(content);
180       msg.readFrom(bais);
181     } finally {
182       bais.close();
183     }
184
185     return msg;
186   }
187
188   /** ***** ***** ***** ***** ***** ***** ***** *****
189    * Externalizable interface
190    * ***** ***** ***** ***** ***** ***** ***** ***** */

191
192   public final void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
193     writeTo((OutputStream JavaDoc) out);
194   }
195
196   public final void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
197     readFrom((InputStream JavaDoc) in);
198   }
199
200   /** ***** ***** ***** ***** ***** ***** ***** *****
201    * Streamable interface
202    * ***** ***** ***** ***** ***** ***** ***** ***** */

203
204   static public void write(AbstractJmsMessage msg,
205                            OutputStream JavaDoc os) throws IOException JavaDoc {
206     if (logger.isLoggable(BasicLevel.DEBUG))
207       logger.log(BasicLevel.DEBUG, "AbstractJmsMessage.write: " + msg);
208
209     if (msg == null) {
210       StreamUtil.writeTo(NULL_CLASS_ID, os);
211     } else {
212       StreamUtil.writeTo(msg.getClassId(), os);
213       msg.writeTo(os);
214     }
215   }
216
217   static public AbstractJmsMessage read(InputStream JavaDoc is) throws IOException JavaDoc, ClassNotFoundException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc {
218     int classid = -1;
219     AbstractJmsMessage msg = null;
220
221     classid = StreamUtil.readIntFrom(is);
222     if (classid != NULL_CLASS_ID) {
223       msg = (AbstractJmsMessage) Class.forName(classnames[classid]).newInstance();
224       msg.readFrom(is);
225     }
226
227     if (logger.isLoggable(BasicLevel.DEBUG))
228       logger.log(BasicLevel.DEBUG, "AbstractJmsMessage.read: " + msg);
229
230     return msg;
231   }
232 }
233
Popular Tags