KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
62 import java.util.Iterator JavaDoc;
63 import java.util.List JavaDoc;
64 import java.util.Map JavaDoc;
65
66 import javax.wsdl.BindingFault;
67 import javax.wsdl.BindingInput;
68 import javax.wsdl.BindingOperation;
69 import javax.wsdl.BindingOutput;
70 import javax.wsdl.Definition;
71 import javax.wsdl.Input;
72 import javax.wsdl.Message;
73 import javax.wsdl.Operation;
74 import javax.wsdl.Output;
75 import javax.wsdl.Port;
76 import javax.wsdl.Service;
77 import javax.wsdl.extensions.ExtensibilityElement;
78 import javax.xml.namespace.QName JavaDoc;
79
80 import org.apache.wsif.WSIFConstants;
81 import org.apache.wsif.WSIFCorrelationId;
82 import org.apache.wsif.WSIFCorrelationService;
83 import org.apache.wsif.WSIFException;
84 import org.apache.wsif.WSIFMessage;
85 import org.apache.wsif.WSIFOperation;
86 import org.apache.wsif.WSIFPort;
87 import org.apache.wsif.WSIFRequest;
88 import org.apache.wsif.WSIFResponse;
89 import org.apache.wsif.WSIFResponseHandler;
90 import org.apache.wsif.base.WSIFDefaultOperation;
91 import org.apache.wsif.logging.MessageLogger;
92 import org.apache.wsif.logging.Trc;
93 import org.apache.wsif.util.WSIFCorrelationServiceLocator;
94 import org.apache.wsif.util.WSIFProperties;
95 import org.apache.wsif.util.WSIFUtils;
96 import org.apache.wsif.util.jms.WSIFJMSCorrelationId;
97 import org.apache.wsif.util.jms.WSIFJMSDestination;
98 import org.apache.wsif.wsdl.extensions.jms.JMSAddress;
99 import org.apache.wsif.wsdl.extensions.jms.JMSBinding;
100 import org.apache.wsif.wsdl.extensions.jms.JMSConstants;
101 import org.apache.wsif.wsdl.extensions.jms.JMSFaultIndicator;
102 import org.apache.wsif.wsdl.extensions.jms.JMSFaultProperty;
103 import org.apache.wsif.wsdl.extensions.jms.JMSInput;
104 import org.apache.wsif.wsdl.extensions.jms.JMSOutput;
105 import org.apache.wsif.wsdl.extensions.jms.JMSProperty;
106 import org.apache.wsif.wsdl.extensions.jms.JMSPropertyValue;
107
108 /**
109  * WSIFOperation_Jms
110  *
111  * @author <a HREF="mailto:ake@de.ibm.com">Hermann Akermann</a>
112  * @author <a HREF="mailto:antelder@apache.org">Ant Elder</a>
113  * @author <a HREF="mailto:seto@ca.ibm.com">Norman Seto</a>
114  */

