1 17 18 19 20 package org.apache.lenya.workflow.impl; 21 22 import java.util.Arrays ; 23 import java.util.HashSet ; 24 import java.util.Set ; 25 26 import org.apache.lenya.workflow.Event; 27 import org.apache.lenya.workflow.Situation; 28 import org.apache.lenya.workflow.State; 29 import org.apache.lenya.workflow.SynchronizedWorkflowInstances; 30 import org.apache.lenya.workflow.WorkflowException; 31 import org.apache.lenya.workflow.WorkflowInstance; 32 import org.apache.log4j.Category; 33 34 38 public class SynchronizedWorkflowInstancesImpl implements SynchronizedWorkflowInstances { 39 40 private static final Category log = Category.getInstance(SynchronizedWorkflowInstancesImpl.class); 41 42 45 public SynchronizedWorkflowInstancesImpl() { 46 } 47 48 53 public SynchronizedWorkflowInstancesImpl( 54 WorkflowInstance[] instances, 55 WorkflowInstance mainInstance) { 56 setInstances(instances); 57 setMainInstance(mainInstance); 58 } 59 60 64 public void setMainInstance(WorkflowInstance mainInstance) { 65 this.mainInstance = mainInstance; 66 } 67 68 private WorkflowInstance[] instances; 69 private WorkflowInstance mainInstance; 70 71 public void setInstances(WorkflowInstance[] instances) { 72 this.instances = instances; 73 } 74 75 public WorkflowInstance[] getInstances() { 76 return instances; 77 } 78 79 83 public Event[] getExecutableEvents(Situation situation) throws WorkflowException { 84 if (log.isDebugEnabled()) { 85 log.debug("Resolving executable events"); 86 } 87 88 WorkflowInstance[] instances = getInstances(); 89 if (instances.length == 0) { 90 throw new WorkflowException("The set must contain at least one workflow instance!"); 91 } 92 93 Event[] events = mainInstance.getExecutableEvents(situation); 94 Set executableEvents = new HashSet (Arrays.asList(events)); 95 96 for (int i = 0; i < events.length; i++) { 97 Event event = events[i]; 98 if (mainInstance.isSynchronized(event)) { 99 100 boolean canFire = true; 101 if (log.isDebugEnabled()) { 102 log.debug(" Transition for event [" + event + "] is synchronized."); 103 } 104 105 boolean sameState = true; 106 State currentState = mainInstance.getCurrentState(); 107 int j = 0; 108 while (j < instances.length && sameState) { 109 sameState = instances[j].getCurrentState().equals(currentState); 110 j++; 111 } 112 if (log.isDebugEnabled()) { 113 log.debug(" All instances are in the same state: [" + sameState + "]"); 114 } 115 116 if (sameState) { 117 for (int k = 0; k < instances.length; k++) { 118 WorkflowInstanceImpl instance = (WorkflowInstanceImpl) instances[k]; 119 if (instance != mainInstance && !instance.getNextTransition(event).canFire(situation, instance)) { 120 canFire = false; 121 if (log.isDebugEnabled()) { 122 log.debug(" Workflow instance [" + instance + "] can not fire."); 123 } 124 } 125 } 126 } else { 127 canFire = false; 128 } 129 130 if (!canFire) { 131 executableEvents.remove(event); 132 if (log.isDebugEnabled()) { 133 log.debug(" Event [" + event + "] can not fire - removing from executable events."); 134 } 135 } 136 } 137 } 138 139 if (log.isDebugEnabled()) { 140 log.debug(" Resolving executable events completed."); 141 } 142 143 return (Event[]) executableEvents.toArray(new Event[executableEvents.size()]); 144 } 145 146 150 public void invoke(Situation situation, Event event) throws WorkflowException { 151 152 if (mainInstance.isSynchronized(event)) { 153 for (int i = 0; i < instances.length; i++) { 154 instances[i].invoke(situation, event); 155 } 156 } 157 else { 158 mainInstance.invoke(situation, event); 159 } 160 } 161 162 } 163 | Popular Tags |