KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.InputStream JavaDoc;
61 import java.io.OutputStream JavaDoc;
62 import java.io.Serializable JavaDoc;
63 import java.util.Iterator JavaDoc;
64 import java.util.List JavaDoc;
65 import java.util.Map JavaDoc;
66
67 import javax.jms.JMSException JavaDoc;
68 import javax.wsdl.Binding;
69 import javax.wsdl.BindingFault;
70 import javax.wsdl.BindingOperation;
71 import javax.wsdl.Fault;
72 import javax.wsdl.Operation;
73 import javax.wsdl.extensions.ExtensibilityElement;
74
75 import org.apache.wsif.WSIFConstants;
76 import org.apache.wsif.WSIFException;
77 import org.apache.wsif.WSIFMessage;
78 import org.apache.wsif.WSIFRequest;
79 import org.apache.wsif.WSIFResponse;
80 import org.apache.wsif.format.WSIFFormatter;
81 import org.apache.wsif.logging.Trc;
82 import org.apache.wsif.util.WSIFUtils;
83 import org.apache.wsif.wsdl.extensions.jms.JMSFault;
84 import org.apache.wsif.wsdl.extensions.jms.JMSFaultIndicator;
85 import org.apache.wsif.wsdl.extensions.jms.JMSFaultProperty;
86 import org.apache.wsif.wsdl.extensions.jms.JMSInput;
87 import org.apache.wsif.wsdl.extensions.jms.JMSOutput;
88 import org.apache.wsif.wsdl.extensions.jms.JMSProperty;
89
90 /**
91  * JMSFormatter
92  *
93  * @author <a HREF="mailto:seto@ca.ibm.com">Norman Seto</a>
94  * @author <a HREF="mailto:antelder@apache.org">Ant Elder</a>
95  */

