KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > common > wsdl1 > AbstractWsdl1Deployer


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

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 JavaDoc;
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 JavaDoc;
38
39 import java.io.File JavaDoc;
40 import java.io.FilenameFilter JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.Map JavaDoc;
43
44 public abstract class AbstractWsdl1Deployer extends AbstractDeployer {
45
46     protected FilenameFilter JavaDoc filter;
47     
48     public AbstractWsdl1Deployer(BaseComponent component) {
49         super(component);
50         filter = new WsdlFilter();
51     }
52
53     /* (non-Javadoc)
54      * @see org.apache.servicemix.common.Deployer#canDeploy(java.lang.String, java.lang.String)
55      */

56     public boolean canDeploy(String JavaDoc serviceUnitName,
57                              String JavaDoc serviceUnitRootPath) {
58         File JavaDoc[] wsdls = new File JavaDoc(serviceUnitRootPath).listFiles(filter);
59         return wsdls != null && wsdls.length > 0;
60     }
61     
62     /* (non-Javadoc)
63      * @see org.apache.servicemix.common.Deployer#deploy(java.lang.String, java.lang.String)
64      */

65     public ServiceUnit deploy(String JavaDoc serviceUnitName,
66                               String JavaDoc serviceUnitRootPath) throws DeploymentException {
67         File JavaDoc[] wsdls = new File JavaDoc(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 JavaDoc wsdl) throws DeploymentException {
85         Document JavaDoc description;
86         Definition definition;
87         try {
88             DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
89             factory.setNamespaceAware(true);
90             description = factory.newDocumentBuilder().parse(wsdl);
91             definition = createWsdlReader().readWSDL(null, description);
92         } catch (Exception JavaDoc e) {
93             throw failure("deploy", "Could not parse " + wsdl, e);
94         }
95         Map JavaDoc services = definition.getServices();
96         if (services.size() == 0) {
97             failure("deploy", "Invalid wsdl " + wsdl + ": no defined services", null);
98         }
99         for (Iterator JavaDoc itSvc = services.values().iterator(); itSvc.hasNext();) {
100             Service svc = (Service) itSvc.next();
101             for (Iterator JavaDoc itPorts = svc.getPorts().values().iterator(); itPorts.hasNext();) {
102                 JbiEndpoint jbiEndpoint = null;
103                 Port port = (Port) itPorts.next();
104                 ExtensibilityElement portElement = null;
105                 for (Iterator JavaDoc 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 JavaDoc 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 JavaDoc {
175
176         public boolean accept(File JavaDoc dir, String JavaDoc name) {
177             return name.endsWith(".wsdl");
178         }
179         
180     }
181     
182 }
183
Popular Tags