1 17 package org.apache.servicemix.common.wsdl1; 18 19 import org.apache.servicemix.common.AbstractDeployer; 20 import org.apache.servicemix.common.BaseComponent; 21 import org.apache.servicemix.common.Endpoint; 22 import org.apache.servicemix.common.ServiceUnit; 23 import org.w3c.dom.Document ; 24 25 import com.ibm.wsdl.Constants; 26 27 import javax.jbi.management.DeploymentException; 28 import javax.wsdl.Binding; 29 import javax.wsdl.Definition; 30 import javax.wsdl.Port; 31 import javax.wsdl.Service; 32 import javax.wsdl.WSDLException; 33 import javax.wsdl.extensions.ExtensibilityElement; 34 import javax.wsdl.extensions.ExtensionRegistry; 35 import javax.wsdl.factory.WSDLFactory; 36 import javax.wsdl.xml.WSDLReader; 37 import javax.xml.parsers.DocumentBuilderFactory ; 38 39 import java.io.File ; 40 import java.io.FilenameFilter ; 41 import java.util.Iterator ; 42 import java.util.Map ; 43 44 public abstract class AbstractWsdl1Deployer extends AbstractDeployer { 45 46 protected FilenameFilter filter; 47 48 public AbstractWsdl1Deployer(BaseComponent component) { 49 super(component); 50 filter = new WsdlFilter(); 51 } 52 53 56 public boolean canDeploy(String serviceUnitName, 57 String serviceUnitRootPath) { 58 File [] wsdls = new File (serviceUnitRootPath).listFiles(filter); 59 return wsdls != null && wsdls.length > 0; 60 } 61 62 65 public ServiceUnit deploy(String serviceUnitName, 66 String serviceUnitRootPath) throws DeploymentException { 67 File [] wsdls = new File (serviceUnitRootPath).listFiles(filter); 68 if (wsdls == null || wsdls.length == 0) { 69 throw failure("deploy", "No wsdl found", null); 70 } 71 ServiceUnit su = createServiceUnit(); 72 su.setComponent(component); 73 su.setName(serviceUnitName); 74 su.setRootPath(serviceUnitRootPath); 75 for (int i = 0; i < wsdls.length; i++) { 76 initFromWsdl(su, wsdls[i]); 77 } 78 if (su.getEndpoints().size() == 0) { 79 throw failure("deploy", "Invalid wsdl: no endpoints found", null); 80 } 81 return su; 82 } 83 84 protected void initFromWsdl(ServiceUnit su, File wsdl) throws DeploymentException { 85 Document description; 86 Definition definition; 87 try { 88 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 89 factory.setNamespaceAware(true); 90 description = factory.newDocumentBuilder().parse(wsdl); 91 definition = createWsdlReader().readWSDL(null, description); 92 } catch (Exception e) { 93 throw failure("deploy", "Could not parse " + wsdl, e); 94 } 95 Map services = definition.getServices(); 96 if (services.size() == 0) { 97 failure("deploy", "Invalid wsdl " + wsdl + ": no defined services", null); 98 } 99 for (Iterator itSvc = services.values().iterator(); itSvc.hasNext();) { 100 Service svc = (Service) itSvc.next(); 101 for (Iterator itPorts = svc.getPorts().values().iterator(); itPorts.hasNext();) { 102 JbiEndpoint jbiEndpoint = null; 103 Port port = (Port) itPorts.next(); 104 ExtensibilityElement portElement = null; 105 for (Iterator itElems = port.getExtensibilityElements().iterator(); itElems.hasNext();) { 106 ExtensibilityElement elem = (ExtensibilityElement) itElems.next(); 107 if (elem instanceof JbiEndpoint) { 108 jbiEndpoint = (JbiEndpoint) elem; 109 } else if (filterPortElement(elem)) { 110 if (portElement == null) { 111 portElement = elem; 112 } else { 113 throw failure("deploy", "Invalid wsdl " + wsdl + ": more than one port element match", null); 114 } 115 } 116 } 117 if (portElement != null) { 118 Binding binding = port.getBinding(); 119 ExtensibilityElement bindingElement = null; 120 for (Iterator itElems = binding.getExtensibilityElements().iterator(); itElems.hasNext();) { 121 ExtensibilityElement elem = (ExtensibilityElement) itElems.next(); 122 if (filterBindingElement(elem)) { 123 if (bindingElement == null) { 124 bindingElement = elem; 125 } else { 126 throw failure("deploy", "Invalid wsdl " + wsdl + ": more than one binding element match", null); 127 } 128 } 129 } 130 if (bindingElement == null) { 131 throw failure("deploy", "Invalid wsdl " + wsdl + ": no matching binding element found", null); 132 } 133 Endpoint ep = createEndpoint(portElement, bindingElement, jbiEndpoint); 134 if (ep != null) { 135 ep.setServiceUnit(su); 136 ep.setDescription(description); 137 ep.setDefinition(definition); 138 ep.setService(svc.getQName()); 139 ep.setEndpoint(port.getName()); 140 ep.setInterfaceName(binding.getPortType().getQName()); 141 su.addEndpoint(ep); 142 } 143 } 144 } 145 } 146 } 147 148 protected WSDLReader createWsdlReader() throws WSDLException { 149 WSDLFactory factory = WSDLFactory.newInstance(); 150 ExtensionRegistry registry = factory.newPopulatedExtensionRegistry(); 151 registerExtensions(registry); 152 WSDLReader reader = factory.newWSDLReader(); 153 reader.setFeature(Constants.FEATURE_VERBOSE, false); 154 reader.setExtensionRegistry(registry); 155 return reader; 156 } 157 158 protected void registerExtensions(ExtensionRegistry registry) { 159 JbiExtension.register(registry); 160 } 161 162 protected ServiceUnit createServiceUnit() { 163 return new ServiceUnit(); 164 } 165 166 protected abstract Endpoint createEndpoint(ExtensibilityElement portElement, 167 ExtensibilityElement bindingElement, 168 JbiEndpoint jbiEndpoint); 169 170 protected abstract boolean filterPortElement(ExtensibilityElement element); 171 172 protected abstract boolean filterBindingElement(ExtensibilityElement element); 173 174 public static class WsdlFilter implements FilenameFilter { 175 176 public boolean accept(File dir, String name) { 177 return name.endsWith(".wsdl"); 178 } 179 180 } 181 182 } 183 | Popular Tags |