nyla.solutions.global.patterns.scripting.SpringExecutable Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nyla.solutions.global Show documentation
Show all versions of nyla.solutions.global Show documentation
Nyla Solutions Global Java API provides support for basic application
utilities (application configuration, data encryption, debugger and text
processing).
The newest version!
package nyla.solutions.global.patterns.scripting;
import java.util.Map;
import nyla.solutions.global.exception.SystemException;
import nyla.solutions.global.patterns.command.Environment;
import nyla.solutions.global.patterns.command.Executable;
import nyla.solutions.global.util.Config;
import nyla.solutions.global.util.Debugger;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.ParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
/**
*
* Execute a spring expression language SPEL
*
* <bean id="cacheProcessDocSummary"
class="solutions.global.patterns.scripting">
<property name="expression"
value="${T(solutions.global.util.Debugger).println('Hello World')}">
</property>
</bean>
<bean id="mkdirPrintingExecutable"
class="com.merck.mrl.global.patterns.scripting.SpringExecutable">
<property name="expression"
value="new java.io.File(${T(solutions.global.util.Config).getProperty('solutions.global.media.RotateImageFileCommand.outputPath')}).mkdir()">
</property>
</bean>
*
* @author Gregory Green
*
*/
public class SpringExecutable implements Executable
{
/**
* Default constructor
*/
public SpringExecutable()
{
parser = new SpelExpressionParser();
}//---------------------------------------------
/**
* Evaluation the boolean based on the evaluation object
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public Integer execute(Environment env)
{
try
{
StandardEvaluationContext context = new StandardEvaluationContext(this.evaluationObject);
Map runtimeVariables = Config.getProperties();
//Map runtimeVariables = new Hashtable();
if(env != null)
{
runtimeVariables.putAll(env.getMap());
}
if(this.variables != null)
runtimeVariables.putAll(this.variables);
//Set variables
context.setVariables(runtimeVariables);
Debugger.println(this,"Parsing expression="+expression);
Expression expression = parser.parseExpression(this.expression,new TemplatedParserContext());
Object object = expression.getValue(context);
if(env != null && valueKey != null && object != null)
{
//put results in environment variable
env.put(valueKey,object);
}
return 0;
}
catch (Exception e)
{
throw new SystemException("expression="+expression+" evaluationObject="+evaluationObject+" \n ERROR:"+Debugger.stackTrace(e));
}
}//---------------------------------------------
/**
* @return the evaluationObject
*/
public Object getEvaluationObject()
{
return evaluationObject;
}//---------------------------------------------
/**
* @param evaluationObject the evaluationObject to set
*/
public void setEvaluationObject(Object evaluationObject)
{
this.evaluationObject = evaluationObject;
}//---------------------------------------------
/**
* @return the expression
*/
public String getExpression()
{
return expression;
}//---------------------------------------------
/**
* @param expression the expression to set
*/
public void setExpression(String expression)
{
this.expression = expression;
}//---------------------------------------------
/**
* @return the variables
*/
public Map