KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > jobs > structural > SequentialJob


1 package org.oddjob.jobs.structural;
2
3
4 import org.oddjob.Stoppable;
5 import org.oddjob.Structural;
6 import org.oddjob.framework.StructuralJob;
7
8 /**
9  * @oddjob.description Executes children in a sequence. The sequence
10  * will only continue to be executed if each child completes.
11  * If a child is not complete, or flags an exception then execution
12  * will terminate and this job's state will reflect that of the
13  * failed child.
14  * <p>
15  * If the failed job is later run manually and completes this Job will
16  * reflect the new state. As such it is useful as a trigger point for
17  * the completion of a sequence of jobs.
18  *
19  * @oddjob.example
20  *
21  * <pre>
22  * &lt;sequential id="trigger-on-me" name="A sequence of two jobs"&gt;
23  * &lt;echo text="This runs first" /&gt;
24  * &lt;echo text="This runs after" /&gt;
25  * &lt;/sequential&gt;
26  * <pre>
27  *
28  *
29  * @author Rob Gordon
30  */

31
32 public class SequentialJob extends StructuralJob
33             implements Structural, Stoppable {
34     private static final long serialVersionUID = 20051206;
35     
36     /**
37      * Add a child.
38      *
39      * @oddjob.element <i>Any</i>
40      * @oddjob.description The child jobs.
41      * @oddjob.required No, but pointless if missing.
42      *
43      * @param child A child
44      */

45     public void addComponent(Object JavaDoc child) {
46         childHelper.addChild(child);
47     }
48
49     public void init() {
50         childHelper.initialise();
51     }
52     
53     /*
54      * (non-Javadoc)
55      * @see org.oddjob.jobs.AbstractJob#execute()
56      */

57     public void execute() throws Exception JavaDoc {
58         Object JavaDoc[] children = childHelper.getChildren();
59         for (int i = 0; i < children.length && !stop; ++i) {
60             if (childHelper.anyNotComplete() ||
61                     childHelper.anyExceptions() != null) {
62                 return;
63             }
64             Object JavaDoc child = children[i];
65             if (!(child instanceof Runnable JavaDoc)) {
66                 continue;
67             }
68             
69             Runnable JavaDoc job = (Runnable JavaDoc) child;
70             job.run();
71         }
72     }
73     
74 }
75
Popular Tags