KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > wsif > providers > jca > WSIFMessage_JCAStreamable


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
59 package org.apache.wsif.providers.jca;
60
61 import org.apache.wsif.*;
62 import org.apache.wsif.format.*;
63 import org.apache.wsif.providers.jca.WSIFUtils_JCA;
64 import org.apache.wsif.logging.*;
65 import java.net.MalformedURLException JavaDoc;
66 import java.net.URL JavaDoc;
67 import java.util.*;
68 import javax.wsdl.*;
69 import javax.wsdl.extensions.ExtensibilityElement;
70
71 /**
72  * The class WSIFMessage_JCAStreamable is a specialized version of the WSIFMessage_JCA to support Resource Adapters
73  * using javax.resource.cci.Streamable.
74  *
75  * @author Michael Beisiegel
76  * @author Piotr Przybylski <piotrp@ca.ibm.com>
77  * @author John Green
78  */

79  
80 public class WSIFMessage_JCAStreamable extends org.apache.wsif.providers.jca.WSIFMessage_JCA implements javax.resource.cci.Streamable JavaDoc {
81     
82     private static final long serialVersionUID = 1L;
83     private Message fieldMessageModel = null;
84     private java.util.HashMap JavaDoc fieldPartNameFormatHandlerMapping = new java.util.HashMap JavaDoc();
85
86     /**
87      * @see org.apache.wsif.providers.jca.WSIFMessage_JCA#WSIFMessage_JCA(Definition, Binding, String, String, String, int)
88      */

89     public WSIFMessage_JCAStreamable(Definition aDefinition, Binding aBinding, String JavaDoc aOperationName, String JavaDoc aInputName, String JavaDoc aOutputName, int aMessageType) {
90         super(aDefinition, aBinding, aOperationName, aInputName, aOutputName, aMessageType);
91         Operation operation = aBinding.getPortType().getOperation(aOperationName, aInputName, aOutputName);
92         switch(aMessageType){
93             case WSIFMessage_JCA.INPUT_MESSAGE:
94                 if(operation.getInput() != null) {
95                     this.fieldMessageModel = operation.getInput().getMessage();
96                     setMessageDefinition(this.fieldMessageModel);
97                 }
98                 break;
99             case WSIFMessage_JCA.OUTPUT_MESSAGE:
100                 if(operation.getOutput() != null) {
101                     this.fieldMessageModel = operation.getOutput().getMessage();
102                     setMessageDefinition(this.fieldMessageModel);
103                 }
104                 break;
105             case WSIFMessage_JCA.FAULT_MESSAGE:
106                 break;
107             default:
108                 // Assume input
109
this.fieldMessageModel = operation.getInput().getMessage();
110                 setMessageDefinition(this.fieldMessageModel);
111                 break;
112         }
113     }
114
115
116     /**
117      * The method to read input stream and create message parts. For each part in the message
118      * (as defined in WSDL), with exception of parts representing ConnectionSpec and InteractionSpec properties,
119      * the format handler is created and its read() method is passed the inputStream. The parts
120      * creation is delayed until they are needed (i.e. when the client invokes <code>getObjectPart</code>).
121      * In this method only the part's format handler is created and stored.
122      *
123      * @see javax.resource.cci.Streamable#read(InputStream)
124      */

125     public void read(java.io.InputStream JavaDoc inputStream) throws java.io.IOException JavaDoc {
126
127         try {
128             Trc.entry(this);
129             if(fieldMessageModel == null)
130                 return;
131             HashMap partsToNotProcess = new HashMap();
132             BindingOperation bindingOperation = fieldBinding.getBindingOperation(fieldOperationName, fieldInputName, fieldOutputName);
133             BindingOutput bindingOutput = bindingOperation.getBindingOutput();
134             if (bindingOutput != null) {
135                 List list = bindingOutput.getExtensibilityElements();
136                 Iterator inputIterator = list.iterator();
137                 while (inputIterator.hasNext()) {
138                     ExtensibilityElement ele = (ExtensibilityElement)inputIterator.next();
139                     if (ele instanceof WSIFBindingOperation_JCAProperty) {
140                         WSIFBindingOperation_JCAProperty prop = (WSIFBindingOperation_JCAProperty)ele;
141                         String JavaDoc partName = prop.getPartName();
142                         partsToNotProcess.put(partName,partName);
143                     }
144                 }
145             }
146             Iterator iterator = this.fieldMessageModel.getOrderedParts(null).iterator();
147             while (iterator.hasNext()) {
148                 Part part = (Part) iterator.next();
149                 String JavaDoc partName = part.getName();
150                 
151                 if (partsToNotProcess.get(partName) != null) continue;
152
153                 WSIFFormatHandler_JCA formatHandler = null;
154                 if (this.fieldPartNameFormatHandlerMapping.containsKey(partName))
155                     formatHandler =
156                         (WSIFFormatHandler_JCA) this.fieldPartNameFormatHandlerMapping.get(partName);
157                 else {
158                     formatHandler = (WSIFFormatHandler_JCA) WSIFUtils_JCA.getFormatHandler(part, this.fieldDefinition, this.fieldBinding);
159                     this.fieldPartNameFormatHandlerMapping.put(partName, formatHandler);
160                 }
161                 formatHandler.read(inputStream);
162             }
163             Trc.exit();
164         }
165         catch (Exception JavaDoc exn1) {
166             Trc.exception(exn1);
167             throw new java.io.IOException JavaDoc(WSIFResource_JCA.get("WSIF1004E", exn1.getLocalizedMessage()));
168         }
169     }
170     
171     
172     /**
173      * Writes the contents of the message parts into the OutputStream. For each part in the message
174      * (as defined in WSDL), except parts representing interactionSpec properties, the format handler
175      * is created, part is set on the format handler and its <code>write</code> method is invoked.
176      * The format handlers are stored in the table.
177      *
178      * @see javax.resource.cci.Streamable#write(OutputStream)
179      */

180     public void write(java.io.OutputStream JavaDoc outputStream) throws java.io.IOException JavaDoc {
181
182         try {
183             Trc.entry(this);
184
185             HashMap partsToNotProcess = new HashMap();
186             BindingOperation bindingOperation = fieldBinding.getBindingOperation(fieldOperationName, fieldInputName, fieldOutputName);
187             BindingInput bindingInput = bindingOperation.getBindingInput();
188             if (bindingInput != null) {
189                 List list = bindingInput.getExtensibilityElements();
190                 Iterator inputIterator = list.iterator();
191                 while (inputIterator.hasNext()) {
192                     ExtensibilityElement ele = (ExtensibilityElement) inputIterator.next();
193                     if (ele instanceof WSIFBindingOperation_JCAProperty) {
194                         WSIFBindingOperation_JCAProperty prop = (WSIFBindingOperation_JCAProperty) ele;
195                         String JavaDoc partName = prop.getPartName();
196                         partsToNotProcess.put(partName, partName);
197                     }
198                 }
199             }
200
201             Iterator iterator = this.getPartNames();
202             while (iterator.hasNext()) {
203                 String JavaDoc partName = (String JavaDoc) iterator.next();
204                 if (partsToNotProcess.get(partName) != null)
205                     continue;
206                 Object JavaDoc oPart = this.parts.get(partName);
207                 WSIFFormatHandler_JCA formatHandler = null;
208                 if (oPart instanceof WSIFFormatPart) {
209                     WSIFFormatPart jcaPart = (WSIFFormatPart) oPart;
210                     if (jcaPart._getFormatHandler() != null) {
211                         formatHandler = (WSIFFormatHandler_JCA) jcaPart._getFormatHandler();
212                         this.fieldPartNameFormatHandlerMapping.put(partName, formatHandler);
213                     }
214                 }
215                 if (formatHandler == null) {
216                     if (this.fieldPartNameFormatHandlerMapping.containsKey(partName))
217                         formatHandler = (WSIFFormatHandler_JCA) this.fieldPartNameFormatHandlerMapping.get(partName);
218                     else {
219                         if (fieldMessageModel == null)
220                             return;
221                         Part part = (Part) this.fieldMessageModel.getPart(partName);
222                         formatHandler = (WSIFFormatHandler_JCA) WSIFUtils_JCA.getFormatHandler(part, this.fieldDefinition, this.fieldBinding);
223                         this.fieldPartNameFormatHandlerMapping.put(partName, formatHandler);
224                     }
225                     formatHandler.setObjectPart(oPart);
226                 }
227                 formatHandler.write(outputStream);
228             }
229             Trc.exit();
230         }
231         catch (Exception JavaDoc exn1) {
232             Trc.exception(exn1);
233             throw new java.io.IOException JavaDoc(WSIFResource_JCA.get("WSIF1005E", exn1.getLocalizedMessage()));
234
235         }
236
237     }
238
239     /**
240      * Returns object part with the given name. If the part had already been created,
241      * returns it. If there is a format handler for this part, the object part is obtained from
242      * it and returned, otherwise it creates the format handler and returns
243      * the object parts form it.
244      *
245      * @see org.apache.wsif.WSIFMessage#getObjectPart(String)
246      */

247     public Object JavaDoc getObjectPart(String JavaDoc partName) {
248
249         Trc.entry(this, partName);
250
251         if(this.parts != null){
252             Object JavaDoc existingPart = this.parts.get(partName);
253             if (existingPart != null)
254                 return existingPart;
255         }
256         try {
257             WSIFFormatHandler_JCA formatHandler =
258                 (WSIFFormatHandler_JCA) this.fieldPartNameFormatHandlerMapping.get(partName);
259             if (formatHandler != null) {
260                 if(this.fieldInteractionSpec != null)
261                     formatHandler.setInteractionSpec(this.fieldInteractionSpec);
262                 Object JavaDoc retPart = formatHandler.getObjectPart();
263                 this.setObjectPart(partName, retPart);
264                 Trc.exit(retPart);
265                 return retPart;
266             }
267             else {
268                 if(fieldMessageModel == null)
269                     return null;
270                 Part part = (Part) this.fieldMessageModel.getPart(partName);
271                 if (part == null) return null;
272                 formatHandler = (WSIFFormatHandler_JCA)WSIFUtils_JCA.getFormatHandler(part, this.fieldDefinition, this.fieldBinding);
273                 if(this.fieldInteractionSpec != null)
274                     formatHandler.setInteractionSpec(this.fieldInteractionSpec);
275                 Object JavaDoc retPart = formatHandler.getObjectPart();
276                 this.setObjectPart(partName, retPart);
277                 Trc.exit(retPart);
278                 return retPart;
279             }
280
281         }
282         catch (Exception JavaDoc exn) {
283             Trc.exception(exn);
284             throw new RuntimeException JavaDoc(WSIFResource_JCA.get("WSIF1007E", exn.getLocalizedMessage()));
285         }
286     }
287
288     /**
289      * Returns object part with the given name and requested representation. If the part had already
290      * been created, it is returned. If there is a format handler for this part, it gets the object part from the
291      * format handler and returns it, otherwise the format handler is created and its
292      * object part is returned.
293      *
294      * @see org.apache.wsif.WSIFMessage#getObjectPart(String)
295      */

296     public Object JavaDoc getObjectPart(String JavaDoc partName, Class JavaDoc sourceClass) {
297
298         Trc.entry(this, partName, sourceClass);
299             
300         try {
301             if (this.parts != null) {
302                 Object JavaDoc existingPart = this.parts.get(partName);
303                 if (existingPart != null) {
304                     if (sourceClass.isAssignableFrom(existingPart.getClass())){
305                         Trc.exit(existingPart);
306                         return existingPart;
307                     }
308                 }
309             }
310         }
311         catch (Exception JavaDoc exn) {
312             Trc.exception(exn);
313         }
314
315         try {
316             WSIFFormatHandler_JCA formatHandler = (WSIFFormatHandler_JCA) this.fieldPartNameFormatHandlerMapping.get(partName);
317             if (formatHandler != null) {
318                 if(this.fieldInteractionSpec != null)
319                     formatHandler.setInteractionSpec(this.fieldInteractionSpec);
320                 Object JavaDoc retSource = formatHandler.getObjectPart(sourceClass);
321                 this.setObjectPart(partName, retSource);
322                 Trc.exit(retSource);
323                 return retSource;
324             }
325             else {
326                 if(fieldMessageModel == null)
327                     return null;
328                 Part part = (Part) this.fieldMessageModel.getPart(partName);
329                 if (part == null) return null;
330                 formatHandler = (WSIFFormatHandler_JCA)WSIFUtils_JCA.getFormatHandler(part, this.fieldDefinition, this.fieldBinding);
331                 if(this.fieldInteractionSpec != null)
332                     formatHandler.setInteractionSpec(this.fieldInteractionSpec);
333                 Object JavaDoc retSource = formatHandler.getObjectPart(sourceClass);
334                 this.setObjectPart(partName, retSource);
335                 Trc.exit(retSource);
336                 return retSource;
337             }
338         }
339         catch (Exception JavaDoc exn) {
340             Trc.exception(exn);
341             throw new RuntimeException JavaDoc(WSIFResource_JCA.get("WSIF1007E", exn.getLocalizedMessage()));
342         }
343     }
344
345     /**
346      * @see org.apache.wsif.WSIFMessage#getPartNames()
347      */

348     public Iterator getPartNames() {
349         
350         try{
351             if(this.fieldMessageModel == null)
352                 return null;
353             return this.fieldMessageModel.getParts().keySet().iterator();
354         }
355         catch(Throwable JavaDoc exn){
356             return null;
357         }
358     }
359
360     /**
361      * @see org.apache.wsif.WSIFMessage#getParts()
362      */

363     public Iterator getParts() {
364         
365         try{
366             Iterator partNames = this.getPartNames();
367             while(partNames.hasNext()){
368                 String JavaDoc nextName = (String JavaDoc)partNames.next();
369                 this.getObjectPart(nextName);
370             }
371             return(this.parts.values().iterator());
372         }
373         catch(Throwable JavaDoc exn){
374             return null;
375         }
376     }
377
378
379 }
Popular Tags