[jbpm-commits] JBoss JBPM SVN: r4309 - in jbpm4/trunk/modules: examples/src/test/resources and 2 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Mon Mar 23 10:18:53 EDT 2009


Author: heiko.braun at jboss.com
Date: 2009-03-23 10:18:52 -0400 (Mon, 23 Mar 2009)
New Revision: 4309

Added:
   jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/script/JuelScriptEngine.java
   jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/script/JuelScriptEngineFactory.java
Modified:
   jbpm4/trunk/modules/distro/src/main/resources/config-tool/jbpm/cfg/part3.jbpm.cfg.xml
   jbpm4/trunk/modules/examples/src/test/resources/jbpm.cfg.xml
   jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/script/ScriptManager.java
   jbpm4/trunk/modules/pvm/src/test/resources/jbpm.cfg.xml
Log:
JBPM-2120:  javax.el.* API conflict in AS 5

Modified: jbpm4/trunk/modules/distro/src/main/resources/config-tool/jbpm/cfg/part3.jbpm.cfg.xml
===================================================================
--- jbpm4/trunk/modules/distro/src/main/resources/config-tool/jbpm/cfg/part3.jbpm.cfg.xml	2009-03-23 10:13:16 UTC (rev 4308)
+++ jbpm4/trunk/modules/distro/src/main/resources/config-tool/jbpm/cfg/part3.jbpm.cfg.xml	2009-03-23 14:18:52 UTC (rev 4309)
@@ -7,7 +7,7 @@
                     default-script-language="juel"
                     read-contexts="execution, environment, process-engine"
                     write-context="">
-        <script-language name="juel" factory="com.sun.script.juel.JuelScriptEngineFactory" />
+        <script-language name="juel" factory="org.jbpm.pvm.internal.script.JuelScriptEngineFactory" />
     </script-manager>
     
     <authentication />

Modified: jbpm4/trunk/modules/examples/src/test/resources/jbpm.cfg.xml
===================================================================
--- jbpm4/trunk/modules/examples/src/test/resources/jbpm.cfg.xml	2009-03-23 10:13:16 UTC (rev 4308)
+++ jbpm4/trunk/modules/examples/src/test/resources/jbpm.cfg.xml	2009-03-23 14:18:52 UTC (rev 4309)
@@ -38,7 +38,7 @@
                     default-script-language="juel"
                     read-contexts="execution, environment, process-engine"
                     write-context="">
-        <script-language name="juel" factory="com.sun.script.juel.JuelScriptEngineFactory" />
+        <script-language name="juel" factory="org.jbpm.pvm.internal.script.JuelScriptEngineFactory" />
     </script-manager>
     
     <authentication />

