View Javadoc

1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * {@link Rule} implementation 
25   * @author Kuali Rice Team (rice.collab@kuali.org)
26   */
27  class RuleImpl implements Rule {
28      /**
29       * The default type of rule expression implementation to use if none is explicitly
30       * specified for the node.
31       */
32      public static final String DEFAULT_RULE_EXPRESSION = "WorkflowAttribute";
33      /**
34       * Package in which rule expression implementations live
35       */
36      private static final String RULE_EXPRESSION_PACKAGE = "org.kuali.rice.kew.rule";
37      /**
38       * The class name suffix all rule expressions should have; e.g. FooRuleExpression
39       */
40      private static final String RULE_EXPRESSION_SUFFIX= "RuleExpression";
41  
42      /**
43       * The BO of the rule definition in the system
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      // loads a RuleExpression implementation
56      protected RuleExpression loadRuleExpression(String type) {
57          if (type == null) {
58              type = DEFAULT_RULE_EXPRESSION;
59          }
60          // type is of the format 'category:qualifier'
61          // we just want the category
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          // load up the rule expression implementation
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 }