Author: fschumacher
Date: Thu Sep 13 18:36:46 2018
New Revision: 1840847

URL: http://svn.apache.org/viewvc?rev=1840847&view=rev
Log:
Allow full JSR-223 for init scripts in JMeter startup

Use jsr223.init.file to allow calling a JSR-223 script on JMeter startup
instead of the envisioned groovy.init.file property.

The engine to use for the init script will be determined by the extension
of the filename. If the filename has no extension or if no engine
can be found for that extension, Groovy will be tried.

Bugzilla Id: 62700

Modified:
    jmeter/trunk/bin/jmeter.properties
    jmeter/trunk/src/core/org/apache/jmeter/JMeter.java
    jmeter/trunk/xdocs/changes.xml
    jmeter/trunk/xdocs/usermanual/properties_reference.xml

Modified: jmeter/trunk/bin/jmeter.properties
URL: 
http://svn.apache.org/viewvc/jmeter/trunk/bin/jmeter.properties?rev=1840847&r1=1840846&r2=1840847&view=diff
==============================================================================
--- jmeter/trunk/bin/jmeter.properties (original)
+++ jmeter/trunk/bin/jmeter.properties Thu Sep 13 18:36:46 2018
@@ -916,15 +916,21 @@ beanshell.server.file=../extras/startup.
 # of Test and Thread Listeners.
 
 #---------------------------------------------------------------------------
-# Groovy function
+# JSR-223 function
 #---------------------------------------------------------------------------
 
-# Path to Groovy file containing script to call on JMeter startup
+# Path to JSR-223 file containing script to call on JMeter startup
+# JMeter will try to guess the engine to use by the extension
+# of the name of the file.
 # This script can use pre-defined variables:
 # log : Logger to log any message
 # props : JMeter Property
 # OUT : System.OUT
-#groovy.init.file=
+#jsr223.init.file=
+
+#---------------------------------------------------------------------------
+# Groovy function
+#---------------------------------------------------------------------------
 
 #Path to Groovy file containing utility functions to make available to 
__groovy function
 #groovy.utilities=

Modified: jmeter/trunk/src/core/org/apache/jmeter/JMeter.java
URL: 
http://svn.apache.org/viewvc/jmeter/trunk/src/core/org/apache/jmeter/JMeter.java?rev=1840847&r1=1840846&r2=1840847&view=diff
==============================================================================
--- jmeter/trunk/src/core/org/apache/jmeter/JMeter.java (original)
+++ jmeter/trunk/src/core/org/apache/jmeter/JMeter.java Thu Sep 13 18:36:46 2018
@@ -40,13 +40,16 @@ import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Locale;
+import java.util.Map;
 import java.util.Properties;
 import java.util.StringTokenizer;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicInteger;
+import java.util.stream.Collectors;
 
 import javax.script.Bindings;
 import javax.script.ScriptEngine;
+import javax.script.ScriptEngineFactory;
 import javax.script.ScriptEngineManager;
 import javax.script.ScriptException;
 import javax.swing.JTree;
@@ -57,7 +60,9 @@ import org.apache.commons.cli.avalon.CLA
 import org.apache.commons.cli.avalon.CLOption;
 import org.apache.commons.cli.avalon.CLOptionDescriptor;
 import org.apache.commons.cli.avalon.CLUtil;
+import org.apache.commons.io.FilenameUtils;
 import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.jmeter.control.ReplaceableController;
 import org.apache.jmeter.engine.ClientJMeterEngine;
 import org.apache.jmeter.engine.DistributedRunner;
@@ -112,6 +117,8 @@ import com.thoughtworks.xstream.converte
  * Main JMeter class; processes options and starts the GUI, non-GUI or server 
as appropriate.
  */
 public class JMeter implements JMeterPlugin {
+    private static final String JSR223_INIT_FILE = "jsr223.init.file";
+
     private static final Logger log = LoggerFactory.getLogger(JMeter.class);
     
     public static final int UDP_PORT_DEFAULT = 4445; // needed for 
ShutdownClient
@@ -674,17 +681,25 @@ public class JMeter implements JMeterPlu
             }
         }
         
