Skip to content

Instantly share code, notes, and snippets.

@ajjain
Created November 22, 2013 06:40
Show Gist options
  • Save ajjain/7595820 to your computer and use it in GitHub Desktop.
Save ajjain/7595820 to your computer and use it in GitHub Desktop.
Activiti custom serialization of process variables.
<bean id="configurationService"
class="ConfigurationService" init-method="init">
<property name="voVars">
<list>
<bean id="leaveProcVarType" class="..CustomVariableType">
<constructor-arg type="java.lang.String" value="Leave" />
<constructor-arg type="java.lang.Class" value="..Leave" />
</bean>
</list>
</property>
<property name="processEngineConfigurationImpl" ref="processEngineConfiguration" />
</bean>
public class ConfigurationService {
/**
* The process engine configuration impl.
**/
private ProcessEngineConfigurationImpl processEngineConfigurationImpl;
/**
* The Custom vars.
*/
private List<CustomVariableType> CustomVars;
/**
* init method for this service bean
*/
public void init(){
logger.debug("inside init");
for(CustomVariableType varType : CustomVars) {
logger.debug("adding type {}", varType.getTypeName());
if(processEngineConfigurationImpl.getVariableTypes() == null){
processEngineConfigurationImpl.setVariableTypes(new DefaultVariableTypes());
}
processEngineConfigurationImpl.getVariableTypes().addType(varType, 0);
Context.setProcessEngineConfiguration(processEngineConfigurationImpl);
}
}
/**
* @return the processEngineConfigurationImpl
*/
public ProcessEngineConfigurationImpl getProcessEngineConfigurationImpl() {
return processEngineConfigurationImpl;
}
/**
* @param processEngineConfigurationImpl the processEngineConfigurationImpl to set
*/
public Customid setProcessEngineConfigurationImpl(
ProcessEngineConfigurationImpl processEngineConfigurationImpl) {
this.processEngineConfigurationImpl = processEngineConfigurationImpl;
}
/**
* @return the CustomVars
*/
public List<CustomVariableType> getCustomVars() {
return CustomVars;
}
/**
* @param CustomVars the CustomVars to set
*/
public void setCustomVars(List<CustomVariableType> CustomVars) {
this.CustomVars = CustomVars;
}
}
/**
* @author ajjain
*
* Markup interface which represent an item which can be used as process variable
* Serialization and Deserialization of such objects in work list application shall be managed independently.
*/
public interface CustomVar {
}
/**
* @author ajjain
*
* Represents Custom variable type in Activiti. This supports serializing process variables as JSON strings
* as byte arrays.
*/
public class CustomVariableType extends ByteArrayType {
/** The type name. */
protected String typeName;
/** The class. */
protected Class<? extends CustomVar> theClass;
/**
* represents the JSON mapper object
*/
private static final ObjectMapper jsonMapper = new ObjectMapper();
/**
* Instantiates a new view object variable type.
*
* @param typeName the type name
* @param theClass the the class
*/
public CustomVariableType(String typeName, Class<? extends WorklistProcVar> theClass) {
logger.trace("constructing custom variable type name {}, class {}", typeName, theClass);
this.typeName = typeName;
this.theClass = theClass;
}
/**
* @return the typeName
*/
@Override
public String getTypeName() {
return typeName;
}
@Override
public Object getValue(ValueFields valueFields) {
logger.trace("CustomVariableType.getValue valueFields {}", valueFields);
byte[] bytes = (byte[]) super.getValue(valueFields);
Object result;
try {
result = jsonMapper.readValue(bytes, this.getTheClass());
logger.trace("getValue returning object {}", result);
return result;
}
catch (Throwable e) {
logger.warn("error in deserializing the output", e);
throw new ActivitiException("Couldn't deserialize object in variable '"+valueFields.getName()+"'", e);
}
}
@Override
public boolean isCachable() {
return false;
}
@Override
public boolean isAbleToStore(Object value) {
logger.trace("isAbleToStore value {}", value, value instanceof WorklistProcVar);
return (value instanceof WorklistProcVar) && (theClass.isAssignableFrom(value.getClass()));
}
@Override
public void setValue(Object value, ValueFields valueFields) {
byte[] byteArray = serialize(value, valueFields);
super.setValue(byteArray, valueFields);
}
/**
* Serialize.
*
* @param value the value
* @param valueFields the value fields
* @return the byte[]
*/
public static byte[] serialize(Object value, ValueFields valueFields) {
if (value == null) {
return null;
}
try {
byte[] valueBytes = jsonMapper.writeValueAsBytes(value);
return valueBytes;
} catch (Exception e) {
throw new ActivitiException("Couldn't serialize value '"+value+"' in variable '"+valueFields.getName()+"'", e);
}
}
/**
* @return the theClass
*/
public Class<?> getTheClass() {
return theClass;
}
/**
* @param theClass the theClass to set
*/
public void setTheClass(Class<? extends WorklistProcVar> theClass) {
this.theClass = theClass;
}
/**
* @param typeName the typeName to set
*/
public void setTypeName(String typeName) {
this.typeName = typeName;
}
/** The logger. */
private Logger logger = LoggerFactory.getLogger(CustomVariableType.class);
/**
* @return the json mapper
*/
public static ObjectMapper getJsonmapper() {
return jsonMapper;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment