KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
61 import java.util.Iterator JavaDoc;
62
63 import javax.jms.JMSException JavaDoc;
64 import javax.jms.ObjectMessage JavaDoc;
65 import javax.jms.TextMessage JavaDoc;
66 import javax.wsdl.Binding;
67 import javax.wsdl.Definition;
68 import javax.wsdl.Message;
69
70 import org.apache.wsif.WSIFException;
71 import org.apache.wsif.base.WSIFDefaultMessage;
72 import org.apache.wsif.format.jms.JMSFormatHandler;
73 import org.apache.wsif.logging.Trc;
74 import org.apache.wsif.util.WSIFUtils;
75 import org.apache.wsif.wsdl.extensions.format.TypeMapping;
76 import org.apache.wsif.wsdl.extensions.jms.JMSBinding;
77 import org.apache.wsif.wsdl.extensions.jms.JMSConstants;
78 /**
79  * JMSMessage
80  *
81  * @author <a HREF="mailto:seto@ca.ibm.com">Norman Seto</a>
82  * @author <a HREF="mailto:antelder@apache.org">Ant Elder</a>
83  */

84 public class JMSMessage extends WSIFDefaultMessage {
85     private static final long serialVersionUID = 1L;
86
87     // FIXME Derive these constants from Format Binding?
88
private static String JavaDoc XML_ENCODING = "XML";
89     private static String JavaDoc JAVA_ENCODING = "Java";
90
91     static String JavaDoc XML_SCHEMA_1999 = "http://www.w3.org/1999/XMLSchema";
92     static String JavaDoc XML_SCHEMA_2000_10 = "http://www.w3.org/2000/10/XMLSchema";
93     static String JavaDoc XML_SCHEMA_2001 = "http://www.w3.org/2001/XMLSchema";
94
95     static final java.util.HashMap JavaDoc PRIMITIVE_JAVA_MAPPING =
96         new java.util.HashMap JavaDoc();
97
98     // Maps the XSD Type to the equivalent Java class
99
// Missing support for anyType
100
static {
101         PRIMITIVE_JAVA_MAPPING.put("anySimpleType", java.lang.String JavaDoc.class);
102         PRIMITIVE_JAVA_MAPPING.put("anyURI", java.lang.String JavaDoc.class);
103         PRIMITIVE_JAVA_MAPPING.put("base64Binary", byte[].class);
104         PRIMITIVE_JAVA_MAPPING.put("boolean", java.lang.Boolean JavaDoc.class);
105         PRIMITIVE_JAVA_MAPPING.put("byte", java.lang.Byte JavaDoc.class);
106         PRIMITIVE_JAVA_MAPPING.put("date", java.util.GregorianCalendar JavaDoc.class);
107         PRIMITIVE_JAVA_MAPPING.put("dateTime", java.util.Date JavaDoc.class);
108         PRIMITIVE_JAVA_MAPPING.put("decimal", java.math.BigDecimal JavaDoc.class);
109         PRIMITIVE_JAVA_MAPPING.put("double", java.lang.Double JavaDoc.class);
110         PRIMITIVE_JAVA_MAPPING.put("duration", java.lang.String JavaDoc.class);
111         PRIMITIVE_JAVA_MAPPING.put("ENTITIES", java.lang.String JavaDoc.class);
112         PRIMITIVE_JAVA_MAPPING.put("ENTITY", java.lang.String JavaDoc.class);
113         PRIMITIVE_JAVA_MAPPING.put("float", java.lang.Float JavaDoc.class);
114         PRIMITIVE_JAVA_MAPPING.put("gDay", java.lang.String JavaDoc.class);
115         PRIMITIVE_JAVA_MAPPING.put("gMonth", java.lang.String JavaDoc.class);
116         PRIMITIVE_JAVA_MAPPING.put("gMonthDay", java.lang.String JavaDoc.class);
117         PRIMITIVE_JAVA_MAPPING.put("gYear", java.lang.String JavaDoc.class);
118         PRIMITIVE_JAVA_MAPPING.put("gYearMonth", java.lang.String JavaDoc.class);
119         PRIMITIVE_JAVA_MAPPING.put("hexBinary", byte[].class);
120         PRIMITIVE_JAVA_MAPPING.put("ID", java.lang.String JavaDoc.class);
121         PRIMITIVE_JAVA_MAPPING.put("IDREF", java.lang.String JavaDoc.class);
122         PRIMITIVE_JAVA_MAPPING.put("IDREFS", java.lang.String JavaDoc.class);
123         PRIMITIVE_JAVA_MAPPING.put("int", java.lang.Integer JavaDoc.class);
124         PRIMITIVE_JAVA_MAPPING.put("integer", java.math.BigInteger JavaDoc.class);
125         PRIMITIVE_JAVA_MAPPING.put("language", java.lang.String JavaDoc.class);
126         PRIMITIVE_JAVA_MAPPING.put("long", java.lang.Long JavaDoc.class);
127         PRIMITIVE_JAVA_MAPPING.put("Name", java.lang.String JavaDoc.class);
128         PRIMITIVE_JAVA_MAPPING.put("NCName", java.lang.String JavaDoc.class);
129         PRIMITIVE_JAVA_MAPPING.put(
130             "negativeInteger",
131             java.math.BigInteger JavaDoc.class);
132         PRIMITIVE_JAVA_MAPPING.put("NMTOKEN", java.lang.String JavaDoc.class);
133         PRIMITIVE_JAVA_MAPPING.put("NMTOKENS", java.lang.String JavaDoc.class);
134         PRIMITIVE_JAVA_MAPPING.put(
135             "nonNegativeInteger",
136             java.math.BigInteger JavaDoc.class);
137         PRIMITIVE_JAVA_MAPPING.put(
138             "nonPositiveInteger",
139             java.math.BigInteger JavaDoc.class);
140         PRIMITIVE_JAVA_MAPPING.put("normalizedString", java.lang.String JavaDoc.class);
141         PRIMITIVE_JAVA_MAPPING.put("NOTATION", java.lang.String JavaDoc.class);
142         PRIMITIVE_JAVA_MAPPING.put(
143             "positiveInteger",
144             java.math.BigInteger JavaDoc.class);
145         PRIMITIVE_JAVA_MAPPING.put("QName", javax.xml.namespace.QName JavaDoc.class);
146         PRIMITIVE_JAVA_MAPPING.put("short", java.lang.Short JavaDoc.class);
147         PRIMITIVE_JAVA_MAPPING.put("string", java.lang.String JavaDoc.class);
148         PRIMITIVE_JAVA_MAPPING.put("time", java.lang.String JavaDoc.class);
149         PRIMITIVE_JAVA_MAPPING.put("token", java.lang.String JavaDoc.class);
150         PRIMITIVE_JAVA_MAPPING.put("unsignedByte", java.lang.Short JavaDoc.class);
151         PRIMITIVE_JAVA_MAPPING.put("unsignedInt", java.lang.Long JavaDoc.class);
152         PRIMITIVE_JAVA_MAPPING.put("unsignedLong", java.math.BigInteger JavaDoc.class);
153         PRIMITIVE_JAVA_MAPPING.put("unsignedShort", java.lang.Integer JavaDoc.class);
154     }
155
156     private Definition fieldDefinitionModel;
157     private Binding fieldBindingModel;
158     private Message JavaDoc fieldMessageModel;
159     private java.util.List JavaDoc fieldMessageParts;
160
161     /**
162      * Constructor for WSIFMessageFormatHandlerImpl
163      */

164     public JMSMessage(
165         Definition definitionModel,
166         Binding bindingModel,
167         Message JavaDoc messageModel,
168         java.util.List JavaDoc parts) {
169         super();
170         Trc.entry(this, definitionModel, bindingModel, messageModel, parts);
171         fieldDefinitionModel = definitionModel;
172         fieldBindingModel = bindingModel;
173         fieldMessageModel = messageModel;
174         fieldMessageParts = parts;
175         Trc.exit();
176     }
177
178     /**
179      * @see JMSFormatHandler#write(Message)
180      */

181     public void write(javax.jms.Message JavaDoc message) throws WSIFException {
182         Trc.entry(this, message);
183
184         if (!isCorrectMessageType(message))
185             throw new WSIFException("Incorrect message type");
186
187         if (message instanceof javax.jms.TextMessage JavaDoc)
188             write((javax.jms.TextMessage JavaDoc) message);
189         else if (message instanceof javax.jms.ObjectMessage JavaDoc)
190             write((javax.jms.ObjectMessage JavaDoc) message);
191         else
192             throw new WSIFException(
193                 "Unsupported Message Type: " + message.getClass().getName());
194         Trc.exit();
195     }
196
197     /**
198      * Writes the JMS TextMessage
199      */

200     private void write(javax.jms.TextMessage JavaDoc message) throws WSIFException {
201         Trc.entry(this, message);
202
203         // Need to determine the format type mapping
204
if (!XML_ENCODING.equals(getFormatEncoding(fieldBindingModel)))
205             throw new WSIFException("Unable to support non XML encodings in a JMS Text Message");
206
207         try {
208             ArrayList JavaDoc al = new ArrayList JavaDoc();
209             for (Iterator JavaDoc i = this.getPartNames(); i.hasNext();) {
210                 al.add(i.next());
211             }
212             String JavaDoc[] partNames = (String JavaDoc[]) al.toArray(new String JavaDoc[al.size()]);
213
214             if (partNames.length == 1) {
215                 // If there is only one part, set it into the message as is
216
String JavaDoc partName = partNames[0];
217
218                 javax.wsdl.Part partModel = fieldMessageModel.getPart(partName);
219
220                 javax.xml.namespace.QName JavaDoc partQName =
221                     new javax.xml.namespace.QName JavaDoc(
222                         fieldMessageModel.getQName().getNamespaceURI(),
223                         partModel.getName());
224
225                 JMSFormatHandler fh = getFormatHandler(partName);
226
227                 if (fh != null) {
228                     fh.setPartQName(partQName);
229                     fh.setObjectPart(parts.get(partName));
230                     fh.write(message);
231                 } else {
232                     // No format handler
233
Object JavaDoc part = parts.get(partName);
234                     message.setText(part.toString());
235                 }
236             } else {
237                 // Use my own XML Structure
238
// Prepare the stream writers and the serializers
239
java.io.ByteArrayOutputStream JavaDoc os =
240                     new java.io.ByteArrayOutputStream JavaDoc();
241                 java.io.OutputStreamWriter JavaDoc writer =
242                     new java.io.OutputStreamWriter JavaDoc(os);
243
244                 org.apache.xml.serialize.OutputFormat format =
245                     new org.apache.xml.serialize.OutputFormat();
246                 org.apache.xml.serialize.XMLSerializer serializer =
247                     new org.apache.xml.serialize.XMLSerializer(writer, format);
248
249                 String JavaDoc namespace = "";
250
251                 // Start the document
252
serializer.startDocument();
253
254                 // Add the message tag
255
serializer.startElement(
256                     namespace,
257                     fieldMessageModel.getQName().getLocalPart(),
258                     "",
259                     new org.xml.sax.helpers.AttributesImpl JavaDoc());
260
261                 // Cycle the parts, invoking each format handler
262
for (int i = 0; i < partNames.length; i++) {
263                     String JavaDoc partName = partNames[i];
264
265                     // Add a beginning tag
266
serializer.startElement(
267                         namespace,
268                         partName,
269                         "",
270                         new org.xml.sax.helpers.AttributesImpl JavaDoc());
271
272                     javax.wsdl.Part partModel =
273                         fieldMessageModel.getPart(partName);
274                     javax.xml.namespace.QName JavaDoc partQName =
275                         new javax.xml.namespace.QName JavaDoc(
276                             fieldMessageModel.getQName().getNamespaceURI(),
277                             partModel.getName());
278
279                     // Determine the format handler
280
JMSFormatHandler fh = getFormatHandler(partName);
281
282                     if (fh != null) {
283                         fh.setPartQName(partQName);
284                         fh.setObjectPart(parts.get(partName));
285
286                         // Send the message since it is the native format
287
// Reinitialize the message content
288
message.setText("");
289                         fh.write(message);
290                         char[] c = message.getText().toCharArray();
291                         serializer.characters(c, 0, c.length);
292                     } else {
293                         // No format handler
294
Object JavaDoc part = parts.get(partName);
295                         char[] c = part.toString().toCharArray();
296                         serializer.characters(c, 0, c.length);
297                     }
298
299                     // Add the end tag
300
serializer.endElement(partName);
301                 }
302
303                 // Add the end message tag
304
serializer.endElement(
305                     fieldMessageModel.getQName().getLocalPart());
306
307                 // End the document
308
serializer.endDocument();
309
310                 writer.flush();
311                 String JavaDoc msgContents = os.toString();
312                 // Put contents into the message
313
message.setText(msgContents);
314             }
315
316         } catch (JMSException JavaDoc e) {
317             Trc.exception(e);
318             throw new WSIFException("Error in write.", e);
319         } catch (java.io.IOException JavaDoc e) {
320             Trc.exception(e);
321             throw new WSIFException("Error in write.", e);
322         } catch (org.xml.sax.SAXException JavaDoc e) {
323             Trc.exception(e);
324             throw new WSIFException("Error in write.", e);
325         }
326         Trc.exit();
327     }
328
329     /**
330      * Writes the JMS ObjectMessage
331      */

332     private void write(javax.jms.ObjectMessage JavaDoc message) throws WSIFException {
333         Trc.entry(this, message);
334
335         // Need to determine the format type mapping
336
if (!JAVA_ENCODING.equals(getFormatEncoding(fieldBindingModel)))
337             throw new WSIFException("Unable to support non Java encodings in a JMS Object Message");
338
339         try {
340
341             ArrayList JavaDoc al = new ArrayList JavaDoc();
342             for (Iterator JavaDoc i = this.getPartNames(); i.hasNext();) {
343                 al.add(i.next());
344             }
345             String JavaDoc[] partNames = (String JavaDoc[]) al.toArray(new String JavaDoc[al.size()]);
346
347             if (partNames.length == 1) {
348                 // If there is only one part, set it into the message as is
349
String JavaDoc partName = partNames[0];
350
351                 javax.wsdl.Part partModel = fieldMessageModel.getPart(partName);
352
353                 javax.xml.namespace.QName JavaDoc partQName =
354                     new javax.xml.namespace.QName JavaDoc(
355                         fieldMessageModel.getQName().getNamespaceURI(),
356                         partModel.getName());
357
358                 JMSFormatHandler fh = getFormatHandler(partName);
359
360                 if (fh != null) {
361                     fh.setPartQName(partQName);
362                     fh.setObjectPart(parts.get(partName));
363                     fh.write(message);
364                 } else {
365                     // No format handler
366
Object JavaDoc part = parts.get(partName);
367                     // Check to see if it is Serializable.
368
// If so, serialize it
369
try {
370                         message.setObject((java.io.Serializable JavaDoc) part);
371                     } catch (ClassCastException JavaDoc e) {
372                         Trc.exception(e);
373                         throw new WSIFException("Unable to serialize a part");
374                     }
375                 }
376             } else {
377                 // Use a hash map to hold the objects
378
java.util.HashMap JavaDoc result = new java.util.HashMap JavaDoc();
379                 for (int i = 0; i < partNames.length; i++) {
380                     String JavaDoc partName = partNames[i];
381
382                     javax.wsdl.Part partModel =
383                         fieldMessageModel.getPart(partName);
384
385                     javax.xml.namespace.QName JavaDoc partQName =
386                         new javax.xml.namespace.QName JavaDoc(
387                             fieldMessageModel.getQName().getNamespaceURI(),
388                             partModel.getName());
389
390                     JMSFormatHandler fh = getFormatHandler(partName);
391
392                     if (fh != null) {
393                         fh.setPartQName(partQName);
394                         fh.setObjectPart(parts.get(partName));
395                         fh.write(message);
396                         result.put(partName, message.getObject());
397                     } else {
398                         // No format handler
399
Object JavaDoc part = parts.get(partName);
400                         // Check to see if it is Serializable.
401
// If so, serialize it
402
try {
403                             result.put(partName, (java.io.Serializable JavaDoc) part);
404                         } catch (ClassCastException JavaDoc e) {
405                             Trc.exception(e);
406                             throw new WSIFException("Unable to serialize a part");
407                         }
408                     }
409                 }
410                 message.setObject(result);
411             }
412
413         } catch (JMSException JavaDoc e) {
414             Trc.exception(e);
415             throw new WSIFException("Error in write.", e);
416         }
417         Trc.exit();
418     }
419
420     /**
421      * @see JMSFormatHandler#read(Message)
422      */

423     public void read(javax.jms.Message JavaDoc message) throws WSIFException {
424         Trc.entry(this, message);
425
426         if (!isCorrectMessageType(message))
427             throw new WSIFException("Incorrect message type");
428
429         if (message instanceof javax.jms.TextMessage JavaDoc)
430             read((javax.jms.TextMessage JavaDoc) message);
431         else if (message instanceof javax.jms.ObjectMessage JavaDoc)
432             read((javax.jms.ObjectMessage JavaDoc) message);
433         else
434             throw new WSIFException(
435                 "Unsupported Message Type: " + message.getClass().getName());
436         Trc.exit();
437     }
438
439     /**
440      * Reads the JMS TextMessage
441      */

442     private void read(javax.jms.TextMessage JavaDoc message) throws WSIFException {
443         Trc.entry(this, message);
444
445         // Need to determine the format type mapping
446
if (!XML_ENCODING.equals(getFormatEncoding(fieldBindingModel)))
447             throw new WSIFException("Unable to support non XML encodings in a JMS Text Message");
448
449         boolean wsifFormat = false;
450
451         try {
452
453             // Read the text into the document
454
javax.xml.parsers.DocumentBuilderFactory JavaDoc factory =
455                 javax.xml.parsers.DocumentBuilderFactory.newInstance();
456             factory.setNamespaceAware(true);
457             factory.setValidating(false);
458
459             javax.xml.parsers.DocumentBuilder JavaDoc builder =
460                 factory.newDocumentBuilder();
461             builder.setErrorHandler(null);
462
463             String JavaDoc text = message.getText();
464             java.io.ByteArrayInputStream JavaDoc is =
465                 new java.io.ByteArrayInputStream JavaDoc(text.getBytes());
466             org.w3c.dom.Document JavaDoc doc = builder.parse(is);
467
468             // Check to see if the document element is the message
469
if (fieldMessageModel
470                 .getQName()
471                 .getLocalPart()
472                 .equals(doc.getDocumentElement().getLocalName())) {
473
474                 wsifFormat = true;
475
476                 // Need to make the message mutable
477
message.clearBody();
478
479                 // Parse the document ignoring the document element which should be the message name
480
// The contents should represent the parts as elements
481
for (org.w3c.dom.Node JavaDoc n =
482                     doc.getDocumentElement().getFirstChild();
483                     n != null;
484                     n = n.getNextSibling()) {
485                     if (n.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
486                         String JavaDoc partName = n.getLocalName();
487
488                         java.io.ByteArrayOutputStream JavaDoc os =
489                             new java.io.ByteArrayOutputStream JavaDoc();
490
491                         org.apache.xml.serialize.OutputFormat format =
492                             new org.apache.xml.serialize.OutputFormat();
493                         org.apache.xml.serialize.TextSerializer serializer =
494                             new org.apache.xml.serialize.TextSerializer();
495                         serializer.setOutputFormat(format);
496                         serializer.setOutputByteStream(os);
497
498                         format.setOmitXMLDeclaration(true);
499                         //format.setOmitDocumentType(true);
500

501                         serializer.serialize((org.w3c.dom.Element JavaDoc) n);
502                         os.flush();
503                         // Get the contents
504
String JavaDoc partText = os.toString();
505
506                         javax.wsdl.Part partModel =
507                             fieldMessageModel.getPart(partName);
508                         JMSFormatHandler fh = getFormatHandler(partName);
509
510                         if (fh != null) {
511
512                             fh.setPartQName(
513                                 new javax.xml.namespace.QName JavaDoc(
514                                     fieldMessageModel
515                                         .getQName()
516                                         .getNamespaceURI(),
517                                     partModel.getName()));
518
519                             message.setText(partText);
520                             fh.read(message);
521                             //fh.setObjectPart(n);
522

523                             setObjectPart(partName, fh.getObjectPart());
524
525                             // ?? Do I want to store the format handler instead???
526
//partToFHMap.put(partKey, formatHandler);
527

528                         } else {
529                             // No format handler - pass the part contents directly
530
setObjectPart(partName, partText);
531                         }
532                     }
533                 }
534
535                 // Reset the message to original text
536
message.setText(text);
537             }
538         } catch (JMSException JavaDoc e) {
539             Trc.exception(e);
540             throw new WSIFException("Error in read.", e);
541         } catch (javax.xml.parsers.ParserConfigurationException JavaDoc e) {
542             Trc.exception(e);
543             throw new WSIFException("Error in read.", e);
544         } catch (Exception JavaDoc e) {
545             Trc.exception(e);
546             // For all other exceptions ignore since it is likely due to parsing of a non-XML document
547
}
548
549         try {
550             if (!wsifFormat) {
551                 // Unknown format - either XML or Text
552
// Pass the contents of the message to each part of the message model
553
Object JavaDoc[] partNames =
554                     fieldMessageParts != null
555                         ? fieldMessageParts.toArray()
556                         : fieldMessageModel.getParts().keySet().toArray();
557
558                 // should only be one part
559
if (partNames.length != 1)
560                     throw new WSIFException(
561                         "There should only be one part defined in "
562                             + fieldMessageModel.getQName().getLocalPart());
563
564                 String JavaDoc partName = partNames[0].toString();
565
566                 javax.wsdl.Part partModel = fieldMessageModel.getPart(partName);
567                 JMSFormatHandler fh = getFormatHandler(partName);
568
569                 if (fh != null) {
570                     fh.setPartQName(
571                         new javax.xml.namespace.QName JavaDoc(
572                             fieldMessageModel.getQName().getNamespaceURI(),
573                             partModel.getName()));
574                     fh.read(message);
575
576                     setObjectPart(partName, fh.getObjectPart());
577
578                     // ?? Do I want to store the format handler instead???
579
//partToFHMap.put(partKey, formatHandler);
580

581                 } else {
582                     // No format handler - pass the part contents directly
583
setObjectPart(partName, message.getText());
584                 }
585             }
586         } catch (JMSException JavaDoc e) {
587             Trc.exception(e);
588             throw new WSIFException("Error in read.", e);
589         }
590         Trc.exit();
591     }
592
593     /**
594      * Reads the JMS ObjectMessage
595      */

596     private void read(javax.jms.ObjectMessage JavaDoc message) throws WSIFException {
597         Trc.entry(this, message);
598
599         // Need to determine the format type mapping
600
if (!JAVA_ENCODING.equals(getFormatEncoding(fieldBindingModel)))
601             throw new WSIFException("Unable to support non Java encodings in a JMS Object Message");
602
603         try {
604             Object JavaDoc object = message.getObject();
605             Object JavaDoc[] partNames =
606                 fieldMessageParts != null
607                     ? fieldMessageParts.toArray()
608                     : fieldMessageModel.getParts().keySet().toArray();
609
610             // Check to see if there are any parts
611
if (partNames.length == 0)
612                 return;
613
614             // Check to see if it is a known format
615
if (object instanceof java.util.Map JavaDoc) {
616                 // Need to make the message mutable
617
message.clearBody();
618
619                 java.util.Map JavaDoc map = (java.util.Map JavaDoc) object;
620
621                 // Cycle through the parts of the model
622
for (int i = 0; i < partNames.length; i++) {
623                     String JavaDoc partName = partNames[i].toString();
624
625                     if (map.containsKey(partName)) {
626                         javax.wsdl.Part partModel =
627                             fieldMessageModel.getPart(partName);
628                         JMSFormatHandler fh = getFormatHandler(partName);
629
630                         if (fh != null) {
631
632                             fh.setPartQName(
633                                 new javax.xml.namespace.QName JavaDoc(
634                                     fieldMessageModel
635                                         .getQName()
636                                         .getNamespaceURI(),
637                                     partModel.getName()));
638                             // Should be serializable since retrieved it over the wire
639
message.setObject(
640                                 (java.io.Serializable JavaDoc) map.get(partName));
641                             fh.read(message);
642                             //fh.setObjectPart(map.get(partName));
643
setObjectPart(partName, fh.getObjectPart());
644
645                             // ?? Do I want to store the format handler instead???
646
//partToFHMap.put(partKey, formatHandler);
647

648                         } else {
649                             // No format handler defined
650
setObjectPart(partName, map.get(partName));
651                         }
652                     }
653
654                 }
655                 // Reset the message to original text
656
message.setObject((java.io.Serializable JavaDoc) object);
657             } else {
658                 // It is an unknown format
659
// Pass the contents of the message to each part of the message model
660

661                 // should only be one part
662
if (partNames.length != 1)
663                     throw new WSIFException(
664                         "There should only be one part defined in "
665                             + fieldMessageModel.getQName().getLocalPart()
666                             + " or the JMS ObjectMessage should be a Map of "
667                             + "all the parts in the message");
668
669                 String JavaDoc partName = partNames[0].toString();
670
671                 javax.wsdl.Part partModel = fieldMessageModel.getPart(partName);
672                 JMSFormatHandler fh = getFormatHandler(partName);
673
674                 if (fh != null) {
675                     fh.setPartQName(
676                         new javax.xml.namespace.QName JavaDoc(
677                             fieldMessageModel.getQName().getNamespaceURI(),
678                             partModel.getName()));
679                     fh.read(message);
680
681                     setObjectPart(partName, fh.getObjectPart());
682
683                     // ?? Do I want to store the format handler instead???
684
//partToFHMap.put(partKey, formatHandler);
685

686                 } else {
687                     // No format handler defined
688
setObjectPart(partName, message.getObject());
689                 }
690
691             }
692         } catch (JMSException JavaDoc e) {
693             Trc.exception(e);
694             throw new WSIFException("Error in read.", e);
695         }
696         Trc.exit();
697     }
698
699     // Move to WSIF utils ??
700
private static String JavaDoc getFormatEncoding(Binding bindingModel) {
701         Trc.entry(null, bindingModel);
702
703         java.util.Iterator JavaDoc iterator =
704             bindingModel.getExtensibilityElements().iterator();
705
706         while (iterator.hasNext()) {
707             javax.wsdl.extensions.ExtensibilityElement ee =
708                 (javax.wsdl.extensions.ExtensibilityElement) iterator.next();
709             if (ee instanceof TypeMapping) {
710                 TypeMapping typeMapping = (TypeMapping) ee;
711                 String JavaDoc s = typeMapping.getEncoding();
712                 Trc.exit(s);
713                 return s;
714             }
715         }
716
717         Trc.exit(null);
718         return null;
719     }
720
721     private JMSFormatHandler getFormatHandler(String JavaDoc partName) {
722         Trc.entry(this, partName);
723
724         javax.wsdl.Part partModel = fieldMessageModel.getPart(partName);
725         JMSFormatHandler fh = null;
726
727         javax.xml.namespace.QName JavaDoc partType = partModel.getTypeName();
728         if (partType == null)
729             partType = partModel.getElementName();
730
731         try {
732             // no type exists or it is not a XSD primitive type
733
if (isSchemaNamespace(partType.getNamespaceURI())
734                 && isXSDPrimitiveType(partType.getLocalPart()))
735                 fh =
736                     new PrimitiveTypeFormatHandler(
737                         (Class JavaDoc) PRIMITIVE_JAVA_MAPPING.get(
738                             partType.getLocalPart().toLowerCase()));
739             else
740                 fh = (JMSFormatHandler)
741                     //antxxx org.apache.wsif.jca.util.JCAUtil.getFormatHandler(
742
WSIFUtils.getFormatHandler(
743             partModel,
744             this.fieldDefinitionModel,
745             this.fieldBindingModel);
746         } catch (java.lang.Exception JavaDoc e) {
747             Trc.exception(e);
748         }
749
750         Trc.exit(fh);
751         return fh;
752     }
753
754     /**
755      * Returns true if the namespace is one of the existing XML Schema namespaces
756      */

757     static boolean isSchemaNamespace(String JavaDoc namespaceURI) {
758         return XML_SCHEMA_1999.equals(namespaceURI)
759             || XML_SCHEMA_2000_10.equals(namespaceURI)
760             || XML_SCHEMA_2001.equals(namespaceURI);
761     }
762
763     static boolean isXSDPrimitiveType(String JavaDoc type) {
764         Trc.entry(null, type);
765
766         Object JavaDoc[] types = PRIMITIVE_JAVA_MAPPING.keySet().toArray();
767         for (int i = 0; i < types.length; i++) {
768             if (types[i].toString().equalsIgnoreCase(type)) {
769                 Trc.exit(true);
770                 return true;
771             }
772         }
773         Trc.exit(false);
774         return false;
775     }
776
777     private int getMessageType() {
778         Trc.entry(this);
779
780         java.util.Iterator JavaDoc iterator =
781             fieldBindingModel.getExtensibilityElements().iterator();
782
783         while (iterator.hasNext()) {
784             javax.wsdl.extensions.ExtensibilityElement ee =
785                 (javax.wsdl.extensions.ExtensibilityElement) iterator.next();
786             if (ee instanceof JMSBinding) {
787                 JMSBinding jmsBinding = (JMSBinding) ee;
788                 int type = jmsBinding.getJmsMessageType();
789                 Trc.exit(type);
790                 return type;
791             }
792         }
793
794         int type =
795             org
796                 .apache
797                 .wsif
798                 .wsdl
799                 .extensions
800                 .jms
801                 .JMSConstants
802                 .MESSAGE_TYPE_NOTSET;
803         Trc.exit(type);
804         return type;
805     }
806
807     private boolean isCorrectMessageType(javax.jms.Message JavaDoc message) {
808         Trc.entry(this, message);
809         int type = getMessageType();
810
811         boolean result =
812             (message instanceof TextMessage JavaDoc
813                 && type == JMSConstants.MESSAGE_TYPE_TEXTMESSAGE)
814                 || (message instanceof ObjectMessage JavaDoc
815                     && type == JMSConstants.MESSAGE_TYPE_OBJECTMESSAGE)
816                 || (message instanceof javax.jms.StreamMessage JavaDoc
817                     && type == JMSConstants.MESSAGE_TYPE_STREAMMESSAGE)
818                 || (message instanceof javax.jms.BytesMessage JavaDoc
819                     && type == JMSConstants.MESSAGE_TYPE_BYTEMESSAGE)
820                 || (message instanceof javax.jms.MapMessage JavaDoc
821                     && type == JMSConstants.MESSAGE_TYPE_MAPMESSAGE);
822
823         Trc.exit(result);
824         return result;
825     }
826 }
Popular Tags