1 package org.smartcomps.twister.engine.priv.util;
2
3 import org.dom4j.Node;
4 import org.dom4j.Document;
5 import org.smartcomps.twister.engine.priv.core.definition.*;
6 import org.smartcomps.twister.engine.exception.CorrelationViolationException;
7 import org.smartcomps.twister.engine.exception.ExecutionException;
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10
11 import java.util.Map;
12 import java.util.Collection;
13 import java.util.HashMap;
14 import java.util.Iterator;
15
16 /***
17 * Extracts correlation values from a Collection of correlation set references
18 * and an XML element.
19 */
20 public class CorrelationExtractor {
21
22 protected static Log log = LogFactory.getLog(CorrelationExtractor.class);
23
24 public static Map extractCorrelationsValues(TwisterProcess process, Collection corrRefs, Node message) throws CorrelationViolationException {
25 HashMap selectedCorrelRefs = new HashMap(corrRefs.size());
26 for (Iterator correlRefIter = corrRefs.iterator(); correlRefIter.hasNext();) {
27 CorrelationRef ref = (CorrelationRef) correlRefIter.next();
28 selectedCorrelRefs.put(ref, extractCorrelationValues(process, ref, message));
29 }
30 return selectedCorrelRefs;
31
32 }
33
34 public static Map extractCorrelationValues(TwisterProcess process, CorrelationRef ref, Node message) throws CorrelationViolationException {
35 CorrelationSet set = process.getCorrelationSet(ref.getSet());
36
37 HashMap propertyMap = new HashMap();
38 for (Iterator propIter = set.getProperties().iterator(); propIter.hasNext();) {
39 String property = (String) propIter.next();
40 propertyMap.put(property, extractPropertyValue(process, property, message));
41 }
42 return propertyMap;
43 }
44
45 public static String extractPropertyValue(TwisterProcess process, String property, Node message) throws CorrelationViolationException {
46 Property prop = process.getProperty(property);
47 if (prop == null) {
48 throw new ExecutionException("Property " + property + " could not be found in process " + process.getName());
49 }
50 PropertyAlias alias = prop.getAlias();
51 Node propertyValue = message.selectSingleNode("/message/" + alias.getPart() + alias.getQuery());
52 // This shouldn't occur as the type of the message should be checked at reception
53 // and the type must be compatible with property aliases.
54 if (propertyValue == null) {
55 log.error("Could not acknowledge a message " + message.asXML() + ". The property with alias " +
56 "/message/" + alias.getPart() + alias.getQuery() + " could not be selected in message.");
57 throw new CorrelationViolationException("Could not acknowledge a message " + message.asXML() + ". The property " +
58 "with alias /message/" + alias.getPart() + alias.getQuery() +
59 " could not be selected in message.");
60 }
61 return propertyValue.getText();
62 }
63 }
This page was automatically generated by Maven