Added: jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/script/JuelScriptEngine.java
===================================================================
--- jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/script/JuelScriptEngine.java	                        (rev 0)
+++ jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/script/JuelScriptEngine.java	2009-03-23 14:18:52 UTC (rev 4309)
@@ -0,0 +1,387 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jbpm.pvm.internal.script;
+
+import de.odysseus.el.util.SimpleResolver;
+
+import javax.el.*;
+import javax.script.*;
+import java.io.IOException;
+import java.io.Reader;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+
+class JuelScriptEngine extends AbstractScriptEngine
+    implements Compilable
+{
+  private ScriptEngineFactory factory;
+  private ExpressionFactory exprFactory;
+
+  public JuelScriptEngine(ScriptEngineFactory factory)
+  {
+    this.factory = factory;
+    this.exprFactory = new de.odysseus.el.ExpressionFactoryImpl();
+  }
+
+  public CompiledScript compile(String script)
+      throws ScriptException
+  {
+    ValueExpression expr = parse(script, this.context);
+
+    return new JuelCompiledScript(this, expr);
+  }
+
+  public CompiledScript compile(Reader reader)
+      throws ScriptException
+  {
+    return compile(readFully(reader));
+  }
+
+  public Object eval(String script, ScriptContext ctx)
+      throws ScriptException
+  {
+    ValueExpression expr = parse(script, ctx);
+
+    return evalExpr(expr, ctx);
+  }
+
+  public Object eval(Reader reader, ScriptContext ctx)
+      throws ScriptException
+  {
+    return eval(readFully(reader), ctx);
+  }
+
+  public ScriptEngineFactory getFactory()
+  {
+    synchronized (this)
+    {
+      if (this.factory == null)
+      {
+        this.factory = new JuelScriptEngineFactory();
+      }
+
+    }
+
+    return this.factory;
+  }
+
+  public Bindings createBindings()
+  {
+    return new SimpleBindings();
+  }
+
+  private ELContext toELContext(final ScriptContext ctx)
+  {
+    Object tmp = ctx.getAttribute("elcontext");
+
+    if (tmp instanceof ELContext)
+    {
+      return ((ELContext)tmp);
+    }
+
+    ctx.setAttribute("context", ctx, 100);
+
+    ctx.setAttribute("out:print", getPrintMethod(), 100);
+
+    SecurityManager manager = System.getSecurityManager();
+
+    if (manager == null)
+    {
+      ctx.setAttribute("lang:import", getImportMethod(), 100);
+    }
+
+    ELContext elContext = new ELContext()
+    {
+      ELResolver resolver = makeResolver();
+      VariableMapper varMapper = new ScriptContextVariableMapper(ctx);
+      FunctionMapper funcMapper = new ScriptContextFunctionMapper(ctx);
+
+      public ELResolver getELResolver()
+      {
+        return this.resolver;
+      }
+
+      public VariableMapper getVariableMapper()
+      {
+        return this.varMapper;
+      }
+
+      public FunctionMapper getFunctionMapper()
+      {
+        return this.funcMapper;
+      }
+
+    };
+    
+    ctx.setAttribute("elcontext", elContext, 100);
+
+    return elContext;
+  }
+
+  private ELResolver makeResolver()
+  {
+    CompositeELResolver chain = new CompositeELResolver();
+
+    chain.add(new ArrayELResolver());
+
+    chain.add(new ListELResolver());
+
+    chain.add(new MapELResolver());
+
+    chain.add(new ResourceBundleELResolver());
+
+    chain.add(new BeanELResolver());
+
+    return new SimpleResolver(chain);
+  }
+
+  private ValueExpression parse(String script, ScriptContext ctx)
+      throws ScriptException
+  {
+    try
+    {
+      return this.exprFactory.createValueExpression(
+          toELContext(ctx), script, Object.class);
+    }
+    catch (ELException elexp)
+    {
+      throw new ScriptException(elexp);
+    }
+  }
+
+  private Object evalExpr(ValueExpression expr, ScriptContext ctx)
+      throws ScriptException
+  {
+    try
+    {
+      return expr.getValue(toELContext(ctx));
+    }
+    catch (ELException elexp)
+    {
+      throw new ScriptException(elexp);
+    }
+  }
+
+  private String readFully(Reader reader)
+      throws ScriptException
+  {
+    int numChars;
+    char[] arr = new char[8192];
+
+    StringBuilder buf = new StringBuilder();
+    try
+    {
+      while ((numChars = reader.read(arr, 0, arr.length)) > 0)
+      {
+        buf.append(arr, 0, numChars);
+      }
+
+    }
+    catch (IOException exp)
+    {
+      throw new ScriptException(exp);
+    }
+
+    return buf.toString();
+  }
+
+  private static Method getPrintMethod()
+  {
+    Class myClass;
+    try
+    {
+      myClass = JuelScriptEngine.class;
+
+      Method method = myClass.getMethod(
+          "print", new Class[] { Object.class });
+
+      return method;
+    }
+    catch (Exception exp)
+    {
+    }
+
+    return null;
+  }
+
+  public static void print(Object obj)
+  {
+    System.out.print(obj);
+  }
+
+  private static Method getImportMethod()
+  {
+    Class myClass;
+    try
+    {
+      myClass = JuelScriptEngine.class;
+
+      Method method = myClass.getMethod(
+          "importFunctions",
+          new Class[] { ScriptContext.class,
+              String.class,
+              Object.class });
+
+      return method;
+    }
+    catch (Exception exp)
+    {
+    }
+
+    return null;
+  }
+
+  public static void importFunctions(ScriptContext ctx, String namespace, Object obj)
+  {
+    Class clazz = null;
+
+    if (obj instanceof Class)
+    {
+      clazz = (Class)obj;
+    } else {
+      if (obj instanceof String)
+      {
+        try
+        {
+          clazz = Class.forName((String)obj);
+        }
+        catch (ClassNotFoundException cnfe)
+        {
+          throw new ELException(cnfe);
+        }
+
+      }
+
+      throw new ELException("Class or class name is missing");
+    }
+
+    Method[] methods = clazz.getMethods();
+
+    for (Method m : methods)
+    {
+      int mod = m.getModifiers();
+
+      if ((Modifier.isStatic(mod)) &&
+          (Modifier.isPublic(mod)))
+      {
+        String name = namespace + ":" + m.getName();
+
+        ctx.setAttribute(name, m,
+            100);
+      }
+    }
+  }
+
+  private class JuelCompiledScript extends CompiledScript
+  {
+    private ValueExpression expr;
+    private ScriptEngine engine;
+
+    JuelCompiledScript(ScriptEngine engine, ValueExpression expr)
+    {
+      this.engine = engine;
+      this.expr = expr;
+    }
+
+    public ScriptEngine getEngine()
+    {
+      return engine;
+    }
+
+    public Object eval(ScriptContext ctx)
+        throws ScriptException
+    {
+      return engine.eval(this.expr.getExpressionString(), ctx);
+    }
+  }
+
+  private class ScriptContextFunctionMapper extends FunctionMapper
+  {
+    private ScriptContext ctx;    
+
+    ScriptContextFunctionMapper(ScriptContext ctx)
+    {
+      this.ctx = ctx;
+    }
+
+    private String getFullName(String prefix, String localName)
+    {
+      return prefix + ":" + localName;
+    }
+
+    public Method resolveFunction(String prefix, String localName)
+    {
+      String fullName = getFullName(prefix, localName);
+
+      int scope = this.ctx.getAttributesScope(fullName);
+
+      if (scope != -1)
+      {
+        Object tmp = this.ctx.getAttribute(fullName);
+
+        return ((tmp instanceof Method) ? (Method)tmp : null);
+      }
+
+      return null;
+    }
+  }
+
+  private class ScriptContextVariableMapper extends VariableMapper
+  {
+    private ScriptContext ctx;
+
+    ScriptContextVariableMapper(ScriptContext ctx)
+    {
+      this.ctx = ctx;
+    }
+
+    public ValueExpression resolveVariable(String variable)
+    {
+      int scope = this.ctx.getAttributesScope(variable);
+
+      if (scope != -1)
+      {
+        Object value = this.ctx.getAttribute(variable, scope);
+
+        if (value instanceof ValueExpression)
+        {
+          return ((ValueExpression)value);
+        }
+
+        return exprFactory.createValueExpression(
+            value, Object.class);
+      }
+
+      return null;
+    }
+
+
+    public ValueExpression setVariable(String variable, ValueExpression value)
+    {
+      ValueExpression oldValue = resolveVariable(variable);
+
+      this.ctx.setAttribute(variable, value, 100);
+
+      return oldValue;
+    }
+  }
+}
\ No newline at end of file

