KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > wsif > util > jms > WSIFJMSProperties


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "WSIF" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2001, 2002, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package org.apache.wsif.util.jms;
59
60 import java.lang.reflect.InvocationTargetException;
61 import java.lang.reflect.Method;
62 import java.util.ArrayList;
63 import java.util.Arrays;
64 import java.util.HashMap;
65 import java.util.Iterator;
66 import java.util.Enumeration;
67
68 import javax.jms.Destination;
69 import javax.jms.JMSException;
70 import javax.jms.Message;
71 import javax.jms.MessageProducer;
72 import org.apache.wsif.WSIFException;
73 import org.apache.wsif.logging.Trc;
74
75 /**
76  * WSIFJMSProperties is a HashMap of jms properties. The WSIFJMSProperties can either be IN
77  * or OUT. If IN, the properties can only set on a QueueSender. If OUT, the properties
78  * can only be got from a message. Reflection is used to set and get the properties in JMS.
79  * Using reflection avoids having a table of properties that would have to updated for
80  * different JMS implementations and different versions of JMS.
81  *
82  * @author Mark Whitlock <whitlock@apache.org>
83  */

84 public class WSIFJMSProperties extends HashMap {
85     private static final long serialVersionUID = 1L;
86     public static final String IN = "in";
87     public static final String OUT = "out";
88     private static final ArrayList allDirections =
89         new ArrayList(Arrays.asList(new Object[] { IN, OUT }));
90
91     private static final String CORRELATIONID = "JMSCorrelationID";
92     private static final String DELIVERYMODE = "JMSDeliveryMode";
93     private static final String DESTINATION = "JMSDestination";
94     private static final String EXPIRATION = "JMSExpiration";
95     private static final String MESSAGEID = "JMSMessageID";
96     private static final String PRIORITY = "JMSPriority";
97     private static final String REDELIVERED = "JMSRedelivered";
98     private static final String REPLYTO = "JMSReplyTo";
99     private static final String TIMESTAMP = "JMSTimestamp";
100     private static final String TIMETOLIVE = "JMSTimeToLive";
101     private static final String TYPE = "JMSType";
102   
103     private static final ArrayList predefinedProps =
104         new ArrayList(Arrays.asList(new Object[]{
105         CORRELATIONID,DELIVERYMODE,DESTINATION,EXPIRATION,PRIORITY,
106         REDELIVERED,REPLYTO,TIMESTAMP,TIMETOLIVE, TYPE}));
107
108     private String direction;
109
110     /**
111      * Constructor for WSIFJMSProperties
112      */

113     public WSIFJMSProperties(String direction) throws WSIFException {
114         super();
115         Trc.entry(this, direction);
116
117         if (!allDirections.contains(direction))
118             throw new WSIFException("Invalid direction " + direction);
119         this.direction = direction;
120         Trc.exit(deep());
121     }
122
123     /**
124      * Constructor for WSIFJMSProperties from another HashMap.
125      */

126     public WSIFJMSProperties(WSIFJMSProperties props) {
127         super(props);
128         Trc.entry(this, props);
129         direction = props.direction;
130         Trc.exit(deep());
131     }
132
133     /**
134      * Set all the properties that have been loaded into this HashMap on a QueueSender.
135      * @return whether any properties were set on this QueueSender
136      */

137     public boolean set(MessageProducer producer, Message message)
138         throws WSIFException {
139         Trc.entry(this, producer, message);
140         if (direction != IN)
141             throw new WSIFException("Only input properties can be set on a MessageProducer");
142     
143         if (isEmpty()) {
144             Trc.exit(false);
145             return false;
146         }
147     
148         for (Iterator it = keySet().iterator(); it.hasNext();)
149             try {
150                 String prop = (String) (it.next());
151                 Object value = get(prop);
152                 Class type = value.getClass();
153     
154                 if (predefinedProps.contains(prop))
155                     try {
156                         if (prop.equals(CORRELATIONID)) {
157                             message.setJMSCorrelationID((String) value);
158                         } else if (prop.equals(DELIVERYMODE)) {
159                             message.setJMSDeliveryMode(
160                                 ((Integer) value).intValue());
161                             producer.setDeliveryMode(((Integer) value).intValue());
162                         } else if (prop.equals(DESTINATION)) {
163                             message.setJMSDestination((Destination) value);
164                         } else if (prop.equals(EXPIRATION)) {
165                             message.setJMSExpiration(((Long) value).longValue());
166                         } else if (prop.equals(PRIORITY)) {
167                             message.setJMSPriority(((Integer) value).intValue());
168                             producer.setPriority(((Integer) value).intValue());
169                         } else if (prop.equals(REDELIVERED)) {
170                             message.setJMSRedelivered(
171                                 ((Boolean) value).booleanValue());
172                         } else if (prop.equals(REPLYTO)) {
173                             message.setJMSReplyTo((Destination) value);
174                         } else if (prop.equals(TIMESTAMP)) {
175                             message.setJMSTimestamp(((Long) value).longValue());
176                         } else if (prop.equals(TIMETOLIVE)) {
177                             producer.setTimeToLive(((Long) value).longValue());
178                         } else if(prop.equals(TYPE)) {
179                             message.setJMSType((String)value);
180                         }
181                     } catch (ClassCastException ce) {
182                         Trc.exception(ce);
183                         throw new WSIFException(
184                             "Unexpected type "
185                                 + type
186                                 + " for JMS property "
187                                 + prop
188                                 + ".");
189                     }
190     
191                 // User defined properties
192
else {
193                     if (type.equals(String.class))
194                         message.setStringProperty(prop, value.toString());
195                     else if (type.equals(Integer.class))
196                         message.setIntProperty(prop, ((Integer) value).intValue());
197                     else if (type.equals(Boolean.class))
198                         message.setBooleanProperty(
199                             prop,
200                             ((Boolean) value).booleanValue());
201                     else if (type.equals(Byte.class))
202                         message.setByteProperty(prop, ((Byte) value).byteValue());
203                     else if (type.equals(Double.class))
204                         message.setDoubleProperty(
205                             prop,
206                             ((Double) value).doubleValue());
207                     else if (type.equals(Float.class))
208                         message.setFloatProperty(
209                             prop,
210                             ((Float) value).floatValue());
211                     else if (type.equals(Long.class))
212                         message.setLongProperty(prop, ((Long) value).longValue());
213                     else if (type.equals(Short.class))
214                         message.setShortProperty(
215                             prop,
216                             ((Short) value).shortValue());
217                     else
218                         message.setObjectProperty(prop, value);
219                 }
220             } catch (JMSException je) {
221                 Trc.exception(je);
222                 throw WSIFJMSConstants.ToWsifException(je);
223             }
224     
225         Trc.exit(true);
226         return true;
227     }
228
229     /**
230      * Get all the properties from a Message and load them into this HashMap.
231      */

232     public void getPropertiesFromMessage(Message message) throws WSIFException {
233         Trc.entry(this, message);
234         if (direction != OUT)
235             throw new WSIFException("Only output properties can be got from a message");
236     
237         clear();
238         try {
239             put(CORRELATIONID, message.getJMSCorrelationID());
240             put(DELIVERYMODE, new Integer(message.getJMSDeliveryMode()));
241             put(DESTINATION, message.getJMSDestination());
242             put(EXPIRATION, new Long(message.getJMSExpiration()));
243             put(MESSAGEID, message.getJMSMessageID());
244             put(PRIORITY, new Integer(message.getJMSPriority()));
245             put(REDELIVERED, new Boolean(message.getJMSRedelivered()));
246             put(REPLYTO, message.getJMSReplyTo());
247             put(TIMESTAMP, new Long(message.getJMSTimestamp()));
248             put(TYPE, message.getJMSType());
249     
250             Enumeration enum = message.getPropertyNames();
251             while (enum.hasMoreElements()) {
252                 String name = (String) enum.nextElement();
253                 put(name, message.getObjectProperty(name));
254             }
255         } catch (JMSException je) {
256             Trc.exception(je);
257             throw WSIFJMSConstants.ToWsifException(je);
258         }
259     
260         if (Trc.ON)
261             Trc.exit(deep());
262     }
263
264     public void clear() {
265         Trc.entry(this);
266         super.clear();
267         Trc.exit();
268     }
269
270     public Object get(Object o1) {
271         Trc.entry(this, o1);
272         Object o2 = super.get(o1);
273         Trc.exit(o2);
274         return o2;
275     }
276
277     public Object put(Object o1, Object o2) {
278         Trc.entry(this, o1, o2);
279         Object o3 = super.put(o1, o2);
280         Trc.exit(o3);
281         return o3;
282     }
283
284     public void putAll(HashMap hm) {
285         Trc.entry(this, hm);
286         super.putAll(hm);
287         Trc.exit();
288     }
289
290     public String toString() {
291         return "WSIFJMSProperties(" + size() + "," + hashCode() + ")";
292     }
293
294     public String deep() {
295         String buff = "";
296         try {
297             buff = new String(super.toString() + "\n");
298             buff += "direction:"
299                 + (direction.equals(IN) ? "in" : direction.equals(OUT) ? "out" : "unknown");
300
301         } catch (Exception e) {
302             Trc.exceptionInTrace(e);
303         }
304         return buff;
305     }
306 }
Popular Tags