KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jbpm.bpel.def;
2
3 import java.util.HashMap JavaDoc;
4 import java.util.Map JavaDoc;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.w3c.dom.Element JavaDoc;
9
10 import org.jbpm.graph.exe.ExecutionContext;
11 import org.jbpm.graph.exe.Token;
12
13 import org.jbpm.bpel.data.def.VariableDefinition;
14 import org.jbpm.bpel.exe.EventInstance;
15 import org.jbpm.bpel.exe.ScopeInstance;
16 import org.jbpm.bpel.exe.state.ActiveState;
17 import org.jbpm.bpel.service.def.CorrelationSetDefinition;
18 import org.jbpm.bpel.service.def.InboundMessageActivity;
19 import org.jbpm.bpel.service.def.Receiver;
20
21 /**
22  * @author Juan Cantu
23  * @version $Revision: 1.2 $ $Date: 2005/06/16 19:15:38 $
24  */

25 public class MessageHandler extends ScopeHandler implements InboundMessageActivity {
26
27   public MessageHandler() {
28     super();
29   }
30  
31   private static final Log log = LogFactory.getLog(MessageHandler.class);
32   private static final long serialVersionUID = 1L;
33   
34   private Receiver receiver;
35   private VariableDefinition variable;
36   private Map JavaDoc correlationSets = new HashMap JavaDoc();
37   
38   public void messageReceived(Receiver receiver, Element JavaDoc element, Token token) {
39     ScopeInstance scopeInstance = ScopeInstance.get(token);
40     if (!scopeInstance.getState().equals(ActiveState.NORMAL_PROCESSING)) {
41       log.error("Incoming events are only accepted when a scope instance is normally processing");
42       return;
43     }
44     
45     Token child = new Token(token, Scope.EVENT_TOKEN + token.getChildren().size());
46     //initialize event variable
47
receiver.getVariable().createInstance(child);
48     //TODO initialize event correlationSets
49
EventInstance.create(child);
50     // read message into the variable
51
receiver.readMessage(element, child);
52     
53     execute(new ExecutionContext(child));
54   }
55   
56   // CompositeActivity override //////////////////////////////////////////////////////////////
57

58   /**{@inheritDoc}*/
59   public VariableDefinition findVariable(String JavaDoc varName) {
60     return variable != null &&
61            variable.getName().equals(varName) ? variable
62                                               : super.findVariable(varName);
63   }
64   
65   /**{@inheritDoc}*/
66   public CorrelationSetDefinition findCorrelationSet(String JavaDoc csName) {
67     CorrelationSetDefinition cs = getCorrelationSet(csName);
68     return cs != null ? cs : super.findCorrelationSet(csName);
69   }
70
71   // message handler properties /////////////////////////////////////////////////////////////
72

73   public void addCorrelationSet(CorrelationSetDefinition correlation) {
74     correlationSets.put(correlation.getName(), correlation);
75   }
76   
77   /**
78    * Gets the correlation set with the given name
79    * @param correlationSetName the correlation set name
80    * @return a correlation set whose name matches the argument
81    */

82   public CorrelationSetDefinition getCorrelationSet(String JavaDoc correlationSetName) {
83     return (CorrelationSetDefinition) correlationSets.get(correlationSetName);
84   }
85
86   public void setCorrelationSets(Map JavaDoc correlationSets) {
87     this.correlationSets = correlationSets;
88   }
89   
90   public Receiver getReceiver() {
91     return receiver;
92   }
93
94   public void setReceiver(Receiver receiver) {
95     this.receiver = receiver;
96   }
97
98   public VariableDefinition getVariableDefinition() {
99     return variable;
100   }
101   
102   public void setVariableDefinition(VariableDefinition variable) {
103     this.variable = variable;
104   }
105 }
106
Popular Tags