Search in sources :

Example 6 with ExpressionFactory

use of javax.el.ExpressionFactory in project sling by apache.

the class PageContextImpl method proprietaryEvaluate.

/**
	 * Proprietary method to evaluate EL expressions. XXX - This method should
	 * go away once the EL interpreter moves out of JSTL and into its own
	 * project. For now, this is necessary because the standard machinery is too
	 * slow.
	 *
	 * @param expression
	 *            The expression to be evaluated
	 * @param expectedType
	 *            The expected resulting type
	 * @param pageContext
	 *            The page context
	 * @param functionMap
	 *            Maps prefix and name to Method
	 * @return The result of the evaluation
	 */
public static Object proprietaryEvaluate(final String expression, final Class expectedType, final PageContext pageContext, final ProtectedFunctionMapper functionMap, final boolean escape) throws ELException {
    Object retValue;
    final ExpressionFactory exprFactory = JspFactory.getDefaultFactory().getJspApplicationContext(pageContext.getServletContext()).getExpressionFactory();
    if (SecurityUtil.isPackageProtectionEnabled()) {
        try {
            retValue = AccessController.doPrivileged(new PrivilegedExceptionAction() {

                public Object run() throws Exception {
                    ELContextImpl ctx = (ELContextImpl) pageContext.getELContext();
                    ctx.setFunctionMapper(new FunctionMapperImpl(functionMap));
                    ValueExpression ve = exprFactory.createValueExpression(ctx, expression, expectedType);
                    return ve.getValue(ctx);
                }
            });
        } catch (PrivilegedActionException ex) {
            Exception realEx = ex.getException();
            if (realEx instanceof ELException) {
                throw (ELException) realEx;
            } else {
                throw new ELException(realEx);
            }
        }
    } else {
        ELContextImpl ctx = (ELContextImpl) pageContext.getELContext();
        ctx.setFunctionMapper(new FunctionMapperImpl(functionMap));
        ValueExpression ve = exprFactory.createValueExpression(ctx, expression, expectedType);
        retValue = ve.getValue(ctx);
    }
    if (escape && retValue != null) {
        retValue = XmlEscape(retValue.toString());
    }
    return retValue;
}
Also used : FunctionMapperImpl(org.apache.sling.scripting.jsp.jasper.el.FunctionMapperImpl) ExpressionFactory(javax.el.ExpressionFactory) PrivilegedActionException(java.security.PrivilegedActionException) ValueExpression(javax.el.ValueExpression) ELContextImpl(org.apache.sling.scripting.jsp.jasper.el.ELContextImpl) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) ELException(javax.servlet.jsp.el.ELException) ServletException(javax.servlet.ServletException) JspException(javax.servlet.jsp.JspException) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) ELException(javax.servlet.jsp.el.ELException) SlingPageException(org.apache.sling.scripting.jsp.SlingPageException)

Example 7 with ExpressionFactory

use of javax.el.ExpressionFactory in project camel by apache.

the class JuelTest method testJuel.

@Test
public void testJuel() throws Exception {
    ExpressionFactory factory = new ExpressionFactoryImpl();
    ELContext context = new SimpleContext();
    ValueExpression valueExpression = factory.createValueExpression(context, "${123 * 2}", Object.class);
    Object value = valueExpression.getValue(context);
    assertEquals("Result is a Long object", 246L, value);
}
Also used : ELContext(javax.el.ELContext) SimpleContext(de.odysseus.el.util.SimpleContext) ExpressionFactory(javax.el.ExpressionFactory) ExpressionFactoryImpl(de.odysseus.el.ExpressionFactoryImpl) ValueExpression(javax.el.ValueExpression) Test(org.junit.Test)

Example 8 with ExpressionFactory

use of javax.el.ExpressionFactory in project camel by apache.

the class DozerEndpoint method initELEngine.

