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 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 name, Object value) { 31 if (value==null) throw new RuntimeException ("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 ("variableInstance is null"); 41 if (variableInstance.getName() == null) 42 throw new IllegalArgumentException ("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 getVariable(String name) { 50 Object value = null; 51 VariableInstance variableInstance = getVariableInstance(name); 52 if (variableInstance != null) { 53 value = variableInstance.getValue(); 54 } else { 56 if (token.isRoot()) { 58 value = null; 59 60 } else { value = getParentMap().getVariable(name); 63 } 64 } 65 return value; 66 } 67 68 public void setVariable(String name, Object value) { 69 VariableInstance variableInstance = getVariableInstance(name); 70 if (variableInstance != null) { 72 variableInstance.setValue(value); 73 74 } else { if (token.isRoot()) { 77 createVariableInstance(name, value); 79 80 } else { getParentMap().setVariable(name, value); 83 } 84 } 85 } 86 87 public boolean hasVariable(String name) { 88 boolean hasVariable = false; 89 VariableInstance variableInstance = getVariableInstance(name); 90 if (variableInstance != null) { 92 hasVariable = true; 93 94 } else if (!token.isRoot()) { 96 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 ) entry.getKey(), entry.getValue()); 109 } 110 } 111 } 112 113 public void deleteVariable(String 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 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 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 } 166 | Popular Tags |