1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kew.rule;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.core.api.exception.RiceIllegalStateException;
20 import org.kuali.rice.core.api.util.ClassLoaderUtils;
21 import org.kuali.rice.kew.engine.RouteContext;
22
23
24
25
26
27 class RuleImpl implements Rule {
28
29
30
31
32 public static final String DEFAULT_RULE_EXPRESSION = "WorkflowAttribute";
33
34
35
36 private static final String RULE_EXPRESSION_PACKAGE = "org.kuali.rice.kew.rule";
37
38
39
40 private static final String RULE_EXPRESSION_SUFFIX= "RuleExpression";
41
42
43
44
45 private final org.kuali.rice.kew.api.rule.Rule ruleDefinition;
46
47 RuleImpl(org.kuali.rice.kew.api.rule.Rule ruleDefinition) {
48 this.ruleDefinition = ruleDefinition;
49 }
50
51 public org.kuali.rice.kew.api.rule.Rule getDefinition() {
52 return ruleDefinition;
53 }
54
55
56 protected RuleExpression loadRuleExpression(String type) {
57 if (type == null) {
58 type = DEFAULT_RULE_EXPRESSION;
59 }
60
61
62 int colon = type.indexOf(':');
63 if (colon == -1) colon = type.length();
64 type = type.substring(0, colon);
65 type = StringUtils.capitalize(type);
66
67
68 String className = RULE_EXPRESSION_PACKAGE + "." + type + RULE_EXPRESSION_SUFFIX;
69 Class<?> ruleExpressionClass;
70 try {
71 ruleExpressionClass = ClassLoaderUtils.getDefaultClassLoader().loadClass(className);
72 } catch (ClassNotFoundException cnfe) {
73 throw new RiceIllegalStateException("Rule expression implementation '" + className + "' not found", cnfe);
74 }
75 if (!RuleExpression.class.isAssignableFrom(ruleExpressionClass)) {
76 throw new RiceIllegalStateException("Specified class '" + ruleExpressionClass + "' does not implement RuleExpression interface");
77 }
78 RuleExpression ruleExpression;
79 try {
80 ruleExpression = ((Class<RuleExpression>) ruleExpressionClass).newInstance();
81 } catch (Exception e) {
82 if (e instanceof RuntimeException) {
83 throw (RuntimeException)e;
84 }
85 throw new RiceIllegalStateException("Error instantiating rule expression implementation '" + ruleExpressionClass + "'", e);
86 }
87
88 return ruleExpression;
89 }
90
91 public RuleExpressionResult evaluate(Rule rule, RouteContext context) {
92 org.kuali.rice.kew.api.rule.RuleExpression ruleExprDef = ruleDefinition.getRuleExpressionDef();
93 String type = DEFAULT_RULE_EXPRESSION;
94 if (ruleExprDef != null) {
95 type = ruleExprDef.getType();
96 }
97 RuleExpression ruleExpression = loadRuleExpression(type);
98 return ruleExpression.evaluate(rule, context);
99 }
100 }