1 package org.smartcomps.twister.deployer.priv;
2
3 import org.dom4j.Document;
4 import org.dom4j.DocumentException;
5 import org.dom4j.DocumentHelper;
6 import org.dom4j.Element;
7 import org.dom4j.Namespace;
8 import org.dom4j.Node;
9 import org.dom4j.io.SAXReader;
10 import org.smartcomps.twister.common.persistence.DBSessionException;
11 import org.smartcomps.twister.common.transaction.TransactionException;
12 import org.smartcomps.twister.common.transaction.TransactionManager;
13 import org.smartcomps.twister.common.util.logger.Logger;
14 import org.smartcomps.twister.common.util.StringUtil;
15 import org.smartcomps.twister.deployer.TwisterDeployer;
16 import org.smartcomps.twister.deployer.exception.DeploymentException;
17 import org.smartcomps.twister.engine.priv.core.definition.ProcessFactory;
18 import org.smartcomps.twister.engine.priv.core.definition.TwisterProcess;
19 import org.smartcomps.twister.common.persistence.CreationException;
20 import org.smartcomps.twister.common.configuration.DeployerConfiguration;
21
22 import java.io.BufferedInputStream;
23 import java.io.File;
24 import java.io.InputStream;
25 import java.net.URL;
26 import java.util.Iterator;
27 import java.util.List;
28
29 /***
30 * Implementation of the TwisterDeployer interface.
31 * <process name="ncname" targetNamespace="uri"
32 * queryLanguage="anyURI"?
33 * expressionLanguage="anyURI"?
34 * suppressJoinFailure="yes|no"?
35 * enableInstanceCompensation="yes|no"?
36 * abstractProcess="yes|no"?
37 * xmlns="http://schemas.xmlsoap.org/ws/2003/03/business-process/">
38 * <partnerLinks>?
39 * <!-- Note: At least one role must be specified. -->
40 * <partnerLink name="ncname" partnerLinkType="qname"
41 * myRole="ncname"? partnerRole="ncname"?>+
42 * </partnerLink>
43 * </partnerLinks>
44 * <partners>?
45 * <partner name="ncname">+
46 * <partnerLink name="ncname"/>+
47 * </partner>
48 * </partners>
49 * <variables>?
50 * <variable name="ncname" messageType="qname"?
51 * type="qname"? element="qname"?/>+
52 * </variables>
53 * <correlationSets>?
54 * <correlationSet name="ncname" properties="qname-list"/>+
55 * </correlationSets>
56 * <faultHandlers>?
57 * <!-- Note: There must be at least one fault handler or default. -->
58 * <catch faultName="qname"? faultVariable="ncname"?>*
59 * activity
60 * </catch>
61 * <catchAll>?
62 * activity
63 * </catchAll>
64 * </faultHandlers>
65 * <compensationHandler>?
66 * activity
67 * </compensationHandler>
68 * <eventHandlers>?
69 * <!-- Note: There must be at least one onMessage or onAlarm handler. -->
70 * <onMessage partnerLink="ncname" portType="qname"
71 * operation="ncname" variable="ncname"?>
72 * <correlations>?
73 * <correlation set="ncname" initiate="yes|no"?>+
74 * </correlations>
75 * activity
76 * </onMessage>
77 * <onAlarm for="duration-expr"? until="deadline-expr"?>*
78 * activity
79 * </onAlarm>
80 * </eventHandlers>
81 * activity
82 * </process>
83 */
84 public class TwisterDeployerImpl implements TwisterDeployer {
85
86 private Logger log = Logger.getLogger(getClass());
87 private final String CLASSNAME = getClass().getName();
88
89 public void deploy(String xmlProcessDescription) throws DeploymentException {
90
91 String methodName = "deploy";
92 log.entering(CLASSNAME, methodName, xmlProcessDescription);
93
94 try {
95 deploy(getDocument(xmlProcessDescription));
96 } catch (DocumentException e) {
97 throw new DeploymentException("unable to deploy the document", e);
98 }
99
100 log.exiting(CLASSNAME, methodName);
101 }
102
103 public void deploy(InputStream xmlProcessDescription) throws DeploymentException {
104 String methodName = "deploy";
105 log.entering(CLASSNAME, methodName, xmlProcessDescription);
106 try {
107 deploy(getDocument(new BufferedInputStream(xmlProcessDescription).toString()));
108 } catch (DocumentException e) {
109 throw new DeploymentException("unable to deploy the document", e);
110 }
111 log.exiting(CLASSNAME, methodName);
112 }
113
114 public void deploy(URL xmlProcessDescription) throws DeploymentException {
115 String methodName = "deploy";
116 log.entering(CLASSNAME, methodName, xmlProcessDescription);
117 try {
118 deploy(getDocument(xmlProcessDescription));
119 } catch (Exception e) {
120 throw new DeploymentException("unable to deploy the document", e);
121 }
122 log.exiting(CLASSNAME, methodName);
123 }
124
125 public void deploy(File xmlProcessDescription) throws DeploymentException {
126 String methodName = "deploy";
127 log.entering(CLASSNAME, methodName, xmlProcessDescription);
128 try {
129 deploy(getDocument(xmlProcessDescription.toURL()));
130 } catch (Exception e) {
131 throw new DeploymentException("unable to deploy the document", e);
132 }
133 log.exiting(CLASSNAME, methodName);
134 }
135
136 int treeLevel = 0;
137 boolean isReceive = false;
138
139 public void preDeploy(Element elt) {
140 treeLevel++;
141 // System.out.print(elt.getName());
142 System.out.println(elt.getName() + " treelevel " + treeLevel);
143 // if ("receive".equals(elt.getName())) {
144 // isReceive = true;
145 // }
146 Iterator it = elt.elementIterator();
147 int elementCount = 0;
148 while (it.hasNext()) {
149 elementCount++;
150 Element element = (Element) it.next();
151 // System.out.println(element.getName() + " elementCount " + elementCount);
152 // treeLevel--;
153 preDeploy(element);
154 // elementCount--;
155 }
156
157 treeLevel--;
158 }
159
160 private void deploy(Document doc) throws DeploymentException {
161 String methodName = "deploy";
162 log.entering(CLASSNAME, methodName, doc);
163 // preDeploy(ActivityDeployer.getActivityElement(doc.getRootElement()));
164 // System.out.println(treeLevel);
165
166 try {
167 log.debug("begin transaction");
168 TransactionManager.beginTransaction();
169
170 Element processElement = doc.getRootElement();
171 log.debug("<process>");
172 TwisterProcess tp = deployProcessElement(processElement);
173 deployProcessDefinitions(processElement, tp);
174 deployPartnerLinksElement(processElement.element("partnerLinks"), tp);
175 deployPartnersElement(processElement.element("partners"), tp);
176 deployVariables(processElement.element("variables"), tp);
177 deployCorrelationSets(processElement.element("correlationSets"), tp);
178 deployFaultHandlers(processElement.element("faultHandlers"), tp);
179 deployCompensationHandler(processElement.element("compensationHandler"), tp);
180 deployEventHandlers(processElement.element("eventHandlers"), tp);
181
182 deployActivity(processElement, tp);
183 log.debug("</process> ");
184 log.debug("commit transaction");
185 TransactionManager.commitTransaction();
186 } catch (TransactionException e) {
187 throw new DeploymentException(e);
188 }
189
190 log.exiting(CLASSNAME, methodName);
191 }
192
193 /***
194 * todo actualy, it processes only properties defined in one file.
195 * todo Deploy also if there are more than one definition file.
196 */
197 private void deployProcessDefinitions(Element processElement, TwisterProcess tp) throws DeploymentException {
198 List list = processElement.declaredNamespaces();
199 for (int i = 0; i < list.size(); i++) {
200 Namespace ns = (Namespace) list.get(i);
201 String nsURI = ns.getURI();
202 List uncheckedDefSchema = DeployerConfiguration.getUncheckedDefSchema();
203 if (!uncheckedDefSchema.contains(nsURI)) {
204 URL nsURL = null;
205 try {
206 String urlLocalMapping = getUrlLocalMapping(nsURI);
207 nsURL = getClass().getClassLoader().getResource(urlLocalMapping);
208 Document doc = getDocument(nsURL);
209 Element rootElement = doc.getRootElement();
210 Iterator propertyAlias = rootElement.elementIterator("propertyAlias");
211 while (propertyAlias.hasNext()) {
212 Element e = (Element) propertyAlias.next();
213 Node propNode = doc.selectSingleNode("//*/property[@name=\"" + e.valueOf("@propertyName") + "\"]");
214
215 ProcessFactory.addProperty(tp,
216 e.valueOf("@propertyName"), propNode.valueOf("@type"),
217 e.valueOf("@messageType"), e.valueOf("@part"), e.valueOf("@query"));
218 rootElement.remove(propNode);
219 }
220 Iterator properties = rootElement.elementIterator("property");
221 while (properties.hasNext()) {
222 Element e = (Element) properties.next();
223 ProcessFactory.addProperty(tp, e.valueOf("@name"), e.valueOf("@type"));
224 }
225 } catch (Exception e) {
226 throw new DeploymentException(e);
227 }
228
229 }
230 }
231 }
232
233 private String getUrlLocalMapping(String nsURI) {
234 String property = null;
235 property = (String) DeployerConfiguration.getProcessDefMapping().get(nsURI);
236 if (property == null) {
237 property = nsURI;
238 }
239 return property;
240 }
241
242 private void deployActivity(Element processElement, TwisterProcess tp) throws DeploymentException, TransactionException {
243 String methodName = "deployActivity";
244 log.entering(CLASSNAME, methodName);
245 Element activityElement = ActivityDeployer.getActivityElement(processElement);
246 if (activityElement != null) {
247 log.debug("<" + activityElement.getName() + ">");
248 ActivityDeployer ad = ActivityDeployerFactory.getActivityDeployer(activityElement.getName());
249 try {
250 ad.deploy(activityElement, tp);
251 log.debug("</" + activityElement.getName() + ">");
252 } catch (DeploymentException e) {
253 TransactionManager.rollbackTransaction();
254 log.error("Transation Rolled Back due to " + e.getMessage());
255 throw new DeploymentException(e);
256 }
257 }
258 log.exiting(CLASSNAME, methodName);
259 }
260
261 /***
262 * Deploy the proces element.
263 *
264 * @param processElement the process DOM element
265 * @return the TwisterProcess corresponding the given process
266 * @throws TransactionException
267 * @throws DeploymentException
268 */
269 private TwisterProcess deployProcessElement(Element processElement) throws TransactionException, DeploymentException {
270 String methodName = "deployProcessElement";
271 log.entering(CLASSNAME, methodName, processElement);
272 String name = processElement.valueOf("@name");
273 String targetNamespace = processElement.valueOf("@targetNamespace");
274 String queryLanguage = processElement.valueOf("@queryLanguage");
275 String expressionLanguage = processElement.valueOf("@expressionLanguage");
276 String suppressJoinFailure = processElement.valueOf("@suppressJoinFailure");
277 String enableInstanceCompensation = processElement.valueOf("@enableInstanceCompensation");
278 String abstractProcess = processElement.valueOf("@abstractProcess");
279 String xmlns = processElement.valueOf("@xmlns");
280 TwisterProcess tp = null;
281 try {
282 tp = ProcessFactory.createProcess(name);
283 } catch (DBSessionException e) {
284 TransactionManager.rollbackTransaction();
285 throw new DeploymentException(e);
286 } catch (CreationException e) {
287 TransactionManager.rollbackTransaction();
288 throw new DeploymentException(e);
289 }
290 log.exiting(CLASSNAME, methodName, tp);
291 return tp;
292 }
293
294 /***
295 * Deployment of the PartnerLinks elements
296 * <p/>
297 * <partnerLinks>?
298 * <!-- Note: At least one role must be specified. -->
299 * <partnerLink name="ncname" partnerLinkType="qname"
300 * myRole="ncname"? partnerRole="ncname"?>+
301 * </partnerLink>
302 * </partnerLinks>
303 *
304 * @param element the partnerLinks DOM element.
305 * @param tp the parent process
306 */
307 private void deployPartnerLinksElement(Element element, TwisterProcess tp) {
308 String methodName = "deployPartnerLinksElement";
309 log.entering(CLASSNAME, methodName, new Object[]{element, tp});
310 if (element != null) {
311 // todo implements 'partnerlinks' elements deployment
312 }
313 log.exiting(CLASSNAME, methodName);
314 return;
315 }
316
317 /***
318 * Deployment of the partners elements.
319 * <p/>
320 * <partners>?
321 * <partner name="ncname">+
322 * <partnerLink name="ncname"/>+
323 * </partner>
324 * </partners>
325 *
326 * @param element the partners DOM element.
327 * @param tp the parent process
328 */
329 private void deployPartnersElement(Element element, TwisterProcess tp) {
330 String methodName = "deployPartnersElement";
331 log.entering(CLASSNAME, methodName, new Object[]{element, tp});
332 if (element != null) {
333 // todo implements 'partners' elements deployment
334 }
335 log.exiting(CLASSNAME, methodName);
336 return;
337 }
338
339 /***
340 * Deployment of the Variables elements
341 * <p/>
342 * <variables>?
343 * <variable name="ncname" messageType="qname"?
344 * type="qname"? element="qname"?/>+
345 * </variables>
346 *
347 * @param element the Variables DOM element.
348 * @param tp the parent process
349 */
350 private void deployVariables(Element element, TwisterProcess tp) {
351 if (element != null) {
352 for (Iterator it = element.elementIterator("variable"); it.hasNext();) {
353 log.debug("<variable>");
354 Element variable = (Element) it.next();
355 String name = variable.valueOf("@name");
356 String messageType = variable.valueOf("@messageType");
357 String type = variable.valueOf("@type");
358 String elmt = variable.valueOf("@element");
359 log.debug("name = " + name);
360 log.debug("messageType = " + messageType);
361 log.debug("type = " + type);
362 log.debug("element = " + elmt);
363 log.debug("</variable>");
364 }
365 }
366 }
367
368 /***
369 * Deployment of the CorrelationSets element
370 * <p/>
371 * <correlationSets>?
372 * <correlationSet name="ncname" properties="qname-list"/>+
373 * </correlationSets>
374 *
375 * @param element the CorrelationSets DOM element.
376 * @param tp the parent process
377 * @throws DeploymentException
378 */
379 private void deployCorrelationSets(Element element, TwisterProcess tp) throws DeploymentException {
380 if (element != null) {
381 for (Iterator it = element.elementIterator("correlationSet"); it.hasNext();) {
382 log.debug("<correlationSet>");
383 Element variable = (Element) it.next();
384 String name = variable.valueOf("@name");
385 String properties = variable.valueOf("@properties");
386 log.debug("name = " + name);
387 log.debug("messageType = " + properties);
388 log.debug("</correlationSet>");
389 try {
390 ProcessFactory.addCorrelation(tp, name, truncNamespace(properties));
391 } catch (Exception e) {
392 throw new DeploymentException(e);
393 }
394 }
395 }
396
397 }
398
399 private String truncNamespace(String string) {
400 int index = string.lastIndexOf(":");
401 return string.substring(index + 1);
402 }
403
404 /***
405 * Deployment of the faultHandlers element
406 * <p/>
407 * <faultHandlers>?
408 * <!-- Note: There must be at least one fault handler or default. -->
409 * <catch faultName="qname"? faultVariable="ncname"?>*
410 * activity
411 * </catch>
412 * <catchAll>?
413 * activity
414 * </catchAll>
415 * </faultHandlers>
416 *
417 * @param element the FaultHandlers DOM element.
418 * @param tp the parent process
419 */
420 private void deployFaultHandlers(Element element, TwisterProcess tp) {
421 if (element != null) {
422 // todo implements 'faulthandlers' elements
423 }
424 }
425
426 /***
427 * Deployment of the compensationHandler elements
428 * <p/>
429 * <compensationHandler>?
430 * activity
431 * </compensationHandler>
432 *
433 * @param element the CompensationHandler DOM element.
434 * @param tp the parent process
435 */
436 private void deployCompensationHandler(Element element, TwisterProcess tp) {
437 if (element != null) {
438 // todo implements 'compensationhandler' elements
439 }
440 }
441
442 /***
443 * Deployment of the eventHandlers elements
444 * <p/>
445 * <eventHandlers>?
446 * <!-- Note: There must be at least one onMessage or onAlarm handler. -->
447 * <onMessage partnerLink="ncname" portType="qname"
448 * operation="ncname" variable="ncname"?>
449 * <correlations>?
450 * <correlation set="ncname" initiate="yes|no"?>+
451 * </correlations>
452 * activity
453 * </onMessage>
454 * <onAlarm for="duration-expr"? until="deadline-expr"?>*
455 * activity
456 * </onAlarm>
457 * </eventHandlers>
458 *
459 * @param element the EventHandlers DOM element.
460 * @param tp the parent process
461 */
462 private void deployEventHandlers(Element element, TwisterProcess tp) {
463 if (element != null) {
464 // todo implements 'eventhandlers' elements
465 }
466 }
467
468 private Document getDocument(String xmlProcessDescription) throws DocumentException {
469 return DocumentHelper.parseText(xmlProcessDescription);
470 }
471
472 private Document getDocument(URL xmlProcessDescription) throws DocumentException {
473 SAXReader reader = new SAXReader();
474 Document document = reader.read(xmlProcessDescription);
475
476 return document;
477 }
478
479
480 }
This page was automatically generated by Maven