KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > wsif > providers > jms > PrimitiveTypeFormatHandler


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.providers.jms;
59
60 import java.text.DateFormat JavaDoc;
61 import java.text.SimpleDateFormat JavaDoc;
62
63 import javax.jms.JMSException JavaDoc;
64 import javax.jms.Message JavaDoc;
65 import javax.xml.namespace.QName JavaDoc;
66
67 import org.apache.wsif.format.jms.JMSFormatHandler;
68 import org.apache.wsif.logging.Trc;
69
70 public class PrimitiveTypeFormatHandler implements JMSFormatHandler {
71     private static final long serialVersionUID = 1L;
72
73     protected static DateFormat JavaDoc GREGORIAN_CALENDAR_DATE_FORMAT =
74         new SimpleDateFormat JavaDoc("yyyy-MM-dd");
75     protected static DateFormat JavaDoc STANDARD_DATE_FORMAT =
76         new SimpleDateFormat JavaDoc("yyyy-MM-dd'T'HH:mm:ss'Z'");
77     protected static DateFormat JavaDoc PRECISE_DATE_FORMAT =
78         new SimpleDateFormat JavaDoc("yyyy-MM-dd'T'HH:mm:ss'.'SSS'Z'");
79
80     QName JavaDoc name;
81     Object JavaDoc primitiveType;
82     Class JavaDoc type;
83     
84     /**
85      * Constructor for CustomerInfoFormatHandler
86      */

87     public PrimitiveTypeFormatHandler(Class JavaDoc type) {
88         super();
89         this.type = type;
90     }
91
92     /**
93      * @see JMSFormatHandler#getElement(String)
94      */

95     public Object JavaDoc getElement(String JavaDoc arg0) {
96         return null;
97     }
98
99     /**
100      * @see JMSFormatHandler#setElement(String, Object)
101      */

102     public void setElement(String JavaDoc arg0, Object JavaDoc arg1) {
103     }
104
105     /**
106      * @see JMSFormatHandler#getElement(String, int)
107      */

108     public Object JavaDoc getElement(String JavaDoc arg0, int arg1) {
109         return null;
110     }
111
112     /**
113      * @see JMSFormatHandler#setElement(String, int, Object)
114      */

115     public void setElement(String JavaDoc arg0, int arg1, Object JavaDoc arg2) {
116     }
117
118     /**
119      * @see JMSFormatHandler#getObjectPart()
120      */

121     public Object JavaDoc getObjectPart() {
122         return primitiveType;
123     }
124
125     /**
126      * @see JMSFormatHandler#getObjectPart(Class)
127      */

128     public Object JavaDoc getObjectPart(Class JavaDoc arg0) {
129         return null;
130     }
131
132     public char getCharPart() {
133         return 0;
134     }
135
136     public byte getBytePart() {
137         return 0;
138     }
139
140     public short getShortPart() {
141         return 0;
142     }
143
144     public int getIntPart() {
145         return 0;
146     }
147
148     public long getLongPart() {
149         return 0;
150     }
151
152     public float getFloatPart() {
153         return 0;
154     }
155
156     public double getDoublePart() {
157         return 0;
158     }
159
160     public QName JavaDoc getPartQName() {
161         return name;
162     }
163
164     public void setObjectPart(Object JavaDoc arg0) {
165         primitiveType = arg0;
166     }
167
168     public void setCharPart(char arg0) {
169     }
170
171     public void setBytePart(byte arg0) {
172     }
173
174     public void setShortPart(short arg0) {
175     }
176
177     public void setIntPart(int arg0) {
178     }
179
180     public void setLongPart(long arg0) {
181     }
182
183     public void setFloatPart(float arg0) {
184     }
185
186     public void setDoublePart(double arg0) {
187     }
188
189     public void setPartQName(QName JavaDoc name) {
190         this.name = name;
191     }
192
193     /**
194      * @see JMSFormatHandler#write(Message)
195      */

196     public void write(Message JavaDoc message) {
197         Trc.entry(this, message);
198         try {
199             if (message instanceof javax.jms.TextMessage JavaDoc)
200                 write((javax.jms.TextMessage JavaDoc) message);
201             else if (message instanceof javax.jms.ObjectMessage JavaDoc)
202                 write((javax.jms.ObjectMessage JavaDoc) message);
203         } catch (Exception JavaDoc e) {
204             Trc.exception(e);
205         }
206         Trc.exit();
207     }
208
209     /**
210      * @see JMSFormatHandler#read(Message)
211      */

212     public void read(Message JavaDoc message) {
213         Trc.entry(this, message);
214         try {
215             if (message instanceof javax.jms.TextMessage JavaDoc)
216                 read((javax.jms.TextMessage JavaDoc) message);
217             else if (message instanceof javax.jms.ObjectMessage JavaDoc)
218                 read((javax.jms.ObjectMessage JavaDoc) message);
219         } catch (Exception JavaDoc e) {
220             Trc.exception(e);
221         }
222         Trc.exit();
223
224     }
225
226     private void write(javax.jms.TextMessage JavaDoc message) throws JMSException JavaDoc {
227         String JavaDoc value = null;
228
229         if (primitiveType == null)
230             value = "";
231         else if (primitiveType instanceof java.util.GregorianCalendar JavaDoc)
232             value =
233                 GREGORIAN_CALENDAR_DATE_FORMAT.format(
234                     ((java.util.GregorianCalendar JavaDoc) primitiveType).getTime());
235         else if (primitiveType instanceof java.util.Date JavaDoc)
236             value = STANDARD_DATE_FORMAT.format((java.util.Date JavaDoc) primitiveType);
237         else if (primitiveType instanceof byte[])
238             value = new String JavaDoc((byte[]) primitiveType);
239         else {
240             // Convert the primitiveType to a String
241
value = primitiveType.toString();
242         }
243
244         if (value != null)
245             message.setText(value);
246     }
247
248     private void write(javax.jms.ObjectMessage JavaDoc message) throws JMSException JavaDoc {
249         if (primitiveType != null && primitiveType instanceof java.io.Serializable JavaDoc)
250             message.setObject((java.io.Serializable JavaDoc) primitiveType);
251     }
252
253     private void read(javax.jms.TextMessage JavaDoc message) throws JMSException JavaDoc {
254         String JavaDoc value = message.getText();
255
256         Class JavaDoc cls = type;
257         try {
258             if (cls == String JavaDoc.class) {
259                 primitiveType = value;
260             }
261             // For byte array class
262
else if (cls == byte[].class) {
263                 primitiveType = value.getBytes();
264             }
265             // For Gregorian Calendar
266
else if (java.util.GregorianCalendar JavaDoc.class.isAssignableFrom(cls)) {
267                 java.util.GregorianCalendar JavaDoc result = new java.util.GregorianCalendar JavaDoc();
268                 result.setTime(GREGORIAN_CALENDAR_DATE_FORMAT.parse(value));
269                 primitiveType = result;
270             }
271             // For Date
272
else if (java.util.Date JavaDoc.class.isAssignableFrom(cls)) {
273                 java.util.Date JavaDoc result = new java.util.Date JavaDoc();
274                 try {
275                     result = STANDARD_DATE_FORMAT.parse(value);
276                 } catch (java.text.ParseException JavaDoc standardException) {
277                     Trc.exception(standardException);
278                     result = PRECISE_DATE_FORMAT.parse(value);
279                 }
280                 primitiveType = result;
281             }
282             // For all the rest
283
else {
284                 java.lang.reflect.Constructor JavaDoc constructor =
285                     cls.getConstructor(new Class JavaDoc[] { String JavaDoc.class });
286                 primitiveType = constructor.newInstance(new Object JavaDoc[] { value });
287             }
288         } catch (Exception JavaDoc e) {
289             Trc.exception(e);
290             throw new JMSException JavaDoc("Unable to parse message");
291         }
292
293     }
294
295     private void read(javax.jms.ObjectMessage JavaDoc message) throws JMSException JavaDoc {
296         primitiveType = message.getObject();
297         // Check to see if the contents is of the expected class type
298
if (!primitiveType.getClass().isAssignableFrom(type))
299             throw new JMSException JavaDoc("Unable to parse message");
300     }
301 }
Popular Tags