KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > wsif > base > WSIFDefaultOperation


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.base;
59
60 import java.util.HashMap JavaDoc;
61 import java.util.Iterator JavaDoc;
62 import java.util.List JavaDoc;
63 import java.util.Map JavaDoc;
64
65 import javax.wsdl.Operation;
66 import javax.xml.namespace.QName JavaDoc;
67
68 import org.apache.wsif.WSIFCorrelationId;
69 import org.apache.wsif.WSIFException;
70 import org.apache.wsif.WSIFMessage;
71 import org.apache.wsif.WSIFOperation;
72 import org.apache.wsif.WSIFPort;
73 import org.apache.wsif.WSIFResponseHandler;
74 import org.apache.wsif.compiler.util.TypeMapping;
75 import org.apache.wsif.logging.Trc;
76 import org.apache.wsif.util.WSIFUtils;
77 import org.apache.wsif.wsdl.extensions.jms.JMSProperty;
78 import org.apache.wsif.wsdl.extensions.jms.JMSPropertyValue;
79
80 public abstract class WSIFDefaultOperation implements WSIFOperation {
81     private static final long serialVersionUID = 1L;
82     transient protected HashMap JavaDoc inJmsProps = new HashMap JavaDoc();
83     transient protected HashMap JavaDoc outJmsProps = new HashMap JavaDoc();
84     transient protected HashMap JavaDoc inJmsPropVals = new HashMap JavaDoc();
85     protected WSIFMessage context;
86     protected boolean closed = false;
87    
88     /**
89      * @see WSIFOperation#executeRequestResponseOperation(WSIFMessage, WSIFMessage, WSIFMessage)
90      */

91     public abstract boolean executeRequestResponseOperation(
92         WSIFMessage input,
93         WSIFMessage output,
94         WSIFMessage fault)
95         throws WSIFException;
96
97     /**
98      * @see WSIFOperation#executeInputOnlyOperation(WSIFMessage)
99      */

100     public abstract void executeInputOnlyOperation(WSIFMessage input)
101         throws WSIFException;
102
103     /**
104      * Default implementation of executeRequestResponseAsync.
105      * By default async operation is not supported so this just
106      * throws an exception.
107      * @see WSIFOperation#executeRequestResponseAsync(WSIFMessage, WSIFResponseHandler)
108      */

109     public WSIFCorrelationId executeRequestResponseAsync(
110         WSIFMessage input,
111         WSIFResponseHandler handler)
112         throws WSIFException {
113         throw new WSIFException("asynchronous operations not supportted");
114     }
115
116     /**
117      * Default implementation of executeRequestResponseAsync.
118      * By default async operation is not supported so this just
119      * throws an exception.
120      * @see WSIFOperation#executeRequestResponseAsync(WSIFMessage)
121      */

122     public WSIFCorrelationId executeRequestResponseAsync(WSIFMessage input)
123         throws WSIFException {
124         throw new WSIFException("asynchronous operations not supportted");
125     }
126
127     /**
128      * Default implemantation of fireAsyncResponse.
129      * By default async operation is not supported so this just
130      * throws an exception.
131      * @see WSIFOperation#fireAsyncResponse(Object)
132      * @param response an Object representing the response
133      */

134     public void fireAsyncResponse(Object JavaDoc response) throws WSIFException {
135         throw new WSIFException("asynchronous operations not supportted");
136     }
137
138     /**
139      * Default implemantation of processAsyncResponse.
140      * By default async operation is not supported so this just
141      * throws an exception.
142      * @see WSIFOperation#processAsyncResponse(Object,WSIFMessage,WSIFMessage)
143      */

144     public boolean processAsyncResponse(
145         Object JavaDoc response,
146         WSIFMessage output,
147         WSIFMessage fault)
148         throws WSIFException {
149         throw new WSIFException("asynchronous operations not supportted");
150     }
151
152     /**
153      * @see WSIFOperation#createInputMessage()
154      */

155     public WSIFMessage createInputMessage() {
156         Trc.entry(this);
157         WSIFMessage msg = new WSIFDefaultMessage();
158         if (msg != null) {
159             // Now find the javax.wsdl.Message & set it on the WSIFMessage
160
try {
161                 msg.setMessageDefinition(
162                     getOperation().getInput().getMessage());
163             } catch (Exception JavaDoc e) {
164                 Trc.ignoredException(e);
165             }
166         }
167         Trc.exit(msg);
168         return msg;
169     }
170
171     /**
172      * @see WSIFOperation#createInputMessage(String)
173      */

174     public WSIFMessage createInputMessage(String JavaDoc name) {
175         Trc.entry(this, name);
176         WSIFMessage msg = new WSIFDefaultMessage();
177         if (msg != null) {
178             msg.setName(name);
179             // Now find the javax.wsdl.Message & set it on the WSIFMessage
180
try {
181                 msg.setMessageDefinition(
182                     getOperation().getInput().getMessage());
183             } catch (Exception JavaDoc e) {
184                 Trc.ignoredException(e);
185             }
186         }
187         Trc.exit(msg);
188         return msg;
189     }
190
191     /**
192      * @see WSIFOperation#createOutputMessage()
193      */

194     public WSIFMessage createOutputMessage() {
195         Trc.entry(this);
196         WSIFMessage msg = new WSIFDefaultMessage();
197         if (msg != null) {
198             // Now find the javax.wsdl.Message & set it on the WSIFMessage
199
try {
200                 msg.setMessageDefinition(
201                     getOperation().getOutput().getMessage());
202             } catch (Exception JavaDoc e) {
203                 Trc.ignoredException(e);
204             }
205         }
206         Trc.exit(msg);
207         return msg;
208     }
209
210     /**
211      * @see WSIFOperation#createOutputMessage(String)
212      */

213     public WSIFMessage createOutputMessage(String JavaDoc name) {
214         Trc.entry(this, name);
215         WSIFMessage msg = new WSIFDefaultMessage();
216         if (msg != null) {
217             msg.setName(name);
218             // Now find the javax.wsdl.Message & set it on the WSIFMessage
219
try {
220                 msg.setMessageDefinition(
221                     getOperation().getOutput().getMessage());
222             } catch (Exception JavaDoc e) {
223                 Trc.ignoredException(e);
224             }
225         }
226         Trc.exit(msg);
227         return msg;
228     }
229
230     /**
231      * @see WSIFOperation#createFaultMessage()
232      */

233     public WSIFMessage createFaultMessage() {
234         Trc.entry(this);
235         WSIFMessage wm = new WSIFDefaultMessage();
236         Trc.exit(wm);
237         return wm;
238     }
239
240     /**
241      * @see WSIFOperation#createFaultMessage(String)
242      */

243     public WSIFMessage createFaultMessage(String JavaDoc name) {
244         Trc.entry(this, name);
245         WSIFMessage msg = new WSIFDefaultMessage();
246         if (msg != null) {
247             msg.setName(name);
248             // Now find the javax.wsdl.Message & set it on the WSIFMessage
249
try {
250                 msg.setMessageDefinition(
251                     getOperation().getFault(name).getMessage());
252             } catch (Exception JavaDoc e) {
253                 Trc.ignoredException(e);
254             }
255         }
256         Trc.exit(msg);
257         return msg;
258     }
259
260     /**
261      * Sets the input Jms properties for this operation
262      */

263     public void setInputJmsProperties(List JavaDoc list) throws WSIFException {
264         Trc.entry(this, list);
265         inJmsProps = makeSomeKindOfJmsMap(list);
266         Trc.exit();
267     }
268     
269     /**
270      * Sets the output Jms properties for this operation
271      */

272     public void setOutputJmsProperties(List JavaDoc list) throws WSIFException {
273         Trc.entry(this, list);
274         outJmsProps = makeSomeKindOfJmsMap(list);
275         Trc.exit();
276     }
277     
278     public void setInputJmsProperties(HashMap JavaDoc hm) {
279         Trc.entry(this, hm);
280         inJmsProps = hm;
281         Trc.exit();
282     }
283     
284     public void setOutputJmsProperties(HashMap JavaDoc hm) {
285         Trc.entry(this, hm);
286         outJmsProps = hm;
287         Trc.exit();
288     }
289     
290     public HashMap JavaDoc getInputJmsProperties() {
291         Trc.entry(this);
292         Trc.exit(inJmsProps);
293         return inJmsProps;
294     }
295     
296     public HashMap JavaDoc getOutputJmsProperties() {
297         Trc.entry(this);
298         Trc.exit(outJmsProps);
299         return outJmsProps;
300     }
301     
302     public abstract WSIFPort getWSIFPort();
303     
304     /**
305      * This method adds new property values to existing HashMap.
306      * Where a property value exists in the existing HashMap and the new list,
307      * this method replaces the existing property value with the new one from the list.
308      */

309     public void addInputJmsPropertyValues(List JavaDoc list) throws WSIFException {
310         Trc.entry(this, list);
311         if (list != null && !list.isEmpty()) {
312             HashMap JavaDoc newPvs = makeSomeKindOfJmsMap(list);
313             newPvs.putAll(inJmsPropVals);
314             inJmsPropVals = newPvs;
315         }
316         Trc.exit();
317     }
318     
319     public void setInputJmsPropertyValues(HashMap JavaDoc hm) {
320         Trc.entry(this, hm);
321         inJmsPropVals = hm;
322         Trc.exit();
323     }
324     
325     public HashMap JavaDoc getInputJmsPropertyValues() {
326         Trc.entry(this);
327         Trc.exit(inJmsPropVals);
328         return inJmsPropVals;
329     }
330     
331     /**
332      * Utility method that sets the jms properties for this operation
333      */

334     protected HashMap JavaDoc makeSomeKindOfJmsMap(List JavaDoc list) throws WSIFException {
335         Trc.entry(this, list);
336         Map JavaDoc simpleTypeReg = WSIFUtils.getSimpleTypesMap();
337         HashMap JavaDoc props = new HashMap JavaDoc(list.size());
338         for (Iterator JavaDoc it = list.iterator(); it.hasNext();) {
339             Object JavaDoc ee = it.next();
340             if (ee instanceof JMSProperty) {
341                 JMSProperty prop = (JMSProperty) ee;
342                 props.put(prop.getPart(), prop.getName());
343             } else if (ee instanceof JMSPropertyValue) {
344                 JMSPropertyValue propVal = (JMSPropertyValue) ee;
345     
346                 String JavaDoc name = propVal.getName();
347                 if (name == null || name.length() == 0)
348                     throw new WSIFException("jms:propertyValue found without a name");
349     
350                 QName JavaDoc type = propVal.getType();
351                 if (type == null)
352                     throw new WSIFException(
353                         "jms:propertyValue " + name + " did not have a type");
354                 if (type.getNamespaceURI() == null || type.getLocalPart() == null)
355                     throw new WSIFException(
356                         "jms:propertyValue " + name + " has a badly formed type");
357     
358                 String JavaDoc value = propVal.getValue();
359                 if (value == null || value.length() == 0)
360                     throw new WSIFException(
361                         "jms:propertyValue " + name + " did not have a value");
362     
363                 TypeMapping tm = (TypeMapping) (simpleTypeReg.get(type));
364                 if (tm == null || tm.javaType == null)
365                     throw new WSIFException(
366                         "jms:propertyValue "
367                             + name
368                             + " had a type that was "
369                             + "unknown or was not a simple type");
370     
371                 Class JavaDoc javaClass = null;
372                 try {
373                     javaClass =
374                         Class.forName(
375                             tm.javaType,
376                             true,
377                             Thread.currentThread().getContextClassLoader());
378                 } catch (ClassNotFoundException JavaDoc cce) {
379                     Trc.exception(cce);
380                     throw new WSIFException(
381                         "Unexpected ClassNotFoundException when processing "
382                             + "jms:propertyValue "
383                             + name
384                             + ". Could not convert the type to a java class. "
385                             + cce);
386                 }
387     
388                 Object JavaDoc obj = null;
389                 try {
390                     if (javaClass.equals(String JavaDoc.class))
391                         obj = value;
392                     else if (javaClass.equals(Integer JavaDoc.class))
393                         obj = new Integer JavaDoc(value);
394                     else if (javaClass.equals(Boolean JavaDoc.class))
395                         obj = new Boolean JavaDoc(value);
396                     else if (javaClass.equals(Byte JavaDoc.class))
397                         obj = new Byte JavaDoc(value);
398                     else if (javaClass.equals(Double JavaDoc.class))
399                         obj = new Double JavaDoc(value);
400                     else if (javaClass.equals(Float JavaDoc.class))
401                         obj = new Float JavaDoc(value);
402                     else if (javaClass.equals(Long JavaDoc.class))
403                         obj = new Long JavaDoc(value);
404                     else if (javaClass.equals(Short JavaDoc.class))
405                         obj = new Short JavaDoc(value);
406                     else
407                         throw new WSIFException(
408                             "jms:propertyValue " + name + " had an invalid type");
409                 } catch (NumberFormatException JavaDoc nfe) {
410                     Trc.exception(nfe);
411                     throw new WSIFException(
412                         "jms:propertyValue "
413                             + name
414                             + " a value that could not "
415                             + "be converted into the specified type. Caught NumberFormatException. "
416                             + nfe);
417                 }
418     
419                 props.put(name, obj);
420             }
421         }
422         Trc.exit(props);
423         return props;
424     }
425
426     /**
427      * Allows the application programmer or stub to pass context
428      * information to the binding. The Port implementation may use
429      * this context - for example to update a SOAP header. There is
430      * no definition of how a Port may utilize the context.
431      */

432     public void setContext(WSIFMessage context) {
433         Trc.entry(this, context);
434         if (context == null) {
435             throw new IllegalArgumentException JavaDoc("context must not be null");
436         }
437         this.context = context;
438         Trc.exit();
439     }
440
441     /**
442      * Gets the context information for this binding.
443      */

444     public WSIFMessage getContext() throws WSIFException {
445         Trc.entry(this);
446         WSIFMessage contextCopy;
447         try {
448             if (this.context == null) {
449                 contextCopy = (WSIFMessage) getWSIFPort().getContext().clone();
450             } else {
451                 contextCopy = (WSIFMessage) this.context.clone();
452             }
453         } catch (CloneNotSupportedException JavaDoc e) {
454             throw new WSIFException(
455                 "CloneNotSupportedException cloning context", e);
456         }
457         Trc.exit(contextCopy);
458         return contextCopy;
459     }
460     
461     abstract protected Operation getOperation() throws Exception JavaDoc;
462     
463     protected void close() throws WSIFException {
464     Trc.entry(this);
465         if (closed)
466             throw new WSIFException("Cannot reuse a WSIFOperation to invoke multiple operations");
467         closed = true;
468         Trc.exit();
469     }
470 }
471
Popular Tags