001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /*
019: * ChainBuilder ESB
020: * Visual Enterprise Integration
021: *
022: * Copyright (C) 2006 Bostech Corporation
023: *
024: * This program is free software; you can redistribute it and/or modify it
025: * under the terms of the GNU General Public License as published by the
026: * Free Software Foundation; either version 2 of the License, or (at your option)
027: * any later version.
028: *
029: * This program is distributed in the hope that it will be useful,
030: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
031: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
032: * for more details.
033: *
034: * You should have received a copy of the GNU General Public License along with
035: * this program; if not, write to the Free Software Foundation, Inc.,
036: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
037: *
038: * $Id: Wsdl1Deployer.java 10598 2007-12-06 08:14:39Z lzheng $
039: */
040: package com.bostechcorp.cbesb.runtime.ccsl.jbi.messaging;
041:
042: import java.io.File;
043: import java.io.FilenameFilter;
044: import java.sql.SQLException;
045: import java.util.ArrayList;
046: import java.util.Collection;
047: import java.util.Iterator;
048: import java.util.List;
049: import java.util.Map;
050: import java.util.Vector;
051:
052: import javax.jbi.management.DeploymentException;
053: import javax.wsdl.Binding;
054: import javax.wsdl.Definition;
055: import javax.wsdl.Port;
056: import javax.wsdl.Service;
057: import javax.wsdl.WSDLException;
058: import javax.wsdl.extensions.ExtensibilityElement;
059: import javax.wsdl.extensions.ExtensionRegistry;
060: import javax.wsdl.factory.WSDLFactory;
061: import javax.wsdl.xml.WSDLReader;
062: import javax.xml.parsers.DocumentBuilderFactory;
063:
064: import org.w3c.dom.Document;
065:
066: import com.bostechcorp.cbesb.common.util.runtimedb.DaoConfig;
067: import com.bostechcorp.cbesb.common.util.runtimedb.vo.EndpointSettingVO;
068: import com.ibatis.sqlmap.client.SqlMapClient;
069: import com.ibm.wsdl.Constants;
070:
071: public abstract class Wsdl1Deployer extends CbDeployer {
072:
073: protected FilenameFilter filter;
074:
075: public Wsdl1Deployer(CbComponent component) {
076: super (component);
077: filter = new WsdlFilter();
078: }
079:
080: /* (non-Javadoc)
081: * @see org.apache.servicemix.common.Deployer#canDeploy(java.lang.String, java.lang.String)
082: */
083: public boolean canDeploy(String serviceUnitName,
084: String serviceUnitRootPath) {
085: File[] wsdls = new File(serviceUnitRootPath).listFiles(filter);
086: return wsdls != null && wsdls.length > 0;
087: }
088:
089: /* (non-Javadoc)
090: * @see org.apache.servicemix.common.Deployer#deploy(java.lang.String, java.lang.String)
091: */
092: public CbServiceUnit deploy(String serviceUnitName,
093: String serviceUnitRootPath) throws DeploymentException {
094: File[] wsdls = new File(serviceUnitRootPath).listFiles(filter);
095: if (wsdls == null || wsdls.length == 0) {
096: throw failure("deploy", "No wsdl found", null);
097: }
098: CbServiceUnit su = createServiceUnit();
099: su.setComponent(component);
100: su.setName(serviceUnitName);
101: su.setRootPath(serviceUnitRootPath);
102: loadServiceUnitDescriptor(su, serviceUnitRootPath);
103: for (int i = 0; i < wsdls.length; i++) {
104: initFromWsdl(su, wsdls[i]);
105: }
106: if (su.getEndpoints().size() == 0) {
107: throw failure("deploy", "Invalid wsdl: no endpoints found",
108: null);
109: } else if (su.getComponent() instanceof CbExtendedComponent) {
110: DaoConfig dao = new DaoConfig();
111: SqlMapClient sqlMap = dao.getSqlMapInstance();
112: Collection<CbEndpoint> epVals = su.endpoints.values();
113: for (CbEndpoint ep : epVals) {
114: if (ep instanceof LifeCycleEndpoint) {
115: LifeCycleEndpoint lcep = (LifeCycleEndpoint) ep;
116: String endpointName = lcep.getEndpoint();
117: logger.debug("endpoint Name is: " + endpointName
118: + ".");
119: EndpointSettingVO settingVO = new EndpointSettingVO();
120: settingVO.setEndpointName(endpointName);
121: List<EndpointSettingVO> resultList = new ArrayList<EndpointSettingVO>();
122: try {
123: resultList = sqlMap.queryForList(
124: "queryEndpointSettingByEndpointName",
125: settingVO);
126: } catch (SQLException e) {
127: logger.error("SQLException occured: "
128: + e.getMessage());
129: }
130: String[][] propertyObjects = new String[resultList
131: .size()][2];
132: for (int i = 0; i < resultList.size(); i++) {
133: EndpointSettingVO endpointSettingVO = (EndpointSettingVO) resultList
134: .get(i);
135: propertyObjects[i][0] = endpointSettingVO
136: .getPropertyName();
137: propertyObjects[i][1] = endpointSettingVO
138: .getPropertyValue();
139: logger.info("Get endpoint["
140: + propertyObjects[i][0] + "] : value["
141: + propertyObjects[i][1]
142: + "] from EndpointSetting Database.");
143: }
144: for (int i = 0; i < propertyObjects.length; i++) {
145: String[] object = propertyObjects[i];
146: lcep.setProperty(object[0], object[1]);
147: }
148: }
149: }
150: }
151: return su;
152: }
153:
154: protected void initFromWsdl(CbServiceUnit su, File wsdl)
155: throws DeploymentException {
156: Document description;
157: Definition definition;
158: Vector<ExtensibilityElement> portElements = new Vector<ExtensibilityElement>();
159: Vector<ExtensibilityElement> bindingElements = new Vector<ExtensibilityElement>();
160:
161: try {
162: DocumentBuilderFactory factory = DocumentBuilderFactory
163: .newInstance();
164: factory.setNamespaceAware(true);
165: description = factory.newDocumentBuilder().parse(wsdl);
166: definition = createWsdlReader().readWSDL(null, description);
167: } catch (Exception e) {
168: throw failure("deploy", "Could not parse " + wsdl, e);
169: }
170: Map services = definition.getServices();
171:
172: if (services.size() == 0) {
173: failure("deploy", "Invalid wsdl " + wsdl
174: + ": no defined services", null);
175: }
176: for (Iterator itSvc = services.values().iterator(); itSvc
177: .hasNext();) {
178: Service svc = (Service) itSvc.next();
179: for (Iterator itPorts = svc.getPorts().values().iterator(); itPorts
180: .hasNext();) {
181: Port port = (Port) itPorts.next();
182: // ExtensibilityElement portElement = null;
183: for (Iterator itElems = port.getExtensibilityElements()
184: .iterator(); itElems.hasNext();) {
185: ExtensibilityElement elem = (ExtensibilityElement) itElems
186: .next();
187: if (filterPortElement(elem)) {
188: portElements.add(elem);
189: // if (portElement == null) {
190: // portElement = elem;
191: // } else {
192: // throw failure("deploy", "Invalid wsdl " + wsdl + ": more than one port element match", null);
193: // }
194: }
195: }
196: if (portElements.size() > 0) {
197: Binding binding = port.getBinding();
198: // ExtensibilityElement bindingElement = null;
199: for (Iterator itElems = binding
200: .getExtensibilityElements().iterator(); itElems
201: .hasNext();) {
202: ExtensibilityElement elem = (ExtensibilityElement) itElems
203: .next();
204: if (filterBindingElement(elem)) {
205: bindingElements.add(elem);
206: // if (bindingElement == null) {
207: // bindingElement = elem;
208: // } else {
209: // throw failure("deploy", "Invalid wsdl " + wsdl + ": more than one binding element match", null);
210: // }
211: }
212: }
213: if (bindingElements.size() == 0) {
214: throw failure(
215: "deploy",
216: "Invalid wsdl "
217: + wsdl
218: + ": no matching binding element found",
219: null);
220: }
221: ExtensibilityElement[] portElementArray = new ExtensibilityElement[portElements
222: .size()];
223: ExtensibilityElement[] bindingElementArray = new ExtensibilityElement[bindingElements
224: .size()];
225: for (int i = 0; i < portElements.size(); i++) {
226: portElementArray[i] = portElements.elementAt(i);
227: }
228: for (int i = 0; i < bindingElements.size(); i++) {
229: bindingElementArray[i] = bindingElements
230: .elementAt(i);
231: }
232: CbEndpoint ep = createEndpoint(portElementArray,
233: bindingElementArray);
234: if (ep != null) {
235: ep.setServiceUnit(su);
236: ep.setDescription(description);
237: ep.setDefinition(definition);
238: ep.setService(svc.getQName());
239: ep.setEndpoint(port.getName());
240: ep.setInterfaceName(binding.getPortType()
241: .getQName());
242: su.addEndpoint(ep);
243: }
244: }
245: }
246: }
247: }
248:
249: protected WSDLReader createWsdlReader() throws WSDLException {
250: WSDLFactory factory = WSDLFactory.newInstance();
251: ExtensionRegistry registry = factory
252: .newPopulatedExtensionRegistry();
253: registerExtensions(registry);
254: WSDLReader reader = factory.newWSDLReader();
255: reader.setFeature(Constants.FEATURE_VERBOSE, false);
256: reader.setExtensionRegistry(registry);
257: return reader;
258: }
259:
260: protected void registerExtensions(ExtensionRegistry registry) {
261: // nothing to do in the base class. sub-class will do it.
262: }
263:
264: protected void loadServiceUnitDescriptor(CbServiceUnit su,
265: String serviceUnitRootPath) {
266: File jbixml = new File(serviceUnitRootPath + File.separator
267: + "META-INF" + File.separator + "jbi.xml");
268: try {
269: ServiceUnitDescriptor suDesc = ServiceUnitDescriptor
270: .load(jbixml);
271: su.serviceUnitDescriptor = suDesc;
272: } catch (Exception e) {
273: logger.error(
274: "Exception caught while loading SU Descriptor:", e);
275: }
276: }
277:
278: protected CbServiceUnit createServiceUnit() {
279: return new CbServiceUnit();
280: }
281:
282: protected abstract CbEndpoint createEndpoint(
283: ExtensibilityElement[] portElement,
284: ExtensibilityElement[] bindingElement);
285:
286: protected abstract boolean filterPortElement(
287: ExtensibilityElement element);
288:
289: protected abstract boolean filterBindingElement(
290: ExtensibilityElement element);
291:
292: public static class WsdlFilter implements FilenameFilter {
293:
294: public boolean accept(File dir, String name) {
295: return name.endsWith(".wsdl");
296: }
297:
298: }
299:
300: }
|