Added: jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/script/JuelScriptEngineFactory.java
===================================================================
--- jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/script/JuelScriptEngineFactory.java	                        (rev 0)
+++ jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/script/JuelScriptEngineFactory.java	2009-03-23 14:18:52 UTC (rev 4309)
@@ -0,0 +1,187 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jbpm.pvm.internal.script;
+
+import javax.script.ScriptEngineFactory;
+import javax.script.ScriptEngine;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Collections;
+
+/**
+ * https://jira.jboss.org/jira/browse/JBPM-2120
+ * 
+ * @author Heiko.Braun <heiko.braun at jboss.com>
+ */
+public class JuelScriptEngineFactory implements ScriptEngineFactory
+{
+
+  private static List<String> names = new ArrayList(1);
+  private static List<String> extensions;
+  private static List<String> mimeTypes;
+
+  public JuelScriptEngineFactory()
+  {
+    super();   
+  }
+
+  static
+  {
+    names.add("juel");
+
+    names = Collections.unmodifiableList(names);
+
+    extensions = names;
+
+    mimeTypes = new ArrayList(0);
+
+    mimeTypes = Collections.unmodifiableList(mimeTypes);
+  }
+
+  public String getEngineName()
+  {
+    return "juel";
+  }
+
+  public String getEngineVersion()
+  {
+    return de.odysseus.el.ExpressionFactoryImpl.class.getPackage().getImplementationVersion();
+  }
+
+  public List<String> getExtensions()
+  {
+    return extensions;
+  }
+
+  public String getLanguageName()
+  {
+    return "JSP 2.1 EL";
+  }
+
+  public String getLanguageVersion()
+  {
+    return "2.1";
+  }
+
+  public String getMethodCallSyntax(String obj, String m, String[] args)
+  {
+    throw new UnsupportedOperationException("getMethodCallSyntax");
+  }
+
+  public List<String> getMimeTypes()
+  {
+    return mimeTypes;
+  }
+
+  public List<String> getNames()
+  {
+    return names;
+  }
+
+  public String getOutputStatement(String toDisplay)
+  {
+    StringBuilder buf = new StringBuilder();
+
+    buf.append("out:print(\"");
+
+    int len = toDisplay.length();
+
+    for (int i = 0; i < len; ++i)
+    {
+      char ch = toDisplay.charAt(i);
+
+      switch (ch)
+      {
+        case '"':
+          buf.append("\\\"");
+
+          break;
+        case '\\':
+          buf.append("\\\\");
+
+          break;
+        default:
+          buf.append(ch);
+      }
+
+    }
+
+    buf.append("\")");
+
+    return buf.toString();
+  }
+
+  public String getParameter(String key)
+  {
+    if (key.equals("javax.script.name"))
+    {
+      return getLanguageName();
+    }
+    if (key.equals("javax.script.engine"))
+    {
+      return getEngineName();
+    }
+    if (key.equals("javax.script.engine_version"))
+    {
+      return getEngineVersion();
+    }
+    if (key.equals("javax.script.language"))
+    {
+      return getLanguageName();
+    }
+    if (key.equals("javax.script.language_version"))
+    {
+      return getLanguageVersion();
+    }
+    if (key.equals("THREADING"))
+    {
+      return "MULTITHREADED";
+    }
+
+    return null;
+  }
+
+  public String getProgram(String[] statements)
+  {
+    StringBuilder buf = new StringBuilder();
+
+    if (statements.length != 0)
+    {
+      for (int i = 0; i < statements.length; ++i)
+      {
+        buf.append("${");
+
+        buf.append(statements[i]);
+
+        buf.append("} ");
+      }
+
+    }
+
+    return buf.toString();
+  }
+
+  public ScriptEngine getScriptEngine()
+  {
+    return new JuelScriptEngine(this);
+  }
+}

