001: /*
002: * JBoss, Home of Professional Open Source
003: * Copyright 2005, JBoss Inc., and individual contributors as indicated
004: * by the @authors tag. See the copyright.txt in the distribution for a
005: * full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jbpm.context.exe;
023:
024: import java.io.Serializable;
025: import java.util.Iterator;
026:
027: import org.jbpm.JbpmException;
028: import org.jbpm.context.exe.variableinstance.NullInstance;
029: import org.jbpm.context.exe.variableinstance.UnpersistableInstance;
030: import org.jbpm.context.log.VariableCreateLog;
031: import org.jbpm.graph.exe.ProcessInstance;
032: import org.jbpm.graph.exe.Token;
033:
034: /**
035: * is a jbpm-internal class that serves as a base class for classes
036: * that store variable values in the database.
037: */
038: public abstract class VariableInstance implements Serializable {
039:
040: private static final long serialVersionUID = 1L;
041:
042: long id = 0;
043: int version = 0;
044: protected String name = null;
045: protected Token token = null;
046: protected TokenVariableMap tokenVariableMap = null;
047: protected ProcessInstance processInstance = null;
048: protected Converter converter = null;
049: protected Object valueCache = null;
050: protected boolean isValueCached = false;
051:
052: // constructors /////////////////////////////////////////////////////////////
053:
054: public VariableInstance() {
055: }
056:
057: public static VariableInstance create(Token token, String name,
058: Object value) {
059:
060: VariableInstance variableInstance = null;
061: if (value == null) {
062: variableInstance = new NullInstance();
063: } else {
064: variableInstance = createVariableInstance(value);
065: }
066:
067: variableInstance.token = token;
068: variableInstance.name = name;
069: variableInstance.processInstance = (token != null ? token
070: .getProcessInstance() : null);
071: if (token != null) {
072: token.addLog(new VariableCreateLog(variableInstance));
073: }
074: variableInstance.setValue(value);
075: return variableInstance;
076: }
077:
078: public static VariableInstance createVariableInstance(Object value) {
079: VariableInstance variableInstance = null;
080:
081: Iterator iter = JbpmType.getJbpmTypes().iterator();
082: while ((iter.hasNext()) && (variableInstance == null)) {
083: JbpmType jbpmType = (JbpmType) iter.next();
084:
085: if (jbpmType.matches(value)) {
086: variableInstance = jbpmType.newVariableInstance();
087: }
088: }
089:
090: if (variableInstance == null) {
091: variableInstance = new UnpersistableInstance();
092: }
093:
094: return variableInstance;
095: }
096:
097: // abstract methods /////////////////////////////////////////////////////////
098:
099: /**
100: * is true if this variable-instance supports the given value, false otherwise.
101: */
102: public abstract boolean isStorable(Object value);
103:
104: /**
105: * is the value, stored by this variable instance.
106: */
107: protected abstract Object getObject();
108:
109: /**
110: * stores the value in this variable instance.
111: */
112: protected abstract void setObject(Object value);
113:
114: // variable management //////////////////////////////////////////////////////
115:
116: public boolean supports(Object value) {
117: if (converter != null) {
118: return converter.supports(value);
119: }
120: return isStorable(value);
121: }
122:
123: public void setValue(Object value) {
124: valueCache = value;
125: isValueCached = true;
126:
127: if (converter != null) {
128: if (!converter.supports(value)) {
129: throw new JbpmException(
130: "the converter '"
131: + converter.getClass().getName()
132: + "' in variable instance '"
133: + this .getClass().getName()
134: + "' does not support values of type '"
135: + value.getClass().getName()
136: + "'. to change the type of a variable, you have to delete it first");
137: }
138: value = converter.convert(value);
139: }
140: if ((value != null) && (!this .isStorable(value))) {
141: throw new JbpmException(
142: "variable instance '"
143: + this .getClass().getName()
144: + "' does not support values of type '"
145: + value.getClass().getName()
146: + "'. to change the type of a variable, you have to delete it first");
147: }
148: setObject(value);
149: }
150:
151: public Object getValue() {
152: if (isValueCached) {
153: return valueCache;
154: }
155: Object value = getObject();
156: if ((value != null) && (converter != null)) {
157: value = converter.revert(value);
158: valueCache = value;
159: isValueCached = true;
160: }
161: return value;
162: }
163:
164: public void removeReferences() {
165: tokenVariableMap = null;
166: token = null;
167: processInstance = null;
168: }
169:
170: // utility methods /////////////////////////////////////////////////////////
171:
172: public String toString() {
173: return "${" + name + "}";
174: }
175:
176: // getters and setters //////////////////////////////////////////////////////
177:
178: public String getName() {
179: return name;
180: }
181:
182: public ProcessInstance getProcessInstance() {
183: return processInstance;
184: }
185:
186: public Token getToken() {
187: return token;
188: }
189:
190: public void setTokenVariableMap(TokenVariableMap tokenVariableMap) {
191: this .tokenVariableMap = tokenVariableMap;
192: }
193:
194: // private static Log log = LogFactory.getLog(VariableInstance.class);
195: }
|