KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > scalagent > kjoram > jms > ConsumerMessages


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2001 - ScalAgent Distributed Technologies
4  * Copyright (C) 1996 - Dyade
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA.
20  *
21  * Initial developer(s): Frederic Maistre (INRIA)
22  * Contributor(s): Nicolas Tachker (ScalAgent)
23  */

24 package com.scalagent.kjoram.jms;
25
26 import com.scalagent.kjoram.messages.Message;
27 import java.util.Hashtable JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.Vector JavaDoc;
30
31 /**
32  * A <code>ConsumerMessages</code> is used by a JMS proxy for sending messages
33  * to a consumer.
34  */

35 public class ConsumerMessages extends AbstractJmsReply
36 {
37   /** Wrapped message. */
38   private Message message = null;
39   /** Wrapped messages. */
40   private Vector JavaDoc messages = null;
41   /** Name of the subscription or the queue the messages come from. */
42   private String JavaDoc comingFrom = null;
43   /** <code>true</code> if the messages come from a queue. */
44   private boolean queueMode;
45
46
47   /**
48    * Constructs a <code>ConsumerMessages</code> instance.
49    *
50    * @param correlationId Reply identifier.
51    * @param message Message to wrap.
52    * @param comingFrom Name of the queue or the subscription the message
53    * come from.
54    * @param queueMode <code>true</code> if the message come from a queue.
55    */

56   public ConsumerMessages(int correlationId, Message message,
57                           String JavaDoc comingFrom, boolean queueMode)
58   {
59     super(correlationId);
60     this.message = message;
61     this.comingFrom = comingFrom;
62     this.queueMode = queueMode;
63     if (message != null) {
64       messages = new Vector JavaDoc();
65       messages.addElement(message);
66     }
67   }
68
69   /**
70    * Constructs a <code>ConsumerMessages</code> instance.
71    *
72    * @param correlationId Reply identifier.
73    * @param messages Messages to wrap.
74    * @param comingFrom Name of the queue or the subscription the messages
75    * comes from.
76    * @param queueMode <code>true</code> if the messages come from a queue.
77    */

78   public ConsumerMessages(int correlationId, Vector JavaDoc messages,
79                           String JavaDoc comingFrom, boolean queueMode)
80   {
81     super(correlationId);
82     this.messages = messages;
83     this.comingFrom = comingFrom;
84     this.queueMode = queueMode;
85   }
86
87   /**
88    * Constructs an empty <code>ConsumerMessages</code> instance.
89    *
90    * @param correlationId Reply identifier.
91    * @param comingFrom Name of the queue or the subscription the reply
92    * comes from.
93    * @param queueMode <code>true</code> if it replies to a queue consumer.
94    */

95   public ConsumerMessages(int correlationId, String JavaDoc comingFrom,
96                           boolean queueMode)
97   {
98     super(correlationId);
99     this.comingFrom = comingFrom;
100     this.queueMode = queueMode;
101     messages = new Vector JavaDoc();
102   }
103
104   /**
105    * Constructs an empty <code>ConsumerMessages</code> instance.
106    */

107   public ConsumerMessages() {
108     messages = new Vector JavaDoc();
109   }
110
111   /** Returns the messages to deliver. */
112   public Vector JavaDoc getMessages()
113   {
114     if (messages == null) {
115       messages = new Vector JavaDoc();
116       if (message != null)
117         messages.addElement(message);
118     }
119     return messages;
120   }
121
122   /**
123    * Returns the name of the queue or the subscription the messages come
124    * from.
125    */

126   public String JavaDoc comesFrom()
127   {
128     return comingFrom;
129   }
130
131   public void setComesFrom(String JavaDoc comingFrom) {
132     this.comingFrom = comingFrom;
133   }
134
135   /** Returns <code>true</code> if the messages come from a queue. */
136   public boolean getQueueMode()
137   {
138     return queueMode;
139   }
140
141   public void setQueueMode(boolean queueMode) {
142     this.queueMode = queueMode;
143   }
144
145   /** Returns the first sent message. */
146   public Message getMessage()
147   {
148     if (messages == null || messages.isEmpty())
149       return null;
150
151     return (Message) messages.elementAt(0);
152   }
153
154   public void addMessage(Message msg) {
155     messages.addElement(msg);
156   }
157
158   public void setMessage(Message msg) {
159     message = msg;
160   }
161
162   /**
163    * Transforms this reply into a hashtable of primitive values that can
164    * be vehiculated through the SOAP protocol.
165    */

166   public Hashtable JavaDoc soapCode() {
167     Hashtable JavaDoc h = super.soapCode();
168     if (comingFrom != null)
169       h.put("comingFrom",comingFrom);
170     h.put("queueMode",new Boolean JavaDoc(queueMode));
171     // Coding and adding the messages into a array:
172
int size = 0;
173     if (messages != null)
174       size = messages.size();
175     if (size > 0) {
176       Vector JavaDoc arrayMsg = new Vector JavaDoc();
177       for (int i = 0; i<size; i++) {
178         Message msg = (Message) messages.elementAt(0);
179         messages.removeElementAt(0);
180         arrayMsg.insertElementAt(msg.soapCode(),i);
181       }
182       if (arrayMsg != null)
183         h.put("arrayMsg",arrayMsg);
184     } else {
185       if (message != null) {
186         h.put("singleMsg",message.soapCode());
187       }
188     }
189     return h;
190   }
191
192   /**
193    * Transforms a hashtable of primitive values into a
194    * <code>ConsumerMessages</code> reply.
195    */

196   public static Object JavaDoc soapDecode(Hashtable JavaDoc h) {
197     ConsumerMessages req = new ConsumerMessages();
198     req.setCorrelationId(((Integer JavaDoc) h.get("correlationId")).intValue());
199     req.setComesFrom((String JavaDoc) h.get("comingFrom"));
200     req.setQueueMode(((Boolean JavaDoc) h.get("queueMode")).booleanValue());
201     Vector JavaDoc arrayMsg = (Vector JavaDoc) h.get("arrayMsg");
202     if (arrayMsg != null) {
203       for (int i = 0; i<arrayMsg.size(); i++)
204         req.addMessage(Message.soapDecode((Hashtable JavaDoc) arrayMsg.elementAt(i)));
205     } else
206       req.setMessage(Message.soapDecode((Hashtable JavaDoc)h.get("singleMsg")));
207     return req;
208   }
209 }
210
Popular Tags