Fix error prone code

This commit is contained in:
Reinhard Pointner 2016-11-25 23:59:26 +08:00
parent 742e3aea2d
commit e11bab1ebf
14 changed files with 22 additions and 42 deletions

View File

@ -1,22 +1,19 @@
package net.filebot;
import static net.filebot.Logging.*;
import static net.filebot.Settings.*;
import static net.filebot.util.FileUtilities.*;
import java.io.File;
import java.util.logging.Level;
public enum ApplicationFolder {
// real user home (the user.home will point to the application-specific container in sandbox environments)
UserHome(isMacSandbox() ? "/Users/" + System.getProperty("user.name") : System.getProperty("user.home")),
UserHome(isMacSandbox() ? System.getProperty("UserHome") : System.getProperty("user.home")),
AppData(System.getProperty("application.dir", UserHome.path(".filebot").getPath())),
AppData(System.getProperty("application.dir", UserHome.resolve(".filebot").getPath())),
Temp(System.getProperty("java.io.tmpdir")),
Cache(System.getProperty("application.cache", AppData.path("cache").getPath()));
Cache(System.getProperty("application.cache", AppData.resolve("cache").getPath()));
private final File path;
@ -24,25 +21,12 @@ public enum ApplicationFolder {
this.path = new File(path);
}
public File get() {
public File getFile() {
return path;
}
public File path(String name) {
return new File(path, name);
}
public File resolve(String name) {
return new File(getCanonicalFile(), name);
}
public File getCanonicalFile() {
try {
return createFolders(path.getCanonicalFile());
} catch (Exception e) {
debug.log(Level.SEVERE, String.format("Failed to create application folder: %s => %s", this, path), e);
return path;
}
return new File(path, name);
}
}

View File

@ -67,7 +67,7 @@ public class CacheManager {
private File acquireDiskStore() throws IOException {
// prepare cache folder for this application instance
File cacheRoot = ApplicationFolder.Cache.getCanonicalFile();
File cacheRoot = ApplicationFolder.Cache.getFile();
for (int i = 0; i < 10; i++) {
File cache = new File(cacheRoot, Integer.toString(i));

View File

@ -31,7 +31,7 @@ public final class HistorySpooler {
Runtime.getRuntime().addShutdownHook(new Thread(HistorySpooler.getInstance()::commit, "HistorySpoolerShutdownHook")); // commit session history on shutdown
}
private final File persistentHistoryFile = ApplicationFolder.AppData.path("history.xml");
private final File persistentHistoryFile = ApplicationFolder.AppData.resolve("history.xml");
private int persistentHistoryTotalSize = -1;
private boolean persistentHistoryEnabled = true;

View File

@ -91,7 +91,7 @@ public class Main {
// clear caches
if (args.clearCache()) {
log.info("Clear cache");
for (File folder : getChildren(ApplicationFolder.Cache.getCanonicalFile(), FOLDERS)) {
for (File folder : getChildren(ApplicationFolder.Cache.getFile(), FOLDERS)) {
log.fine("* Delete " + folder);
delete(folder);
}
@ -109,7 +109,7 @@ public class Main {
initializeLogging(args);
// make sure java.io.tmpdir exists
createFolders(ApplicationFolder.Temp.get());
createFolders(ApplicationFolder.Temp.getFile());
// initialize this stuff before anything else
CacheManager.getInstance();
@ -365,7 +365,7 @@ public class Main {
System.setProperty("sun.net.client.defaultReadTimeout", "60000");
System.setProperty("swing.crossplatformlaf", "javax.swing.plaf.nimbus.NimbusLookAndFeel");
System.setProperty("grape.root", ApplicationFolder.AppData.path("grape").getPath());
System.setProperty("grape.root", ApplicationFolder.AppData.resolve("grape").getPath());
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");
if (args.unixfs) {
@ -389,7 +389,7 @@ public class Main {
// log errors to file
try {
Handler error = createSimpleFileHandler(ApplicationFolder.AppData.path("error.log"), Level.WARNING);
Handler error = createSimpleFileHandler(ApplicationFolder.AppData.resolve("error.log"), Level.WARNING);
log.addHandler(error);
debug.addHandler(error);
} catch (Exception e) {
@ -401,7 +401,7 @@ public class Main {
if (args.logFile != null) {
File logFile = new File(args.logFile);
if (!logFile.isAbsolute()) {
logFile = new File(ApplicationFolder.AppData.path("logs"), logFile.getPath()).getAbsoluteFile(); // by default resolve relative paths against {applicationFolder}/logs/{logFile}
logFile = new File(ApplicationFolder.AppData.resolve("logs"), logFile.getPath()).getAbsoluteFile(); // by default resolve relative paths against {applicationFolder}/logs/{logFile}
}
if (!logFile.exists() && !logFile.getParentFile().mkdirs() && !logFile.createNewFile()) {
throw new IOException("Failed to create log file: " + logFile);

View File

@ -191,10 +191,6 @@ public final class Settings {
return applicationArguments;
}
public static File getApplicationFolder() {
return ApplicationFolder.AppData.get(); // added for script compatibility
}
public static Settings forPackage(Class<?> type) {
return new Settings(Preferences.userNodeForPackage(type));
}

View File

@ -118,7 +118,7 @@ public class GroovyPad extends JFrame {
try {
// use this default value so people can easily submit bug reports with fn:sysinfo logs
File pad = ApplicationFolder.AppData.path("pad.groovy");
File pad = ApplicationFolder.AppData.resolve("pad.groovy");
if (!pad.exists()) {
ScriptShellMethods.saveAs(DEFAULT_SCRIPT, pad);

View File

@ -91,7 +91,7 @@ public class ExpressionFormatFunctions {
File f = new File(path);
if (!f.isAbsolute()) {
f = ApplicationFolder.UserHome.path(path);
f = ApplicationFolder.UserHome.resolve(path);
}
if (isMacSandbox()) {

View File

@ -928,7 +928,7 @@ public class MediaBindingBean {
@Define("home")
public File getUserHome() {
return ApplicationFolder.UserHome.getCanonicalFile();
return ApplicationFolder.UserHome.getFile();
}
@Define("output")

View File

@ -57,7 +57,7 @@ public class SecureCompiledScript extends CompiledScript {
// write permissions for cache and temp folders
for (ApplicationFolder it : ApplicationFolder.values()) {
permissions.add(new FilePermission(it.get() + File.separator + "-", "read, write, delete"));
permissions.add(new FilePermission(it.getFile().getAbsolutePath() + File.separator + "-", "read, write, delete"));
}
return permissions;

View File

@ -1012,7 +1012,7 @@ public class MediaDetection {
}
public static boolean isStructureRoot(File folder) throws Exception {
return isVolumeRoot(folder) || releaseInfo.getStructureRootPattern().matcher(folder.getName()).matches() || ApplicationFolder.UserHome.get().equals(folder.getParentFile());
return isVolumeRoot(folder) || releaseInfo.getStructureRootPattern().matcher(folder.getName()).matches() || ApplicationFolder.UserHome.getFile().equals(folder.getParentFile());
}
public static File getStructureRoot(File file) throws Exception {

View File

@ -220,7 +220,7 @@ public class ReleaseInfo {
if (volumeRoots == null) {
Set<File> volumes = new HashSet<File>();
File home = ApplicationFolder.UserHome.get();
File home = ApplicationFolder.UserHome.getFile();
List<File> roots = getFileSystemRoots();
// user root folder

View File

@ -83,7 +83,7 @@ class ExpressionFormatter implements MatchFormatter {
// resolve against home folder
if (destination.startsWith("~")) {
return ApplicationFolder.UserHome.path(destination.substring(1)).getAbsolutePath();
return ApplicationFolder.UserHome.resolve(destination.substring(1)).getAbsolutePath();
}
// try to resolve against structure root folder by default

View File

@ -160,7 +160,7 @@ public class RenamePanel extends JComponent {
renameModel.useFormatter(FileInfo.class, new FileNameFormatter());
}
RenameListCellRenderer cellrenderer = new RenameListCellRenderer(renameModel, ApplicationFolder.UserHome.getCanonicalFile());
RenameListCellRenderer cellrenderer = new RenameListCellRenderer(renameModel, ApplicationFolder.UserHome.getFile());
namesList.getListComponent().setCellRenderer(cellrenderer);
filesList.getListComponent().setCellRenderer(cellrenderer);

View File

@ -179,8 +179,8 @@ public final class FileUtilities {
}
}
public static File createFolders(File folder) throws IOException {
return Files.createDirectories(folder.toPath()).toFile();
public static void createFolders(File folder) throws IOException {
Files.createDirectories(folder.toPath());
}
private static final String WIN_THUMBNAIL_STORE = "Thumbs.db";