KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jbpm.bpel.def;
2
3 import java.util.*;
4 import java.util.Collection JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Map JavaDoc;
8
9 import org.jbpm.graph.def.Node;
10 import org.jbpm.graph.def.ProcessDefinition;
11 import org.jbpm.graph.def.Transition;
12 import org.jbpm.graph.exe.ExecutionContext;
13 import org.jbpm.graph.exe.Token;
14
15 import org.jbpm.bpel.data.def.Snippet;
16
17 /**
18  * Represents a BPEL structured activity.
19  * @see "WS-BPEL 2.0 §12"
20  * @author Juan Cantú
21  * @version $Revision: 1.7 $ $Date: 2005/06/03 00:13:42 $
22  */

23 public abstract class StructuredActivity extends CompositeActivity {
24   private transient Map JavaDoc nodesMap = null;
25   protected List JavaDoc nodes;
26   protected StructureStart start;
27   protected StructureEnd end;
28   
29   public StructuredActivity() {
30     start = new StructureStart(this);
31     end = new StructureEnd(this);
32   }
33   
34   public StructuredActivity(String JavaDoc name) {
35     super(name);
36     start = new StructureStart(this);
37     end = new StructureEnd(this);
38   }
39   
40   // behaviour methods /////////////////////////////////////////////////////////////////////////////
41

42   public void enter(ExecutionContext token) {
43     start.enter(token);
44   }
45   
46   public void execute(ExecutionContext context) {
47     start.leave(context, start.getDefaultLeavingTransition());
48   }
49   
50   public void leave(ExecutionContext token) {
51     end.leave(token);
52   }
53   
54   public void skip(ExecutionContext context) {
55     setNegativeLinkStatus(context.getToken());
56     end.leave(context, getDefaultLeavingTransition());
57   }
58   
59   public void setNegativeLinkStatus(Token token) {
60     for(Iterator JavaDoc it = getNodes().iterator(); it.hasNext();) {
61       ((Activity)it.next()).setNegativeLinkStatus(token);
62     }
63     
64     end.setNegativeLinkStatus(token);
65   }
66
67   // structure delimiters /////////////////////////////////////////////////////////////////////////////
68

69   public StructureStart getStart() {
70     return start;
71   }
72   
73   public StructureEnd getEnd() {
74     return end;
75   }
76
77   // children management /////////////////////////////////////////////////////////////////////////////
78

79   protected void attachChild(Activity activity) {
80     addImplicitTransitions(activity);
81     
82     if (nodes == null) nodes = new ArrayList();
83     nodes.add(activity);
84     activity.compositeActivity = this;
85     nodesMap = null;
86   }
87   
88   protected void detachChild(Activity activity) {
89     removeImplicitTransitions(activity);
90     if (nodes.remove(activity)) {
91       activity.compositeActivity = null;
92       nodesMap = null;
93     }
94   }
95   
96   // javadoc description in NodeCollection
97
public Node addNode(Node node) {
98     if (node == null) throw new IllegalArgumentException JavaDoc("can't add a null activity to a composite activity");
99     attachChild((Activity) node);
100     return node;
101   }
102
103   // javadoc description in NodeCollection
104
public Node removeNode(Node node) {
105     if(node == null) throw new IllegalArgumentException JavaDoc("can't remove a null activity from a composite activity");
106     if( nodes == null || !nodes.contains(node) ) return null;
107     detachChild((Activity) node);
108     return node;
109   }
110   
111   // javadoc description in NodeCollection
112
public void reorderNode(int oldIndex, int newIndex) {
113     if (nodes!=null) {
114       Object JavaDoc reorderedObject = nodes.remove(oldIndex);
115       nodes.add(newIndex, reorderedObject);
116     } else {
117       throw new IndexOutOfBoundsException JavaDoc("no collection present");
118     }
119   }
120   
121   // javadoc description in NodeCollection
122
public List JavaDoc getNodes() {
123     return nodes;
124   }
125
126   // javadoc description in NodeCollection
127
public Map JavaDoc getNodesMap() {
128     if ( (nodesMap==null)
129          && (nodes!=null) ) {
130       nodesMap = new HashMap();
131       Iterator JavaDoc iter = nodes.iterator();
132       while (iter.hasNext()) {
133         Node node = (Node) iter.next();
134         nodesMap.put(node.getName(),node);
135       }
136     }
137     return nodesMap;
138   }
139
140   // javadoc description in NodeCollection
141
public Node getNode(String JavaDoc name) {
142     return (Node) getNodesMap().get(name);
143   }
144
145   // javadoc description in NodeCollection
146
public boolean hasNode(String JavaDoc name) {
147     return getNodesMap().containsKey(name);
148   }
149
150   // javadoc description in NodeCollection
151
public String JavaDoc generateNodeName() {
152     return ProcessDefinition.generateNodeName(nodes);
153   }
154
155   // javadoc description in NodeCollection
156
public Node findNode(String JavaDoc hierarchicalName) {
157     return ProcessDefinition.findNode(this, hierarchicalName);
158   }
159   
160   protected void addImplicitTransitions(Activity activity) {
161     //the default behavior is to connect the activity to the structure delimiters
162
start.connect(activity);
163     activity.connect(end);
164   }
165   
166   protected void removeImplicitTransitions(Activity activity) {
167     //the default behavior is to remove the activity incoming and outgoing transitions
168
start.disconnect(activity);
169     activity.disconnect(end);
170   }
171
172   // bpel activity standard attributes and elements /////////////////////////////////////////////////////////////////////////////
173

174   public Snippet getJoinCondition() {
175     return start != null ? start.getJoinCondition() : null;
176   }
177   
178   public void setJoinCondition(Snippet joinCondition) {
179     if(start != null) {
180       start.setJoinCondition(joinCondition);
181     }
182   }
183   
184   public Collection JavaDoc getSources() {
185     return end.getSources();
186   }
187   
188   public void addSource(Link link) {
189     end.addSource(link);
190   }
191   
192   public Link getSource(String JavaDoc linkName) {
193     return end.getSource(linkName);
194   }
195   
196   public Link getTarget(String JavaDoc linkName) {
197     return start != null ? start.getTarget(linkName) : null;
198   }
199   
200   public Collection JavaDoc getTargets() {
201     return start != null ? start.getTargets() : null;
202   }
203   
204   public void addTarget(Link link) {
205     start.addTarget(link);
206   }
207   
208   // node properties /////////////////////////////////////////////////////////////////////////////
209

210   public Transition getDefaultArrivingTransition() {
211     return start != null ? start.getDefaultArrivingTransition() : null;
212   }
213   
214   public Transition getDefaultLeavingTransition() {
215     return end != null ? end.getDefaultLeavingTransition() : null;
216   }
217   
218   public Transition addArrivingTransition(Transition transition) {
219     if(start != null) {
220       start.addArrivingTransition(transition);
221     }
222     
223     return transition;
224   }
225   
226   public Transition addLeavingTransition(Transition transition) {
227     end.addLeavingTransition(transition);
228     return transition;
229   }
230   
231   /**
232    * Handles the incoming flow of execution of structured activities.
233    */

234   public static class StructureStart extends Activity {
235     private static final long serialVersionUID = 1L;
236     private StructuredActivity startStructure;
237     
238     StructureStart() {
239     }
240     
241     public StructureStart(StructuredActivity activity) {
242       startStructure = activity;
243     }
244     
245     public void leave(ExecutionContext context) {
246       getCompositeActivity().execute(context);
247     }
248     
249     public void skip(ExecutionContext context) {
250       getCompositeActivity().skip(context);
251     }
252     
253     public CompositeActivity getCompositeActivity() {
254       return startStructure;
255     }
256     
257     public String JavaDoc getName() {
258       return startStructure.getName();
259     }
260   }
261  
262   /**
263    * Handles the outgoing flow of execution of structured activities.
264    */

265   public static class StructureEnd extends Activity {
266     private static final long serialVersionUID = 1L;
267     private StructuredActivity endStructure;
268     
269     StructureEnd() {
270     }
271     
272     public StructureEnd(StructuredActivity activity) {
273       endStructure = activity;
274     }
275     
276     public void execute(ExecutionContext context) {
277       ((Activity)getParent()).leave(context);
278     }
279     
280     public CompositeActivity getCompositeActivity() {
281       return endStructure;
282     }
283     
284     public String JavaDoc getName() {
285       return endStructure.getName();
286     }
287   }
288   
289 }
290
Popular Tags