Author: sebb
Date: Wed Oct 12 22:36:29 2011
New Revision: 1182604

URL: http://svn.apache.org/viewvc?rev=1182604&view=rev
Log:
Bug 52016 - It would be useful to support Jexl2

Added:
    
jakarta/jmeter/trunk/src/functions/org/apache/jmeter/functions/Jexl2Function.java
   (with props)
Modified:
    jakarta/jmeter/trunk/build.properties
    jakarta/jmeter/trunk/build.xml
    jakarta/jmeter/trunk/xdocs/changes.xml
    jakarta/jmeter/trunk/xdocs/usermanual/functions.xml

Modified: jakarta/jmeter/trunk/build.properties
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/trunk/build.properties?rev=1182604&r1=1182603&r2=1182604&view=diff
==============================================================================
--- jakarta/jmeter/trunk/build.properties (original)
+++ jakarta/jmeter/trunk/build.properties Wed Oct 12 22:36:29 2011
@@ -77,6 +77,10 @@ commons-jexl.jar            = commons-je
 commons-jexl.loc            = ${maven2.repo}/commons-jexl/commons-jexl/1.1
 commons-jexl.md5            = 3F7735D20FCE1DBE05F62FF7A7B178DC
 
+commons-jexl2.jar           = commons-jexl-2.0.1.jar
+commons-jexl2.loc           = 
${maven2.repo}/org/apache/commons/commons-jexl/2.0.1
+commons-jexl2.md5           = 98a410281a57d744991e64e5c9789920
+
 commons-lang.jar            = commons-lang-2.6.jar
 commons-lang.loc            = ${maven2.repo}/commons-lang/commons-lang/2.6
 commons-lang.md5            = 4d5c1693079575b362edf41500630bbd

Modified: jakarta/jmeter/trunk/build.xml
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/trunk/build.xml?rev=1182604&r1=1182603&r2=1182604&view=diff
==============================================================================
--- jakarta/jmeter/trunk/build.xml (original)
+++ jakarta/jmeter/trunk/build.xml Wed Oct 12 22:36:29 2011
@@ -330,6 +330,7 @@
     <include name="${lib.dir}/${commons-httpclient.jar}"/>
     <include name="${lib.dir}/${commons-io.jar}"/>
     <include name="${lib.dir}/${commons-jexl.jar}"/>
+    <include name="${lib.dir}/${commons-jexl2.jar}"/>
     <include name="${lib.dir}/${commons-lang.jar}"/>
     <include name="${lib.dir}/${commons-logging.jar}"/>
     <include name="${lib.dir}/${commons-net.jar}"/>
@@ -390,6 +391,7 @@
     <pathelement location="${lib.dir}/${commons-httpclient.jar}"/>
     <pathelement location="${lib.dir}/${commons-io.jar}"/>
     <pathelement location="${lib.dir}/${commons-jexl.jar}"/>
+    <pathelement location="${lib.dir}/${commons-jexl2.jar}"/>
     <pathelement location="${lib.dir}/${commons-lang.jar}"/>
     <pathelement location="${lib.dir}/${commons-logging.jar}"/>
     <pathelement location="${lib.dir}/${commons-net.jar}"/>
@@ -2195,6 +2197,7 @@ run JMeter unless all the JMeter jars ar
         <process_jarfile jarname="commons-httpclient"/>
         <process_jarfile jarname="commons-io"/>
         <process_jarfile jarname="commons-jexl"/>
+        <process_jarfile jarname="commons-jexl2"/>
         <process_jarfile jarname="commons-lang"/>
         <process_jarfile jarname="commons-logging"/>
         <process_jarfile jarname="commons-net"/>

Added: 
jakarta/jmeter/trunk/src/functions/org/apache/jmeter/functions/Jexl2Function.java
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/functions/org/apache/jmeter/functions/Jexl2Function.java?rev=1182604&view=auto
==============================================================================
--- 
jakarta/jmeter/trunk/src/functions/org/apache/jmeter/functions/Jexl2Function.java
 (added)
+++ 
jakarta/jmeter/trunk/src/functions/org/apache/jmeter/functions/Jexl2Function.java
 Wed Oct 12 22:36:29 2011
