001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2007 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id: ServiceDescriptionHandler.java 9243 2007-09-25 00:49:54Z lzheng $
023: */
024: package com.bostechcorp.cbesb.runtime.ccsl.jbi.messaging;
025:
026: import java.util.Iterator;
027: import java.util.List;
028: import java.util.Map;
029: import java.util.Map.Entry;
030:
031: import javax.jbi.JBIException;
032: import javax.jbi.component.ComponentContext;
033: import javax.jbi.servicedesc.ServiceEndpoint;
034: import javax.wsdl.Definition;
035: import javax.wsdl.Input;
036: import javax.wsdl.Operation;
037: import javax.wsdl.Output;
038: import javax.wsdl.PortType;
039: import javax.wsdl.factory.WSDLFactory;
040: import javax.wsdl.xml.WSDLReader;
041: import javax.xml.namespace.QName;
042:
043: import org.apache.commons.logging.Log;
044: import org.apache.commons.logging.LogFactory;
045: import org.w3c.dom.Document;
046:
047: public class ServiceDescriptionHandler {
048:
049: public static final String WSDL_11_NAMESPACE = "http://schemas.xmlsoap.org/wsdl/";
050: public static final String WSDL_20_NAMESPACE = "http://www.w3.org/ns/wsdl";
051:
052: private static Log log = LogFactory
053: .getLog(ServiceDescriptionHandler.class);
054:
055: private ServiceInfo svcInfo = null;
056: private Document svcDescDoc = null;
057: private boolean wsdl11;
058: private PortType portType;
059:
060: protected ServiceDescriptionHandler() {
061:
062: }
063:
064: /**
065: * @return the wsdl11
066: */
067: public boolean isWsdl11() {
068: return wsdl11;
069: }
070:
071: /**
072: * @param wsdl11 the wsdl11 to set
073: */
074: public void setWsdl11(boolean wsdl11) {
075: this .wsdl11 = wsdl11;
076: }
077:
078: public List<Operation> getOperations() {
079: if (wsdl11 && portType != null) {
080: return portType.getOperations();
081: }
082: return null;
083: }
084:
085: public Operation getFirstOperation() {
086: if (wsdl11 && portType != null) {
087: if (portType.getOperations().size() > 0) {
088: return (Operation) portType.getOperations().get(0);
089: }
090: }
091: return null;
092: }
093:
094: public QName getInputMessageQName(Operation operation) {
095: if (wsdl11) {
096: Input input = operation.getInput();
097: if (input != null) {
098: return input.getMessage().getQName();
099: }
100: }
101: return null;
102: }
103:
104: public String getInputName(Operation operation) {
105: if (wsdl11) {
106: Input input = operation.getInput();
107: if (input != null) {
108: return input.getName();
109: }
110: }
111: return null;
112: }
113:
114: public QName getOutputMessageQName(Operation operation) {
115: if (wsdl11) {
116: Output output = operation.getOutput();
117: if (output != null) {
118: return output.getMessage().getQName();
119: }
120: }
121: return null;
122: }
123:
124: public String getOutputName(Operation operation) {
125: if (wsdl11) {
126: Output output = operation.getOutput();
127: if (output != null) {
128: return output.getName();
129: }
130: }
131: return null;
132: }
133:
134: public static ServiceDescriptionHandler getInstance(Document wsdlDoc) {
135: ServiceDescriptionHandler instance = new ServiceDescriptionHandler();
136: instance.svcInfo = null;
137: instance.initFromDoc(wsdlDoc);
138: return instance;
139: }
140:
141: public static ServiceDescriptionHandler getInstance(
142: ServiceInfo serviceInfo, ComponentContext context) {
143: if (serviceInfo != null && serviceInfo.getServiceName() != null
144: && serviceInfo.getEndpointName() != null) {
145: ServiceEndpoint ep = context.getEndpoint(serviceInfo
146: .getServiceName(), serviceInfo.getEndpointName());
147: try {
148: Document svcDesc = context.getEndpointDescriptor(ep);
149: if (svcDesc != null) {
150: log
151: .debug("getEndpointDescriptor returnded WSDL, trying to process...");
152: ServiceDescriptionHandler instance = new ServiceDescriptionHandler();
153: instance.svcInfo = serviceInfo;
154: instance.initFromDoc(svcDesc);
155: return instance;
156: } else {
157: log.debug("getEndpointDescriptor returned null.");
158: }
159: } catch (JBIException e) {
160: log
161: .error(
162: "Caught Exception retrieving service description",
163: e);
164: }
165: } else {
166: log
167: .info("Unable to retrieve Service Description, ServiceInfo is null.");
168: }
169: return null;
170: }
171:
172: private void initFromDoc(Document wsdlDoc) {
173:
174: svcDescDoc = wsdlDoc;
175:
176: String namespace = wsdlDoc.getDocumentElement()
177: .getNamespaceURI();
178: if (WSDL_11_NAMESPACE.equals(namespace)) {
179: wsdl11 = true;
180: try {
181: log.debug("processing WSDL 1.1 document...");
182: processWsdl11Doc(wsdlDoc);
183: } catch (Exception e) {
184: log
185: .error(
186: "Exception while processing Service Description",
187: e);
188: }
189: } else if (WSDL_20_NAMESPACE.equals(namespace)) {
190: wsdl11 = false;
191:
192: } else {
193: log
194: .error("Unable to process Service Description document. Returned doc is not WSDL 1.1 or 2.0");
195: }
196:
197: }
198:
199: private void processWsdl11Doc(Document wsdlDoc) throws Exception {
200: WSDLFactory wsdlFactory = WSDLFactory.newInstance();
201: // Create the WSDL Reader object
202: WSDLReader reader = wsdlFactory.newWSDLReader();
203: // Read the WSDL and get the top-level Definition object
204: Definition wsdlDefinition = reader.readWSDL(null, wsdlDoc);
205: if (svcInfo != null) {
206: // Get the PortType specified by the ServiceInfo setting
207: portType = wsdlDefinition.getPortType(svcInfo
208: .getInterfaceName());
209: } else {
210: // Get the first PortType
211: Map portTypes = wsdlDefinition.getPortTypes();
212: Iterator iter = portTypes.entrySet().iterator();
213: if (iter.hasNext()) {
214: Entry entry = (Entry) iter.next();
215: portType = (PortType) entry.getValue();
216: }
217:
218: }
219: if (portType == null) {
220: log.error("WSDL does not have a portType defined.");
221: //Set to WSDL 2.0 so errors don't occur trying to use wrapper.
222: wsdl11 = false;
223: }
224: if (getFirstOperation() == null) {
225: log
226: .error("WSDL does not define an operation for portType: "
227: + portType.getQName());
228: wsdl11 = false;
229: }
230: }
231:
232: }
|