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