KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > bpel > exe > state > ActiveState


1 package org.jbpm.bpel.exe.state;
2
3 import org.jbpm.graph.exe.Token;
4
5 import org.jbpm.bpel.def.Scope;
6 import org.jbpm.bpel.exe.InstanceIterator;
7 import org.jbpm.bpel.exe.ScopeInstance;
8 import org.jbpm.bpel.exe.ScopeInstance.ScopeIterator;
9
10 /**
11  * @author Juan Cantú
12  * @version $Revision: 1.6 $ $Date: 2005/06/16 19:15:36 $
13  */

14 public abstract class ActiveState extends ScopeState {
15
16   public static final ActiveState NORMAL_PROCESSING = new ActiveState("normalProcessing", 0) {
17
18     private static final long serialVersionUID = 1L;
19
20     public void completed(ScopeInstance scope) {
21       //disable events
22
Token eventToken = scope.getToken().getChild(Scope.EVENTS_TOKEN);
23       if(eventToken != null) {
24         scope.getDefinition().disableEvents(eventToken);
25       }
26       
27       if ( !hasPendingEvents(scope) ) {
28         //set completed if no events left
29
enterCompleted(scope);
30       } else {
31         //otherwise set a events pending state and wait for their completion
32
scope.setState(EVENTS_PENDING);
33       }
34     }
35   };
36   
37   public static final ActiveState EVENTS_PENDING = new ActiveState("eventsPending", 1) {
38     
39     private static final long serialVersionUID = 1L;
40
41     public void completed(ScopeInstance scope) {
42       //assert scope.getToken().hasEnded() : "no tokens should remain " +
43
//"when a context with pending events completes";
44
enterCompleted(scope);
45     }
46   };
47
48   /**
49    * Constructs an active state identified by the given name.
50    * @param name
51    */

52   protected ActiveState(String JavaDoc name, int code) {
53     super(name, code);
54   }
55   
56   public void terminate(ScopeInstance scope) {
57     TerminatingState.enterTerminating(scope);
58   }
59
60   public void faulted(ScopeInstance scope) {
61     FaultingState.enterFaulting(scope);
62   }
63
64   protected void enterCompleted(ScopeInstance scope) {
65     //set completed state
66
scope.setState(EndedState.COMPLETED);
67     scope.notifyCompletion();
68   }
69   
70   public boolean hasPendingEvents(ScopeInstance scope) {
71     Token eventToken = scope.getToken().getChild(Scope.EVENTS_TOKEN);
72     if(eventToken == null) return false;
73     
74     InstanceIterator it = new ScopeIterator(eventToken);
75     
76     while (it.hasNext()) {
77       ScopeInstance child = (ScopeInstance) it.next();
78       if(!child.getState().equals(EndedState.COMPLETED)) return true;
79     }
80     
81     return false;
82   }
83 }
84
Popular Tags