KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > context > exe > TokenVariableMap


1 package org.jbpm.context.exe;
2
3 import java.io.*;
4 import java.util.*;
5 import org.jbpm.context.log.*;
6 import org.jbpm.graph.exe.*;
7
8 /**
9  * is a jbpm-internal map of variables related to one {@link Token}.
10  * Each token has it's own map of variables, thereby creating
11  * hierarchy and scoping of process variables.
12  */

13 public class TokenVariableMap implements Serializable {
14
15   private static final long serialVersionUID = 1L;
16
17   long id = 0;
18   protected Token token = null;
19   protected ContextInstance contextInstance = null;
20   protected Map variableInstances = null;
21
22   public TokenVariableMap() {
23   }
24
25   public TokenVariableMap(Token token, ContextInstance contextInstance) {
26     this.token = token;
27     this.contextInstance = contextInstance;
28   }
29
30   public void createVariableInstance(String JavaDoc name, Object JavaDoc value) {
31     if (value==null) throw new RuntimeException JavaDoc("can't create jbpm process variable '"+name+"' with a null value");
32     VariableInstance variableInstance = VariableInstance.create(this, name, value.getClass());
33     addVariableInstance(variableInstance);
34     token.addLog(new VariableCreateLog(variableInstance));
35     variableInstance.setValue(value);
36   }
37
38   public void addVariableInstance(VariableInstance variableInstance) {
39     if (variableInstance == null)
40       throw new NullPointerException JavaDoc("variableInstance is null");
41     if (variableInstance.getName() == null)
42       throw new IllegalArgumentException JavaDoc("variableInstance does not have a name");
43     if (variableInstances == null) {
44       variableInstances = new HashMap();
45     }
46     variableInstances.put(variableInstance.getName(), variableInstance);
47   }
48
49   public Object JavaDoc getVariable(String JavaDoc name) {
50     Object JavaDoc value = null;
51     VariableInstance variableInstance = getVariableInstance(name);
52     if (variableInstance != null) {
53       value = variableInstance.getValue();
54     } else { // the variable instance is not present in this map
55

56       // if this is the map of the root token
57
if (token.isRoot()) {
58         value = null;
59         
60       } else { // this is a child token
61
// so let's action to the parent token's TokenVariableMap
62
value = getParentMap().getVariable(name);
63       }
64     }
65     return value;
66   }
67
68   public void setVariable(String JavaDoc name, Object JavaDoc value) {
69     VariableInstance variableInstance = getVariableInstance(name);
70     // if the variable instance is present in this map
71
if (variableInstance != null) {
72       variableInstance.setValue(value);
73       
74     } else { // the variable instance is not present in this map
75
// if this is the map of the root token
76
if (token.isRoot()) {
77         // create the variable instance
78
createVariableInstance(name, value);
79         
80       } else { // this is a child token
81
// so let's action to the parent token's TokenVariableMap
82
getParentMap().setVariable(name, value);
83       }
84     }
85   }
86
87   public boolean hasVariable(String JavaDoc name) {
88     boolean hasVariable = false;
89     VariableInstance variableInstance = getVariableInstance(name);
90     // if the variable instance is present in this map
91
if (variableInstance != null) {
92       hasVariable = true;
93       
94     // if this is not the map of the root token
95
} else if (!token.isRoot()) {
96       // this is a child token
97
// so let's action to the parent token's TokenVariableMap
98
hasVariable = getParentMap().hasVariable(name);
99     }
100     return hasVariable;
101   }
102
103   public void addVariables(Map variables) {
104     if (variables!=null) {
105       Iterator iter = variables.entrySet().iterator();
106       while (iter.hasNext()) {
107         Map.Entry entry = (Map.Entry) iter.next();
108         setVariable((String JavaDoc) entry.getKey(), entry.getValue());
109       }
110     }
111   }
112
113   public void deleteVariable(String JavaDoc name) {
114     if (variableInstances!=null) {
115       VariableInstance variableInstance = (VariableInstance) variableInstances.remove(name);
116       if (variableInstance!=null) {
117         variableInstance.removeTokenVariableMapReference();
118         token.addLog(new VariableDeleteLog(variableInstance));
119       }
120     }
121   }
122
123   void collectAllVariables(Map variables) {
124     if (variableInstances != null) {
125       Iterator iter = variableInstances.values().iterator();
126       while (iter.hasNext()) {
127         VariableInstance variableInstance = (VariableInstance) iter.next();
128         variables.put(variableInstance.getName(), variableInstance.getValue());
129       }
130     }
131     if (!token.isRoot()) {
132       getParentMap().collectAllVariables(variables);
133     }
134   }
135
136   VariableInstance getVariableInstance(String JavaDoc name) {
137     VariableInstance variableInstance = null;
138     if (variableInstances != null) {
139       variableInstance = (VariableInstance) variableInstances.get(name);
140     }
141     return variableInstance;
142   }
143
144   TokenVariableMap getParentMap() {
145     Token parentToken = token.getParent();
146     return contextInstance.getTokenVariableMap(parentToken);
147   }
148
149   // getters and setters
150
/////////////////////////////////////////////////////////////////////////////
151

152   public ContextInstance getContextInstance() {
153     return contextInstance;
154   }
155
156   public Token getToken() {
157     return token;
158   }
159
160   public Map getVariableInstances() {
161     return variableInstances;
162   }
163
164   // private static final Log log = LogFactory.getLog(TokenVariableMap.class);
165
}
166
Popular Tags