KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jbpm.bpel.def;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6
7 import org.w3c.dom.Element JavaDoc;
8
9 import org.jbpm.graph.def.Transition;
10 import org.jbpm.graph.exe.ExecutionContext;
11 import org.jbpm.graph.exe.Token;
12
13 import org.jbpm.bpel.service.def.InboundMessageActivity;
14 import org.jbpm.bpel.service.def.Receiver;
15 import org.jbpm.bpel.timer.def.Alarm;
16 import org.jbpm.bpel.timer.def.AlarmListener;
17
18 /**
19  * The pick activity awaits the occurrence of one of a set of events and then performs
20  * the activity associated with the event that occurred.
21  * @see "WS-BPEL 2.0 §12.4"
22  * @author Juan Cantú
23  * @version $Revision: 1.7 $ $Date: 2005/06/03 00:13:42 $
24  */

25 public class Pick extends StructuredActivity implements AlarmListener, InboundMessageActivity {
26
27   private static final long serialVersionUID = 1L;
28
29   private boolean createInstance;
30   private List JavaDoc onMessages = new ArrayList JavaDoc();
31   private List JavaDoc onAlarms = new ArrayList JavaDoc();
32
33   public Pick() {
34   }
35   
36   public Pick(String JavaDoc name) {
37     super(name);
38   }
39   
40   // behaviour methods
41
/////////////////////////////////////////////////////////////////////////////
42

43   public void execute(ExecutionContext context) {
44     receiveRequests(context.getToken());
45   }
46   
47   public void messageReceived(Receiver receiver, Element JavaDoc element, Token token) {
48     if (getStart() != token.getNode()) {
49       throw new AssertionError JavaDoc("token must be located at pick start: " +
50           "token=" + token + ", node=" + token.getNode());
51     }
52     closeReceivers(token);
53     receiver.readMessage(element, token);
54     pickPath(new ExecutionContext(token), getOnMessage(receiver));
55   }
56   
57   public void alarmTriggered(Token token, Alarm alarm) {
58     if (getStart() != token.getNode()) {
59       throw new AssertionError JavaDoc("token must be located at pick start: " +
60           "token=" + token + ", node=" + token.getNode());
61     }
62     closeReceivers(token);
63     pickPath(new ExecutionContext(token), getOnAlarm(alarm));
64   }
65  
66   public void receiveRequests(Token token) {
67     for(Iterator JavaDoc it = onMessages.iterator(); it.hasNext();) {
68       ((Receiver) it.next()).receive(token);
69     }
70   }
71   
72   public void closeReceivers(Token token) {
73     for(Iterator JavaDoc it = onMessages.iterator(); it.hasNext();) {
74       ((Receiver) it.next()).close(token);
75     }
76   }
77   
78   protected void pickPath(ExecutionContext context, Activity activity) {
79     for(Iterator JavaDoc it = nodes.iterator(); it.hasNext();) {
80       Activity anActivity = (Activity) it.next();
81       if(!activity.equals(anActivity)) {
82         anActivity.setNegativeLinkStatus(context.getToken());
83       }
84     }
85     
86     Transition transition = activity.getDefaultArrivingTransition();
87     getStart().leave(context, transition);
88   }
89   
90   // children management /////////////////////////////////////////////////////////////////////////////
91

92   public void attachChild(Activity activity) {
93     super.attachChild(activity);
94     onMessages.add(null);
95     int alarms = onAlarms.size();
96     
97     //if alarms exist, move the activity at the last receptor index
98
if(alarms > 0) {
99       int receptorIndex = onMessages.size() - 1;
100       nodes.remove(receptorIndex + alarms);
101       nodes.add(receptorIndex, activity);
102     }
103   }
104   
105   public void detachChild(Activity activity) {
106     int index = nodes.indexOf(activity);
107     if(index < onMessages.size()) {
108       onMessages.remove(index);
109     }
110     else {
111       onAlarms.remove(index - onMessages.size());
112     }
113     super.detachChild(activity);
114   }
115   
116   // event properties /////////////////////////////////////////////////////////////////////////////
117

118   public List JavaDoc getOnMessages() {
119     return onMessages;
120   }
121   
122   public Activity getOnMessage(Receiver receiver) {
123     return (Activity) nodes.get(onMessages.indexOf(receiver));
124   }
125   
126   public void setOnMessage(Activity activity, Receiver receiver) {
127     int currentPosition = nodes.indexOf(activity);
128     
129     if(currentPosition < onMessages.size()) {
130       onMessages.set(currentPosition, receiver);
131     }
132     else {
133       onAlarms.remove(currentPosition - onMessages.size());
134       nodes.remove(currentPosition);
135       nodes.add(onMessages.size(), activity);
136       onMessages.add(receiver);
137     }
138   }
139   
140   public List JavaDoc getOnAlarms() {
141     return onAlarms;
142   }
143   
144   public Activity getOnAlarm(Alarm alarm) {
145     return (Activity) nodes.get(onMessages.size() + onAlarms.indexOf(alarm));
146   }
147   
148   public void setOnAlarm(Activity activity, Alarm alarm) {
149     int currentPosition = nodes.indexOf(activity);
150     
151     if(currentPosition >= onMessages.size()) {
152       int alarmIndex = currentPosition - onMessages.size();
153       onAlarms.set(alarmIndex, alarm);
154     }
155     else {
156       onMessages.remove(currentPosition);
157       onAlarms.add(alarm);
158       nodes.remove(currentPosition);
159       nodes.add(activity);
160     }
161   }
162   
163   // Pick properties
164
/////////////////////////////////////////////////////////////////////////////
165

166   public boolean isCreateInstance() {
167     return createInstance;
168   }
169   
170   public void setCreateInstance(boolean createInstance) {
171     this.createInstance = createInstance;
172   }
173
174   /**{@inheritDoc}*/
175   public void accept(BpelVisitor visitor) {
176     visitor.visit(this);
177   }
178 }
Popular Tags