All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.amattioli.workstate.actions.BeanShellTransitionAction Maven / Gradle / Ivy

There is a newer version: 1.0.7
Show newest version
package it.amattioli.workstate.actions;

import java.util.*;
import bsh.Interpreter;
import it.amattioli.workstate.exceptions.*;

public class BeanShellTransitionAction extends AbstractTransitionAction {
	private String code;

	public BeanShellTransitionAction(String code) {
		if (code == null) {
			// TODO: EXCEPTION !!!!!!!!!
		}
		this.code = code.trim();
	}

	private void registerAttributes(Interpreter i, AttributeReader attrs) throws Exception {
		Map attrMap = attrs.getAllAttributes();
		for (Map.Entry curr : attrMap.entrySet()) {
			i.set(curr.getKey(), curr.getValue());
		}
	}

	public void doAction(AttributeReader event, AttributeHandler state) throws WorkflowException {
		try {
			Interpreter i = new Interpreter();
			registerAttributes(i, state);
			registerAttributes(i, event);
			i.eval(code);
			for (String currKey : state.getAllAttributes().keySet()) {
				state.setAttribute(currKey, i.get(currKey));
			}
		} catch (Exception e) {
			throw new WorkflowException("", e);
		}
	}

	public boolean equals(Object o) {
		boolean result = false;
		if (o instanceof BeanShellTransitionAction) {
			BeanShellTransitionAction action = (BeanShellTransitionAction) o;
			result = this.code.equals(action.code);
		}
		return result;
	}

	public String toString() {
		return "{ " + code + " }";
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy