001: /*
002: * The Apache Software License, Version 1.1
003: *
004: *
005: * Copyright (c) 2002 The Apache Software Foundation. All rights
006: * reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Apache Software Foundation (http://www.apache.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "WSIF" and "Apache Software Foundation" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation and was
052: * originally based on software copyright (c) 2001, 2002, International
053: * Business Machines, Inc., http://www.apache.org. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package org.apache.wsif.providers.jca;
059:
060: import org.apache.wsif.*;
061: import org.apache.wsif.format.*;
062: import org.apache.wsif.providers.jca.WSIFUtils_JCA;
063: import org.apache.wsif.logging.*;
064: import java.net.MalformedURLException;
065: import java.net.URL;
066: import java.util.*;
067: import javax.wsdl.*;
068: import javax.wsdl.extensions.ExtensibilityElement;
069:
070: /**
071: * The class WSIFMessage_JCAStreamable is a specialized version of the WSIFMessage_JCA to support Resource Adapters
072: * using javax.resource.cci.Streamable.
073: *
074: * @author Michael Beisiegel
075: * @author Piotr Przybylski <piotrp@ca.ibm.com>
076: * @author John Green
077: */
078:
079: public class WSIFMessage_JCAStreamable extends
080: org.apache.wsif.providers.jca.WSIFMessage_JCA implements
081: javax.resource.cci.Streamable {
082:
083: private static final long serialVersionUID = 1L;
084: private Message fieldMessageModel = null;
085: private java.util.HashMap fieldPartNameFormatHandlerMapping = new java.util.HashMap();
086:
087: /**
088: * @see org.apache.wsif.providers.jca.WSIFMessage_JCA#WSIFMessage_JCA(Definition, Binding, String, String, String, int)
089: */
090: public WSIFMessage_JCAStreamable(Definition aDefinition,
091: Binding aBinding, String aOperationName, String aInputName,
092: String aOutputName, int aMessageType) {
093: super (aDefinition, aBinding, aOperationName, aInputName,
094: aOutputName, aMessageType);
095: Operation operation = aBinding.getPortType().getOperation(
096: aOperationName, aInputName, aOutputName);
097: switch (aMessageType) {
098: case WSIFMessage_JCA.INPUT_MESSAGE:
099: if (operation.getInput() != null) {
100: this .fieldMessageModel = operation.getInput()
101: .getMessage();
102: setMessageDefinition(this .fieldMessageModel);
103: }
104: break;
105: case WSIFMessage_JCA.OUTPUT_MESSAGE:
106: if (operation.getOutput() != null) {
107: this .fieldMessageModel = operation.getOutput()
108: .getMessage();
109: setMessageDefinition(this .fieldMessageModel);
110: }
111: break;
112: case WSIFMessage_JCA.FAULT_MESSAGE:
113: break;
114: default:
115: // Assume input
116: this .fieldMessageModel = operation.getInput().getMessage();
117: setMessageDefinition(this .fieldMessageModel);
118: break;
119: }
120: }
121:
122: /**
123: * The method to read input stream and create message parts. For each part in the message
124: * (as defined in WSDL), with exception of parts representing ConnectionSpec and InteractionSpec properties,
125: * the format handler is created and its read() method is passed the inputStream. The parts
126: * creation is delayed until they are needed (i.e. when the client invokes <code>getObjectPart</code>).
127: * In this method only the part's format handler is created and stored.
128: *
129: * @see javax.resource.cci.Streamable#read(InputStream)
130: */
131: public void read(java.io.InputStream inputStream)
132: throws java.io.IOException {
133:
134: try {
135: Trc.entry(this );
136: if (fieldMessageModel == null)
137: return;
138: HashMap partsToNotProcess = new HashMap();
139: BindingOperation bindingOperation = fieldBinding
140: .getBindingOperation(fieldOperationName,
141: fieldInputName, fieldOutputName);
142: BindingOutput bindingOutput = bindingOperation
143: .getBindingOutput();
144: if (bindingOutput != null) {
145: List list = bindingOutput.getExtensibilityElements();
146: Iterator inputIterator = list.iterator();
147: while (inputIterator.hasNext()) {
148: ExtensibilityElement ele = (ExtensibilityElement) inputIterator
149: .next();
150: if (ele instanceof WSIFBindingOperation_JCAProperty) {
151: WSIFBindingOperation_JCAProperty prop = (WSIFBindingOperation_JCAProperty) ele;
152: String partName = prop.getPartName();
153: partsToNotProcess.put(partName, partName);
154: }
155: }
156: }
157: Iterator iterator = this .fieldMessageModel.getOrderedParts(
158: null).iterator();
159: while (iterator.hasNext()) {
160: Part part = (Part) iterator.next();
161: String partName = part.getName();
162:
163: if (partsToNotProcess.get(partName) != null)
164: continue;
165:
166: WSIFFormatHandler_JCA formatHandler = null;
167: if (this .fieldPartNameFormatHandlerMapping
168: .containsKey(partName))
169: formatHandler = (WSIFFormatHandler_JCA) this .fieldPartNameFormatHandlerMapping
170: .get(partName);
171: else {
172: formatHandler = (WSIFFormatHandler_JCA) WSIFUtils_JCA
173: .getFormatHandler(part,
174: this .fieldDefinition,
175: this .fieldBinding);
176: this .fieldPartNameFormatHandlerMapping.put(
177: partName, formatHandler);
178: }
179: formatHandler.read(inputStream);
180: }
181: Trc.exit();
182: } catch (Exception exn1) {
183: Trc.exception(exn1);
184: throw new java.io.IOException(WSIFResource_JCA.get(
185: "WSIF1004E", exn1.getLocalizedMessage()));
186: }
187: }
188:
189: /**
190: * Writes the contents of the message parts into the OutputStream. For each part in the message
191: * (as defined in WSDL), except parts representing interactionSpec properties, the format handler
192: * is created, part is set on the format handler and its <code>write</code> method is invoked.
193: * The format handlers are stored in the table.
194: *
195: * @see javax.resource.cci.Streamable#write(OutputStream)
196: */
197: public void write(java.io.OutputStream outputStream)
198: throws java.io.IOException {
199:
200: try {
201: Trc.entry(this );
202:
203: HashMap partsToNotProcess = new HashMap();
204: BindingOperation bindingOperation = fieldBinding
205: .getBindingOperation(fieldOperationName,
206: fieldInputName, fieldOutputName);
207: BindingInput bindingInput = bindingOperation
208: .getBindingInput();
209: if (bindingInput != null) {
210: List list = bindingInput.getExtensibilityElements();
211: Iterator inputIterator = list.iterator();
212: while (inputIterator.hasNext()) {
213: ExtensibilityElement ele = (ExtensibilityElement) inputIterator
214: .next();
215: if (ele instanceof WSIFBindingOperation_JCAProperty) {
216: WSIFBindingOperation_JCAProperty prop = (WSIFBindingOperation_JCAProperty) ele;
217: String partName = prop.getPartName();
218: partsToNotProcess.put(partName, partName);
219: }
220: }
221: }
222:
223: Iterator iterator = this .getPartNames();
224: while (iterator.hasNext()) {
225: String partName = (String) iterator.next();
226: if (partsToNotProcess.get(partName) != null)
227: continue;
228: Object oPart = this .parts.get(partName);
229: WSIFFormatHandler_JCA formatHandler = null;
230: if (oPart instanceof WSIFFormatPart) {
231: WSIFFormatPart jcaPart = (WSIFFormatPart) oPart;
232: if (jcaPart._getFormatHandler() != null) {
233: formatHandler = (WSIFFormatHandler_JCA) jcaPart
234: ._getFormatHandler();
235: this .fieldPartNameFormatHandlerMapping.put(
236: partName, formatHandler);
237: }
238: }
239: if (formatHandler == null) {
240: if (this .fieldPartNameFormatHandlerMapping
241: .containsKey(partName))
242: formatHandler = (WSIFFormatHandler_JCA) this .fieldPartNameFormatHandlerMapping
243: .get(partName);
244: else {
245: if (fieldMessageModel == null)
246: return;
247: Part part = (Part) this .fieldMessageModel
248: .getPart(partName);
249: formatHandler = (WSIFFormatHandler_JCA) WSIFUtils_JCA
250: .getFormatHandler(part,
251: this .fieldDefinition,
252: this .fieldBinding);
253: this .fieldPartNameFormatHandlerMapping.put(
254: partName, formatHandler);
255: }
256: formatHandler.setObjectPart(oPart);
257: }
258: formatHandler.write(outputStream);
259: }
260: Trc.exit();
261: } catch (Exception exn1) {
262: Trc.exception(exn1);
263: throw new java.io.IOException(WSIFResource_JCA.get(
264: "WSIF1005E", exn1.getLocalizedMessage()));
265:
266: }
267:
268: }
269:
270: /**
271: * Returns object part with the given name. If the part had already been created,
272: * returns it. If there is a format handler for this part, the object part is obtained from
273: * it and returned, otherwise it creates the format handler and returns
274: * the object parts form it.
275: *
276: * @see org.apache.wsif.WSIFMessage#getObjectPart(String)
277: */
278: public Object getObjectPart(String partName) {
279:
280: Trc.entry(this , partName);
281:
282: if (this .parts != null) {
283: Object existingPart = this .parts.get(partName);
284: if (existingPart != null)
285: return existingPart;
286: }
287: try {
288: WSIFFormatHandler_JCA formatHandler = (WSIFFormatHandler_JCA) this .fieldPartNameFormatHandlerMapping
289: .get(partName);
290: if (formatHandler != null) {
291: if (this .fieldInteractionSpec != null)
292: formatHandler
293: .setInteractionSpec(this .fieldInteractionSpec);
294: Object retPart = formatHandler.getObjectPart();
295: this .setObjectPart(partName, retPart);
296: Trc.exit(retPart);
297: return retPart;
298: } else {
299: if (fieldMessageModel == null)
300: return null;
301: Part part = (Part) this .fieldMessageModel
302: .getPart(partName);
303: if (part == null)
304: return null;
305: formatHandler = (WSIFFormatHandler_JCA) WSIFUtils_JCA
306: .getFormatHandler(part, this .fieldDefinition,
307: this .fieldBinding);
308: if (this .fieldInteractionSpec != null)
309: formatHandler
310: .setInteractionSpec(this .fieldInteractionSpec);
311: Object retPart = formatHandler.getObjectPart();
312: this .setObjectPart(partName, retPart);
313: Trc.exit(retPart);
314: return retPart;
315: }
316:
317: } catch (Exception exn) {
318: Trc.exception(exn);
319: throw new RuntimeException(WSIFResource_JCA.get(
320: "WSIF1007E", exn.getLocalizedMessage()));
321: }
322: }
323:
324: /**
325: * Returns object part with the given name and requested representation. If the part had already
326: * been created, it is returned. If there is a format handler for this part, it gets the object part from the
327: * format handler and returns it, otherwise the format handler is created and its
328: * object part is returned.
329: *
330: * @see org.apache.wsif.WSIFMessage#getObjectPart(String)
331: */
332: public Object getObjectPart(String partName, Class sourceClass) {
333:
334: Trc.entry(this , partName, sourceClass);
335:
336: try {
337: if (this .parts != null) {
338: Object existingPart = this .parts.get(partName);
339: if (existingPart != null) {
340: if (sourceClass.isAssignableFrom(existingPart
341: .getClass())) {
342: Trc.exit(existingPart);
343: return existingPart;
344: }
345: }
346: }
347: } catch (Exception exn) {
348: Trc.exception(exn);
349: }
350:
351: try {
352: WSIFFormatHandler_JCA formatHandler = (WSIFFormatHandler_JCA) this .fieldPartNameFormatHandlerMapping
353: .get(partName);
354: if (formatHandler != null) {
355: if (this .fieldInteractionSpec != null)
356: formatHandler
357: .setInteractionSpec(this .fieldInteractionSpec);
358: Object retSource = formatHandler
359: .getObjectPart(sourceClass);
360: this .setObjectPart(partName, retSource);
361: Trc.exit(retSource);
362: return retSource;
363: } else {
364: if (fieldMessageModel == null)
365: return null;
366: Part part = (Part) this .fieldMessageModel
367: .getPart(partName);
368: if (part == null)
369: return null;
370: formatHandler = (WSIFFormatHandler_JCA) WSIFUtils_JCA
371: .getFormatHandler(part, this .fieldDefinition,
372: this .fieldBinding);
373: if (this .fieldInteractionSpec != null)
374: formatHandler
375: .setInteractionSpec(this .fieldInteractionSpec);
376: Object retSource = formatHandler
377: .getObjectPart(sourceClass);
378: this .setObjectPart(partName, retSource);
379: Trc.exit(retSource);
380: return retSource;
381: }
382: } catch (Exception exn) {
383: Trc.exception(exn);
384: throw new RuntimeException(WSIFResource_JCA.get(
385: "WSIF1007E", exn.getLocalizedMessage()));
386: }
387: }
388:
389: /**
390: * @see org.apache.wsif.WSIFMessage#getPartNames()
391: */
392: public Iterator getPartNames() {
393:
394: try {
395: if (this .fieldMessageModel == null)
396: return null;
397: return this .fieldMessageModel.getParts().keySet()
398: .iterator();
399: } catch (Throwable exn) {
400: return null;
401: }
402: }
403:
404: /**
405: * @see org.apache.wsif.WSIFMessage#getParts()
406: */
407: public Iterator getParts() {
408:
409: try {
410: Iterator partNames = this .getPartNames();
411: while (partNames.hasNext()) {
412: String nextName = (String) partNames.next();
413: this .getObjectPart(nextName);
414: }
415: return (this .parts.values().iterator());
416: } catch (Throwable exn) {
417: return null;
418: }
419: }
420:
421: }
|