KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > bpe > BPEDeployer


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.bpe;
18
19 import java.io.File JavaDoc;
20 import java.io.FilenameFilter JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import javax.jbi.management.DeploymentException;
26 import javax.wsdl.BindingFault;
27 import javax.wsdl.BindingInput;
28 import javax.wsdl.BindingOperation;
29 import javax.wsdl.BindingOutput;
30 import javax.wsdl.Definition;
31 import javax.wsdl.Import;
32 import javax.wsdl.Message;
33 import javax.wsdl.Port;
34 import javax.wsdl.Service;
35 import javax.wsdl.WSDLException;
36 import javax.wsdl.extensions.ExtensionRegistry;
37 import javax.wsdl.factory.WSDLFactory;
38 import javax.wsdl.xml.WSDLReader;
39 import javax.wsdl.xml.WSDLWriter;
40
41 import org.apache.ode.bpe.bped.DeployTypeEnum;
42 import org.apache.ode.bpe.bped.EventDirector;
43 import org.apache.ode.bpe.bped.IDeployer;
44 import org.apache.ode.bpe.wsdl.extensions.BPEAction;
45 import org.apache.ode.bpe.wsdl.extensions.BPEActionSerializer;
46 import org.apache.ode.bpe.wsdl.extensions.BPEFault;
47 import org.apache.ode.bpe.wsdl.extensions.BPEFaultSerializer;
48 import org.apache.ode.bpe.wsdl.extensions.BPEInput;
49 import org.apache.ode.bpe.wsdl.extensions.BPEInputSerializer;
50 import org.apache.ode.bpe.wsdl.extensions.BPELProperty;
51 import org.apache.ode.bpe.wsdl.extensions.BPELPropertyAlias;
52 import org.apache.ode.bpe.wsdl.extensions.BPELPropertyAliasSerializer;
53 import org.apache.ode.bpe.wsdl.extensions.BPELPropertySerializer;
54 import org.apache.ode.bpe.wsdl.extensions.BPEOutput;
55 import org.apache.ode.bpe.wsdl.extensions.BPEOutputSerializer;
56 import org.apache.ode.bpe.wsdl.extensions.BPEVariableMap;
57 import org.apache.ode.bpe.wsdl.extensions.BPEVariableMapSerializer;
58 import org.apache.ode.bpe.wsdl.extensions.ExtentionConstants;
59 import org.apache.ode.bpe.deployment.bpel.BPELDefinitionKey;
60 import org.apache.servicemix.bpe.util.FileSystemJarInputStream;
61 import org.apache.servicemix.common.AbstractDeployer;
62 import org.apache.servicemix.common.ServiceUnit;
63 import org.apache.servicemix.common.tools.wsdl.WSDLFlattener;
64 import org.w3c.dom.Document JavaDoc;
65
66 import com.ibm.wsdl.Constants;
67
68 public class BPEDeployer extends AbstractDeployer {
69
70     protected FilenameFilter JavaDoc filter;
71     
72     public BPEDeployer(BPEComponent component) {
73         super(component);
74         filter = new BpelFilter();
75     }
76
77     public boolean canDeploy(String JavaDoc serviceUnitName, String JavaDoc serviceUnitRootPath) {
78         File JavaDoc[] bpels = new File JavaDoc(serviceUnitRootPath).listFiles(filter);
79         return bpels != null && bpels.length == 1;
80     }
81
82     public ServiceUnit deploy(String JavaDoc serviceUnitName, String JavaDoc serviceUnitRootPath) throws DeploymentException {
83         try {
84             EventDirector ed = ((BPEComponent) component).getEventDirector();
85             IDeployer deployer = ed.getDeployer(DeployTypeEnum.BPEL);
86             Collection JavaDoc defKeys = deployer.loadDefinition(new FileSystemJarInputStream(new File JavaDoc(serviceUnitRootPath)), false);
87             // Build the Service Unit
88
BPEServiceUnit su = new BPEServiceUnit();
89             su.setComponent(component);
90             su.setName(serviceUnitName);
91             su.setRootPath(serviceUnitRootPath);
92             su.setDefinitionKeys(defKeys);
93             Definition rootDef = loadMainWsdl(serviceUnitRootPath);
94             checkDefinition(rootDef, true);
95             su.setDefinition(rootDef);
96             WSDLWriter writer = WSDLFactory.newInstance().newWSDLWriter();
97             WSDLFlattener flattener = new WSDLFlattener(rootDef);
98             for (Iterator JavaDoc it = rootDef.getServices().values().iterator(); it.hasNext();) {
99                 Service svc = (Service) it.next();
100                 for (Iterator JavaDoc it2 = svc.getPorts().values().iterator(); it2.hasNext();) {
101                     Port pt = (Port) it2.next();
102                     BPEEndpoint ep = new BPEEndpoint();
103                     ep.setServiceUnit(su);
104                     ep.setInterfaceName(pt.getBinding().getPortType().getQName());
105                     ep.setService(svc.getQName());
106                     ep.setEndpoint(pt.getName());
107                     Definition def = flattener.getDefinition(ep.getInterfaceName());
108                     Document JavaDoc desc = writer.getDocument(def);
109                     ep.setDefinition(def);
110                     ep.setDescription(desc);
111                     // Retrieve wsdl
112
su.addEndpoint(ep);
113                 }
114             }
115             return su;
116         } catch (DeploymentException e) {
117             throw e;
118         } catch (Exception JavaDoc e) {
119             throw new DeploymentException(e);
120         }
121     }
122
123     public void undeploy(ServiceUnit su) throws DeploymentException {
124         try {
125             BPEServiceUnit bpeSU = (BPEServiceUnit)su;
126             EventDirector ed = ((BPEComponent) component).getEventDirector();
127             IDeployer deployer = ed.getDeployer(DeployTypeEnum.BPEL);
128             for (Iterator JavaDoc i = bpeSU.getDefinitionKeys().iterator(); i.hasNext(); ) {
129                 deployer.removeDefinition(((BPELDefinitionKey)i.next()).getKey());
130             }
131             su.shutDown();
132         } catch (DeploymentException e) {
133             throw e;
134         } catch (Exception JavaDoc e) {
135             throw new DeploymentException(e);
136         }
137     }
138
139     protected void checkDefinition(Definition rootDef, boolean main) throws DeploymentException {
140         // Check that messages have only one part named "payload"
141
Collection JavaDoc msgs = rootDef.getMessages().values();
142         for (Iterator JavaDoc iter = msgs.iterator(); iter.hasNext();) {
143             Message msg = (Message) iter.next();
144             if (msg.isUndefined()) {
145                 throw failure("deploy",
146                         "WSDL Message '" + msg.getQName() + "' is undefined. Check namespaces.", null);
147             }
148             if (msg.getParts().size() > 1) {
149                 throw failure("deploy",
150                         "WSDL Message '" + msg.getQName() + "' has more than one part", null);
151             }
152         }
153         // Check imported wsdls
154
Collection JavaDoc imports = rootDef.getImports().values();
155         for (Iterator JavaDoc iter = imports.iterator(); iter.hasNext();) {
156             List JavaDoc imps = (List JavaDoc) iter.next();
157             for (Iterator JavaDoc iterator = imps.iterator(); iterator.hasNext();) {
158                 Import imp = (Import) iterator.next();
159                 checkDefinition(imp.getDefinition(), false);
160             }
161         }
162     }
163
164     private Definition loadMainWsdl(String JavaDoc serviceUnitRootPath) throws WSDLException {
165         File JavaDoc[] bpels = new File JavaDoc(serviceUnitRootPath).listFiles(filter);
166         String JavaDoc bpel = bpels[0].getAbsoluteFile().toURI().toString();
167         String JavaDoc wsdl = bpel.substring(0, bpel.length() - 4) + "wsdl";
168         WSDLReader reader = WSDLFactory.newInstance().newWSDLReader();
169         reader.setFeature(Constants.FEATURE_VERBOSE, false);
170         reader.setExtensionRegistry(getExtentionRegistry());
171         // Parse the document and include any imported WSDL documents
172
Definition ret = reader.readWSDL(null, wsdl);
173         return ret;
174     }
175     
176     private ExtensionRegistry getExtentionRegistry() {
177         
178         // Use IBM's implementation as a base registry. They have implemented
179
// extensibility objects for SOAP,HTTP,MIME and we should not
180
// loose these functions.
181
ExtensionRegistry er = new com.ibm.wsdl.extensions.PopulatedExtensionRegistry();
182         
183         BPELPropertySerializer bpelPropSerializer = new BPELPropertySerializer();
184         BPELPropertyAliasSerializer bpelPropAliasSerializer = new BPELPropertyAliasSerializer();
185         BPEActionSerializer bpeActionSerializer = new BPEActionSerializer();
186         BPEInputSerializer bpeInputSerializer = new BPEInputSerializer();
187         BPEOutputSerializer bpeOutputSerializer = new BPEOutputSerializer();
188         BPEFaultSerializer bpeFaultSerializer = new BPEFaultSerializer();
189         BPEVariableMapSerializer bpeVariableSerializer = new BPEVariableMapSerializer();
190
191         // Register the BPEL extension points
192
er.registerSerializer(Definition.class,
193                            ExtentionConstants.Q_ELEM_BPEL_PROPERTY,
194                            bpelPropSerializer);
195         er.registerDeserializer(Definition.class,
196                              ExtentionConstants.Q_ELEM_BPEL_PROPERTY,
197                              bpelPropSerializer);
198         er.mapExtensionTypes(Definition.class,
199                           ExtentionConstants.Q_ELEM_BPEL_PROPERTY,
200                             BPELProperty.class);
201         er.registerSerializer(Definition.class,
202                            ExtentionConstants.Q_ELEM_BPEL_PROPERTY_ALIAS,
203                            bpelPropAliasSerializer);
204         er.registerDeserializer(Definition.class,
205                              ExtentionConstants.Q_ELEM_BPEL_PROPERTY_ALIAS,
206                              bpelPropAliasSerializer);
207         er.mapExtensionTypes(Definition.class,
208                           ExtentionConstants.Q_ELEM_BPEL_PROPERTY_ALIAS,
209                             BPELPropertyAlias.class);
210                             
211         // register the BPE extension points
212
er.registerSerializer(BindingOperation.class,
213                             ExtentionConstants.Q_ELEM_BPE_ACTION,
214                             bpeActionSerializer);
215         er.registerDeserializer(BindingOperation.class,
216                             ExtentionConstants.Q_ELEM_BPE_ACTION,
217                             bpeActionSerializer);
218         er.mapExtensionTypes(BindingOperation.class,
219                             ExtentionConstants.Q_ELEM_BPE_ACTION,
220                             BPEAction.class);
221         er.registerSerializer(BindingInput.class,
222                             ExtentionConstants.Q_ELEM_BPE_INPUT,
223                             bpeInputSerializer);
224         er.registerDeserializer(BindingInput.class,
225                             ExtentionConstants.Q_ELEM_BPE_INPUT,
226                             bpeInputSerializer);
227         er.mapExtensionTypes(BindingInput.class,
228                             ExtentionConstants.Q_ELEM_BPE_INPUT,
229                             BPEInput.class);
230         er.registerSerializer(BindingOutput.class,
231                             ExtentionConstants.Q_ELEM_BPE_OUTPUT,
232                             bpeOutputSerializer);
233         er.registerDeserializer(BindingOutput.class,
234                             ExtentionConstants.Q_ELEM_BPE_OUTPUT,
235                             bpeOutputSerializer);
236         er.mapExtensionTypes(BindingOutput.class,
237                             ExtentionConstants.Q_ELEM_BPE_OUTPUT,
238                             BPEOutput.class);
239         
240         er.registerSerializer(BindingFault.class,
241                             ExtentionConstants.Q_ELEM_BPE_FAULT,
242                             bpeFaultSerializer);
243         er.registerDeserializer(BindingFault.class,
244                             ExtentionConstants.Q_ELEM_BPE_FAULT,
245                             bpeFaultSerializer);
246         er.mapExtensionTypes(BindingFault.class,
247                             ExtentionConstants.Q_ELEM_BPE_FAULT,
248                             BPEFault.class);
249         
250         er.registerSerializer(Definition.class,
251                             ExtentionConstants.Q_ELEM_BPE_VAR,
252                             bpeVariableSerializer);
253         er.registerDeserializer(Definition.class,
254                             ExtentionConstants.Q_ELEM_BPE_VAR,
255                             bpeVariableSerializer);
256         er.mapExtensionTypes(Definition.class,
257                             ExtentionConstants.Q_ELEM_BPE_VAR,
258                             BPEVariableMap.class);
259                             
260         return er;
261     }
262     
263     public static class BpelFilter implements FilenameFilter JavaDoc {
264
265         public boolean accept(File JavaDoc dir, String JavaDoc name) {
266             return name.endsWith(".bpel");
267         }
268         
269     }
270 }
271
Popular Tags