KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jbpm.context.exe;
2
3 import java.io.Serializable JavaDoc;
4 import java.util.Iterator JavaDoc;
5
6 import org.jbpm.db.JbpmSession;
7 import org.jbpm.graph.exe.ProcessInstance;
8 import org.jbpm.graph.exe.Token;
9
10 /**
11  * is a jbpm-internal class that serves as a base class for classes
12  * that store variable values in the database.
13  */

14 public abstract class VariableInstance implements Serializable JavaDoc {
15
16   private static final long serialVersionUID = 1L;
17   
18   long id = 0;
19   protected String JavaDoc name = null;
20   protected Token token = null;
21   protected TokenVariableMap tokenVariableMap = null;
22   protected ProcessInstance processInstance = null;
23   protected Converter converter = null;
24
25   // constructors /////////////////////////////////////////////////////////////
26

27   public VariableInstance() {
28   }
29
30   // public static VariableInstance create(Token token, String name, Object value) {
31
public static VariableInstance create(TokenVariableMap tokenVariableMap, String JavaDoc name, Class JavaDoc valueClass) {
32     Token token = tokenVariableMap.token;
33     ProcessInstance processInstance = (token!=null ? token.getProcessInstance() : null );
34     VariableInstance variableInstance = createVariableInstance(valueClass);
35     variableInstance.tokenVariableMap = tokenVariableMap;
36     variableInstance.token = token;
37     variableInstance.processInstance = processInstance;
38     variableInstance.name = name;
39     return variableInstance;
40   }
41
42   // abstract methods /////////////////////////////////////////////////////////
43

44   /**
45    * is true if this variable-instance supports the given type, false otherwise.
46    */

47   protected abstract boolean supports(Class JavaDoc clazz);
48   /**
49    * is the value, stored by this variable instance.
50    */

51   protected abstract Object JavaDoc getObject();
52   /**
53    * stores the value in this variable instance.
54    */

55   protected abstract void setObject(Object JavaDoc value);
56
57   // variable management //////////////////////////////////////////////////////
58

59   public static VariableInstance createVariableInstance(Class JavaDoc javaType) {
60     VariableInstance variableInstance = null;
61     
62     Iterator JavaDoc iter = JbpmType.getJbpmTypes().iterator();
63     while ( (iter.hasNext())
64             && (variableInstance==null) ){
65       JbpmType jbpmType = (JbpmType) iter.next();
66
67       // isMatch indicates wether the given javaType matches this jbpmType
68
boolean isMatch = false;
69       if ( (jbpmType.variableClass!=null)
70            && (jbpmType.variableClass.isAssignableFrom(javaType)) ) {
71         isMatch = true;
72       } else {
73         if (javaType.getName().equals(jbpmType.variableClassName)) {
74           isMatch = true;
75         } else if ("{serializable-classes}".equals(jbpmType.variableClassName)) {
76           isMatch = (Serializable JavaDoc.class.isAssignableFrom(javaType));
77         } else if ("{hibernateable-long-id-classes}".equals(jbpmType.variableClassName)) {
78           JbpmSession currentJbpmSession = JbpmSession.getCurrentJbpmSession();
79           isMatch = ( (currentJbpmSession!=null)
80                       && (currentJbpmSession.getJbpmSessionFactory().isHibernatableWithLongId(javaType)));
81         } else if ("{hibernateable-string-id-classes}".equals(jbpmType.variableClassName)) {
82           JbpmSession currentJbpmSession = JbpmSession.getCurrentJbpmSession();
83           isMatch = ( (currentJbpmSession!=null)
84                   && (currentJbpmSession.getJbpmSessionFactory().isHibernatableWithStringId(javaType)));
85         }
86       }
87       
88       if (isMatch) {
89         variableInstance = createVariableInstance(jbpmType);
90       }
91     }
92     
93     if (variableInstance==null) {
94       throw new RuntimeException JavaDoc("contents of jbpm.varmapping.properties does not specify how jbpm should store objects of type '"+javaType.getName()+"' in the database");
95     }
96     
97     return variableInstance;
98   }
99
100   private static VariableInstance createVariableInstance(JbpmType jbpmType) {
101     VariableInstance variableInstance;
102     try {
103       variableInstance = (VariableInstance) jbpmType.variableInstanceClass.newInstance();
104       variableInstance.converter = jbpmType.converter;
105     } catch (Exception JavaDoc e) {
106       throw new RuntimeException JavaDoc("couldn't instantiate variable instance of class '"+jbpmType.getClass().getName()+"'", e);
107     }
108     return variableInstance;
109   }
110
111   public void setValue(Object JavaDoc value) {
112     if ( (value!=null)
113          && (converter!=null) ) {
114       if (! converter.supports(value.getClass())) {
115         throw new RuntimeException JavaDoc("the converter '"+converter.getClass().getName()+"' in variable instance '"+this.getClass().getName()+"' does not support values of type '"+value.getClass().getName()+"'. to change the type of a variable, you have to delete it first");
116       }
117       value = converter.convert(value);
118     }
119     if ( (value!=null)
120          && (! this.supports(value.getClass())) ) {
121       throw new RuntimeException JavaDoc("variable instance '"+this.getClass().getName()+"' does not support values of type '"+value.getClass().getName()+"'. to change the type of a variable, you have to delete it first");
122     }
123     setObject(value);
124   }
125
126   public Object JavaDoc getValue() {
127     Object JavaDoc value = getObject();
128     if ( (value!=null)
129          && (converter!=null) ) {
130       value = converter.revert(value);
131     }
132     return value;
133   }
134   
135
136   public void removeTokenVariableMapReference() {
137     this.tokenVariableMap = null;
138     this.token = null;
139     this.processInstance = null;
140   }
141
142
143   // utility methods /////////////////////////////////////////////////////////
144

145   public String JavaDoc toString() {
146     return "${"+name+"}";
147   }
148   
149   // getters and setters //////////////////////////////////////////////////////
150

151   public String JavaDoc getName() {
152     return name;
153   }
154
155   public ProcessInstance getProcessInstance() {
156     return processInstance;
157   }
158
159   public Token getToken() {
160     return token;
161   }
162 }
163
Popular Tags