You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by sr...@apache.org on 2011/01/17 12:39:25 UTC

svn commit: r1059891 - /mahout/trunk/core/src/main/java/org/apache/mahout/driver/MahoutDriver.java

Author: srowen
Date: Mon Jan 17 11:39:25 2011
New Revision: 1059891

URL: http://svn.apache.org/viewvc?rev=1059891&view=rev
Log:
MAHOUT-584 Avoid NPE when main props file can't be found

Modified:
    mahout/trunk/core/src/main/java/org/apache/mahout/driver/MahoutDriver.java

Modified: mahout/trunk/core/src/main/java/org/apache/mahout/driver/MahoutDriver.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/driver/MahoutDriver.java?rev=1059891&r1=1059890&r2=1059891&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/driver/MahoutDriver.java (original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/driver/MahoutDriver.java Mon Jan 17 11:39:25 2011
@@ -93,17 +93,13 @@ public final class MahoutDriver {
 
   public static void main(String[] args) throws Throwable {
     ProgramDriver programDriver = new ProgramDriver();
-    Properties mainClasses = new Properties();
-    InputStream propsStream = Thread.currentThread()
-                                    .getContextClassLoader()
-                                    .getResourceAsStream("driver.classes.props");
 
-    try {
-      mainClasses.load(propsStream);
-    } catch (IOException e) {
-      //try getting the default one
-      propsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("driver.classes.default.props");
-      mainClasses.load(propsStream);
+    Properties mainClasses = loadProperties("driver.classes.props");
+    if (mainClasses == null) {
+      mainClasses = loadProperties("driver.classes.default.props");
+    }
+    if (mainClasses == null) {
+      throw new IOException("Can't load any properties file?");
     }
 
     boolean foundShortName = false;
@@ -123,15 +119,10 @@ public final class MahoutDriver {
     }
     shift(args);
 
-    InputStream defaultsStream = Thread.currentThread()
-                                       .getContextClassLoader()
-                                       .getResourceAsStream(progName + ".props");
-
-    Properties mainProps = new Properties();
-    if (defaultsStream != null) { // can't find props file, use empty props.
-      mainProps.load(defaultsStream);
-    } else {
+    Properties mainProps = loadProperties(progName + ".props");
+    if (mainProps == null) {
       log.warn("No " + progName + ".props found on classpath, will use command-line arguments only");
+      mainProps = new Properties();
     }
     Map<String,String[]> argMap = new HashMap<String,String[]>();
     int i = 0;
@@ -187,6 +178,24 @@ public final class MahoutDriver {
     }
   }
 
+  private static Properties loadProperties(String resource) throws IOException {
+    InputStream propsStream =
+        Thread.currentThread().getContextClassLoader().getResourceAsStream("driver.classes.props");
+    if (propsStream != null) {
+      try {
+        Properties properties = new Properties();
+        properties.load(propsStream);
+        return properties;
+      } catch (IOException ioe) {
+        log.warn("Error while loading {}", resource, ioe);
+        // Continue
+      } finally {
+        propsStream.close();
+      }
+    }
+    return null;
+  }
+
   private static String[] shift(String[] args) {
     System.arraycopy(args, 1, args, 0, args.length - 1);
     args[args.length - 1] = null;