ActivitiEventDispatcherImpl.java 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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.delegate.event.impl;

15
import org.activiti.engine.delegate.event.ActivitiEntityEvent;
16 17 18 19
import org.activiti.engine.delegate.event.ActivitiEvent;
import org.activiti.engine.delegate.event.ActivitiEventDispatcher;
import org.activiti.engine.delegate.event.ActivitiEventListener;
import org.activiti.engine.delegate.event.ActivitiEventType;
20
import org.activiti.engine.impl.context.Context;
21
import org.activiti.engine.impl.interceptor.CommandContext;
22
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
23
import org.activiti.engine.repository.ProcessDefinition;
24 25 26 27 28 29 30 31

/**
 * Class capable of dispatching events.
 * 
 * @author Frederik Heremans
 */
public class ActivitiEventDispatcherImpl implements ActivitiEventDispatcher {

32
	protected ActivitiEventSupport eventSupport;
33
	protected boolean enabled = true;
34

35
	public ActivitiEventDispatcherImpl() {
36
		eventSupport = new ActivitiEventSupport();
37 38
	}

39
	public void setEnabled(boolean enabled) {
40 41 42
		this.enabled = enabled;
	}

43
	public boolean isEnabled() {
44 45
		return enabled;
	}
46 47

	@Override
48 49 50
	public void addEventListener(ActivitiEventListener listenerToAdd) {
		eventSupport.addEventListener(listenerToAdd);
	}
51 52

	@Override
53
	public void addEventListener(ActivitiEventListener listenerToAdd, ActivitiEventType... types) {
54
		eventSupport.addEventListener(listenerToAdd, types);
55
	}
56 57

	@Override
58
	public void removeEventListener(ActivitiEventListener listenerToRemove) {
59
		eventSupport.removeEventListener(listenerToRemove);
60
	}
61 62

	@Override
63 64
	public void dispatchEvent(ActivitiEvent event) {
		if (enabled) {
65
			eventSupport.dispatchEvent(event);
66
		}
67 68 69

		// Check if a process context is active. If so, we also call the
		// process-definition
70
		// specific listeners (if any).
71
		if (Context.isExecutionContextActive()) {
72
			ProcessDefinitionEntity definition = Context.getExecutionContext().getProcessDefinition();
73
			if (definition != null) {
74
				definition.getEventSupport().dispatchEvent(event);
75
			}
76
		} else {
77 78 79 80 81 82 83 84 85 86
			// Try getting hold of the Process definition, based on the process
			// definition-key, if a
			// context is active
			CommandContext commandContext = Context.getCommandContext();
			if (commandContext != null) {
				ProcessDefinitionEntity processDefinition = extractProcessDefinitionEntityFromEvent(event);
				if (processDefinition != null) {
					processDefinition.getEventSupport().dispatchEvent(event);
				}
			}
87
		}
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
	}

	/**
	 * In case no process-context is active, this method attempts to extract a
	 * process-definition based on the event. In case it's an event related to an
	 * entity, this can be deducted by inspecting the entity, without additional
	 * queries to the database.
	 * 
	 * If not an entity-related event, the process-definition will be retrieved
	 * based on the processDefinitionId (if filled in). This requires an
	 * additional query to the database in case not already cached. However,
	 * queries will only occur when the definition is not yet in the cache, which
	 * is very unlikely to happen, unless evicted.
	 * 
	 * @param event
	 * @return
	 */
	protected ProcessDefinitionEntity extractProcessDefinitionEntityFromEvent(ActivitiEvent event) {
		ProcessDefinitionEntity result = null;

		if (event.getProcessDefinitionId() != null) {
			result = Context.getProcessEngineConfiguration().getDeploymentManager().getProcessDefinitionCache()
			    .get(event.getProcessDefinitionId());
			if (result != null) {
				result = Context.getProcessEngineConfiguration().getDeploymentManager().resolveProcessDefinition(result);
			}
		}

		if(result == null && event instanceof ActivitiEntityEvent) {
			Object entity = ((ActivitiEntityEvent) event).getEntity();
			if(entity instanceof ProcessDefinition) {
				result = (ProcessDefinitionEntity) entity;
			}
		}
		return result;
	}

125
}