KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > beanflow > JoinSupport


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.beanflow;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Set JavaDoc;
23
24 /**
25  * A useful base class for a activity which joins on the success of a collection
26  * of child activities.
27  *
28  * @version $Revision: $
29  */

30 public abstract class JoinSupport extends TimeoutActivity {
31
32     private List JavaDoc<Activity> children = new ArrayList JavaDoc<Activity>();
33     private Set JavaDoc<Activity> toBeStarted = new HashSet JavaDoc();
34
35     public JoinSupport() {
36     }
37
38     public JoinSupport(List JavaDoc<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 JavaDoc
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 JavaDoc
94     protected void doStart() {
95         super.doStart();
96
97         // lets make sure that the child activities are started properly
98
synchronized (children) {
99             for (Activity child : toBeStarted) {
100                 child.start();
101             }
102             toBeStarted.clear();
103         }
104     }
105
106     /**
107      * Decide whether or not we are done based on the number of children, the
108      * number of child activities stopped and the number of failed activities
109      */

110     protected abstract void onChildStateChange(int childCount, int stoppedCount, int failedCount);
111
112 }
113
Popular Tags