@@ -0,0 +1,137 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jmeter.functions;
+
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.commons.jexl2.Expression;
+import org.apache.commons.jexl2.JexlContext;
+import org.apache.commons.jexl2.JexlEngine;
+import org.apache.commons.jexl2.MapContext;
+
+import org.apache.jmeter.engine.util.CompoundVariable;
+import org.apache.jmeter.samplers.SampleResult;
+import org.apache.jmeter.samplers.Sampler;
+import org.apache.jmeter.threads.JMeterContext;
+import org.apache.jmeter.threads.JMeterContextService;
+import org.apache.jmeter.threads.JMeterVariables;
+import org.apache.jmeter.util.JMeterUtils;
+import org.apache.jorphan.logging.LoggingManager;
+import org.apache.log.Logger;
+
+/**
+ * A function which understands Commons JEXL2
+ */
+// For unit tests, see TestJexlFunction
+public class Jexl2Function extends AbstractFunction {
+
+    private static final Logger log = LoggingManager.getLoggerForClass();
+
+    private static final String KEY = "__jexl2"; //$NON-NLS-1$
+
+    private static final List<String> desc = new LinkedList<String>();
+
+    // TODO should the engine be static?
+    private static final JexlEngine jexl = new JexlEngine();
+    static {
+       jexl.setCache(512);
+       jexl.setLenient(false);
+       jexl.setSilent(false);
+    }
+
+    static
+    {
+        desc.add(JMeterUtils.getResString("jexl_expression")); //$NON-NLS-1$
+        desc.add(JMeterUtils.getResString("function_name_paropt"));// 
$NON-NLS1$
+    }
+
+    private Object[] values;
+
+    /** {@inheritDoc} */
+    @Override
+    public synchronized String execute(SampleResult previousResult, Sampler 
currentSampler)
+            throws InvalidVariableException
+    {
+        String str = ""; //$NON-NLS-1$
+
+        CompoundVariable var = (CompoundVariable) values[0];
+        String exp = var.execute();
+
+        String varName = ""; //$NON-NLS-1$
+        if (values.length > 1) {
+            varName = ((CompoundVariable) values[1]).execute().trim();
+        }
+
+        JMeterContext jmctx = JMeterContextService.getContext();
+        JMeterVariables vars = jmctx.getVariables();
+
+        try
+        {
+            JexlContext jc = new MapContext();
+            jc.set("log", log); //$NON-NLS-1$
+            jc.set("ctx", jmctx); //$NON-NLS-1$
+            jc.set("vars", vars); //$NON-NLS-1$
+            jc.set("props", JMeterUtils.getJMeterProperties()); //$NON-NLS-1$
+            // Previously mis-spelt as theadName
+            jc.set("threadName", Thread.currentThread().getName()); 
//$NON-NLS-1$
+            jc.set("sampler", currentSampler); //$NON-NLS-1$ (may be null)
+            jc.set("sampleResult", previousResult); //$NON-NLS-1$ (may be null)
+            jc.set("OUT", System.out);//$NON-NLS-1$
+
+            // Now evaluate the script, getting the result
+            Expression e = jexl.createExpression( exp );
+            Object o = e.evaluate(jc);
+            if (o != null)
+            {
+                str = o.toString();
+            }
+            if (vars != null && varName.length() > 0) {// vars will be null on 
TestPlan
+                vars.put(varName, str);
+            }
+        } catch (Exception e)
+        {
+            log.error("An error occurred while evaluating the expression \""
+                    + exp + "\"\n",e);
+        }
+        return str;
+    }
+
+    /** {@inheritDoc} */
+    public List<String> getArgumentDesc()
+    {
+        return desc;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public String getReferenceKey()
+    {
+        return KEY;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public synchronized void setParameters(Collection<CompoundVariable> 
parameters)
+            throws InvalidVariableException
+    {
+        checkParameterCount(parameters, 1, 2);
+        values = parameters.toArray();
+    }
+
+}
\ No newline at end of file

Propchange: 
jakarta/jmeter/trunk/src/functions/org/apache/jmeter/functions/Jexl2Function.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
jakarta/jmeter/trunk/src/functions/org/apache/jmeter/functions/Jexl2Function.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: jakarta/jmeter/trunk/xdocs/changes.xml
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/trunk/xdocs/changes.xml?rev=1182604&r1=1182603&r2=1182604&view=diff
==============================================================================
--- jakarta/jmeter/trunk/xdocs/changes.xml (original)
+++ jakarta/jmeter/trunk/xdocs/changes.xml Wed Oct 12 22:36:29 2011
@@ -154,6 +154,7 @@ Mirror server now uses default port 8081
 <h3>Functions</h3>
 <ul>
 <li>Bug 52006 - Create a function RandomString to generate random Strings</li>
+<li>Bug 52016 - It would be useful to support Jexl2</li>
 </ul>
 
 <h3>I18N</h3>

Modified: jakarta/jmeter/trunk/xdocs/usermanual/functions.xml
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/trunk/xdocs/usermanual/functions.xml?rev=1182604&r1=1182603&r2=1182604&view=diff
==============================================================================
--- jakarta/jmeter/trunk/xdocs/usermanual/functions.xml (original)
+++ jakarta/jmeter/trunk/xdocs/usermanual/functions.xml Wed Oct 12 22:36:29 2011
@@ -104,7 +104,7 @@ and need to be referenced using the __P 
         <tr><td>Calculation</td><td> <a 
href="#__RandomString">RandomString</a></td><td>generate a random 
string</td></tr>
         <tr><td>Scripting</td><td> <a 
href="#__BeanShell">BeanShell</a></td><td>run a BeanShell script</td></tr>
         <tr><td>Scripting</td><td> <a 
href="#__javaScript">javaScript</a></td><td>process JavaScript (Mozilla 
Rhino)</td></tr>
-        <tr><td>Scripting</td><td> <a href="#__jexl">jexl</a></td><td>evaluate 
a Commons Jexl expression</td></tr>
+        <tr><td>Scripting</td><td> <a href="#__jexl">jexl, 
jexl2</a></td><td>evaluate a Commons Jexl expression</td></tr>
         <tr><td>Properties</td><td> <a href="#__property">property</a> 
</td><td>read a property</td></tr>
         <tr><td>Properties</td><td> <a href="#__P">P</a></td><td>read a 
property (shorthand method)</td></tr>
         <tr><td>Properties</td><td> <a 
href="#__setProperty">setProperty</a></td><td>set a JMeter property</td></tr>
@@ -896,12 +896,13 @@ time.YMD=yyMMdd
 </p>
 </component>
 
-<component index="&sect-num;.5.18" name="__jexl">
+<component index="&sect-num;.5.18" name="__jexl" tag="__jexl2">
 <description>
        <p>The jexl function returns the result of evaluating a 
        <a href="http://jakarta.apache.org/commons/jexl";>Commons JEXL 
expression</a>. 
        See links below for more information on JEXL expressions.
        </p>
+    <p>The __jexl function uses Commons JEXL 1, and the __jexl2 function uses 
Commons JEXL 2</p>
        <ul>
        <li><a href="http://commons.apache.org/jexl/reference/syntax.html";>JEXL 
syntax description</a></li>
        <li><a 
href="http://commons.apache.org/jexl/reference/examples.html#Example_Expressions";>JEXL
 examples</a></li>



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to