1 17 package org.apache.servicemix.beanflow; 18 19 import java.util.ArrayList ; 20 import java.util.HashSet ; 21 import java.util.List ; 22 import java.util.Set ; 23 24 30 public abstract class JoinSupport extends TimeoutActivity { 31 32 private List <Activity> children = new ArrayList <Activity>(); 33 private Set <Activity> toBeStarted = new HashSet (); 34 35 public JoinSupport() { 36 } 37 38 public JoinSupport(List <Activity> activities) { 39 synchronized (children) { 40 for (Activity activity : activities) { 41 activity.getState().addRunnable(this); 42 children.add(activity); 43 toBeStarted.add(activity); 44 } 45 } 46 } 47 48 public JoinSupport(Activity... activities) { 49 synchronized (children) { 50 for (Activity activity : activities) { 51 activity.getState().addRunnable(this); 52 children.add(activity); 53 toBeStarted.add(activity); 54 } 55 } 56 } 57 58 public void fork(Activity child) { 59 synchronized (children) { 60 child.getState().addRunnable(this); 61 children.add(child); 62 child.start(); 63 } 64 } 65 66 public void cancelFork(Activity child) { 67 synchronized (children) { 68 child.getState().removeRunnable(this); 69 children.remove(child); 70 child.stop(); 71 } 72 } 73 74 @Override 75 protected void onValidStateChange() { 76 int childCount = 0; 77 int stoppedCount = 0; 78 int failedCount = 0; 79 synchronized (children) { 80 childCount = children.size(); 81 for (Activity child : children) { 82 if (child.isStopped()) { 83 stoppedCount++; 84 if (child.isFailed()) { 85 failedCount++; 86 } 87 } 88 } 89 } 90 onChildStateChange(childCount, stoppedCount, failedCount); 91 } 92 93 @Override 94 protected void doStart() { 95 super.doStart(); 96 97 synchronized (children) { 99 for (Activity child : toBeStarted) { 100 child.start(); 101 } 102 toBeStarted.clear(); 103 } 104 } 105 106 110 protected abstract void onChildStateChange(int childCount, int stoppedCount, int failedCount); 111 112 } 113 | Popular Tags |