ExpressionManager.java 4.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.activiti5.engine.impl.el;

import java.util.Map;

17 18
import org.activiti.engine.delegate.Expression;
import org.activiti.engine.delegate.VariableScope;
19 20 21 22 23 24 25 26 27 28 29 30 31 32
import org.activiti5.engine.impl.bpmn.data.ItemInstance;
import org.activiti5.engine.impl.javax.el.ArrayELResolver;
import org.activiti5.engine.impl.javax.el.BeanELResolver;
import org.activiti5.engine.impl.javax.el.CompositeELResolver;
import org.activiti5.engine.impl.javax.el.DynamicBeanPropertyELResolver;
import org.activiti5.engine.impl.javax.el.ELContext;
import org.activiti5.engine.impl.javax.el.ELResolver;
import org.activiti5.engine.impl.javax.el.ExpressionFactory;
import org.activiti5.engine.impl.javax.el.ListELResolver;
import org.activiti5.engine.impl.javax.el.MapELResolver;
import org.activiti5.engine.impl.javax.el.ValueExpression;
import org.activiti5.engine.impl.juel.ExpressionFactoryImpl;
import org.activiti5.engine.impl.persistence.entity.VariableScopeImpl;

33

34 35 36 37 38
/**
 * <p>
 * Central manager for all expressions.
 * </p>
 * <p>
39 40
 * Process parsers will use this to build expression objects that are stored in
 * the process definitions.
41 42
 * </p>
 * <p>
43 44
 * Then also this class is used as an entry point for runtime evaluation of the
 * expressions.
45 46 47 48 49 50 51 52
 * </p>
 * 
 * @author Tom Baeyens
 * @author Dave Syer
 * @author Frederik Heremans
 */
public class ExpressionManager {

53 54 55 56
  protected ExpressionFactory expressionFactory;
  // Default implementation (does nothing)
  protected ELContext parsingElContext = new ParsingElContext();
  protected Map<Object, Object> beans;
57 58
  
  
59
  public ExpressionManager() {
60
	    this(null);
61
  }
62
  
63
  public ExpressionManager(boolean initFactory) {
64
	    this(null, false);
65
  }
66
  
67
  public ExpressionManager(Map<Object, Object> beans) {
68
	  this(beans, true);
69
  }
70
  
71
  public ExpressionManager(Map<Object, Object> beans, boolean initFactory) {
72 73 74
	    // Use the ExpressionFactoryImpl in activiti build in version of juel, with parametrised method expressions enabled
	    expressionFactory = new ExpressionFactoryImpl();
	    this.beans = beans;
75 76
  }

77 78
 
  
79 80 81 82 83 84 85 86 87 88 89 90 91 92
  public Expression createExpression(String expression) {
    ValueExpression valueExpression = expressionFactory.createValueExpression(parsingElContext, expression.trim(), Object.class);
    return new JuelExpression(valueExpression, expression);
  }

  public void setExpressionFactory(ExpressionFactory expressionFactory) {
    this.expressionFactory = expressionFactory;
  }

  public ELContext getElContext(VariableScope variableScope) {
    ELContext elContext = null;
    if (variableScope instanceof VariableScopeImpl) {
      VariableScopeImpl variableScopeImpl = (VariableScopeImpl) variableScope;
      elContext = variableScopeImpl.getCachedElContext();
T
Tijs Rademakers 已提交
93
    }
94 95
    
    if (elContext==null) {
96 97
      elContext = createElContext(variableScope);
      if (variableScope instanceof VariableScopeImpl) {
98
        ((VariableScopeImpl)variableScope).setCachedElContext(elContext);
99
      }
T
Tijs Rademakers 已提交
100 101
    }

102 103
    return elContext;
  }
T
Tijs Rademakers 已提交
104

105 106 107 108
  protected ActivitiElContext createElContext(VariableScope variableScope) {
    ELResolver elResolver = createElResolver(variableScope);
    return new ActivitiElContext(elResolver);
  }
T
Tijs Rademakers 已提交
109

110 111 112
  protected ELResolver createElResolver(VariableScope variableScope) {
    CompositeELResolver elResolver = new CompositeELResolver();
    elResolver.add(new VariableScopeElResolver(variableScope));
113 114 115
    
    if(beans != null) {
      // ACT-1102: Also expose all beans in configuration when using standalone activiti, not
116 117
      // in spring-context
      elResolver.add(new ReadOnlyMapELResolver(beans));
118
    }
119
    
120 121 122
    elResolver.add(new ArrayELResolver());
    elResolver.add(new ListELResolver());
    elResolver.add(new MapELResolver());
123
    elResolver.add(new DynamicBeanPropertyELResolver(ItemInstance.class, "getFieldValue", "setFieldValue")); //TODO: needs verification
124 125 126 127
    elResolver.add(new BeanELResolver());
    return elResolver;
  }

128 129 130
	public Map<Object, Object> getBeans() {
		return beans;
	}
T
Tijs Rademakers 已提交
131

132 133 134 135
	public void setBeans(Map<Object, Object> beans) {
		this.beans = beans;
	}
  
136
}