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 | 0 | 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 | 0 | RuleImpl(org.kuali.rice.kew.api.rule.Rule ruleDefinition) { |
48 | 0 | this.ruleDefinition = ruleDefinition; |
49 | 0 | } |
50 | |
|
51 | |
public org.kuali.rice.kew.api.rule.Rule getDefinition() { |
52 | 0 | return ruleDefinition; |
53 | |
} |
54 | |
|
55 | |
|
56 | |
protected RuleExpression loadRuleExpression(String type) { |
57 | 0 | if (type == null) { |
58 | 0 | type = DEFAULT_RULE_EXPRESSION; |
59 | |
} |
60 | |
|
61 | |
|
62 | 0 | int colon = type.indexOf(':'); |
63 | 0 | if (colon == -1) colon = type.length(); |
64 | 0 | type = type.substring(0, colon); |
65 | 0 | type = StringUtils.capitalize(type); |
66 | |
|
67 | |
|
68 | 0 | String className = RULE_EXPRESSION_PACKAGE + "." + type + RULE_EXPRESSION_SUFFIX; |
69 | |
Class<?> ruleExpressionClass; |
70 | |
try { |
71 | 0 | ruleExpressionClass = ClassLoaderUtils.getDefaultClassLoader().loadClass(className); |
72 | 0 | } catch (ClassNotFoundException cnfe) { |
73 | 0 | throw new RiceIllegalStateException("Rule expression implementation '" + className + "' not found", cnfe); |
74 | 0 | } |
75 | 0 | if (!RuleExpression.class.isAssignableFrom(ruleExpressionClass)) { |
76 | 0 | throw new RiceIllegalStateException("Specified class '" + ruleExpressionClass + "' does not implement RuleExpression interface"); |
77 | |
} |
78 | |
RuleExpression ruleExpression; |
79 | |
try { |
80 | 0 | ruleExpression = ((Class<RuleExpression>) ruleExpressionClass).newInstance(); |
81 | 0 | } catch (Exception e) { |
82 | 0 | if (e instanceof RuntimeException) { |
83 | 0 | throw (RuntimeException)e; |
84 | |
} |
85 | 0 | throw new RiceIllegalStateException("Error instantiating rule expression implementation '" + ruleExpressionClass + "'", e); |
86 | 0 | } |
87 | |
|
88 | 0 | return ruleExpression; |
89 | |
} |
90 | |
|
91 | |
public RuleExpressionResult evaluate(Rule rule, RouteContext context) { |
92 | 0 | org.kuali.rice.kew.api.rule.RuleExpression ruleExprDef = ruleDefinition.getRuleExpressionDef(); |
93 | 0 | String type = DEFAULT_RULE_EXPRESSION; |
94 | 0 | if (ruleExprDef != null) { |
95 | 0 | type = ruleExprDef.getType(); |
96 | |
} |
97 | 0 | RuleExpression ruleExpression = loadRuleExpression(type); |
98 | 0 | return ruleExpression.evaluate(rule, context); |
99 | |
} |
100 | |
} |