You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jmeter.apache.org by pm...@apache.org on 2016/12/30 23:47:25 UTC

svn commit: r1776662 - in /jmeter/trunk/src/core/org/apache/jmeter: JMeter.java NewDriver.java testelement/TestPlan.java

Author: pmouawad
Date: Fri Dec 30 23:47:25 2016
New Revision: 1776662

URL: http://svn.apache.org/viewvc?rev=1776662&view=rev
Log:
sonar : fix security errors
Now we fail if issues happens on JAR loading 

Modified:
    jmeter/trunk/src/core/org/apache/jmeter/JMeter.java
    jmeter/trunk/src/core/org/apache/jmeter/NewDriver.java
    jmeter/trunk/src/core/org/apache/jmeter/testelement/TestPlan.java

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=1776662&r1=1776661&r2=1776662&view=diff
==============================================================================
--- jmeter/trunk/src/core/org/apache/jmeter/JMeter.java (original)
+++ jmeter/trunk/src/core/org/apache/jmeter/JMeter.java Fri Dec 30 23:47:25 2016
@@ -564,13 +564,13 @@ public class JMeter implements JMeterPlu
     }
 
     // Update classloader if necessary
-    private void updateClassLoader() {
+    private void updateClassLoader() throws MalformedURLException {
         updatePath("search_paths",";", true); //$NON-NLS-1$//$NON-NLS-2$
         updatePath("user.classpath",File.pathSeparator, true);//$NON-NLS-1$
         updatePath("plugin_dependency_paths",";", false);//$NON-NLS-1$
     }
 
-    private void updatePath(String property, String sep, boolean cp) {
+    private void updatePath(String property, String sep, boolean cp) throws MalformedURLException {
         String userpath= JMeterUtils.getPropDefault(property,"");// $NON-NLS-1$
         if (userpath.length() <= 0) { 
             return; 
@@ -585,11 +585,7 @@ public class JMeter implements JMeterPlu
             } else {
                 if (cp) {
                     log.info("Adding to classpath and loader: "+path);
-                    try {
-                        NewDriver.addPath(path);
-                    } catch (MalformedURLException e) {
-                        log.warn("Error adding: "+path+" "+e.getLocalizedMessage());
-                    }
+                    NewDriver.addPath(path);
                 } else {
                     log.info("Adding to loader: "+path);
                     NewDriver.addURL(path);

Modified: jmeter/trunk/src/core/org/apache/jmeter/NewDriver.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/core/org/apache/jmeter/NewDriver.java?rev=1776662&r1=1776661&r2=1776662&view=diff
==============================================================================
--- jmeter/trunk/src/core/org/apache/jmeter/NewDriver.java (original)
+++ jmeter/trunk/src/core/org/apache/jmeter/NewDriver.java Fri Dec 30 23:47:25 2016
@@ -22,10 +22,13 @@ package org.apache.jmeter;
 import java.io.File;
 import java.io.FilenameFilter;
 import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringWriter;
 import java.lang.reflect.Method;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.security.AccessController;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.LinkedList;
 import java.util.List;
@@ -51,6 +54,8 @@ public final class NewDriver {
     /** The directory JMeter is installed in. */
     private static final String jmDir;
 
+    private static final List<Exception> EXCEPTIONS_IN_INIT = new ArrayList<>();
+
     static {
         final List<URL> jars = new LinkedList<>();
         final String initial_classpath = System.getProperty(JAVA_CLASS_PATH);
@@ -116,8 +121,8 @@ public final class NewDriver {
                     jars.add(new File(s).toURI().toURL());// See Java bug 4496398
                     classpath.append(CLASSPATH_SEPARATOR);
                     classpath.append(s);
-                } catch (MalformedURLException e) {
-                    e.printStackTrace();
+                } catch (MalformedURLException e) { // NOSONAR
+                    EXCEPTIONS_IN_INIT.add(new Exception("Error adding jar:"+libJar.getAbsolutePath(), e));
                 }
             }
         }
@@ -166,21 +171,14 @@ public final class NewDriver {
      * Add a URL to the loader classpath only; does not update the system classpath.
      *
      * @param path to be added.
+     * @throws MalformedURLException 
      */
-    public static void addURL(String path) {
+    public static void addURL(String path) throws MalformedURLException {
         File furl = new File(path);
-        try {
-            loader.addURL(furl.toURI().toURL()); // See Java bug 4496398
-        } catch (MalformedURLException e) {
-            e.printStackTrace();
-        }
+        loader.addURL(furl.toURI().toURL()); // See Java bug 4496398
         File[] jars = listJars(furl);
         for (File jar : jars) {
-            try {
-                loader.addURL(jar.toURI().toURL()); // See Java bug 4496398
-            } catch (MalformedURLException e) {
-                e.printStackTrace();
-            }
+            loader.addURL(jar.toURI().toURL()); // See Java bug 4496398
         }
     }
 
@@ -216,13 +214,9 @@ public final class NewDriver {
         sb.append(path);
         File[] jars = listJars(file);
         for (File jar : jars) {
-            try {
-                loader.addURL(jar.toURI().toURL()); // See Java bug 4496398
-                sb.append(CLASSPATH_SEPARATOR);
-                sb.append(jar.getPath());
-            } catch (MalformedURLException e) {
-                e.printStackTrace();
-            }
+            loader.addURL(jar.toURI().toURL()); // See Java bug 4496398
+            sb.append(CLASSPATH_SEPARATOR);
+            sb.append(jar.getPath());
         }
 
         // ClassFinder needs this
@@ -246,20 +240,40 @@ public final class NewDriver {
      *            the command line arguments
      */
     public static void main(String[] args) {
-        Thread.currentThread().setContextClassLoader(loader);
-        if (System.getProperty("log4j.configuration") == null) {// $NON-NLS-1$ $NON-NLS-2$
-            File conf = new File(jmDir, "bin" + File.separator + "log4j.conf");// $NON-NLS-1$ $NON-NLS-2$
-            System.setProperty("log4j.configuration", "file:" + conf);
+        if(!EXCEPTIONS_IN_INIT.isEmpty()) {
+            System.err.println("Configuration error during init, see exceptions:"+exceptionsToString(EXCEPTIONS_IN_INIT));
+        } else {
+            Thread.currentThread().setContextClassLoader(loader);
+            if (System.getProperty("log4j.configuration") == null) {// $NON-NLS-1$ $NON-NLS-2$
+                File conf = new File(jmDir, "bin" + File.separator + "log4j.conf");// $NON-NLS-1$ $NON-NLS-2$
+                System.setProperty("log4j.configuration", "file:" + conf);
+            }
+    
+            try {
+                Class<?> initialClass = loader.loadClass("org.apache.jmeter.JMeter");// $NON-NLS-1$
+                Object instance = initialClass.newInstance();
+                Method startup = initialClass.getMethod("start", new Class[] { new String[0].getClass() });// $NON-NLS-1$
+                startup.invoke(instance, new Object[] { args });
+            } catch(Throwable e){ // NOSONAR We want to log home directory in case of exception
+                e.printStackTrace(); // NOSONAR No logger at this step
+                System.err.println("JMeter home directory was detected as: "+jmDir);
+            }
         }
+    }
 
-        try {
-            Class<?> initialClass = loader.loadClass("org.apache.jmeter.JMeter");// $NON-NLS-1$
-            Object instance = initialClass.newInstance();
-            Method startup = initialClass.getMethod("start", new Class[] { new String[0].getClass() });// $NON-NLS-1$
-            startup.invoke(instance, new Object[] { args });
-        } catch(Throwable e){ // NOSONAR We want to log home directory in case of exception
-            e.printStackTrace(); // NOSONAR No logger at this step
-            System.err.println("JMeter home directory was detected as: "+jmDir);
+    /**
+     * @param exceptionsInInit List of {@link Exception}
+     * @return String
+     */
+    private static String exceptionsToString(List<Exception> exceptionsInInit) {
+        StringBuilder builder = new StringBuilder();
+        for (Exception exception : exceptionsInInit) {
+            StringWriter stringWriter = new StringWriter();
+            PrintWriter printWriter = new PrintWriter(stringWriter);
+            exception.printStackTrace(printWriter); // NOSONAR 
+            builder.append(stringWriter.toString())
+                .append("\r\n");
         }
+        return builder.toString();
     }
 }

Modified: jmeter/trunk/src/core/org/apache/jmeter/testelement/TestPlan.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/core/org/apache/jmeter/testelement/TestPlan.java?rev=1776662&r1=1776661&r2=1776662&view=diff
==============================================================================
--- jmeter/trunk/src/core/org/apache/jmeter/testelement/TestPlan.java (original)
+++ jmeter/trunk/src/core/org/apache/jmeter/testelement/TestPlan.java Fri Dec 30 23:47:25 2016
@@ -20,6 +20,7 @@ package org.apache.jmeter.testelement;
 
 import java.io.IOException;
 import java.io.Serializable;
+import java.net.MalformedURLException;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
@@ -253,8 +254,13 @@ public class TestPlan extends AbstractTe
         // we set the classpath
         String[] paths = this.getTestPlanClasspathArray();
         for (String path : paths) {
-            NewDriver.addURL(path);
-            log.info("add " + path + " to classpath");
+            try {
+                NewDriver.addURL(path);
+                log.info("added " + path + " to classpath");
+            } catch (MalformedURLException e) {
+                // TODO Should we continue the test or fail ?
+                log.error("Error adding " + path + " to classpath", e);
+            }
         }
     }