0 Replies Latest reply on Nov 5, 2008 10:12 PM by Ashley Westwell

    Using Seam EL outside a web application

    Ashley Westwell Newbie
      Good Afternoon

      I am attempting to use Seam's EL in a standalone application (Non web). I have taken a sample from a blog at following blog http://weblogs.java.net/blog/felipeal/archive/2008/10/adding_el_suppo.html as a starting point. I tried to modify it and use Seams EL API without success.

      Anyone care to comment on what I am doing wrong?

      I am getting the following exception

      javax.el.ELException: Provider org.jboss.seam.el.SeamExpressionFactory could not be instantiated: java.lang.InstantiationException: org.jboss.seam.el.SeamExpressionFactory
           at javax.el.FactoryFinder.newInstance(FactoryFinder.java:69)
           at javax.el.FactoryFinder.find(FactoryFinder.java:152)
           at javax.el.ExpressionFactory.newInstance(ExpressionFactory.java:154)
           at javax.el.ExpressionFactory.newInstance(ExpressionFactory.java:125)
           at com.sandvine.commons.el.test.BlogTest.<clinit>(BlogTest.java:38)
           at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
           at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
           at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
           at java.lang.reflect.Constructor.newInstance(Constructor.java:494)
           at junit.framework.TestSuite.createTest(TestSuite.java:61)
           at junit.framework.TestSuite.addTestMethod(TestSuite.java:283)
           at junit.framework.TestSuite.<init>(TestSuite.java:146)
           at org.junit.internal.runners.JUnit38ClassRunner.<init>(JUnit38ClassRunner.java:67)
           at org.junit.internal.builders.JUnit3Builder.runnerForClass(JUnit3Builder.java:14)
           at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
           at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
           at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
           at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
           at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:26)
           at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:24)
           at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:40)
           at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:30)
           at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:445)
           at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
           at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
           at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
      Caused by: java.lang.InstantiationException: org.jboss.seam.el.SeamExpressionFactory
           at java.lang.Class.newInstance0(Class.java:335)
           at java.lang.Class.newInstance(Class.java:303)
           at javax.el.FactoryFinder.newInstance(FactoryFinder.java:64)
           ... 25 more


      BlogTest.java

      package test;

      import javax.el.ExpressionFactory;
      import javax.el.ValueExpression;

      import junit.framework.TestCase;
      import org.jboss.seam.el.SeamExpressionFactory;

      public class BlogTest extends TestCase {

           public void testDoItdoIt() {
                String elText = "Dude, where is my ${car}?";
                SampleContext context = new SampleContext();
                context.bind("car", "EL tutorial");
                Object convertedText = doSomeMagic(context, elText);
                assertEquals("Dude, where is my EL tutorial?", (String) convertedText);
           }

           private Object doSomeMagic(SampleContext context, String elText) {
                if (factory == null) {
                     throw new RuntimeException(
                               "could not get instance of factory class " + factoryClass);
                }
                ValueExpression converted = factory.createValueExpression(context,
                          elText, Object.class);
                return converted.getValue(context);
           }

           //private static final String factoryClass = "org.apache.el.ExpressionFactoryImpl";
           private static final String factoryClass = "org.jboss.seam.el.SeamExpressionFactory";
           private static final ExpressionFactory factory;

           static {
                System.setProperty("javax.el.ExpressionFactory", factoryClass);
                ExpressionFactory tmpFactory = null;
                try {
                     tmpFactory = ExpressionFactory.newInstance();
                } catch (RuntimeException e) {
                     e.printStackTrace();
                     System.out.println(e.getMessage());
                }
                factory = tmpFactory;
           }

      }


      SampleContext.jav


      package test;

      import java.lang.reflect.Method;
      import java.util.HashMap;
      import java.util.Map;

      import javax.el.BeanELResolver;
      import javax.el.ELContext;
      import javax.el.ELResolver;
      import javax.el.FunctionMapper;
      import javax.el.ValueExpression;
      import javax.el.VariableMapper;

      import org.jboss.el.ValueExpressionLiteral;

      public class SampleContext extends ELContext {

           private final BeanELResolver resolver = new BeanELResolver();
           private final FunctionMapper functionMapper = new NoopFunctionMapper();
           private final VariableMapper variableMapper = new VariableMapperImpl();
           private final Map variables = new HashMap();

           @Override
           public ELResolver getELResolver() {
                return resolver;
           }

           @Override
           public FunctionMapper getFunctionMapper() {
                return functionMapper;
           }

           @Override
           public VariableMapper getVariableMapper() {
                return variableMapper;
           }

           public void bind(String variable, Object obj) {
                variables.put(variable, new ValueExpressionLiteral(obj,
                          Object.class));
           }

           private class VariableMapperImpl extends VariableMapper {
                
                public ValueExpression resolveVariable(String s) {
                     return (ValueExpression) variables.get(s);
                }

                public ValueExpression setVariable(String s, ValueExpression valueExpression) {
                     return (ValueExpression) (variables.put(s, valueExpression));
                }
           }

           private class NoopFunctionMapper extends FunctionMapper {
                public Method resolveFunction(String s, String s1) {
                     return null;
                }
           }

      }