You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@twill.apache.org by ch...@apache.org on 2015/11/12 07:20:18 UTC

incubator-twill git commit: (TWILL-155) Sort file listed from directory in TwillLauncher

Repository: incubator-twill
Updated Branches:
  refs/heads/master 161032a20 -> 87b063cac


(TWILL-155) Sort file listed from directory in TwillLauncher

This gives a deterministic behavior on the ClassLoader created
by TwillLauncher

This closes #71 on GitHub

Signed-off-by: Terence Yim <ch...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/incubator-twill/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-twill/commit/87b063ca
Tree: http://git-wip-us.apache.org/repos/asf/incubator-twill/tree/87b063ca
Diff: http://git-wip-us.apache.org/repos/asf/incubator-twill/diff/87b063ca

Branch: refs/heads/master
Commit: 87b063cac642d4a605679049faa4e662b71cfc46
Parents: 161032a
Author: Terence Yim <ch...@apache.org>
Authored: Wed Nov 11 14:03:27 2015 -0800
Committer: Terence Yim <ch...@apache.org>
Committed: Wed Nov 11 22:20:05 2015 -0800

----------------------------------------------------------------------
 .../apache/twill/launcher/TwillLauncher.java    | 36 +++++++++++++-------
 1 file changed, 24 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-twill/blob/87b063ca/twill-core/src/main/java/org/apache/twill/launcher/TwillLauncher.java
----------------------------------------------------------------------
diff --git a/twill-core/src/main/java/org/apache/twill/launcher/TwillLauncher.java b/twill-core/src/main/java/org/apache/twill/launcher/TwillLauncher.java
index 3405709..171b22a 100644
--- a/twill-core/src/main/java/org/apache/twill/launcher/TwillLauncher.java
+++ b/twill-core/src/main/java/org/apache/twill/launcher/TwillLauncher.java
@@ -36,6 +36,7 @@ import java.nio.charset.Charset;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.jar.JarEntry;
@@ -142,18 +143,15 @@ public final class TwillLauncher {
 
   private static URLClassLoader createClassLoader(File dir, boolean useClassPath) {
     try {
-      List<URL> urls = new ArrayList<URL>();
+      List<URL> urls = new ArrayList<>();
       urls.add(dir.toURI().toURL());
       urls.add(new File(dir, "classes").toURI().toURL());
       urls.add(new File(dir, "resources").toURI().toURL());
 
       File libDir = new File(dir, "lib");
-      File[] files = libDir.listFiles();
-      if (files != null) {
-        for (File file : files) {
-          if (file.getName().endsWith(".jar")) {
-            urls.add(file.toURI().toURL());
-          }
+      for (File file : listFiles(libDir)) {
+        if (file.getName().endsWith(".jar")) {
+          urls.add(file.toURI().toURL());
         }
       }
 
@@ -163,7 +161,7 @@ public final class TwillLauncher {
 
       addClassPathsToList(urls, Constants.APPLICATION_CLASSPATH);
 
-      return new URLClassLoader(urls.toArray(new URL[0]));
+      return new URLClassLoader(urls.toArray(new URL[urls.size()]));
 
     } catch (Exception e) {
       throw new IllegalStateException(e);
@@ -190,12 +188,12 @@ public final class TwillLauncher {
     if (classpath.endsWith("/*")) {
       // Grab all .jar files
       File dir = new File(classpath.substring(0, classpath.length() - 2));
-      File[] files = dir.listFiles();
-      if (files == null || files.length == 0) {
+      List<File> files = listFiles(dir);
+      if (files.isEmpty()) {
         return singleItem(dir.toURI().toURL());
       }
 
-      List<URL> result = new ArrayList<URL>(files.length);
+      List<URL> result = new ArrayList<>(files.size());
       for (File file : files) {
         if (file.getName().endsWith(".jar")) {
           result.add(file.toURI().toURL());
@@ -208,7 +206,7 @@ public final class TwillLauncher {
   }
 
   private static Collection<URL> singleItem(URL url) {
-    List<URL> result = new ArrayList<URL>(1);
+    List<URL> result = new ArrayList<>(1);
     result.add(url);
     return result;
   }
@@ -233,4 +231,18 @@ public final class TwillLauncher {
     }
     dir.delete();
   }
+
+  /**
+   * Returns a sorted list of {@link File} under the given directory. The list will be empty if
+   * the given directory is empty, not exist or not a directory.
+   */
+  private static List<File> listFiles(File dir) {
+    File[] files = dir.listFiles();
+    if (files == null || files.length == 0) {
+      return Collections.emptyList();
+    }
+    List<File> fileList = Arrays.asList(files);
+    Collections.sort(fileList);
+    return fileList;
+  }
 }