-        // Should we run a Groovy script on startup?
-        String groovyInit = JMeterUtils.getProperty("groovy.init.file");// 
$NON-NLS-1$
-        if (groovyInit != null){
-            log.info("Running Groovy init script in file: {}", groovyInit);
-            File file = new File(groovyInit);
+        // Should we run a JSR223 script on startup?
+        String jsr223Init = JMeterUtils.getProperty(JSR223_INIT_FILE);// 
$NON-NLS-1$
+        if (jsr223Init != null){
+            log.info("Running JSR-223 init script in file: {}", jsr223Init);
+            File file = new File(jsr223Init);
             if(file.exists() && file.canRead()) {
+                String extension = 
StringUtils.defaultIfBlank(FilenameUtils.getExtension(jsr223Init), "Groovy");
                 try (FileReader reader = new FileReader(file)) {
                     ScriptEngineManager scriptEngineManager = new 
ScriptEngineManager();
-                    ScriptEngine engine = 
scriptEngineManager.getEngineByName("Groovy");
+                    ScriptEngine engine = 
scriptEngineManager.getEngineByExtension(extension);
+                    if (engine == null) {
+                        log.warn(
+                                "No script engine found for [{}]. Will try to 
use Groovy. Possible engines and their extensions are: {}",
+                                extension, 
getEnginesAndExtensions(scriptEngineManager));
+                        extension = "Groovy";
+                        engine = 
scriptEngineManager.getEngineByName(extension);
+                    }
                     Bindings bindings = engine.createBindings();
-                    final Logger logger = 
LoggerFactory.getLogger("groovy.init.file");
+                    final Logger logger = 
LoggerFactory.getLogger(JSR223_INIT_FILE);
                     bindings.put("log", logger); // $NON-NLS-1$ (this name is 
fixed)
                     Properties props = JMeterUtils.getJMeterProperties();
                     bindings.put("props", props); // $NON-NLS-1$ (this name is 
fixed)
@@ -692,14 +707,22 @@ public class JMeter implements JMeterPlu
                     bindings.put("OUT", System.out); // NOSONAR $NON-NLS-1$ 
(this name is fixed)
                     engine.eval(reader, bindings);
                 } catch (IOException | ScriptException ex) {
-                    log.error("Error running init script {}: {}", groovyInit, 
ex);
+                    log.error("Error running init script {} with engine for 
{}: {}", jsr223Init, extension, ex);
                 }
             } else {
-                log.error("Script {} referenced by property {} is not readable 
or does not exist", file.getAbsolutePath(), "groovy.init.file");
+                log.error("Script {} referenced by property {} is not readable 
or does not exist", file.getAbsolutePath(), JSR223_INIT_FILE);
             }
         }
     }
 
+
+    private Map<String, List<String>> 
getEnginesAndExtensions(ScriptEngineManager scriptEngineManager) {
+        return scriptEngineManager.getEngineFactories().stream()
+                .collect(Collectors.toMap(
+                        f -> f.getLanguageName() + " (" + 
f.getLanguageVersion() + ")",
+                        ScriptEngineFactory::getExtensions));
+    }
+
     /**
      * Sets a proxy server for the JVM if the command line arguments are
      * specified.

Modified: jmeter/trunk/xdocs/changes.xml
URL: 
http://svn.apache.org/viewvc/jmeter/trunk/xdocs/changes.xml?rev=1840847&r1=1840846&r2=1840847&view=diff
==============================================================================
--- jmeter/trunk/xdocs/changes.xml [utf-8] (original)
+++ jmeter/trunk/xdocs/changes.xml [utf-8] Thu Sep 13 18:36:46 2018
@@ -206,7 +206,7 @@ this behaviour, set <code>httpclient.res
   <li><bug>62470</bug>CSV Output : Enable logging of sub results when 
<code>jmeter.save.saveservice.subresults=true</code>. Contributed by Ubik Load 
Pack (support at ubikloadpack.com)</li>
   <li><bug>62473</bug>Setting "<code>saveservice_properties</code>" has 
counter intuitive behaviour</li>
   <li><bug>62354</bug>Correct calculation and usage of units for second per 
user (reported by jffagot05 at gmail.com)</li>
-  <li><bug>62700</bug>Introduce groovy.init.file to allow calling a groovy 
script on JMeter startup</li>
+  <li><bug>62700</bug>Introduce <code>jsr223.init.file</code> to allow calling 
a JSR-223 script on JMeter startup</li>
   <li><bug>62128</bug>Try to guess <code>JMETER_HOME</code> correctly, when 
<code>jmeter.bat</code> is called from a batch file in another directory. 
Contributed by logox01 (logox01 at gmx.at)</li>
   <li><pr>386</pr>Add parameter support for RMI keystore creation scripts. 
Contributed by Logan Mauzaize (t524467 at airfrance.fr)</li>
   <li><bug>62065</bug>Use Maven artifact for JAF Module instead of embedded 
module</li>

Modified: jmeter/trunk/xdocs/usermanual/properties_reference.xml
URL: 
http://svn.apache.org/viewvc/jmeter/trunk/xdocs/usermanual/properties_reference.xml?rev=1840847&r1=1840846&r2=1840847&view=diff
==============================================================================
--- jmeter/trunk/xdocs/usermanual/properties_reference.xml (original)
+++ jmeter/trunk/xdocs/usermanual/properties_reference.xml Thu Sep 13 18:36:46 
2018
@@ -1496,10 +1496,6 @@ JMETER-SERVER</source>
     confirmation dialogue.<br/>
     Defaults to: <code>false</code>
 </property>
-<property name="jsr223.compiled_scripts_cache_size">
-    Used by JSR223 elements.<br/>
-    Size of compiled scripts cache.<br/>
-    Defaults to: <code>100</code></property>
 </properties>
 </section>
 <section name="&sect-num;.36 Classpath configuration" anchor="classpath">
@@ -1877,20 +1873,33 @@ JMETER-SERVER</source>
 <section name="&sect-num;.43 Advanced Groovy Scripting configuration" 
anchor="groovy">
 <description>Advanced properties for configuration of scripting in 
Groovy</description>
 <properties>
-    <property name="groovy.init.file">
-    Path to Groovy file containing script to call on JMeter startup. <br/>
-    This script can use pre-defined variables:
+    <property name="groovy.utilities">
+    Path to Groovy file containing utility functions to make available to 
<funclink name="__groovy"/> function.<br/>
+    Defaults to <code>bin/utility.groovy</code> 
+    </property>
+</properties>
+</section>
+
+<section name="&sect-num;.44 Advanced JSR-223 Scripting configuration" 
anchor="jsr223">
+<description>Advanced properties for configuration of scripting in 
JSR-223</description>
+<properties>
+    <property name="jsr223.init.file">
+    <p>Path to JSR-223 file containing script to call on JMeter startup.</p>
+    <p>The actual scripting engine to use will be determined by the extension
+    of the init file name. If the file name has no extension, or no scripting
+    engine could be found for that extension, Groovy will be used.</p>
+    <p>This script can use pre-defined variables:</p>
     <ul>
         <li><code>log</code>: Logger to log any message, uses SLF4J 
library</li>
         <li><code>props</code>: JMeter Properties</li>
         <li><code>OUT</code>: System.OUT, useful to write in the console</li>
     </ul>
-    No script is defined by default. 
-    </property>
-    <property name="groovy.utilities">
-    Path to Groovy file containing utility functions to make available to 
__groovy function. <br/>
-    Defaults to <code>bin/utility.groovy</code> 
+    No script is defined by default.
     </property>
+    <property name="jsr223.compiled_scripts_cache_size">
+    Used by JSR-223 elements.<br/>
+    Size of compiled scripts cache.<br/>
+    Defaults to: <code>100</code></property>
 </properties>
 </section>
 <!-- 


Reply via email to