Permalink
Browse files

Small enhancements to EntityEcaRule.java: parse <eca> element only on…

…ce, improve thread safety, add some accessor methods.

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@1447108 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
adrian-crum committed Feb 18, 2013
1 parent 9a9c92f commit 1facfbcee9ecaece19a5aab046b946991f08bf08
Showing with 39 additions and 34 deletions.
  1. +39 −34 framework/entityext/src/org/ofbiz/entityext/eca/EntityEcaRule.java
@@ -18,12 +18,12 @@
*******************************************************************************/
package org.ofbiz.entityext.eca;
-import java.util.HashSet;
+import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
-import javolution.util.FastList;
import javolution.util.FastMap;
import org.ofbiz.base.util.Debug;
@@ -34,54 +34,47 @@
import org.w3c.dom.Element;
/**
- * EntityEcaRule
+ * Entity event-condition-action rule.
*/
@SuppressWarnings("serial")
public final class EntityEcaRule implements java.io.Serializable {
public static final String module = EntityEcaRule.class.getName();
- private static final Set<String> nameSet = new HashSet<String>(2);
- static {
- nameSet.add("set");
- nameSet.add("action");
- }
-
- protected final String entityName;
- protected final String operationName;
- protected final String eventName;
- protected final boolean runOnError;
- protected final List<EntityEcaCondition> conditions = FastList.newInstance();
- protected final List<Object> actionsAndSets = FastList.newInstance();
- protected boolean enabled = true;
+ private final String entityName;
+ private final String operationName;
+ private final String eventName;
+ private final boolean runOnError;
+ private final List<EntityEcaCondition> conditions;
+ private final List<Object> actionsAndSets;
+ private boolean enabled = true;
public EntityEcaRule(Element eca) {
this.entityName = eca.getAttribute("entity");
this.operationName = eca.getAttribute("operation");
this.eventName = eca.getAttribute("event");
this.runOnError = "true".equals(eca.getAttribute("run-on-error"));
-
- for (Element element: UtilXml.childElementList(eca, "condition")) {
- conditions.add(new EntityEcaCondition(element, true));
- }
-
- for (Element element: UtilXml.childElementList(eca, "condition-field")) {
- conditions.add(new EntityEcaCondition(element, false));
- }
-
- if (Debug.verboseOn()) {
- Debug.logVerbose("Conditions: " + conditions, module);
- }
-
- for (Element actionOrSetElement: UtilXml.childElementList(eca, nameSet)) {
- if ("action".equals(actionOrSetElement.getNodeName())) {
- this.actionsAndSets.add(new EntityEcaAction(actionOrSetElement));
+ ArrayList<EntityEcaCondition> conditions = new ArrayList<EntityEcaCondition>();
+ ArrayList<Object> actionsAndSets = new ArrayList<Object>();
+ for (Element element: UtilXml.childElementList(eca)) {
+ if ("condition".equals(element.getNodeName())) {
+ conditions.add(new EntityEcaCondition(element, true));
+ } else if ("condition-field".equals(element.getNodeName())) {
+ conditions.add(new EntityEcaCondition(element, false));
+ } else if ("action".equals(element.getNodeName())) {
+ actionsAndSets.add(new EntityEcaAction(element));
+ } else if ("set".equals(element.getNodeName())) {
+ actionsAndSets.add(new EntityEcaSetField(element));
} else {
- this.actionsAndSets.add(new EntityEcaSetField(actionOrSetElement));
+ Debug.logWarning("Invalid eca child element " + element.getNodeName(), module);
}
}
-
+ conditions.trimToSize();
+ this.conditions = Collections.unmodifiableList(conditions);
+ actionsAndSets.trimToSize();
+ this.actionsAndSets = Collections.unmodifiableList(actionsAndSets);
if (Debug.verboseOn()) {
+ Debug.logVerbose("Conditions: " + conditions, module);
Debug.logVerbose("actions and sets (intermixed): " + actionsAndSets, module);
}
}
@@ -102,6 +95,14 @@ public boolean getRunOnError() {
return this.runOnError;
}
+ public List<Object> getActionsAndSets() {
+ return this.actionsAndSets;
+ }
+
+ public List<EntityEcaCondition> getConditions() {
+ return this.conditions;
+ }
+
public void eval(String currentOperation, DispatchContext dctx, GenericEntity value, boolean isError, Set<String> actionsRun) throws GenericEntityException {
if (!enabled) {
Debug.logInfo("Entity ECA [" + this.entityName + "] on [" + this.eventName + "] is disabled; not running.", module);
@@ -148,6 +149,10 @@ public void eval(String currentOperation, DispatchContext dctx, GenericEntity va
}
}
+ /**
+ * @deprecated Not thread-safe, no replacement.
+ * @param enabled
+ */
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

0 comments on commit 1facfbc

Please sign in to comment.