001: /*
002: * The Apache Software License, Version 1.1
003: *
004: *
005: * Copyright (c) 2002 The Apache Software Foundation. All rights
006: * reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Apache Software Foundation (http://www.apache.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "WSIF" and "Apache Software Foundation" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation and was
052: * originally based on software copyright (c) 2001, 2002, International
053: * Business Machines, Inc., http://www.apache.org. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package org.apache.wsif.util.jms;
059:
060: import java.lang.reflect.InvocationTargetException;
061: import java.lang.reflect.Method;
062: import java.util.ArrayList;
063: import java.util.Arrays;
064: import java.util.HashMap;
065: import java.util.Iterator;
066: import java.util.Enumeration;
067:
068: import javax.jms.Destination;
069: import javax.jms.JMSException;
070: import javax.jms.Message;
071: import javax.jms.MessageProducer;
072: import org.apache.wsif.WSIFException;
073: import org.apache.wsif.logging.Trc;
074:
075: /**
076: * WSIFJMSProperties is a HashMap of jms properties. The WSIFJMSProperties can either be IN
077: * or OUT. If IN, the properties can only set on a QueueSender. If OUT, the properties
078: * can only be got from a message. Reflection is used to set and get the properties in JMS.
079: * Using reflection avoids having a table of properties that would have to updated for
080: * different JMS implementations and different versions of JMS.
081: *
082: * @author Mark Whitlock <whitlock@apache.org>
083: */
084: public class WSIFJMSProperties extends HashMap {
085: private static final long serialVersionUID = 1L;
086: public static final String IN = "in";
087: public static final String OUT = "out";
088: private static final ArrayList allDirections = new ArrayList(Arrays
089: .asList(new Object[] { IN, OUT }));
090:
091: private static final String CORRELATIONID = "JMSCorrelationID";
092: private static final String DELIVERYMODE = "JMSDeliveryMode";
093: private static final String DESTINATION = "JMSDestination";
094: private static final String EXPIRATION = "JMSExpiration";
095: private static final String MESSAGEID = "JMSMessageID";
096: private static final String PRIORITY = "JMSPriority";
097: private static final String REDELIVERED = "JMSRedelivered";
098: private static final String REPLYTO = "JMSReplyTo";
099: 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 = new ArrayList(
104: Arrays.asList(new Object[] { CORRELATIONID, DELIVERYMODE,
105: DESTINATION, EXPIRATION, PRIORITY, REDELIVERED,
106: 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(
142: "Only input properties can be set on a MessageProducer");
143:
144: if (isEmpty()) {
145: Trc.exit(false);
146: return false;
147: }
148:
149: for (Iterator it = keySet().iterator(); it.hasNext();)
150: try {
151: String prop = (String) (it.next());
152: Object value = get(prop);
153: Class type = value.getClass();
154:
155: if (predefinedProps.contains(prop))
156: try {
157: if (prop.equals(CORRELATIONID)) {
158: message.setJMSCorrelationID((String) value);
159: } else if (prop.equals(DELIVERYMODE)) {
160: message
161: .setJMSDeliveryMode(((Integer) value)
162: .intValue());
163: producer.setDeliveryMode(((Integer) value)
164: .intValue());
165: } else if (prop.equals(DESTINATION)) {
166: message
167: .setJMSDestination((Destination) value);
168: } else if (prop.equals(EXPIRATION)) {
169: message.setJMSExpiration(((Long) value)
170: .longValue());
171: } else if (prop.equals(PRIORITY)) {
172: message.setJMSPriority(((Integer) value)
173: .intValue());
174: producer.setPriority(((Integer) value)
175: .intValue());
176: } else if (prop.equals(REDELIVERED)) {
177: message.setJMSRedelivered(((Boolean) value)
178: .booleanValue());
179: } else if (prop.equals(REPLYTO)) {
180: message.setJMSReplyTo((Destination) value);
181: } else if (prop.equals(TIMESTAMP)) {
182: message.setJMSTimestamp(((Long) value)
183: .longValue());
184: } else if (prop.equals(TIMETOLIVE)) {
185: producer.setTimeToLive(((Long) value)
186: .longValue());
187: } else if (prop.equals(TYPE)) {
188: message.setJMSType((String) value);
189: }
190: } catch (ClassCastException ce) {
191: Trc.exception(ce);
192: throw new WSIFException("Unexpected type "
193: + type + " for JMS property " + prop
194: + ".");
195: }
196:
197: // User defined properties
198: else {
199: if (type.equals(String.class))
200: message.setStringProperty(prop, value
201: .toString());
202: else if (type.equals(Integer.class))
203: message.setIntProperty(prop, ((Integer) value)
204: .intValue());
205: else if (type.equals(Boolean.class))
206: message.setBooleanProperty(prop,
207: ((Boolean) value).booleanValue());
208: else if (type.equals(Byte.class))
209: message.setByteProperty(prop, ((Byte) value)
210: .byteValue());
211: else if (type.equals(Double.class))
212: message.setDoubleProperty(prop,
213: ((Double) value).doubleValue());
214: else if (type.equals(Float.class))
215: message.setFloatProperty(prop, ((Float) value)
216: .floatValue());
217: else if (type.equals(Long.class))
218: message.setLongProperty(prop, ((Long) value)
219: .longValue());
220: else if (type.equals(Short.class))
221: message.setShortProperty(prop, ((Short) value)
222: .shortValue());
223: else
224: message.setObjectProperty(prop, value);
225: }
226: } catch (JMSException je) {
227: Trc.exception(je);
228: throw WSIFJMSConstants.ToWsifException(je);
229: }
230:
231: Trc.exit(true);
232: return true;
233: }
234:
235: /**
236: * Get all the properties from a Message and load them into this HashMap.
237: */
238: public void getPropertiesFromMessage(Message message) throws WSIFException {
239: Trc.entry(this , message);
240: if (direction != OUT)
241: throw new WSIFException("Only output properties can be got from a message");
242:
243: clear();
244: try {
245: put(CORRELATIONID, message.getJMSCorrelationID());
246: put(DELIVERYMODE, new Integer(message.getJMSDeliveryMode()));
247: put(DESTINATION, message.getJMSDestination());
248: put(EXPIRATION, new Long(message.getJMSExpiration()));
249: put(MESSAGEID, message.getJMSMessageID());
250: put(PRIORITY, new Integer(message.getJMSPriority()));
251: put(REDELIVERED, new Boolean(message.getJMSRedelivered()));
252: put(REPLYTO, message.getJMSReplyTo());
253: put(TIMESTAMP, new Long(message.getJMSTimestamp()));
254: put(TYPE, message.getJMSType());
255:
256: Enumeration enum = message.getPropertyNames();
257: while (enum.hasMoreElements()) {
258: String name = (String) enum.nextElement();
259: put(name, message.getObjectProperty(name));
260: }
261: } catch (JMSException je) {
262: Trc.exception(je);
263: throw WSIFJMSConstants.ToWsifException(je);
264: }
265:
266: if (Trc.ON)
267: Trc.exit(deep());
268: }
269:
270: public void clear() {
271: Trc.entry(this );
272: super .clear();
273: Trc.exit();
274: }
275:
276: public Object get(Object o1) {
277: Trc.entry(this , o1);
278: Object o2 = super .get(o1);
279: Trc.exit(o2);
280: return o2;
281: }
282:
283: public Object put(Object o1, Object o2) {
284: Trc.entry(this , o1, o2);
285: Object o3 = super .put(o1, o2);
286: Trc.exit(o3);
287: return o3;
288: }
289:
290: public void putAll(HashMap hm) {
291: Trc.entry(this , hm);
292: super .putAll(hm);
293: Trc.exit();
294: }
295:
296: public String toString() {
297: return "WSIFJMSProperties(" + size() + "," + hashCode() + ")";
298: }
299:
300: public String deep() {
301: String buff = "";
302: try {
303: buff = new String(super .toString() + "\n");
304: buff += "direction:"
305: + (direction.equals(IN) ? "in" : direction
306: .equals(OUT) ? "out" : "unknown");
307:
308: } catch (Exception e) {
309: Trc.exceptionInTrace(e);
310: }
311: return buff;
312: }
313: }
|