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: WsdlInfo.java 11051 2007-12-19 02:23:48Z lzheng $
023: */
024:
025: package com.bostechcorp.cbesb.common.wsdl;
026:
027: import java.io.File;
028: import java.io.IOException;
029: import java.util.ArrayList;
030: import java.util.HashMap;
031: import java.util.Iterator;
032: import java.util.LinkedHashMap;
033: import java.util.List;
034: import java.util.Map;
035: import java.util.Set;
036: import java.util.Vector;
037:
038: import javax.wsdl.Definition;
039: import javax.wsdl.Service;
040: import javax.wsdl.Types;
041: import javax.wsdl.WSDLException;
042: import javax.wsdl.factory.WSDLFactory;
043: import javax.wsdl.xml.WSDLReader;
044:
045: import org.apache.commons.logging.Log;
046: import org.apache.commons.logging.LogFactory;
047:
048: import com.bostechcorp.cbesb.common.util.FileUtil;
049: import com.ibm.wsdl.extensions.schema.SchemaImpl;
050: import com.ibm.wsdl.extensions.schema.SchemaImportImpl;
051: import com.ibm.wsdl.extensions.schema.SchemaReferenceImpl;
052:
053: public class WsdlInfo extends ComponentInfo {
054:
055: protected String wsdlBaseName;
056:
057: protected Definition wsdlDefinition;
058:
059: protected TypesInfo wsdlTypes;
060:
061: /** JWSDL Factory instance */
062: protected WSDLFactory wsdlFactory = null;
063:
064: /** The default SOAP encoding to use. */
065: public final static String DEFAULT_SOAP_ENCODING_STYLE = "http://schemas.xmlsoap.org/soap/encoding/";
066:
067: // List of services contained by the loaded WSDL
068: private LinkedHashMap<String, ServiceInfo> services;
069:
070: protected static transient Log logger = LogFactory
071: .getLog(WsdlInfo.class);
072:
073: private WsdlInfo() {
074: services = new LinkedHashMap<String, ServiceInfo>();
075: try {
076: wsdlFactory = WSDLFactory.newInstance();
077: }
078:
079: catch (Throwable t) {
080: logger.error("Exception in WsdlInfo(): " + t.getMessage());
081: if (logger.isDebugEnabled()) {
082: logger.debug("Exception in WsdlInfo():", t);
083: }
084: }
085: }
086:
087: public static WsdlInfo load(File wsdlFile) throws WSDLException {
088: WsdlInfo wsdlInfo = new WsdlInfo();
089: wsdlInfo.loadInstance(wsdlFile);
090:
091: return wsdlInfo;
092: }
093:
094: private void loadInstance(File wsdlFile) throws WSDLException {
095: setWsdlBaseNameFromFilename(wsdlFile);
096:
097: // Create the WSDL Reader object
098: WSDLReader reader = wsdlFactory.newWSDLReader();
099:
100: // Read the WSDL and get the top-level Definition object
101: wsdlDefinition = reader.readWSDL(null, wsdlFile
102: .getAbsolutePath());
103:
104: // Process all schemas defined in the WSDL types
105: wsdlTypes = TypesInfo.load(this );
106:
107: // Get the services defined in the document
108: Map svcs = wsdlDefinition.getServices();
109:
110: if (svcs != null) {
111: // Create a component for each service defined
112: Iterator svcIter = svcs.values().iterator();
113:
114: for (int i = 0; svcIter.hasNext(); i++) {
115: Service wsdlService = (Service) svcIter.next();
116: ServiceInfo serviceInfo = ServiceInfo.load(this ,
117: wsdlService);
118: services.put(serviceInfo.getName(), serviceInfo);
119: }
120: }
121: }
122:
123: private void setWsdlBaseNameFromFilename(File wsdlFile) {
124: // Remove the file extension from the wsdl if it exists,
125: // and use that as the base name for the schemas.
126: String wsdlFileName = wsdlFile.getName();
127: int index = wsdlFileName.lastIndexOf(".");
128: if (index > 0) {
129: wsdlBaseName = wsdlFileName.substring(0, index);
130: } else {
131: wsdlBaseName = wsdlFileName;
132: }
133: }
134:
135: public String[] getServiceNames() {
136: String[] retArray = null;
137: Set serviceNames = services.keySet();
138: retArray = new String[serviceNames.size()];
139: int i = 0;
140: for (Iterator iter = serviceNames.iterator(); iter.hasNext();) {
141: retArray[i] = (String) iter.next();
142: i++;
143: }
144: return retArray;
145: }
146:
147: public List getServices() {
148: return new ArrayList<ServiceInfo>(services.values());
149: }
150:
151: public ServiceInfo getServiceInfo(String serviceName) {
152: ServiceInfo service = null;
153: if (services != null) {
154: service = services.get(serviceName);
155: }
156: return service;
157: }
158:
159: public void saveTypesAsSchemas(File destDir) throws IOException {
160: wsdlTypes.saveSchemas(destDir, wsdlBaseName);
161: }
162:
163: public String getDefaultLocationURI() {
164: String locationURI = null;
165: if (services.size() > 0) {
166: List svcs = getServices();
167: // Get the first Service
168: ServiceInfo svc = (ServiceInfo) svcs.get(0);
169: List ports = svc.getPorts();
170: if (ports.size() > 0) {
171: // Get the first Port
172: PortInfo port = (PortInfo) ports.get(0);
173: locationURI = port.getLocationURI();
174: }
175: }
176:
177: return locationURI;
178: }
179:
180: /**
181: * For debugging
182: */
183: public String toString() {
184: StringBuffer buffer = new StringBuffer();
185: buffer.append(wsdlTypes.toString() + "\n");
186: for (Iterator iter = getServices().iterator(); iter.hasNext();) {
187: ServiceInfo service = (ServiceInfo) iter.next();
188: buffer.append(service.toString() + "\n");
189: }
190: return buffer.toString();
191: }
192:
193: /**
194: * @return the wsdlDefinition
195: */
196: public Definition getWsdlDefinition() {
197: return wsdlDefinition;
198: }
199:
200: /**
201: * exctacts all external resources URI's, resolve the path and return the
202: * Map<Source,Destination>
203: *
204: * Note: this method soes not execute any copy operation
205: * Note: the sourceWsdl itself is not included in returned Map
206: *
207: * @param sourceWsdl -
208: * absolute file to the wsdl file
209: * @param destinationFolder -
210: * folder to which wsdl file will be copyed
211: * @return - Map<Source,Destination>
212: * @throws WSDLException
213: */
214: public static HashMap<String, String> extractFilesForCopy(
215: String sourceWsdl, String destinationFolder)
216: throws WSDLException {
217: HashMap<String, String> map = new HashMap<String, String>();
218:
219: //first the wsdl file
220: // pre preocess paths
221: File sourceWsdlFile = new File(sourceWsdl);
222: String sourceWsdlString = sourceWsdlFile.toURI().getPath()
223: .substring(1);
224: String destinationFolderString = new File(destinationFolder)
225: .toURI().getPath().substring(1);
226: if (!destinationFolderString.endsWith("/"))
227: destinationFolderString += "/";
228: //
229: WsdlInfo wsdl = load(sourceWsdlFile);
230: Types t = wsdl.getWsdlDefinition().getTypes();
231: for (Object extElementObj : t.getExtensibilityElements()) {
232: if (extElementObj instanceof SchemaImpl) {
233: SchemaImpl schema = (SchemaImpl) extElementObj;
234: // parsing imports
235: schema.getImports().values();
236: for (Object importObj : schema.getImports().values()) {
237: if (importObj instanceof Vector) {
238: Vector importVector = (Vector) importObj;
239: for (Object importVectorItem : importVector) {
240: if (importVectorItem instanceof SchemaImportImpl) {
241: SchemaImportImpl sii = (SchemaImportImpl) importVectorItem;
242: if (sii.getSchemaLocationURI() != null) {
243: String schemaSource = FileUtil
244: .processLocationURI(
245: sourceWsdlString,
246: sii
247: .getSchemaLocationURI());
248: String schemaDestination = FileUtil
249: .processLocationURI(
250: destinationFolderString,
251: sii
252: .getSchemaLocationURI());
253: map.put(schemaSource,
254: schemaDestination);
255: }
256: }
257: }
258: }
259: }
260: // parse includes
261: for (Object includeObj : schema.getIncludes()) {
262: if (includeObj instanceof SchemaReferenceImpl) {
263: String schemaSource = FileUtil
264: .processLocationURI(
265: sourceWsdlString,
266: ((SchemaReferenceImpl) includeObj)
267: .getSchemaLocationURI());
268: String schemaDestination = FileUtil
269: .processLocationURI(
270: destinationFolderString,
271: ((SchemaReferenceImpl) includeObj)
272: .getSchemaLocationURI());
273: map.put(schemaSource, schemaDestination);
274: }
275: }
276: }
277: }
278: return map;
279: }
280: }
|