KickJava   Java API By Example, From Geeks To Geeks.

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


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>ProducerMessages</code> instance is sent by a
33  * <code>MessageProducer</code> when sending messages.
34  */

35 public class ProducerMessages extends AbstractJmsRequest
36 {
37   /** The wrapped message. */
38   private Message message = null;
39   /** The wrapped messages. */
40   private Vector JavaDoc messages = null;
41
42
43   /**
44    * Constructs a <code>ProducerMessages</code> instance.
45    *
46    * @param dest Name of the destination the messages are sent to.
47    */

48   public ProducerMessages(String JavaDoc dest)
49   {
50     super(dest);
51     messages = new Vector JavaDoc();
52   }
53
54   /**
55    * Constructs a <code>ProducerMessages</code> instance carrying a single
56    * message.
57    *
58    * @param dest Name of the destination the messages are sent to.
59    * @param msg Message to carry.
60    */

61   public ProducerMessages(String JavaDoc dest, Message msg)
62   {
63     super(dest);
64     message = msg;
65   }
66
67
68   /** Adds a message to deliver. */
69   public void addMessage(Message msg)
70   {
71     messages.addElement(msg);
72   }
73
74   /** set a message to deliver. */
75   public void setMessage(Message msg) {
76     message = msg;
77   }
78
79   /** Adds messages to deliver. */
80   public void addMessages(Vector JavaDoc msgs)
81   {
82     for (Enumeration JavaDoc e = msgs.elements(); e.hasMoreElements(); )
83       messages.addElement(e.nextElement());
84   }
85
86   /** Returns the produced messages. */
87   public Vector JavaDoc getMessages()
88   {
89     if (message != null) {
90       Vector JavaDoc vec = new Vector JavaDoc();
91       vec.addElement(message);
92       return vec;
93     }
94     return messages;
95   }
96   
97   /**
98    * Transforms this request into a hashtable of primitive values that can
99    * be vehiculated through the SOAP protocol.
100    */

101   public Hashtable JavaDoc soapCode() {
102     Hashtable JavaDoc h = super.soapCode();
103     // Coding and adding the messages into a array:
104
int size = 0;
105     if (messages != null)
106       size = messages.size();
107     if (size > 0) {
108       Vector JavaDoc arrayMsg = new Vector JavaDoc();
109       for (int i = 0; i<size; i++) {
110         Message msg = (Message) messages.elementAt(0);
111         messages.removeElementAt(0);
112         arrayMsg.insertElementAt(msg.soapCode(),i);
113       }
114       if (!arrayMsg.isEmpty())
115         h.put("arrayMsg",arrayMsg);
116     } else {
117       if (message != null) {
118         h.put("singleMsg",message.soapCode());
119       }
120     }
121     return h;
122   }
123
124   /**
125    * Transforms a hastable of primitive values into a
126    * <code>ProducerMessages</code> request.
127    */

128   public static Object JavaDoc soapDecode(Hashtable JavaDoc h) {
129     ProducerMessages req = new ProducerMessages(null);
130     req.setRequestId(((Integer JavaDoc) h.get("requestId")).intValue());
131     req.setTarget((String JavaDoc) h.get("target"));
132     Vector JavaDoc arrayMsg = (Vector JavaDoc) h.get("arrayMsg");
133     if (arrayMsg != null) {
134       for (int i = 0; i<arrayMsg.size(); i++)
135         req.addMessage(Message.soapDecode((Hashtable JavaDoc) arrayMsg.elementAt(i)));
136     } else
137       req.setMessage(Message.soapDecode((Hashtable JavaDoc)h.get("singleMsg")));
138     return req;
139   }
140 }
141
Popular Tags