public void initELEngine() {
    String elprop = System.getProperty("javax.el.ExpressionFactory");
    ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    try {
        ClassLoader appcl = getCamelContext().getApplicationContextClassLoader();
        ClassLoader auxcl = appcl != null ? appcl : DozerEndpoint.class.getClassLoader();
        Thread.currentThread().setContextClassLoader(auxcl);
        try {
            Class<?> clazz = auxcl.loadClass("com.sun.el.ExpressionFactoryImpl");
            ExpressionFactory factory = (ExpressionFactory) clazz.newInstance();
            System.setProperty("javax.el.ExpressionFactory", factory.getClass().getName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
            LOG.debug("Cannot load glasfish expression engine, using default");
        }
        ELEngine engine = new ELEngine();
        engine.init();
        BeanContainer.getInstance().setElEngine(engine);
        ElementReader reader = new ExpressionElementReader(engine);
        BeanContainer.getInstance().setElementReader(reader);
    } finally {
        Thread.currentThread().setContextClassLoader(tccl);
        if (elprop != null) {
            System.setProperty("javax.el.ExpressionFactory", elprop);
        } else {
            System.clearProperty("javax.el.ExpressionFactory");
        }
    }
}
Also used : ExpressionFactory(javax.el.ExpressionFactory) ExpressionElementReader(org.dozer.loader.xml.ExpressionElementReader) ELEngine(org.dozer.loader.xml.ELEngine) ElementReader(org.dozer.loader.xml.ElementReader) ExpressionElementReader(org.dozer.loader.xml.ExpressionElementReader)

Example 9 with ExpressionFactory

use of javax.el.ExpressionFactory in project core by weld.

the class ELResolverTest method testResolveBeanPropertyOfProducerBean.

/**
 * Test that the WeldELResolver only works to resolve the base of an EL
 * expression, in this case from a producer method. Once the base is
 * resolved, the remainder of the expression should be delegated to the
 * standard chain of property resolvers. If the WeldELResolver oversteps
 * its bounds by trying to resolve the property against the Weld
 * namespace, the test will fail.
 */
@Test
public void testResolveBeanPropertyOfProducerBean() {
    ELContext elContext = EL.createELContext(beanManager);
    ExpressionFactory exprFactory = EL.EXPRESSION_FACTORY;
    Object value = exprFactory.createValueExpression(elContext, "#{beerOnTap.style}", String.class).getValue(elContext);
    Assert.assertEquals("IPA", value);
}
Also used : ELContext(javax.el.ELContext) ExpressionFactory(javax.el.ExpressionFactory) Test(org.junit.Test)

Example 10 with ExpressionFactory

use of javax.el.ExpressionFactory in project core by weld.

the class WeldApplication method init.

private void init() {
    ExpressionFactory expressionFactory = this.expressionFactory;
    BeanManager beanManager = null;
    if (expressionFactory == null && (expressionFactory = super.getExpressionFactory()) != null && (beanManager = beanManager()) != null) {
        elResolver.beanManagerReady(beanManager);
        this.expressionFactory = beanManager.wrapExpressionFactory(expressionFactory);
    }
}
Also used : ExpressionFactory(javax.el.ExpressionFactory) BeanManager(javax.enterprise.inject.spi.BeanManager)

Aggregations

ExpressionFactory (javax.el.ExpressionFactory)39 ValueExpression (javax.el.ValueExpression)28 Test (org.junit.Test)24 ELContext (javax.el.ELContext)22 ELContextImpl (org.apache.jasper.el.ELContextImpl)15 ELException (javax.el.ELException)4 ValueReference (javax.el.ValueReference)3 IOException (java.io.IOException)2 StringTokenizer (java.util.StringTokenizer)2 BeanManager (javax.enterprise.inject.spi.BeanManager)2 FacesException (javax.faces.FacesException)2 FacesContext (javax.faces.context.FacesContext)2 ExpressionFactoryImpl (de.odysseus.el.ExpressionFactoryImpl)1 SimpleContext (de.odysseus.el.util.SimpleContext)1 PrivilegedActionException (java.security.PrivilegedActionException)1 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ELResolver (javax.el.ELResolver)1 ServletContextEvent (javax.servlet.ServletContextEvent)1