Modified: jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/script/ScriptManager.java
===================================================================
--- jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/script/ScriptManager.java	2009-03-23 10:13:16 UTC (rev 4308)
+++ jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/script/ScriptManager.java	2009-03-23 14:18:52 UTC (rev 4309)
@@ -62,7 +62,7 @@
           "                  default-script-language='beanshell' " +
           "                  read-contexts='execution, environment, process-engine' " +
           "                  write-context=''>" +
-          "    <script-language name='juel' factory='com.sun.script.juel.JuelScriptEngineFactory' />" +
+          "    <script-language name='juel' factory='org.jbpm.pvm.internal.script.JuelScriptEngineFactory' />" +
           "  </script-manager>" +
           "</objects>"
         )

Modified: jbpm4/trunk/modules/pvm/src/test/resources/jbpm.cfg.xml
===================================================================
--- jbpm4/trunk/modules/pvm/src/test/resources/jbpm.cfg.xml	2009-03-23 10:13:16 UTC (rev 4308)
+++ jbpm4/trunk/modules/pvm/src/test/resources/jbpm.cfg.xml	2009-03-23 14:18:52 UTC (rev 4309)
@@ -34,7 +34,7 @@
                     default-script-language="juel"
                     read-contexts="execution, environment, process-engine"
                     write-context="">
-        <script-language name="juel" factory="com.sun.script.juel.JuelScriptEngineFactory" />
+        <script-language name="juel" factory="org.jbpm.pvm.internal.script.JuelScriptEngineFactory" />
     </script-manager>
     
     <authentication />




More information about the jbpm-commits mailing list