115 public class WSIFOperation_Jms
116     extends WSIFDefaultOperation
117     implements WSIFOperation {
118
119     private static final long serialVersionUID = 1L;
120
121     protected Port fieldBasePort;
122     protected BindingOperation fieldBindingOperation;
123     protected WSIFPort_Jms fieldJmsPort;
124
125     protected Operation fieldOperation;
126
127     private boolean inputOnlyOp = false;
128
129     // input message
130
protected String JavaDoc fieldInputMessageName;
131     protected HashMap JavaDoc fieldInputJmsPropertyValues;
132     protected HashMap JavaDoc fieldInputJmsProperties;
133     protected JMSInput fieldInput;
134
135     // output message
136
protected String JavaDoc fieldOutputMessageName;
137     protected HashMap JavaDoc fieldOutputProperties;
138     protected JMSOutput fieldOutput;
139
140     private WSIFResponseHandler handler;
141     private boolean asyncOperation;
142     private JMSFormatter formatter;
143
144     private long syncTimeout;
145     private long asyncTimeout;
146
147     /**
148      * ctor
149      */

150     public WSIFOperation_Jms(
151         Port basePort,
152         BindingOperation bindingOperation,
153         WSIFPort_Jms jmsPort)
154         throws WSIFException {
155
156         Trc.entry(this, basePort, bindingOperation, jmsPort);
157
158         fieldBasePort = basePort;
159         fieldBindingOperation = bindingOperation;
160         fieldJmsPort = jmsPort;
161
162         syncTimeout = WSIFProperties.getSyncTimeout();
163         asyncTimeout = WSIFProperties.getAsyncTimeout();
164
165         if (Trc.ON)
166             Trc.exit(deep());
167     }
168
169     /**
170     * Create a new copy of this object. This is not a clone, since
171     * it does not copy the referenced objects as well.
172     */

173     public WSIFOperation_Jms copy() throws WSIFException {
174         Trc.entry(this);
175         WSIFOperation_Jms woj =
176             new WSIFOperation_Jms(
177                 fieldBasePort,
178                 fieldBindingOperation,
179                 fieldJmsPort);
180         if (Trc.ON)
181             Trc.exit(woj.deep());
182         return woj;
183     }
184
185     /**
186      * executeRequestResponseOperation(WSIFMessage, WSIFMessage, WSIFMessage)
187      *
188      * synchronous execution is NOT supported for JMS
189      *
190      */

191     public boolean executeRequestResponseOperation(
192         WSIFMessage input,
193         WSIFMessage output,
194         WSIFMessage fault)
195         throws WSIFException {
196
197         Trc.entry(this, input, output, fault);
198         close();
199
200         if (!fieldJmsPort.supportsSync())
201             throw new WSIFException("synchronous operations not available");
202
203         setAsyncOperation(false);
204
205         boolean operationSucceeded = true;
206         try {
207             getOperation();
208
209             // send the jms message
210
String JavaDoc correlId = sendJmsMessage(input);
211
212             WSIFJMSDestination jmsDest = fieldJmsPort.getJmsDestination();
213             javax.jms.Message JavaDoc response = jmsDest.receive(correlId, syncTimeout);
214             operationSucceeded = receiveJmsMessage(response, output, fault);
215
216         } catch (Exception JavaDoc ex) {
217             Trc.exception(ex);
218
219             // Log message
220
MessageLogger.log(
221                 "WSIF.0005E",
222                 "Jms",
223                 fieldBindingOperation.getName());
224
225             throw new WSIFException(
226                 this
227                     + " : Could not invoke '"
228                     + fieldBindingOperation.getName()
229                     + "'",
230                 ex);
231         }
232
233         Trc.exit(operationSucceeded);
234         return operationSucceeded;
235     }
236
237     /**
238      * executeInputOnlyOperation(WSIFMessage)
239      * WSDL transmission primitive Notification (fire&forget)
240      */

241     public void executeInputOnlyOperation(WSIFMessage input)
242         throws WSIFException {
243         Trc.entry(this, input);
244         inputOnlyOp = true;
245         executeRequestResponseAsync(input, null);
246         Trc.exit();
247     }
248
249     /**
250      * executeRequestResponseAsync(WSIFMessage)
251      * This is the simple async form where the client is expected
252      * to handle the correlating of the response.
253      * @param input input message to send to the operation
254      *
255      * @return the correlation ID or the request. The correlation ID
256      * is used to associate the request with the WSIFOperation.
257      *
258      * @exception WSIFException if something goes wrong.
259      */

260     public WSIFCorrelationId executeRequestResponseAsync(WSIFMessage input)
261         throws WSIFException {
262         return executeRequestResponseAsync(input, null);
263     }
264
265     /**
266      * executeRequestResponseAsync(WSIFMessage, WSIFResponseHandler)
267      *
268      * @param input input message to send to the operation
269      * @param handler the response handler that will be notified
270      * when the asynchronous response becomes available.
271      *
272      * @return the correlation ID or the request. The correlation ID
273      * is used to associate the request with the WSIFOperation.
274      *
275      * @exception WSIFException if something goes wrong.
276      */

277     public WSIFCorrelationId executeRequestResponseAsync(
278         WSIFMessage input,
279         WSIFResponseHandler handler)
280         throws WSIFException {
281
282         Trc.entry(this, input, handler);
283         close();
284
285         if (!fieldJmsPort.supportsAsync())
286             throw new WSIFException("asynchronous operations not available");
287
288         setAsyncOperation(true);
289         WSIFCorrelationId correlId = null;
290
291         try {
292
293             getOperation();
294             this.handler = handler;
295
296             if (inputOnlyOp) {
297                 sendJmsMessage(input);
298             } else {
299                 if (handler == null) {
300                     correlId = new WSIFJMSCorrelationId(sendJmsMessage(input));
301                 } else {
302                     WSIFCorrelationService cs =
303                         WSIFCorrelationServiceLocator.getCorrelationService();
304                     synchronized (cs) {
305                         correlId =
306                             new WSIFJMSCorrelationId(sendJmsMessage(input));
307                         //register it to the correlation service
308
cs.put(correlId, this, asyncTimeout);
309                     }
310                 }
311             }
312         } catch (Exception JavaDoc ex) {
313             Trc.exception(ex);
314
315             // Log message
316
MessageLogger.log(
317                 "WSIF.0005E",
318                 "Jms",
319                 fieldBindingOperation.getName());
320
321             throw new WSIFException(
322                 this
323                     + " : Could not invoke '"
324                     + fieldBindingOperation.getName()
325                     + "'",
326                 ex);
327         }
328
329         Trc.exit(correlId);
330         return correlId;
331     }
332
333     public boolean processAsyncResponse(
334         Object JavaDoc responseObject,
335         WSIFMessage output,
336         WSIFMessage fault)
337         throws WSIFException {
338
339         Trc.entry(this, responseObject, output, fault);
340
341         boolean ok = false;
342         try {
343
344             getOperation();
345
346             // set output message name
347
output.setName(fieldBindingOperation.getBindingOutput().getName());
348             ok = receiveJmsMessage(responseObject, output, fault);
349
350         } catch (Exception JavaDoc ex) {
351             Trc.exception(ex);
352
353             // Log message
354
MessageLogger.log(
355                 "WSIF.0005E",
356                 "Jms",
357                 fieldBindingOperation.getName());
358
359             throw new WSIFException(
360                 this
361                     + " : Could not invoke '"
362                     + fieldBindingOperation.getName()
363                     + "'",
364                 ex);
365         }
366
367         Trc.exit(ok);
368         return ok;
369     }
370
371     /**
372      * fireAsyncResponse is called when a response has been received
373      * for a previous executeRequestResponseAsync call.
374      * @param response an Object representing the response
375      */

376
377     public void fireAsyncResponse(Object JavaDoc response) throws WSIFException {
378         Trc.entry(this, response);
379
380         // use processAsyncResponse
381
// **NS
382
WSIFMessage output = createOutputMessage();
383         WSIFMessage fault = createFaultMessage();
384
385         receiveJmsMessage(response, output, fault);
386
387         handler.executeAsyncResponse(output, fault);
388         Trc.exit();
389     }
390
391     //
392
// helper
393
//
394

395     /**
396      * send jms message
397      */

398     private String JavaDoc sendJmsMessage(WSIFMessage input) throws WSIFException {
399
400         String JavaDoc correlId = null;
401         WSIFJMSDestination jmsDest = fieldJmsPort.getJmsDestination();
402
403         setPropertyValues();
404
405         /**
406          * set the parts onto the message
407          */

408         // The input message parts can either be a jms property
409
// or an input part. Input parts not defined in the
410
// WSDL will be ignored. Input parts will only be used
411
// if there is not a jms:input atribute, or the part
412
// name is in the jms:input list. Parts defined in the
413
// WSDL but not in the input message defualt to null.
414
String JavaDoc partName;
415         ArrayList JavaDoc wsdlInputParts = getWSDLInputPartNames();
416         HashMap JavaDoc propertyParts = getPropertyParts();
417         WSIFMessage message = createInputMessage();
418         for (Iterator JavaDoc i = input.getPartNames(); i.hasNext();) {
419             partName = (String JavaDoc) i.next();
420             if (propertyParts.containsKey(partName)) {
421                 String JavaDoc name = (String JavaDoc) propertyParts.get(partName);
422                 Object JavaDoc value = input.getObjectPart(partName);
423                 if (!timeoutProperty(name, value)) {
424                     jmsDest.setProperty(name, value);
425                 }
426             } else if (wsdlInputParts.contains(partName)) {
427                 wsdlInputParts.remove(partName);
428                 if (fieldInput == null) {
429                     message.setObjectPart(
430                         partName,
431                         input.getObjectPart(partName));
432                 } else if (fieldInput.getParts().contains(partName)) {
433                     message.setObjectPart(
434                         partName,
435                         input.getObjectPart(partName));
436                 }
437             }
438         }
439
440         // default missing parts to null
441
for (Iterator JavaDoc i = wsdlInputParts.iterator(); i.hasNext();) {
442             message.setObjectPart((String JavaDoc) i.next(), null);
443         }
444
445         // properties from the context
446
setDestinationContext();
447
448         Service serviceModel = null;
449         Definition def = fieldJmsPort.getDefinition();
450         Map JavaDoc services = WSIFUtils.getAllItems(def, "Service");
451         Port port = fieldJmsPort.getPortModel();
452         for (Iterator JavaDoc i = services.values().iterator(); i.hasNext();) {
453             Service s = (Service) i.next();
454             if (s.getPorts().values().contains(port)) {
455                 serviceModel = s;
456                 break;
457             }
458         }
459         if (serviceModel == null) {
460             throw new WSIFException("cannot find service for port: " + port);
461         }
462
463         formatter = (JMSFormatter) fieldJmsPort.getFormatter();
464
465         WSIFRequest request = new WSIFRequest(serviceModel.getQName());
466         request.setIncomingMessage(message);
467         request.setOperationName(fieldOperation.getName());
468         request.setPortName(fieldJmsPort.getPortModel().getName());
469         request.setInputName(fieldBindingOperation.getBindingInput().getName());
470
471         javax.jms.Message JavaDoc jmsMessage =
472             jmsDest.createMessage(getJMSMessageType());
473
474         formatter.formatRequest(request, jmsMessage);
475
476         // **NS No support for listener - add that in
477
// The basis is off the handler - if handler is defined, we start up the listener
478
if (isAsyncOperation() && handler != null)
479             jmsDest.setAsyncMode(true);
480         correlId = jmsDest.send(jmsMessage, null, !inputOnlyOp);
481
482         return correlId;
483
484     }
485
486     /**
487      * receivesend jms message
488      */

489     private boolean receiveJmsMessage(
490         Object JavaDoc responseObject,
491         WSIFMessage output,
492         WSIFMessage fault)
493         throws WSIFException {
494         Trc.entry(this, responseObject, output, fault);
495
496         if (!(responseObject instanceof javax.jms.Message JavaDoc))
497             throw new WSIFException("Object is not of type javax.jms.Message");
498
499         // The WSIFJMSDestination probably didn't receive this message, so
500
// tell it about the message so we can inquire about its JMS properties
501
// later.
502
fieldJmsPort.getJmsDestination().setLastMessage(
503             (javax.jms.Message JavaDoc) responseObject);
504         
505         WSIFResponse resp =
506             formatter.unformatResponse((javax.jms.Message JavaDoc) responseObject);
507
508         if (resp.getIsFault()) {
509             formatter.copyTo(resp.getOutgoingMessage(), fault);
510             setFaultProperties(fault, formatter.getLastBindingFault());
511
512             Trc.exit(false);
513             return false;
514         } else {
515
516             // the output message contains all response parts
517
// even if not defined in the WSDL. Any parts
518
// defined in the WSDL but not in the response
519
// default to null
520
ArrayList JavaDoc wsdlOutputParts = getWSDLOutputPartNames();
521             WSIFMessage m = resp.getOutgoingMessage();
522             if (m != null) {
523                 String JavaDoc partName;
524                 for (Iterator JavaDoc i = m.getPartNames(); i.hasNext();) {
525                     partName = (String JavaDoc) i.next();
526                     output.setObjectPart(partName, m.getObjectPart(partName));
527                     wsdlOutputParts.remove(partName);
528                 }
529             }
530
531             for (Iterator JavaDoc i = wsdlOutputParts.iterator(); i.hasNext();) {
532                 output.setObjectPart((String JavaDoc) i.next(), null);
533             }
534
535             setOutProperties(output);
536             Trc.exit(true);
537             return true;
538         }
539     }
540
541     /**
542      * This merges all properties from the various places into
543      * a single HashMap with a key of the part name, and value
544      * the property that part is for.
545      */

546     private HashMap JavaDoc getPropertyParts() {
547         HashMap JavaDoc propertyParts = new HashMap JavaDoc();
548
549         // the input message properties
550
if (fieldInputJmsProperties != null) {
551             for (Iterator JavaDoc i = fieldInputJmsProperties.keySet().iterator();
552                 i.hasNext();
553                 ) {
554                 String JavaDoc propertyName = (String JavaDoc) i.next();
555                 propertyParts.put(
556                     propertyName,
557                     fieldInputJmsProperties.get(propertyName));
558             }
559         }
560
561         return propertyParts;
562     }
563
564     private void setPropertyValues() throws WSIFException {
565         String JavaDoc value;
566         WSIFJMSDestination jmsDest = fieldJmsPort.getJmsDestination();
567         // First set the default properties identifying the JMS message
568
try {
569             value = fieldOperation.getName();
570             if (value != null && value.length() > 0) {
571                 jmsDest.setProperty(
572                     WSIFConstants.JMS_PROP_OPERATION_NAME,
573                     value);
574             }
575         } catch (WSIFException ex) {
576             Trc.ignoredException(ex);
577         }
578
579         try {
580             BindingInput bi = fieldBindingOperation.getBindingInput();
581             value = (bi == null) ? null : bi.getName();
582             if (value != null && value.length() > 0) {
583                 jmsDest.setProperty(WSIFConstants.JMS_PROP_INPUT_NAME, value);
584             }
585         } catch (WSIFException ex) {
586             Trc.ignoredException(ex);
587         }
588
589         try {
590             BindingOutput bo = fieldBindingOperation.getBindingOutput();
591             value = (bo == null) ? null : bo.getName();
592             if (value != null && value.length() > 0) {
593                 jmsDest.setProperty(WSIFConstants.JMS_PROP_OUTPUT_NAME, value);
594             }
595         } catch (WSIFException ex) {
596             Trc.ignoredException(ex);
597         }
598
599         // propertyValues from jms:address
600
try {
601             JMSAddress ja = fieldJmsPort.getObjectReference();
602             if (ja != null) {
603                 setJMSPropertyValues(ja.getJMSPropertyValues());
604             }
605         } catch (WSIFException ex) {
606             Trc.ignoredException(ex);
607         }
608
609         // propertyValues from the input message
610
if (fieldInputJmsPropertyValues != null) {
611             try {
612                 setJmsPropertyValues(fieldInputJmsPropertyValues);
613             } catch (WSIFException ex) {
614                 Trc.ignoredException(ex);
615             }
616         }
617     }
618
619     /**
620      * sets the output message properties from the received JMS message.
621      */

622     private void setOutProperties(WSIFMessage output) throws WSIFException {
623
624         if (output != null && fieldOutputProperties != null) {
625             WSIFJMSDestination jmsDest = fieldJmsPort.getJmsDestination();
626             for (Iterator JavaDoc i = fieldOutputProperties.keySet().iterator();
627                 i.hasNext();
628                 ) {
629                 String JavaDoc partName = (String JavaDoc) i.next();
630                 String JavaDoc propertyName =
631                     (String JavaDoc) fieldOutputProperties.get(partName);
632                 try {
633                     Object JavaDoc propertyValue = jmsDest.getProperty(propertyName);
634                     output.setObjectPart(partName, propertyValue);
635                 } catch (WSIFException ex) {
636                     Trc.ignoredException(ex);
637                 }
638             }
639         }
640     }
641
642     private void setFaultProperties(
643         WSIFMessage fault,
644         BindingFault bindingFault) throws WSIFException{
645
646         Trc.entry(this, fault, bindingFault);
647
648         WSIFJMSDestination jmsDest = fieldJmsPort.getJmsDestination();
649         Iterator JavaDoc it = bindingFault.getExtensibilityElements().iterator();
650         while (it.hasNext()) {
651             Object JavaDoc ele = it.next();
652             if (ele instanceof JMSFaultIndicator) {
653                 JMSFaultIndicator indic = (JMSFaultIndicator) ele;
654                 List JavaDoc fProps = indic.getJMSFaultProperties();
655                 Iterator JavaDoc itFProp = fProps.iterator();
656                 while (itFProp.hasNext()) {
657                     Object JavaDoc next = itFProp.next();
658                     if (next instanceof JMSFaultProperty) {
659                         JMSFaultProperty fProp = (JMSFaultProperty) next;
660                         String JavaDoc partName = fProp.getPart();
661                         String JavaDoc propName = fProp.getName();
662
663                         if (partName != null && partName.length() > 0)
664                             try {
665                                 Object JavaDoc propValue =
666                                     jmsDest.getProperty(propName);
667                                 fault.setObjectPart(partName, propValue);
668                             } catch (WSIFException ex) {
669                                 Trc.ignoredException(ex);
670                             }
671                     }
672                 }
673             } else if (ele instanceof JMSProperty) {
674                 JMSProperty prop = (JMSProperty) ele;
675                 String JavaDoc partName = prop.getPart();
676                 String JavaDoc propName = prop.getName();
677
678                 try {
679                     Object JavaDoc propValue = jmsDest.getProperty(propName);
680                     fault.setObjectPart(partName, propValue);
681                 } catch (WSIFException ex) {
682                     Trc.ignoredException(ex);
683                 }
684             }
685         }
686
687         Trc.exit();
688     }
689
690     /**
691      * set the specified jms property values
692      */

693     private void setJMSPropertyValues(List JavaDoc propertyValues)
694         throws WSIFException {
695         if (propertyValues != null) {
696             WSIFJMSDestination jmsDest = fieldJmsPort.getJmsDestination();
697             for (Iterator JavaDoc i = propertyValues.iterator(); i.hasNext();) {
698                 JMSPropertyValue pv = (JMSPropertyValue) i.next();
699                 if (pv != null) {
700                     Object JavaDoc o = getObjectValue(pv.getType(), pv.getValue());
701                     if (!timeoutProperty(pv.getName(), o)) {
702                         jmsDest.setProperty(pv.getName(), o);
703                     }
704                 }
705             }
706         }
707     }
708
709     /**
710      * set the specified jms header values
711      */

712     private void setJmsPropertyValues(HashMap JavaDoc attr) throws WSIFException {
713         //FIXME
714
// Need to separate out between header values and property values
715
Iterator JavaDoc iter = attr.keySet().iterator();
716         while (iter.hasNext()) {
717             String JavaDoc attName = (String JavaDoc) iter.next();
718
719             JMSPropertyValue value = (JMSPropertyValue) attr.get(attName);
720
721             if (value != null) {
722                 Object JavaDoc o = getObjectValue(value.getType(), value.getValue());
723                 if (!timeoutProperty(attName, o)) {
724                     WSIFJMSDestination jmsDest = fieldJmsPort.getJmsDestination();
725                     jmsDest.setProperty(attName, o);
726                 }
727             }
728         }
729     }
730
731     private boolean timeoutProperty(String JavaDoc propertyName, Object JavaDoc value) {
732         boolean isTimeoutProperty = false;
733         if (WSIFConstants.WSIF_PROP_SYNC_TIMEOUT.equals(propertyName)) {
734             isTimeoutProperty = true;
735             try {
736                 syncTimeout = Long.parseLong(value.toString());
737                 Trc.event(this, "overridding syncTimeout to " + syncTimeout);
738             } catch (NumberFormatException JavaDoc ex) {
739                 Trc.ignoredException(ex);
740             }
741         } else if (
742             WSIFConstants.WSIF_PROP_ASYNC_TIMEOUT.equals(propertyName)) {
743             isTimeoutProperty = true;
744             try {
745                 asyncTimeout = Long.parseLong(value.toString());
746                 Trc.event(this, "overridding asyncTimeout to " + syncTimeout);
747             } catch (NumberFormatException JavaDoc ex) {
748                 Trc.ignoredException(ex);
749             }
750         }
751         return isTimeoutProperty;
752     }
753
754     /**
755      * get the specified operation (w/ input and output message)
756      */

757     protected Operation getOperation() throws Exception JavaDoc {
758
759         if (fieldOperation == null) {
760             // <input> and <output> tags in binding operations are not mandatory
761
// so deal with null BindingInputs or BindingOutputs
762
if (fieldBindingOperation.getBindingInput() != null)
763                 fieldInputMessageName =
764                     fieldBindingOperation.getBindingInput().getName();
765
766             if (fieldBindingOperation.getBindingOutput() != null)
767                 fieldOutputMessageName =
768                     fieldBindingOperation.getBindingOutput().getName();
769
770             // Get operation
771
fieldOperation =
772                 fieldBasePort.getBinding().getPortType().getOperation(
773                     fieldBindingOperation.getName(),
774                     fieldInputMessageName,
775                     fieldOutputMessageName);
776
777             //
778
// Jms extensions on Input
779
//
780
if (fieldBindingOperation.getBindingInput() != null) {
781                 Iterator JavaDoc inputIterator =
782                     fieldBindingOperation
783                         .getBindingInput()
784                         .getExtensibilityElements()
785                         .iterator();
786                 fieldInputJmsProperties = new HashMap JavaDoc();
787                 fieldInputJmsPropertyValues = new HashMap JavaDoc();
788                 while (inputIterator.hasNext()) {
789                     ExtensibilityElement ele =
790                         (ExtensibilityElement) inputIterator.next();
791                     if (ele instanceof JMSInput) {
792                         fieldInput = (JMSInput) ele;
793                     } else if (ele instanceof JMSProperty) {
794                         fieldInputJmsProperties.put(
795                             ((JMSProperty) ele).getPart(),
796                             ((JMSProperty) ele).getName());
797                     } else if (ele instanceof JMSPropertyValue) {
798                         fieldInputJmsPropertyValues.put(
799                             ((JMSPropertyValue) ele).getName(),
800                             ele);
801                     }
802                 }
803             }
804
805             //
806
// Jms extensions on Output
807
//
808
if (fieldBindingOperation.getBindingOutput() != null) {
809                 Iterator JavaDoc outputIterator =
810                     fieldBindingOperation
811                         .getBindingOutput()
812                         .getExtensibilityElements()
813                         .iterator();
814                 fieldOutputProperties = new HashMap JavaDoc();
815                 while (outputIterator.hasNext()) {
816                     ExtensibilityElement ele =
817                         (ExtensibilityElement) outputIterator.next();
818                     if (ele instanceof JMSOutput) {
819                         fieldOutput = (JMSOutput) ele;
820                     } else if (ele instanceof JMSProperty) {
821                         fieldOutputProperties.put(
822                             ((JMSProperty) ele).getPart(),
823                             ((JMSProperty) ele).getName());
824                     }
825                 }
826
827             }
828         }
829
830         if (fieldOperation == null) {
831             throw new WSIFException(
832                 "Unable to resolve Jms binding for operation '"
833                     + fieldBindingOperation.getName()
834                     + ":"
835                     + fieldInputMessageName
836                     + ":"
837                     + fieldOutputMessageName
838                     + "'");
839         }
840
841         return fieldOperation;
842     }
843
844     private Object JavaDoc getObjectValue(QName JavaDoc type, String JavaDoc value)
845         throws WSIFException {
846
847         Object JavaDoc primitiveType = null;
848
849         if (JMSMessage.isSchemaNamespace(type.getNamespaceURI())
850             && JMSMessage.isXSDPrimitiveType(type.getLocalPart())) {
851             Class JavaDoc cls =
852                 (Class JavaDoc) JMSMessage.PRIMITIVE_JAVA_MAPPING.get(
853                     type.getLocalPart().toLowerCase());
854
855             if (cls == String JavaDoc.class) {
856                 primitiveType = value;
857             }
858             // For byte array class
859
else if (cls == byte[].class) {
860                 primitiveType = value.getBytes();
861             }
862             // For Gregorian Calendar && Date
863
else if (
864                 java.util.GregorianCalendar JavaDoc.class.isAssignableFrom(cls)
865                     || java.util.Date JavaDoc.class.isAssignableFrom(cls)) {
866                 // DON"T DO ANYTHING since unable to know what the format is
867
}
868             // For all the rest
869
else {
870                 try {
871                     java.lang.reflect.Constructor JavaDoc constructor =
872                         cls.getConstructor(new Class JavaDoc[] { String JavaDoc.class });
873                     primitiveType =
874                         constructor.newInstance(new Object JavaDoc[] { value });
875                 } catch (Exception JavaDoc e) {
876                     Trc.ignoredException(e);
877                 }
878             }
879         }
880
881         if (primitiveType != null)
882             return primitiveType;
883         else
884             throw new WSIFException(
885                 "Unable to create the java object for XSD Type '"
886                     + type.toString()
887                     + "' using value '"
888                     + value
889                     + "'");
890     }
891
892     //FIXME - Should I place somewhere else??
893
public int getJMSMessageType() {
894         Trc.entry(this);
895         Iterator JavaDoc bindingIterator =
896             fieldBasePort.getBinding().getExtensibilityElements().iterator();
897
898         while (bindingIterator.hasNext()) {
899             try {
900                 ExtensibilityElement ele =
901                     (ExtensibilityElement) bindingIterator.next();
902                 if (JMSConstants
903                     .Q_ELEM_JMS_BINDING
904                     .equals(ele.getElementType())) {
905
906                     int t = ((JMSBinding) ele).getJmsMessageType();
907                     Trc.exit(t);
908                     return t;
909                 }
910             } catch (ClassCastException JavaDoc exn) {
911                 Trc.ignoredException(exn);
912             }
913         }
914         Trc.exit(JMSConstants.MESSAGE_TYPE_NOTSET);
915         return JMSConstants.MESSAGE_TYPE_NOTSET;
916     }
917
918     /**
919      * Sets if the currently executing request is an asynchronous request.
920      *
921      * @param b true if the current request is a asynchronous request,
922      * otherwise false
923      */

924     private void setAsyncOperation(boolean b) {
925         asyncOperation = b;
926     }
927
928     /**
929      * Tests if the currently executing request is an asynchronous request.
930      *
931      * @return true if the current request is a asynchronous request,
932      * otherwise false
933      */

934     public boolean isAsyncOperation() {
935         Trc.entry(this);
936         Trc.exit(asyncOperation);
937         return asyncOperation;
938     }
939
940     /**
941      * This sets up any context JMS property values in the Destination
942      */

943     private void setDestinationContext() throws WSIFException{
944         if (context == null) {
945             return;
946         }
947         HashMap JavaDoc jmsProps = new HashMap JavaDoc();
948         for (Iterator JavaDoc i = context.getPartNames(); i.hasNext();) {
949             String JavaDoc partName = (String JavaDoc) i.next();
950             try {
951                 Object JavaDoc value = context.getObjectPart(partName);
952                 if (!timeoutProperty(partName, value)) {
953                     if (partName
954                         .startsWith(WSIFConstants.CONTEXT_JMS_PREFIX)) {
955                         jmsProps.put(
956                             partName.substring(
957                                 WSIFConstants.CONTEXT_JMS_PREFIX.length()),
958                             value);
959                     }
960                 }
961             } catch (WSIFException ex) {
962                 Trc.ignoredException(ex);
963             }
964         }
965         if (jmsProps.size() > 0) {
966             WSIFJMSDestination jmsDest = fieldJmsPort.getJmsDestination();
967             jmsDest.setProperties(jmsProps);
968         }
969     }
970
971     private ArrayList JavaDoc getWSDLInputPartNames() {
972         ArrayList JavaDoc wsdlInputParts = new ArrayList JavaDoc();
973         Input wsdlInput = fieldOperation.getInput();
974         if (wsdlInput != null) {
975             Message inputMessage = wsdlInput.getMessage();
976             String JavaDoc partName;
977             for (Iterator JavaDoc i = inputMessage.getParts().keySet().iterator();
978                 i.hasNext();
979                 ) {
980                 partName = (String JavaDoc) i.next();
981                 if (fieldInputJmsProperties == null
982                     || !fieldInputJmsProperties.containsKey(partName)) {
983                     wsdlInputParts.add(partName);
984                 }
985             }
986         }
987         return wsdlInputParts;
988     }
989
990     private ArrayList JavaDoc getWSDLOutputPartNames() {
991         ArrayList JavaDoc wsdlOutputParts = new ArrayList JavaDoc();
992         Output wsdlOutput = fieldOperation.getOutput();
993         if (wsdlOutput != null) {
994             Message outputMessage = wsdlOutput.getMessage();
995             String JavaDoc partName;
996             for (Iterator JavaDoc i = outputMessage.getParts().keySet().iterator();
997                 i.hasNext();
998                 ) {
999                 partName = (String JavaDoc) i.next();
1000                if (fieldOutputProperties == null
1001                    || !fieldOutputProperties.containsKey(partName)) {
1002                    wsdlOutputParts.add(partName);
1003                }
1004            }
1005        }
1006        return wsdlOutputParts;
1007    }
1008
1009    public WSIFPort getWSIFPort() {
1010        Trc.entry(this);
1011        Trc.exit(fieldJmsPort);
1012        return fieldJmsPort;
1013    }
1014    
1015    public String JavaDoc deep() {
1016        String JavaDoc buff = "";
1017        try {
1018            buff = new String JavaDoc(super.toString() + ":\n");
1019            buff += "basePort:" + Trc.brief(fieldBasePort);
1020            buff += " bindingOperation:" + Trc.brief(fieldBindingOperation);
1021            buff += " wsifPort_Jms:" + fieldJmsPort;
1022            buff += " Operation:" + Trc.brief(fieldOperation);
1023            buff += " InputOnlyOperation:" + inputOnlyOp;
1024            buff += " JmsMessageType:" + getJMSMessageType();
1025            buff += " inputMessageName:" + fieldInputMessageName;
1026            buff += " InputJmsPropertyValues:" + fieldInputJmsPropertyValues;
1027            buff += " InputProperty:" + fieldInputJmsProperties;
1028            buff += " Input:" + fieldInput;
1029            buff += " outputMessageName:" + fieldOutputMessageName;
1030            buff += " OutputProperties:" + fieldOutputProperties;
1031            buff += " Output:" + fieldOutput;
1032            buff += " Formater:" + formatter;
1033            buff += " handler:" + handler;
1034            buff += " asyncOperation:" + asyncOperation;
1035        } catch (Exception JavaDoc e) {
1036            Trc.exceptionInTrace(e);
1037        }
1038        return buff;
1039    }
1040
1041}
Popular Tags