KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > bpel > def > ProcessInstanceTrigger


1 package org.jbpm.bpel.def;
2
3 import java.util.Collection JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.Map JavaDoc;
6
7 import org.jbpm.graph.exe.ExecutionContext;
8 import org.jbpm.graph.exe.Token;
9
10 import org.jbpm.bpel.service.def.Receiver;
11
12 /**
13  * @author Juan Cantu
14  * @version $Revision: 1.4 $ $Date: 2005/06/16 19:15:38 $
15  */

16 class ProcessInstanceTrigger implements BpelVisitor {
17
18   private final ExecutionContext context;
19   private final Receiver trigger;
20   
21   ProcessInstanceTrigger(ExecutionContext context, Receiver trigger) {
22     this.context = context;
23     this.trigger = trigger;
24   }
25   
26   /**{@inheritDoc}*/
27   public void visit(BpelDefinition process) {
28     process.getRoot().accept(this);
29   }
30   
31   /**{@inheritDoc}*/
32   public void visit(Empty empty) {
33     validateNonInitial(empty);
34   }
35  
36   /**{@inheritDoc}*/
37   public void visit(Receive receive) {
38     if(!trigger.equals( receive.getReceiver() )) {
39       receive.enter(context);
40     }
41     else if( receive.isCreateInstance() ) {
42       receive.leave(context);
43     }
44     else {
45       throw new RuntimeException JavaDoc( "receive " + receive.getFullyQualifiedName() + " can't create instances" );
46     }
47   }
48
49   /**{@inheritDoc}*/
50   public void visit(Reply reply) {
51     validateNonInitial(reply);
52   }
53
54   /**{@inheritDoc}*/
55   public void visit(Invoke invoke) {
56     validateNonInitial(invoke);
57   }
58
59   /**{@inheritDoc}*/
60   public void visit(Assign assign) {
61     validateNonInitial(assign);
62   }
63
64   /**{@inheritDoc}*/
65   public void visit(Throw throwActivity) {
66     validateNonInitial(throwActivity);
67   }
68
69   /**{@inheritDoc}*/
70   public void visit(Exit exit) {
71     validateNonInitial(exit);
72   }
73
74   /**{@inheritDoc}*/
75   public void visit(Wait wait) {
76     validateNonInitial(wait);
77   }
78
79   /**{@inheritDoc}*/
80   public void visit(Sequence sequence) {
81     ((Activity)sequence.getNodes().get(0)).accept(this);
82   }
83
84   /**{@inheritDoc}*/
85   public void visit(Switch switchActivity) {
86     validateNonInitial(switchActivity);
87   }
88
89   /**{@inheritDoc}*/
90   public void visit(While whileActivity) {
91     validateNonInitial(whileActivity);
92   }
93
94   /**{@inheritDoc}*/
95   public void visit(Pick pick) {
96     Collection JavaDoc onMessages = pick.getOnMessages();
97     
98     if(!onMessages.contains(trigger)) {
99       pick.getStart().enter(context);
100     }
101     else if( pick.isCreateInstance() ){
102       pick.pickPath(context, pick.getOnMessage(trigger));
103     }
104     else {
105       throw new RuntimeException JavaDoc("pick " + pick.getFullyQualifiedName() + " can't create instances");
106     }
107   }
108
109   /**{@inheritDoc}*/
110   public void visit(Flow flow) {
111     Token token = context.getToken();
112     if(flow.createVariableContext()) token = new Token(token, flow.getName());
113     flow.initLinks(token);
114     // phase one: collect all the flow tokens
115
Map JavaDoc flowTokens = flow.createFlowTokens(token);
116     // phase two: visit children with their corresponding execution context
117
Iterator JavaDoc iter = flowTokens.keySet().iterator();
118     //stop spawning the new childs if the parent token is completed abruptly
119
while( iter.hasNext() && !token.hasEnded()) {
120       Activity child = (Activity) iter.next();
121       Token forkedToken = (Token) flowTokens.get(child);
122       ExecutionContext childExecutionContext = new ExecutionContext(forkedToken);
123       child.accept(new ProcessInstanceTrigger(childExecutionContext, trigger));
124     }
125   }
126
127   public void visit(Scope scope) {
128     validateNonInitial(scope);
129   }
130
131   /**{@inheritDoc}*/
132   public void visit(Compensate compensate) {
133     throw new RuntimeException JavaDoc("activity " + compensate.getFullyQualifiedName() + " can't be initial");
134   }
135
136   /**{@inheritDoc}*/
137   public void visit(Rethrow rethrow) {
138     throw new RuntimeException JavaDoc("activity " + rethrow.getFullyQualifiedName() + " can't be initial");
139   }
140   
141   /**{@inheritDoc}*/
142   public void visit(Validate validate) {
143     validateNonInitial(validate);
144   }
145   
146   /**{@inheritDoc}*/
147   private static void validateNonInitial(Activity activity) {
148     if(activity.getTargets().size() == 0) {
149       throw new RuntimeException JavaDoc("activity " + activity.getFullyQualifiedName() + " can't be initial");
150     }
151   }
152 }
Popular Tags