Browse Source

Switch to Path for setting up config directory.

Also in this change ditch the resource manager for working out
the current working directory, the JVM provides this for us
(user.dir), much less can go wrong with this.

Change-Id: I5746ee043b5974df2b59eb9c586661ac815051d8
Reviewed-on: http://gerrit.dmdirc.com/4070
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Chris Smith <chris@dmdirc.com>
pull/1/head
Greg Holmes 9 years ago
parent
commit
2661262fa4

+ 14
- 11
src/com/dmdirc/commandline/CommandLineParser.java View File

@@ -29,11 +29,13 @@ import com.dmdirc.logger.ErrorLevel;
29 29
 import com.dmdirc.logger.Logger;
30 30
 import com.dmdirc.util.InvalidURIException;
31 31
 import com.dmdirc.util.URIParser;
32
-import com.dmdirc.util.resourcemanager.DMDircResourceManager;
33 32
 
34 33
 import java.io.File;
35 34
 import java.io.IOException;
36 35
 import java.net.URI;
36
+import java.nio.file.Files;
37
+import java.nio.file.Path;
38
+import java.nio.file.Paths;
37 39
 import java.rmi.RemoteException;
38 40
 import java.util.ArrayList;
39 41
 import java.util.List;
@@ -221,7 +223,7 @@ public class CommandLineParser {
221 223
                 doConnect(param);
222 224
                 break;
223 225
             case 'd':
224
-                doDirectory(param);
226
+                doDirectory(Paths.get(param));
225 227
                 break;
226 228
             case 'e':
227 229
                 doExisting();
@@ -236,7 +238,7 @@ public class CommandLineParser {
236 238
                 launcherVersion = param;
237 239
                 break;
238 240
             case 'p':
239
-                doDirectory(DMDircResourceManager.getWorkingDirectory());
241
+                doDirectory(Paths.get(System.getProperty("user.dir")));
240 242
                 break;
241 243
             case 'r':
242 244
                 disablereporting = true;
@@ -318,17 +320,18 @@ public class CommandLineParser {
318 320
      *
319 321
      * @param dir The new config directory
320 322
      */
321
-    private void doDirectory(final String dir) {
322
-        try {
323
-            final File file = new File(dir);
324
-            if (!file.exists() && !file.mkdirs()) {
323
+    private void doDirectory(final Path dir) {
324
+        if (!Files.exists(dir)) {
325
+            try {
326
+                Files.createDirectories(dir);
327
+            } catch (IOException ex) {
325 328
                 System.err.println("Unable to create directory " + dir);
326 329
                 System.exit(1);
327 330
             }
328
-            configDirectory = file.getCanonicalPath() + File.separator;
329
-        } catch (IOException ex) {
330
-            System.err.println("Unable to resolve directory " + dir + ": ");
331
-            System.err.println("\t" + ex.getMessage());
331
+        }
332
+        configDirectory = dir.toAbsolutePath().toString() + File.separator;
333
+        if (!Files.exists(Paths.get(configDirectory))) {
334
+            System.err.println("Unable to resolve directory " + dir);
332 335
             System.exit(1);
333 336
         }
334 337
     }

+ 0
- 25
src/com/dmdirc/util/resourcemanager/DMDircResourceManager.java View File

@@ -22,18 +22,15 @@
22 22
 
23 23
 package com.dmdirc.util.resourcemanager;
24 24
 
25
-import com.dmdirc.Main;
26 25
 import com.dmdirc.logger.ErrorLevel;
27 26
 import com.dmdirc.logger.Logger;
28 27
 
29
-import java.io.File;
30 28
 import java.io.UnsupportedEncodingException;
31 29
 import java.net.MalformedURLException;
32 30
 import java.net.URI;
33 31
 import java.net.URISyntaxException;
34 32
 import java.net.URL;
35 33
 import java.net.URLDecoder;
36
-import java.security.CodeSource;
37 34
 
38 35
 /**
39 36
  * Provides utility methods for working with resources from the DMDirc distribution.
@@ -44,28 +41,6 @@ public final class DMDircResourceManager {
44 41
         //Prevent instantiation
45 42
     }
46 43
 
47
-    /**
48
-     * Returns the working directory for the application.
49
-     *
50
-     * @return Current working directory
51
-     */
52
-    public static synchronized String getWorkingDirectory() {
53
-        final CodeSource codeSource = Main.class.getProtectionDomain().getCodeSource();
54
-        try {
55
-            final File jarFile = new File(codeSource.getLocation().toURI().getPath());
56
-            final String jarDir;
57
-            if (isRunningFromJar()) {
58
-                jarDir = jarFile.getParentFile().getPath();
59
-            } else {
60
-                jarDir = jarFile.getPath();
61
-            }
62
-            return URLDecoder.decode(jarDir, "UTF-8");
63
-        } catch (URISyntaxException | UnsupportedEncodingException ex) {
64
-            Logger.appError(ErrorLevel.FATAL, "Unable to find working directory", ex);
65
-            return null;
66
-        }
67
-    }
68
-
69 44
     /**
70 45
      * Returns the working directory for the application or the jar name.
71 46
      *

Loading…
Cancel
Save