Polishing EL integration.
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package org.springframework.binding.expression.el;
|
||||
|
||||
import javax.el.ELContext;
|
||||
import javax.el.ELResolver;
|
||||
import javax.el.FunctionMapper;
|
||||
import javax.el.VariableMapper;
|
||||
|
||||
/**
|
||||
* A default {@link ELContextFactory} for facilitating use of EL for expression evaluation.
|
||||
* @author Jeremy Grelle
|
||||
*
|
||||
*/
|
||||
public class DefaultELContextFactory implements ELContextFactory {
|
||||
|
||||
/**
|
||||
* Configures and returns a simple EL context to use to parse EL expressions.
|
||||
* @return The configured simple ELContext instance.
|
||||
*/
|
||||
public ELContext getParseTimeELContext() {
|
||||
return new SimpleELContext();
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures and returns a simple EL context to use to evaluate EL expressions on the given base target object.
|
||||
* @return The configured simple ELContext instance.
|
||||
*/
|
||||
public ELContext getEvalTimeELContext(Object target) {
|
||||
return new SimpleELContext(target);
|
||||
}
|
||||
|
||||
private static class SimpleELContext extends ELContext {
|
||||
private DefaultELResolver resolver = new DefaultELResolver();
|
||||
|
||||
public SimpleELContext() {
|
||||
|
||||
}
|
||||
|
||||
public SimpleELContext(Object target) {
|
||||
this.resolver.setTarget(target);
|
||||
}
|
||||
|
||||
public ELResolver getELResolver() {
|
||||
return resolver;
|
||||
}
|
||||
|
||||
public FunctionMapper getFunctionMapper() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public VariableMapper getVariableMapper() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.springframework.binding.expression.el;
|
||||
|
||||
import javax.el.ELContext;
|
||||
import javax.el.ELResolver;
|
||||
|
||||
/**
|
||||
* A factory for creating a EL context object that will be used to evaluate a target object of an EL expression.
|
||||
@@ -10,11 +11,18 @@ import javax.el.ELContext;
|
||||
public interface ELContextFactory {
|
||||
|
||||
/**
|
||||
* Configures and returns a {@link DelegatingELContext} to be used in evaluating EL expressions on the given base
|
||||
* target object.
|
||||
*
|
||||
* @return DelegatingELContext The configured DelegatingELContext instance.
|
||||
* Configures and returns an {@link ELContext} to be used in parsing EL expressions.
|
||||
* @return ELContext The configured ELContext instance for parsing expressions.
|
||||
*/
|
||||
public ELContext getELContext(Object target);
|
||||
public ELContext getParseTimeELContext();
|
||||
|
||||
/**
|
||||
* Configures and returns an {@link ELContext} to be used in evaluating EL expressions on the given base target
|
||||
* object. In certain environments the target will be null and the base object of the expression is expected to be
|
||||
* resolved via the ELContext's {@link ELResolver} chain.
|
||||
* @param target The base object for the expression evaluation.
|
||||
* @return ELContext The configured ELContext instance for evaluating expressions.
|
||||
*/
|
||||
public ELContext getEvalTimeELContext(Object target);
|
||||
|
||||
}
|
||||
@@ -59,7 +59,7 @@ public class ELExpression implements SettableExpression {
|
||||
* @return {@link ELContext} The thread-bound {@link ELContext} instance.
|
||||
*/
|
||||
protected ELContext getELContext(Object target) {
|
||||
ELContext ctx = factory.getELContext(target);
|
||||
ELContext ctx = factory.getEvalTimeELContext(target);
|
||||
return ctx;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,25 +2,18 @@ package org.springframework.binding.expression.el;
|
||||
|
||||
import javax.el.ELContext;
|
||||
import javax.el.ELException;
|
||||
import javax.el.ELResolver;
|
||||
import javax.el.ExpressionFactory;
|
||||
import javax.el.FunctionMapper;
|
||||
import javax.el.VariableMapper;
|
||||
|
||||
import org.jboss.el.ExpressionFactoryImpl;
|
||||
import org.springframework.binding.expression.Expression;
|
||||
import org.springframework.binding.expression.ExpressionParser;
|
||||
import org.springframework.binding.expression.ParserException;
|
||||
import org.springframework.binding.expression.SettableExpression;
|
||||
|
||||
/**
|
||||
* An expression parser that parses EL expressions. Beyond standard EL expression parsing, it makes use of the ability
|
||||
* of the JBoss-EL implementation to parse dynamic method invocations such as foo.bar() (the EL spec currently only
|
||||
* provides out-of-the-box support for functions).
|
||||
*
|
||||
* An expression parser that parses EL expressions.
|
||||
* @author Jeremy Grelle
|
||||
*/
|
||||
public class JBossELExpressionParser implements ExpressionParser {
|
||||
public class ELExpressionParser implements ExpressionParser {
|
||||
|
||||
/**
|
||||
* The expression prefix for deferred EL expressions.
|
||||
@@ -50,12 +43,13 @@ public class JBossELExpressionParser implements ExpressionParser {
|
||||
/**
|
||||
* The ExpressionFactory for constructing EL expressions
|
||||
*/
|
||||
private ExpressionFactory factory = new ExpressionFactoryImpl();
|
||||
private ExpressionFactory expressionFactory;
|
||||
|
||||
/**
|
||||
* Creates a new EL expression parser for standalone usage.
|
||||
*/
|
||||
public JBossELExpressionParser() {
|
||||
public ELExpressionParser(ExpressionFactory expressionFactory) {
|
||||
this.expressionFactory = expressionFactory;
|
||||
this.contextFactory = new DefaultELContextFactory();
|
||||
}
|
||||
|
||||
@@ -64,7 +58,8 @@ public class JBossELExpressionParser implements ExpressionParser {
|
||||
*
|
||||
* @param contextFactory the context factory
|
||||
*/
|
||||
public JBossELExpressionParser(ELContextFactory contextFactory) {
|
||||
public ELExpressionParser(ExpressionFactory expressionFactory, ELContextFactory contextFactory) {
|
||||
this.expressionFactory = expressionFactory;
|
||||
this.contextFactory = contextFactory;
|
||||
}
|
||||
|
||||
@@ -118,43 +113,12 @@ public class JBossELExpressionParser implements ExpressionParser {
|
||||
* @throws ParserException
|
||||
*/
|
||||
protected SettableExpression doParseSettableExpression(String expressionString) throws ParserException {
|
||||
ELContext ctx = contextFactory.getELContext(null);
|
||||
ELContext ctx = contextFactory.getParseTimeELContext();
|
||||
try {
|
||||
return new ELExpression(contextFactory, factory.createValueExpression(ctx, expressionString, Object.class));
|
||||
return new ELExpression(contextFactory, expressionFactory.createValueExpression(ctx, expressionString,
|
||||
Object.class));
|
||||
} catch (ELException ex) {
|
||||
throw new ParserException(expressionString, ex);
|
||||
}
|
||||
}
|
||||
|
||||
static class DefaultELContextFactory implements ELContextFactory {
|
||||
|
||||
/**
|
||||
* Configures and returns a simple EL context to use to evaluate EL expressions on the given base target object.
|
||||
* @return The configured simple ELContext instance.
|
||||
*/
|
||||
public ELContext getELContext(Object target) {
|
||||
return new SimpleELContext(target);
|
||||
}
|
||||
|
||||
private static class SimpleELContext extends ELContext {
|
||||
private DefaultELResolver resolver;
|
||||
|
||||
public SimpleELContext(Object target) {
|
||||
this.resolver = new DefaultELResolver();
|
||||
this.resolver.setTarget(target);
|
||||
}
|
||||
|
||||
public ELResolver getELResolver() {
|
||||
return resolver;
|
||||
}
|
||||
|
||||
public FunctionMapper getFunctionMapper() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public VariableMapper getVariableMapper() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,8 @@ import java.util.Map;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
import org.easymock.EasyMock;
|
||||
import org.jboss.el.ExpressionFactoryImpl;
|
||||
import org.springframework.binding.expression.Expression;
|
||||
import org.springframework.binding.expression.support.TestBean;
|
||||
import org.springframework.binding.expression.support.TestMethods;
|
||||
@@ -14,22 +15,20 @@ import org.springframework.binding.expression.support.TestMethods;
|
||||
* Tests to exercise the extended method invoking expression extensions of JBoss-el.
|
||||
* @author Jeremy Grelle
|
||||
*/
|
||||
public class JBossELExpressionParserTests extends TestCase {
|
||||
public class ELExpressionParserTests extends TestCase {
|
||||
|
||||
JBossELExpressionParser parser = new JBossELExpressionParser();
|
||||
ELExpressionParser parser = new ELExpressionParser(new ExpressionFactoryImpl());
|
||||
|
||||
Map context;
|
||||
|
||||
Map container;
|
||||
|
||||
TestMethods target;
|
||||
MockControl targetMockControl;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
context = new HashMap();
|
||||
container = new HashMap();
|
||||
targetMockControl = MockControl.createControl(TestMethods.class);
|
||||
target = (TestMethods) targetMockControl.getMock();
|
||||
target = (TestMethods) EasyMock.createMock(TestMethods.class);
|
||||
context.put("container", container);
|
||||
container.put("myObject", target);
|
||||
}
|
||||
@@ -39,10 +38,10 @@ public class JBossELExpressionParserTests extends TestCase {
|
||||
int param = 5;
|
||||
container.put("param1", new Integer(param));
|
||||
target.doSomethingWithInt(param);
|
||||
targetMockControl.replay();
|
||||
EasyMock.replay(new Object[] { target });
|
||||
|
||||
parser.parseExpression(expression).evaluate(context, null);
|
||||
targetMockControl.verify();
|
||||
EasyMock.verify(new Object[] { target });
|
||||
|
||||
}
|
||||
|
||||
@@ -51,11 +50,11 @@ public class JBossELExpressionParserTests extends TestCase {
|
||||
String expression = "#{container.myObject.returnStringFromInt(container.param1)}";
|
||||
int param = 5;
|
||||
container.put("param1", new Integer(param));
|
||||
targetMockControl.expectAndReturn(target.returnStringFromInt(param), expected);
|
||||
targetMockControl.replay();
|
||||
EasyMock.expect(target.returnStringFromInt(param)).andReturn(expected);
|
||||
EasyMock.replay(new Object[] { target });
|
||||
|
||||
String result = (String) parser.parseExpression(expression).evaluate(context, null);
|
||||
targetMockControl.verify();
|
||||
EasyMock.verify(new Object[] { target });
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@@ -66,11 +65,11 @@ public class JBossELExpressionParserTests extends TestCase {
|
||||
container.put("param1", new Integer(param1));
|
||||
TestBean param2 = new TestBean();
|
||||
container.put("param2", param2);
|
||||
targetMockControl.expectAndReturn(target.returnStringFromIntAndObject(param1, param2), expected);
|
||||
targetMockControl.replay();
|
||||
EasyMock.expect(target.returnStringFromIntAndObject(param1, param2)).andReturn(expected);
|
||||
EasyMock.replay(new Object[] { target });
|
||||
|
||||
String result = (String) parser.parseExpression(expression).evaluate(context, null);
|
||||
targetMockControl.verify();
|
||||
EasyMock.verify(new Object[] { target });
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@@ -18,10 +18,11 @@ package org.springframework.binding.expression.support;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.jboss.el.ExpressionFactoryImpl;
|
||||
import org.springframework.binding.expression.EvaluationException;
|
||||
import org.springframework.binding.expression.ExpressionParser;
|
||||
import org.springframework.binding.expression.ParserException;
|
||||
import org.springframework.binding.expression.el.JBossELExpressionParser;
|
||||
import org.springframework.binding.expression.el.ELExpressionParser;
|
||||
import org.springframework.binding.expression.ognl.OgnlExpressionParser;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
@@ -48,9 +49,12 @@ public class SimpleExpressionTests extends TestCase {
|
||||
suite.addTest(new SimpleExpressionTests("testGetValue", new BeanWrapperExpressionParser(), "$"));
|
||||
suite.addTest(new SimpleExpressionTests("testSetValue", new BeanWrapperExpressionParser(), "$"));
|
||||
suite.addTest(new SimpleExpressionTests("testSyntaxError", new BeanWrapperExpressionParser(), "$"));
|
||||
suite.addTest(new SimpleExpressionTests("testGetValue", new JBossELExpressionParser(), "#"));
|
||||
suite.addTest(new SimpleExpressionTests("testSetValue", new JBossELExpressionParser(), "#"));
|
||||
suite.addTest(new SimpleExpressionTests("testSyntaxError", new JBossELExpressionParser(), "#"));
|
||||
suite.addTest(new SimpleExpressionTests("testGetValue", new ELExpressionParser(new ExpressionFactoryImpl()),
|
||||
"#"));
|
||||
suite.addTest(new SimpleExpressionTests("testSetValue", new ELExpressionParser(new ExpressionFactoryImpl()),
|
||||
"#"));
|
||||
suite.addTest(new SimpleExpressionTests("testSyntaxError", new ELExpressionParser(new ExpressionFactoryImpl()),
|
||||
"#"));
|
||||
return suite;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user