Browse Source

Switch some file usages to paths.

pull/23/head
Chris Smith 9 years ago
parent
commit
7da1c15401

+ 5
- 5
src/com/dmdirc/commandparser/aliases/AliasesModule.java View File

@@ -27,7 +27,7 @@ import com.dmdirc.commandline.CommandLineOptionsModule.DirectoryType;
27 27
 import com.dmdirc.interfaces.Migrator;
28 28
 import com.dmdirc.interfaces.SystemLifecycleComponent;
29 29
 
30
-import java.nio.file.Paths;
30
+import java.nio.file.Path;
31 31
 
32 32
 import javax.inject.Singleton;
33 33
 
@@ -43,9 +43,9 @@ public class AliasesModule {
43 43
     @Provides
44 44
     @Singleton
45 45
     public AliasStore getAliasStore(
46
-            @Directory(DirectoryType.BASE) final String directory,
46
+            @Directory(DirectoryType.BASE) final Path directory,
47 47
             final AliasFactory factory) {
48
-        return new YamlAliasStore(Paths.get(directory, "aliases.yml"), factory);
48
+        return new YamlAliasStore(directory.resolve("aliases.yml"), factory);
49 49
     }
50 50
 
51 51
     @Provides(type = Provides.Type.SET)
@@ -59,8 +59,8 @@ public class AliasesModule {
59 59
     }
60 60
 
61 61
     @Provides(type = Provides.Type.SET)
62
-    public Migrator getDefaultsMigrator(@Directory(DirectoryType.BASE) final String directory) {
63
-        return new DefaultAliasInstaller(Paths.get(directory, "aliases.yml"));
62
+    public Migrator getDefaultsMigrator(@Directory(DirectoryType.BASE) final Path directory) {
63
+        return new DefaultAliasInstaller(directory.resolve("aliases.yml"));
64 64
     }
65 65
 
66 66
 }

+ 3
- 3
src/com/dmdirc/commandparser/auto/AutoCommandModule.java View File

@@ -25,7 +25,7 @@ package com.dmdirc.commandparser.auto;
25 25
 import com.dmdirc.interfaces.Migrator;
26 26
 import com.dmdirc.interfaces.SystemLifecycleComponent;
27 27
 
28
-import java.nio.file.Paths;
28
+import java.nio.file.Path;
29 29
 
30 30
 import javax.inject.Singleton;
31 31
 
@@ -44,8 +44,8 @@ public class AutoCommandModule {
44 44
     @Provides
45 45
     @Singleton
46 46
     public AutoCommandStore getAutoCommandStore(
47
-            @Directory(DirectoryType.BASE) final String directory) {
48
-        return new YamlAutoCommandStore(Paths.get(directory, "auto-commands.yml"));
47
+            @Directory(DirectoryType.BASE) final Path directory) {
48
+        return new YamlAutoCommandStore(directory.resolve("auto-commands.yml"));
49 49
     }
50 50
 
51 51
     @Provides(type = Provides.Type.SET)

+ 1
- 1
src/com/dmdirc/config/ConfigModule.java View File

@@ -63,7 +63,7 @@ public class ConfigModule {
63 63
     public IdentityManager getIdentityManager(
64 64
             @Directory(DirectoryType.BASE) final Path baseDirectory,
65 65
             @Directory(DirectoryType.IDENTITIES) final Path identitiesDirectory,
66
-            @Directory(DirectoryType.ERRORS) final String errorsDirectory,
66
+            @Directory(DirectoryType.ERRORS) final Path errorsDirectory,
67 67
             final CommandLineParser commandLineParser,
68 68
             final DMDircMBassador eventBus) {
69 69
         final IdentityManager identityManager = new IdentityManager(baseDirectory,

+ 3
- 2
src/com/dmdirc/logger/ErrorManager.java View File

@@ -31,6 +31,7 @@ import com.dmdirc.ui.FatalErrorDialog;
31 31
 import com.dmdirc.util.collections.ListenerList;
32 32
 
33 33
 import java.awt.GraphicsEnvironment;
34
+import java.nio.file.Path;
34 35
 import java.util.Date;
35 36
 import java.util.LinkedList;
36 37
 import java.util.List;
@@ -72,7 +73,7 @@ public class ErrorManager implements ConfigChangeListener {
72 73
     /** Config to read settings from. */
73 74
     private AggregateConfigProvider config;
74 75
     /** Directory to store errors in. */
75
-    private String errorsDirectory;
76
+    private Path errorsDirectory;
76 77
 
77 78
     /** Creates a new instance of ErrorListDialog. */
78 79
     public ErrorManager() {
@@ -87,7 +88,7 @@ public class ErrorManager implements ConfigChangeListener {
87 88
      * @param directory    The directory to store errors in, if enabled.
88 89
      * @param eventBus     The event bus to listen for error events on.
89 90
      */
90
-    public void initialise(final AggregateConfigProvider globalConfig, final String directory,
91
+    public void initialise(final AggregateConfigProvider globalConfig, final Path directory,
91 92
             final DMDircMBassador eventBus) {
92 93
         eventBus.subscribe(this);
93 94
         RavenFactory.registerFactory(new DefaultRavenFactory());

+ 31
- 22
src/com/dmdirc/logger/ProgramError.java View File

@@ -22,12 +22,12 @@
22 22
 
23 23
 package com.dmdirc.logger;
24 24
 
25
-import java.io.File;
26
-import java.io.FileOutputStream;
27 25
 import java.io.IOException;
28 26
 import java.io.OutputStream;
29 27
 import java.io.PrintWriter;
30 28
 import java.io.Serializable;
29
+import java.nio.file.Files;
30
+import java.nio.file.Path;
31 31
 import java.text.DateFormat;
32 32
 import java.text.SimpleDateFormat;
33 33
 import java.util.Date;
@@ -45,7 +45,7 @@ public final class ProgramError implements Serializable {
45 45
     /** A version number for this class. */
46 46
     private static final long serialVersionUID = 3;
47 47
     /** Directory used to store errors. */
48
-    private static File errorDir;
48
+    private static Path errorDir;
49 49
     /** Semaphore used to serialise write access. */
50 50
     private static final Semaphore WRITING_SEM = new Semaphore(1);
51 51
     /** The reporter to use to send this error. */
@@ -208,7 +208,7 @@ public final class ProgramError implements Serializable {
208 208
      *
209 209
      * @param directory The directory to save the error in.
210 210
      */
211
-    public void save(final String directory) {
211
+    public void save(final Path directory) {
212 212
         try (PrintWriter out = new PrintWriter(getErrorFile(directory), true)) {
213 213
             out.println("Date:" + getDate());
214 214
             out.println("Level: " + getLevel());
@@ -229,30 +229,39 @@ public final class ProgramError implements Serializable {
229 229
      * @return BufferedOutputStream to write to the error file
230 230
      */
231 231
     @SuppressWarnings("PMD.SystemPrintln")
232
-    private OutputStream getErrorFile(final String directory) {
232
+    private OutputStream getErrorFile(final Path directory) {
233 233
         WRITING_SEM.acquireUninterruptibly();
234 234
 
235
-        if (errorDir == null || !errorDir.exists()) {
236
-            errorDir = new File(directory);
237
-            if (!errorDir.exists()) {
238
-                errorDir.mkdirs();
235
+        try {
236
+            if (errorDir == null || !Files.exists(errorDir)) {
237
+                errorDir = directory;
238
+                if (!Files.exists(errorDir)) {
239
+                    Files.createDirectories(errorDir);
240
+                }
239 241
             }
240
-        }
241
-
242
-        final String logName = getDate().getTime() + "-" + getLevel();
243 242
 
244
-        final File errorFile = new File(errorDir, logName + ".log");
245
-
246
-        if (errorFile.exists()) {
247
-            boolean rename = false;
248
-            for (int i = 0; !rename; i++) {
249
-                rename = errorFile.renameTo(new File(errorDir, logName + '-' + i + ".log"));
243
+            final String logName = getDate().getTime() + "-" + getLevel();
244
+
245
+            final Path errorFile = errorDir.resolve(logName + ".log");
246
+
247
+            if (Files.exists(errorFile)) {
248
+                boolean rename = false;
249
+                for (int i = 0; !rename; i++) {
250
+                    try {
251
+                        Files.move(errorFile, errorDir.resolve(logName + '-' + i + ".log"));
252
+                        rename = true;
253
+                    } catch (IOException ex) {
254
+                        rename = false;
255
+                        if (i > 20) {
256
+                            // Something's probably catestrophically wrong. Give up.
257
+                            throw ex;
258
+                        }
259
+                    }
260
+                }
250 261
             }
251
-        }
252 262
 
253
-        try {
254
-            errorFile.createNewFile();
255
-            return new FileOutputStream(errorFile);
263
+            Files.createFile(errorFile);
264
+            return Files.newOutputStream(errorFile);
256 265
         } catch (IOException ex) {
257 266
             System.err.println("Error creating new file: ");
258 267
             ex.printStackTrace();

Loading…
Cancel
Save