96 public class JMSFormatter implements WSIFFormatter, Serializable JavaDoc {
97
98     private static final long serialVersionUID = 1L;
99
100     private javax.wsdl.Definition fieldDefinition;
101     private javax.wsdl.Port fieldPort;
102
103     private String JavaDoc reqOpName;
104     private String JavaDoc reqInputName;
105     private String JavaDoc reqOutputName;
106     private BindingFault lastBindingFault = null;
107
108     /**
109      * Constructor for JMSFormatter
110      */

111     public JMSFormatter(javax.wsdl.Definition def, javax.wsdl.Port port) {
112         super();
113         Trc.entry(this,def,port);
114         fieldDefinition = def;
115         fieldPort = port;
116         Trc.exit();
117     }
118
119     /**
120      * @see WSIFFormatter#formatRequest(WSIFRequest, OutputStream)
121      */

122     public void formatRequest(WSIFRequest req, OutputStream JavaDoc out) {
123         Trc.entry(this, req);
124         Trc.exit();
125     }
126
127     /**
128      * @see WSIFFormatter#unformatRequest(InputStream)
129      */

130     public WSIFRequest unformatRequest(InputStream JavaDoc in) {
131         Trc.entry(this);
132         Trc.exit();
133         return null;
134     }
135
136     /**
137      * @see WSIFFormatter#formatResponse(WSIFResponse, OutputStream)
138      */

139     public void formatResponse(WSIFResponse resp, OutputStream JavaDoc out) {
140         Trc.entry(this, resp);
141         Trc.exit();
142     }
143
144     /**
145      * @see WSIFFormatter#unformatResponse(InputStream)
146      */

147     public WSIFResponse unformatResponse(InputStream JavaDoc in) {
148         Trc.entry(this);
149         Trc.exit();
150         return null;
151     }
152
153     public void formatRequest(WSIFRequest req, javax.jms.Message JavaDoc out)
154         throws WSIFException {
155         Trc.entry(this, req, out);
156
157         Binding binding = fieldPort.getBinding();
158         BindingOperation bop =
159             binding.getBindingOperation(
160                 req.getOperationName(),
161                 req.getInputName(),
162                 req.getOutputName());
163         javax.wsdl.Message inMessage = bop.getOperation().getInput().getMessage();
164
165         org.apache.wsif.providers.jms.JMSMessage fhMsg =
166             new org.apache.wsif.providers.jms.JMSMessage(
167                 fieldDefinition,
168                 binding,
169                 inMessage,
170                 getInputParts(bop));
171
172         copyTo(req.getIncomingMessage(), fhMsg);
173         fhMsg.write(out);
174
175         setOperationName( req.getOperationName() );
176         setInputName( req.getInputName() );
177         setOutputName( req.getOutputName() );
178         
179         Trc.exit();
180     }
181
182     public WSIFRequest unformatRequest(javax.jms.Message JavaDoc in)
183         throws WSIFException {
184         Trc.entry(this, in);
185         // Need to construct the request.
186
WSIFRequest req = createRequest(in);
187
188         Binding binding = fieldPort.getBinding();
189         BindingOperation bop =
190             binding.getBindingOperation(
191                 req.getOperationName(),
192                 req.getInputName(),
193                 req.getOutputName());
194
195         javax.wsdl.Message inMessage = bop.getOperation().getInput().getMessage();
196
197         org.apache.wsif.providers.jms.JMSMessage fhMsg =
198             new org.apache.wsif.providers.jms.JMSMessage(
199                 fieldDefinition,
200                 binding,
201                 inMessage,
202                 getInputParts(bop));
203
204         fhMsg.read(in);
205
206         req.setIncomingMessage(fhMsg);
207
208         Trc.exit(req);
209         return req;
210     }
211
212     protected WSIFRequest createRequest(javax.jms.Message JavaDoc msg)
213         throws WSIFException {
214         Trc.entry(this, msg);
215         javax.xml.namespace.QName JavaDoc serviceName = null;
216         String JavaDoc operationName = null;
217         String JavaDoc input = null;
218         String JavaDoc output = null;
219
220         Binding fieldBinding = fieldPort.getBinding();
221         if (fieldBinding == null)
222             throw new WSIFException("Unable to locate Binding");
223
224         //Locate Service
225
Map JavaDoc sm = WSIFUtils.getAllItems( fieldDefinition, "Service" );
226         Iterator JavaDoc services = sm.values().iterator();
227         while (services.hasNext() && serviceName ==null) {
228             javax.wsdl.Service s = (javax.wsdl.Service)services.next();
229                     
230             for (Iterator JavaDoc ports = s.getPorts().values().iterator(); ports.hasNext();) {
231                 javax.wsdl.Port p = (javax.wsdl.Port)ports.next();
232                 if (p.getBinding() != null && p.getBinding().getQName().equals(fieldBinding.getQName())) {
233                     // Binding found
234
serviceName = s.getQName();
235                     break;
236                 }
237             }
238         }
239         
240         if (serviceName == null)
241             throw new WSIFException("Unable to locate Service");
242
243
244         // Find the operation, if the Port only contains 1 operation then use
245
// that one, otherwise use the one defined in the JMS message properties.
246
javax.wsdl.PortType portType = fieldBinding.getPortType();
247         if (portType!= null) {
248            List JavaDoc operations = portType.getOperations();
249            if (operations != null && operations.size() == 1 ) {
250                  javax.wsdl.Operation o = (javax.wsdl.Operation)operations.get(0);
251                  operationName = o.getName();
252                  if (o.getInput() != null) {
253                     input = o.getInput().getName();
254                  }
255                  if (o.getOutput() != null) {
256                     output = o.getOutput().getName();
257                  }
258             }
259         }
260
261         if (operationName == null) {
262            try {
263               operationName = msg.getStringProperty( WSIFConstants.JMS_PROP_OPERATION_NAME );
264               input = msg.getStringProperty( WSIFConstants.JMS_PROP_INPUT_NAME );
265               output = msg.getStringProperty( WSIFConstants.JMS_PROP_OUTPUT_NAME );
266            } catch (javax.jms.JMSException JavaDoc e) {
267               Trc.exception(e);
268            }
269         }
270  
271         if (operationName == null)
272            throw new WSIFException( "Unable to determine Operation" );
273
274                 
275         WSIFRequest request = new WSIFRequest( serviceName );
276         request.setPortName( fieldPort.getName() );
277         request.setOperationName( operationName );
278         request.setInputName( input );
279         request.setOutputName( output );
280         Trc.exit( request );
281         return request;
282     }
283
284     protected WSIFResponse createResponse(javax.jms.Message JavaDoc msg)
285         throws WSIFException {
286         Trc.entry(this, msg);
287         javax.xml.namespace.QName JavaDoc serviceName = null;
288         String JavaDoc operationName = null;
289         String JavaDoc input = null;
290         String JavaDoc output = null;
291
292         Binding fieldBinding = fieldPort.getBinding();
293         if (fieldBinding == null)
294             throw new WSIFException("Unable to locate Binding");
295
296         //Locate Service
297
Map JavaDoc sm = WSIFUtils.getAllItems( fieldDefinition, "Service" );
298         Iterator JavaDoc services = sm.values().iterator();
299         while (services.hasNext() && serviceName ==null) {
300             javax.wsdl.Service s = (javax.wsdl.Service)services.next();
301                     
302             for (Iterator JavaDoc ports = s.getPorts().values().iterator(); ports.hasNext();) {
303                 javax.wsdl.Port p = (javax.wsdl.Port)ports.next();
304                 if (p.getBinding() != null && p.getBinding().getQName().equals(fieldBinding.getQName())) {
305                     // Binding found
306
serviceName = s.getQName();
307                     break;
308                 }
309             }
310         }
311         
312         if (serviceName == null)
313             throw new WSIFException("Unable to locate Service");
314
315         // Locate the Operation from the stored values
316
operationName = getOperationName();
317         input = getInputName();
318         output = getOutputName();
319
320         if (operationName == null || operationName.equals("")) {
321             // Nothing defined. Resort to getting the info from the wsdl document
322
javax.wsdl.PortType portType = fieldBinding.getPortType();
323             if (portType!= null) {
324                 List JavaDoc operations = portType.getOperations();
325                 if (operations != null) {
326                     if (operations.size() == 1) {
327                         javax.wsdl.Operation o = (javax.wsdl.Operation)operations.get(0);
328                         operationName = o.getName();
329                         if (o.getInput() != null)
330                             input = o.getInput().getName();
331                         if (o.getOutput() != null)
332                             output = o.getOutput().getName();
333                     }
334                     else
335                         throw new WSIFException("Port Type definition contains multiple operations.");
336                 }
337             }
338         }
339
340         if (operationName == null)
341             throw new WSIFException("Unable to locate Operation");
342                 
343         WSIFResponse response = new WSIFResponse(serviceName);
344         response.setPortName(fieldPort.getName());
345         response.setOperationName(operationName);
346         response.setInputName(input);
347         response.setOutputName(output);
348         Trc.exit(response);
349         return response;
350     }
351
352     public void formatResponse(WSIFResponse resp, javax.jms.Message JavaDoc out)
353         throws WSIFException {
354         Trc.entry(this, resp, out);
355         Binding binding = fieldPort.getBinding();
356         BindingOperation bop =
357             binding.getBindingOperation(
358                 resp.getOperationName(),
359                 resp.getInputName(),
360                 resp.getOutputName());
361         javax.wsdl.Message outMessage = bop.getOperation().getOutput().getMessage();
362
363         org.apache.wsif.providers.jms.JMSMessage fhMsg =
364             new org.apache.wsif.providers.jms.JMSMessage(
365                 fieldDefinition,
366                 binding,
367                 outMessage,
368                 getOutputParts(bop));
369
370         copyTo(resp.getOutgoingMessage(), fhMsg);
371         fhMsg.write(out);
372
373         Trc.exit();
374     }
375
376     /**
377      * @see WSIFFormatter#unformatResponse(InputStream)
378      */

379     public WSIFResponse unformatResponse(javax.jms.Message JavaDoc out)
380         throws WSIFException {
381             
382         Trc.entry(this, out);
383         // Need to construct the response.
384
WSIFResponse resp = createResponse(out);
385
386         Binding binding = fieldPort.getBinding();
387         BindingOperation bop =
388             binding.getBindingOperation(
389                 resp.getOperationName(),
390                 resp.getInputName(),
391                 resp.getOutputName());
392                 
393         if (unformatResponseFault(resp, out, binding, bop)) {
394             Trc.exit(resp);
395             return resp;
396         }
397         
398         Operation op = bop.getOperation();
399         javax.wsdl.Output output = op.getOutput();
400         if ( output != null ) {
401            javax.wsdl.Message outMessage = output.getMessage();
402
403            org.apache.wsif.providers.jms.JMSMessage fhMsg =
404               new org.apache.wsif.providers.jms.JMSMessage(
405                  fieldDefinition,
406                  binding,
407                  outMessage,
408                  getOutputParts(bop));
409
410            fhMsg.read(out);
411
412            resp.setOutgoingMessage(fhMsg);
413         }
414
415         Trc.exit(resp);
416         return resp;
417     }
418
419     /**
420      * If this message is a fault message, unformat it, else do nothing
421      * @return true for a fault, false if it is not a fault.
422      */

423     private boolean unformatResponseFault(
424         WSIFResponse resp,
425         javax.jms.Message JavaDoc out,
426         Binding binding,
427         BindingOperation bop)
428         throws WSIFException {
429
430         Trc.entry(this, resp, out, binding, bop);
431
432         // Iterate through the BindingOperation to find all the <jms:faultIndicators.
433
Map JavaDoc bndFs = bop.getBindingFaults();
434         if (bndFs != null && !bndFs.isEmpty()) {
435             Iterator JavaDoc itBndFNames = bndFs.keySet().iterator();
436             while (itBndFNames.hasNext()) {
437                 String JavaDoc bndFName = (String JavaDoc) itBndFNames.next();
438                 BindingFault bndF = (BindingFault) bndFs.get(bndFName);
439
440                 List JavaDoc bndFElems = bndF.getExtensibilityElements();
441                 if (bndFElems == null || bndFElems.isEmpty())
442                     continue;
443
444                 Iterator JavaDoc itBndFElems = bndFElems.iterator();
445                 while (itBndFElems.hasNext()) {
446                     Object JavaDoc bndFElem = itBndFElems.next();
447                     
448                     // Ignore anything that isn't a fault indicator, since
449
// those will be dealt with by unformatFaultMessage.
450
if (bndFElem instanceof JMSFaultIndicator) {
451                         JMSFaultIndicator indic = (JMSFaultIndicator) bndFElem;
452
453                         // Only the first fault indicator that matches is used.
454
// If others match, then this error is ignored.
455
if (matchesFaultIndicator(indic, out)) {
456                             WSIFMessage msg =
457                                 unformatFaultMessage(
458                                     out,
459                                     binding,
460                                     bop,
461                                     bndFName,
462                                     bndF);
463
464                             resp.setOutgoingMessage(msg);
465                             resp.setIsFault(true);
466                             lastBindingFault = bndF;
467                             Trc.exit(true);
468                             return true;
469                         }
470                     }
471                 }
472             }
473         }
474         Trc.exit(false);
475         return false;
476     }
477
478     /**
479      * Look to see whether this JMSFaultIndicator matches this output JMS message
480      * by seeing whether the faultProperty exists on the message and has the
481      * same value.
482      *
483      * It would be good to first see if there are any properties on the message
484      * that might indicate a fault so this method could return false quickly.
485      * Unfortunately I assume all JMS messages will have a number of properties
486      * set so this performance enhancement is not available.
487      *
488      * @return true for a match, false for no match.
489      */

490     private boolean matchesFaultIndicator(
491         JMSFaultIndicator indic,
492         javax.jms.Message JavaDoc out) {
493         Trc.entry(this, indic, out);
494
495         // Fault indicators must have type=property. Other types are ignored.
496
String JavaDoc type = indic.getType();
497         if (type == null || !"property".equals(type)) {
498             Trc.exit(false);
499             return false;
500         }
501
502         List JavaDoc fProps = indic.getJMSFaultProperties();
503         if (fProps == null || fProps.isEmpty()) {
504             Trc.exit(false);
505             return false;
506         }
507
508         Iterator JavaDoc itFProps = fProps.iterator();
509         while (itFProps.hasNext()) {
510             JMSFaultProperty fProp = (JMSFaultProperty) itFProps.next();
511             String JavaDoc propName = fProp.getName();
512             if (propName == null
513                 || fProp.getType() == null
514                 || fProp.getValue() == null)
515                 continue;
516
517             Object JavaDoc propValue = null;
518             try {
519                 if (!out.propertyExists(propName))
520                     continue;
521
522                 propValue = out.getObjectProperty(propName);
523             } catch (JMSException JavaDoc je) {
524                 Trc.ignoredException(je);
525                 continue;
526             }
527
528             if (propValue != null
529                 && fProp.getValue().equals(propValue.toString())) {
530                 Trc.exit(true);
531                 return true;
532             }
533         }
534
535         Trc.exit(false);
536         return false;
537     }
538
539     private WSIFMessage unformatFaultMessage(
540         javax.jms.Message JavaDoc out,
541         Binding binding,
542         BindingOperation bop,
543         String JavaDoc bndFName,
544         BindingFault bndF)
545         throws WSIFException {
546         Trc.entry(this, out, binding, bop, bndFName, bndF);
547
548         Operation op = bop.getOperation();
549         Fault fault = op.getFault(bndFName);
550         if (fault == null)
551             throw new WSIFException(
552                 "No fault "
553                     + bndFName
554                     + " found in operation "
555                     + op.getName());
556
557         JMSMessage fhMsg =
558             new JMSMessage(
559                 fieldDefinition,
560                 binding,
561                 fault.getMessage(),
562                 getFaultParts(bndF));
563
564         fhMsg.read(out);
565         Trc.exit(fhMsg);
566         return fhMsg;
567
568     }
569     
570     void copyTo(
571         WSIFMessage source,
572         WSIFMessage target)
573         throws WSIFException {
574         if (source == null || target == null)
575             return;
576         for (Iterator JavaDoc i = source.getPartNames(); i.hasNext();) {
577             String JavaDoc partName = i.next().toString();
578             target.setObjectPart(partName, source.getObjectPart(partName));
579         }
580     }
581
582     private List JavaDoc getInputParts(
583         BindingOperation bindingOperation) {
584         if (bindingOperation.getBindingInput() != null) {
585             Iterator JavaDoc inputIterator =
586                 bindingOperation.getBindingInput().getExtensibilityElements().iterator();
587
588             while (inputIterator.hasNext()) {
589                 ExtensibilityElement ele =
590                     (ExtensibilityElement) inputIterator.next();
591                 if (ele instanceof JMSInput) {
592                     return ((JMSInput) ele).getParts();
593                 }
594             }
595         }
596         return null;
597     }
598
599     private List JavaDoc getOutputParts(
600         BindingOperation bindingOperation) {
601         if (bindingOperation.getBindingOutput() != null) {
602             Iterator JavaDoc outputIterator =
603                 bindingOperation.getBindingOutput().getExtensibilityElements().iterator();
604
605             while (outputIterator.hasNext()) {
606                 ExtensibilityElement ele =
607                     (ExtensibilityElement) outputIterator.next();
608                 if (ele instanceof JMSOutput) {
609                     return ((JMSOutput) ele).getParts();
610                 }
611             }
612         }
613         return null;
614     }
615
616     private List JavaDoc getFaultParts(BindingFault bindingFault) {
617         Trc.entry(this, bindingFault);
618
619         List JavaDoc list = null;
620         if (bindingFault != null) {
621             Iterator JavaDoc it = bindingFault.getExtensibilityElements().iterator();
622             while (it.hasNext()) {
623                 Object JavaDoc ele = it.next();
624                 if (ele instanceof JMSFault) {
625                     list = ((JMSFault) ele).getParts();
626                     break;
627                 }
628             }
629         }
630         Trc.exit(list);
631         return list;
632     }
633
634     private String JavaDoc getOperationName() {
635        return reqOpName;
636     }
637
638     private void setOperationName(String JavaDoc s) {
639        reqOpName = s;
640     }
641
642     private String JavaDoc getInputName() {
643        return reqInputName;
644     }
645
646     private void setInputName(String JavaDoc s) {
647        reqInputName = s;
648     }
649
650     private String JavaDoc getOutputName() {
651        return reqOutputName;
652     }
653
654     private void setOutputName(String JavaDoc s) {
655        reqOutputName = s;
656     }
657     
658     public BindingFault getLastBindingFault() {
659         Trc.entry(this);
660         Trc.exit(lastBindingFault);
661         return lastBindingFault;
662     }
663
664 }
Popular Tags