1 package org.jbpm.context.exe; 2 3 import java.io.Serializable ; 4 import java.util.Iterator ; 5 6 import org.jbpm.db.JbpmSession; 7 import org.jbpm.graph.exe.ProcessInstance; 8 import org.jbpm.graph.exe.Token; 9 10 14 public abstract class VariableInstance implements Serializable { 15 16 private static final long serialVersionUID = 1L; 17 18 long id = 0; 19 protected String name = null; 20 protected Token token = null; 21 protected TokenVariableMap tokenVariableMap = null; 22 protected ProcessInstance processInstance = null; 23 protected Converter converter = null; 24 25 27 public VariableInstance() { 28 } 29 30 public static VariableInstance create(TokenVariableMap tokenVariableMap, String name, Class 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 44 47 protected abstract boolean supports(Class clazz); 48 51 protected abstract Object getObject(); 52 55 protected abstract void setObject(Object value); 56 57 59 public static VariableInstance createVariableInstance(Class javaType) { 60 VariableInstance variableInstance = null; 61 62 Iterator iter = JbpmType.getJbpmTypes().iterator(); 63 while ( (iter.hasNext()) 64 && (variableInstance==null) ){ 65 JbpmType jbpmType = (JbpmType) iter.next(); 66 67 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 .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 ("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 e) { 106 throw new RuntimeException ("couldn't instantiate variable instance of class '"+jbpmType.getClass().getName()+"'", e); 107 } 108 return variableInstance; 109 } 110 111 public void setValue(Object value) { 112 if ( (value!=null) 113 && (converter!=null) ) { 114 if (! converter.supports(value.getClass())) { 115 throw new RuntimeException ("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 ("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 getValue() { 127 Object 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 145 public String toString() { 146 return "${"+name+"}"; 147 } 148 149 151 public String 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 |