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

nyla.solutions.global.patterns.scripting.SpringSpELScripting Maven / Gradle / Ivy

Go to download

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.Hashtable;
import java.util.Map;

import nyla.solutions.global.exception.SystemException;
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
 * 
 * Sample test cases
 * 
 *
		SpringSpELScripting spelScripting = new SpringSpELScripting();
		//${T(solutions.global.util.Debugger).println('Hello World')}"
		String results = spelScripting.interpret(
		"new nyla.solutions.global.patterns.scripting.SpringSpELScriptingTest().getName()", null);
		
		Assert.assertEquals("SpringSpEL",results );
		
		
		 results = spelScripting.interpret(
					"T(java.util.Calendar).getInstance().getTime().getTime().toString()", null);
		 
		 Assert.assertTrue(results != null && !results.contains("java.util.Calendar"));
		 
		HashMap variables = new HashMap();
		variables.put("hello", "world");
		spelScripting.setVariables(variables);
		
		//get from SpringSpELScriptingTest object
		results = spelScripting.interpret(
				"name", new SpringSpELScriptingTest());
		
		Assert.assertEquals("SpringSpEL",results );
		
		//get from variable
		results = spelScripting.interpret(
				"get('hello')", null);
		
		Assert.assertEquals("world",results );
		
		results = spelScripting.interpret(
				"length().toString()", "world");
		
		Assert.assertEquals("5",results );
 * 
* @author Gregory Green * */ public class SpringSpELScripting implements Scripting { /** * Default constructor */ public SpringSpELScripting() { parser = new SpelExpressionParser(); }//--------------------------------------------- /** * Evaluation the boolean based on the evaluation object */ @SuppressWarnings({ "unchecked" }) public ReturnType interpret(String expression, EvaluationObjectType evaluationObject) { //if(this.evaluationObject == null) // throw new RequiredException // ("this.evaluationObject in SpringExecutable"); try { // Map runtimeVariables = Config.getProperties(); // // if(this.variables != null) // runtimeVariables.putAll(this.variables); Debugger.println(this,"Parsing expression="+expression); //Expression exp = parser.parseExpression(expression,new TemplatedParserContext()); Expression exp = parser.parseExpression(expression); if(evaluationObject != null) { StandardEvaluationContext context = new StandardEvaluationContext(evaluationObject); if(this.variables != null) context.setVariables(this.variables); return (ReturnType)exp.getValue(context); } else { if(this.variables != null) { StandardEvaluationContext context = new StandardEvaluationContext(this.variables); return (ReturnType)exp.getValue(context); } return (ReturnType)exp.getValue(); } //return (ReturnType)exp.getValue(context); } catch (Exception e) { throw new SystemException("expression="+expression+" evaluationObject="+evaluationObject+" \n ERROR:"+e.getMessage(),e); } }//--------------------------------------------- /** * @return the variables */ public Map getVariables() { if(this.variables == null) this.variables = new Hashtable(); return variables; }//--------------------------------------------- /** * @param variables the variables to set */ @SuppressWarnings("unchecked") public void setVariables(Map variables) { this.variables = (Map)variables; }//--------------------------------------------- public static class TemplatedParserContext implements ParserContext { public String getExpressionPrefix() { return "${"; } public String getExpressionSuffix() { return "}"; } public boolean isTemplate() { return true; } }//--------------------------------------------- private Map variables = null; private ExpressionParser parser; }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy