DelegateExpressionActivitiEventListener.java 2.7 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.activiti.engine.impl.bpmn.helper;

import org.activiti.engine.ActivitiIllegalArgumentException;
import org.activiti.engine.delegate.Expression;
17
import org.activiti.engine.delegate.event.ActivitiEntityEvent;
18 19 20 21 22 23 24
import org.activiti.engine.delegate.event.ActivitiEvent;
import org.activiti.engine.delegate.event.ActivitiEventListener;
import org.activiti.engine.impl.el.NoExecutionVariableScope;

/**
 * An {@link ActivitiEventListener} implementation which resolves an expression
 * to a delegate {@link ActivitiEventListener} instance and uses this for event notification.
25 26 27 28
 * <br><br>
 * In case an entityClass was passed in the constructor, only events that are {@link ActivitiEntityEvent}'s
 * that target an entity of the given type, are dispatched to the delegate.
 * 
29 30
 * @author Frederik Heremans
 */
31
public class DelegateExpressionActivitiEventListener extends BaseDelegateEventListener {
32 33 34 35

	protected Expression expression;
	protected boolean failOnException = true;

36
	public DelegateExpressionActivitiEventListener(Expression expression, Class<?> entityClass) {
37
		this.expression = expression;
38
		setEntityClass(entityClass);
39 40 41 42
	}

	@Override
	public void onEvent(ActivitiEvent event) {
43 44
		if(isValidEvent(event)) {
			NoExecutionVariableScope scope = new NoExecutionVariableScope();
45
			
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
			Object delegate = expression.getValue(scope);
			if (delegate instanceof ActivitiEventListener) {
				// Cache result of isFailOnException() from delegate-instance until next
				// event is received. This prevents us from having to resolve the expression twice when
				// an error occurs.
				failOnException = ((ActivitiEventListener) delegate).isFailOnException();
				
				// Call the delegate
				((ActivitiEventListener) delegate).onEvent(event);
			} else {
				
				// Force failing, since the exception we're about to throw cannot be ignored, because it
				// did not originate from the listener itself
				failOnException = true;
				throw new ActivitiIllegalArgumentException("Delegate expression " + expression
						+ " did not resolve to an implementation of " + ActivitiEventListener.class.getName());
			}
63 64 65 66 67 68 69 70 71
		}
	}

	@Override
	public boolean isFailOnException() {
		return failOnException;
	}

}