001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)WrapperBuilderImpl.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.wsdl11wrapper.impl;
030:
031: import com.sun.jbi.wsdl11wrapper.HelperFactory;
032: import com.sun.jbi.wsdl11wrapper.WrapperBuilder;
033: import com.sun.jbi.wsdl11wrapper.WrapperProcessingException;
034: import com.sun.jbi.wsdl11wrapper.util.WrapperUtil;
035:
036: import java.util.Map;
037: import java.util.HashMap;
038: import java.util.List;
039: import java.util.Iterator;
040: import javax.wsdl.Definition;
041: import javax.wsdl.Message;
042: import javax.wsdl.Part;
043: import javax.wsdl.WSDLException;
044: import javax.wsdl.factory.WSDLFactory;
045: import javax.xml.namespace.QName;
046: import org.w3c.dom.Element;
047: import org.w3c.dom.Node;
048: import org.w3c.dom.NodeList;
049: import org.w3c.dom.Document;
050:
051: import javax.xml.parsers.DocumentBuilder;
052: import javax.xml.parsers.DocumentBuilderFactory;
053: import javax.xml.parsers.ParserConfigurationException;
054:
055: /**
056: * Assists in building the WSDL 1.1 wrapper for normalized messages specified by JBI
057: *
058: * Usage sequence:
059: * initialize()
060: * addParts() or multiple addPart() calls
061: * getResult()
062: *
063: * The same instance should not be used by multiple threads concurrently as it is
064: * not guaranteed to be thread safe - and maintains state
065: *
066: * The re-use of the same instance however is encouraged.
067: *
068: * @author aegloff
069: */
070: public class WrapperBuilderImpl implements WrapperBuilder {
071:
072: DocumentBuilder mBuilder;
073: Document normalDoc;
074: Element jbiMessageWrapper;
075: Message messageDefinition;
076: WSDLInfo info;
077: boolean isResultPrepared;
078: Map mPartNameToPartNodes;
079:
080: /**
081: * Creates a new instance
082: *
083: * The preferred way to create and use this builder implementation is to use
084: * the <code>HelperFactory</code> instead.
085: */
086: public WrapperBuilderImpl() throws WrapperProcessingException {
087: }
088:
089: /**
090: * Re-sets the result document, sets the WSDL message definition of the message to normalize
091: */
092: public void initialize(Document docToPopulate,
093: Message wsdlMessageDefinition,
094: String operationBindingMessageName)
095: throws WrapperProcessingException {
096: isResultPrepared = false;
097: mPartNameToPartNodes = null;
098: this .normalDoc = docToPopulate;
099: this .messageDefinition = wsdlMessageDefinition;
100: if (docToPopulate == null) {
101: try {
102: normalDoc = newDocument();
103: } catch (ParserConfigurationException ex) {
104: throw new WrapperProcessingException(
105: "Failed to create an empty target document for building the wrapped normalized message: "
106: + ex.getMessage(), ex);
107: }
108: }
109:
110: info = WSDLInfo.getInstance(wsdlMessageDefinition);
111:
112: // Add a message wrapper
113: jbiMessageWrapper = WrapperUtil.createJBIMessageWrapper(
114: normalDoc, info.getMessageType(),
115: operationBindingMessageName);
116: normalDoc.appendChild(jbiMessageWrapper);
117: }
118:
119: /**
120: * Add a part in the right position (wrapped in a JBI part wrapper) to the JBI message wrapper element
121: * @param partName the name of the message part
122: * @param partNode the part node (payload)
123: * The part node does not have to be associated with the normalDoc yet, it will be imported
124: */
125: public void addPart(String partName, org.w3c.dom.NodeList partNodes)
126: throws WrapperProcessingException {
127: List partsOrder = info.getPartsOrder();
128: int partPos = partsOrder.indexOf(partName);
129: if (partPos > -1) {
130: if (mPartNameToPartNodes == null) {
131: mPartNameToPartNodes = new HashMap();
132: }
133: mPartNameToPartNodes.put(partName, partNodes);
134: } else {
135: throw new WrapperProcessingException(
136: "Unknown part "
137: + partName
138: + " is not defined in the WSDL message definition for "
139: + info.getMessageType()
140: + ". Cannot add part to normalized message.");
141: }
142: }
143:
144: /**
145: * Add a part in the right position (wrapped in a JBI part wrapper) to the JBI message wrapper element
146: * @param partName the name of the message part
147: * @param partNode the part node (payload)
148: * The part node does not have to be associated with the normalDoc yet, it will be imported
149: */
150: public void addPart(String partName, Element partNode)
151: throws WrapperProcessingException {
152: addPart(partName, new NodeListImpl(partNode));
153: /*
154: List partsOrder = info.getPartsOrder();
155: int partPos = partsOrder.indexOf(partName);
156: if (partPos > -1) {
157: if (mPartNameToPartNodes == null) {
158: mPartNameToPartNodes = new HashMap();
159: }
160: mPartNameToPartNodes.put(partName, partNode);
161: } else {
162: throw new WrapperProcessingException("Unknown part " + partName + " is not defined in the WSDL message definition for " + info.getMessageType() + ". Cannot add part to normalized message.");
163: }
164: */
165: }
166:
167: /**
168: * Add parts in the right order (each wrapped in a JBI part wrapper) to the passed in JBI message wrapper element
169: * The jbiMessageWrapper must be a node of the normalDoc.
170: * @param normalDoc The target document of the normalization
171: * @param jbiMessageWrapper The message wrapper element to add the jbi part wrappers and part payload to
172: * @param messageDefinintion the WSDL message definition
173: * @param partNameToPartNodes a mapping from the part name to the part NodeList (payload).
174: * The part node does not have to be associated with the normalDoc yet, it will be imported
175: */
176: public void addParts(Map partNameToPartNodes) {
177: mPartNameToPartNodes = partNameToPartNodes;
178: }
179:
180: /**
181: * Retrieve the Normalized Message with the parts added (addPart(s)) since the initialize() call
182: * Make sure to only retrieve the result after all desired parts have been added.
183: *
184: * Parts added after the result of a build squence has been retrieved already
185: * (meaning without starting a new build squence by calling initialize) will NOT be included
186: * in subsequent retrievals of Result
187: */
188: public Document getResult() throws WrapperProcessingException {
189: if (!isResultPrepared) {
190: Iterator partsIter = info.getOrderedMessageParts()
191: .iterator();
192: while (partsIter.hasNext()) {
193: Part aPart = (Part) partsIter.next();
194: String partName = aPart.getName();
195: NodeList nodes = null;
196: if (mPartNameToPartNodes != null) {
197: Object payload = mPartNameToPartNodes.get(partName);
198: if (payload instanceof NodeList) {
199: nodes = (NodeList) payload;
200: } else {
201: throw new WrapperProcessingException(
202: "Part payload added is not a NodeList: "
203: + (payload == null ? "payload is null"
204: : payload.getClass()
205: .getName()));
206: }
207: }
208: Element wrapperElem = WrapperUtil.importJBIWrappedPart(
209: normalDoc, nodes);
210: jbiMessageWrapper.appendChild(wrapperElem);
211: }
212: isResultPrepared = true;
213: }
214: return normalDoc;
215: }
216:
217: public Message getStatusMessage() throws WrapperProcessingException {
218: Message msg = null;
219:
220: try {
221: WSDLFactory factory = javax.wsdl.factory.WSDLFactory
222: .newInstance();
223: Definition def = factory.newDefinition();
224: msg = def.createMessage();
225: msg.setQName(new QName(WrapperBuilder.STATUS_TAG));
226: Part part = def.createPart();
227: part.setName(WrapperBuilder.RESULT_TAG);
228: msg.addPart(part);
229: } catch (WSDLException ex) {
230: throw new WrapperProcessingException(ex);
231: }
232:
233: return msg;
234: }
235:
236: public Document getStatusDocument(String statusMessage)
237: throws WrapperProcessingException {
238: Document res = null;
239: try {
240: Document doc = DocumentBuilderFactory.newInstance()
241: .newDocumentBuilder().newDocument();
242:
243: WrapperBuilder builder = HelperFactory.createBuilder();
244: builder.initialize(doc, getStatusMessage(), null);
245: // add the "wrapper" elements for "normalizing" doc/lit
246: Element statusElem = doc
247: .createElement(WrapperBuilder.STATUS_TAG);
248: statusElem.appendChild(doc.createTextNode(statusMessage));
249: builder.addPart(WrapperBuilder.RESULT_TAG, statusElem);
250: res = builder.getResult();
251: } catch (ParserConfigurationException ex) {
252: throw new WrapperProcessingException(ex);
253: }
254:
255: return res;
256: }
257:
258: private Document newDocument() throws ParserConfigurationException {
259: if (mBuilder == null) {
260: DocumentBuilderFactory factory = DocumentBuilderFactory
261: .newInstance();
262: mBuilder = factory.newDocumentBuilder();
263: }
264: return mBuilder.newDocument();
265: }
266:
267: }
|