From 582761e663a2485a77045d28a3963f3e51ecca5d Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Thu, 27 Nov 2014 09:39:27 +0100 Subject: [PATCH] Clarify naming and role of clean flag Prior to this change, the "clean flag" was named `user.data.clean.dir`, but in fact it cleaned the *app* data dir, not the *user* data dir. This is the difference betwen deleting ~/Library/Application Support (the "user data dir") vs. ~/Library/Application Support/Bitsquare (the "app data dir") The name of this flag has been updated to `--app.data.dir.clean` to reflect the fact that it cleans the latter vs. the former. The processing of this flag has also been updated. It's default value (false) is now assigned during the creation of the default properties source in BitsquareEnvironment, and instead of reading and handling the flag directly in the BitsquareEnvironment constructor, we now handle cleaning in BitsquareApp#initAppDir, where logic for handling the creation of the directory already exists. See #291 --- .../io/bitsquare/app/BitsquareEnvironment.java | 15 +++++---------- .../java/io/bitsquare/app/gui/BitsquareApp.java | 13 ++++++++----- .../io/bitsquare/app/gui/BitsquareAppMain.java | 6 +++--- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/main/java/io/bitsquare/app/BitsquareEnvironment.java b/src/main/java/io/bitsquare/app/BitsquareEnvironment.java index dff4796ea3..1686cbde3b 100644 --- a/src/main/java/io/bitsquare/app/BitsquareEnvironment.java +++ b/src/main/java/io/bitsquare/app/BitsquareEnvironment.java @@ -25,8 +25,6 @@ import io.bitsquare.persistence.Persistence; import io.bitsquare.util.Utilities; import io.bitsquare.util.spring.JOptCommandLinePropertySource; -import java.io.File; - import java.nio.file.Paths; import java.util.Properties; @@ -40,7 +38,6 @@ import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.support.ResourcePropertySource; -import org.springframework.util.FileSystemUtils; import static com.google.common.base.Preconditions.checkNotNull; @@ -48,7 +45,6 @@ public class BitsquareEnvironment extends StandardEnvironment { public static final String APP_VERSION_KEY = "app.version"; - public static final String USER_DATA_CLEAN_DIR_KEY = "user.data.clean.dir"; public static final String USER_DATA_DIR_KEY = "user.data.dir"; public static final String DEFAULT_USER_DATA_DIR = defaultUserDataDir(); @@ -58,6 +54,9 @@ public class BitsquareEnvironment extends StandardEnvironment { public static final String APP_DATA_DIR_KEY = "app.data.dir"; public static final String DEFAULT_APP_DATA_DIR = appDataDir(DEFAULT_USER_DATA_DIR, DEFAULT_APP_NAME); + public static final String APP_DATA_DIR_CLEAN_KEY = "app.data.dir.clean"; + public static final String DEFAULT_APP_DATA_DIR_CLEAN = "false"; + static final String BITSQUARE_DEFAULT_PROPERTY_SOURCE_NAME = "bitsquareDefaultProperties"; static final String BITSQUARE_CLASSPATH_PROPERTY_SOURCE_NAME = "bitsquareClasspathProperties"; static final String BITSQUARE_FILESYSTEM_PROPERTY_SOURCE_NAME = "bitsquareFilesystemProperties"; @@ -94,12 +93,6 @@ public class BitsquareEnvironment extends StandardEnvironment { } catch (Exception ex) { throw new BitsquareException(ex); } - - boolean cleanUserDataDir = commandLineProperties.containsProperty(USER_DATA_CLEAN_DIR_KEY) && - commandLineProperties.getProperty(USER_DATA_CLEAN_DIR_KEY).equals("true"); - if (cleanUserDataDir) { - FileSystemUtils.deleteRecursively(new File(appDataDir)); - } } @@ -121,6 +114,8 @@ public class BitsquareEnvironment extends StandardEnvironment { PropertySource defaultProperties() throws Exception { return new PropertiesPropertySource(BITSQUARE_DEFAULT_PROPERTY_SOURCE_NAME, new Properties() {{ setProperty(APP_DATA_DIR_KEY, appDataDir); + setProperty(APP_DATA_DIR_CLEAN_KEY, DEFAULT_APP_DATA_DIR_CLEAN); + setProperty(APP_NAME_KEY, appName); setProperty(UserAgent.NAME_KEY, appName); diff --git a/src/main/java/io/bitsquare/app/gui/BitsquareApp.java b/src/main/java/io/bitsquare/app/gui/BitsquareApp.java index 7df2909cd3..928817f4a5 100644 --- a/src/main/java/io/bitsquare/app/gui/BitsquareApp.java +++ b/src/main/java/io/bitsquare/app/gui/BitsquareApp.java @@ -50,6 +50,7 @@ import javafx.scene.input.*; import javafx.stage.Stage; import org.springframework.core.env.Environment; +import org.springframework.util.FileSystemUtils; import static io.bitsquare.app.BitsquareEnvironment.*; @@ -78,7 +79,7 @@ public class BitsquareApp extends Application { // initialize the application data directory (if necessary) - initAppDir(env.getRequiredProperty(APP_DATA_DIR_KEY)); + initAppDir(env.getRequiredProperty(APP_DATA_DIR_KEY), env.getProperty(APP_DATA_DIR_CLEAN_KEY, boolean.class)); // load and apply any stored settings @@ -152,13 +153,15 @@ public class BitsquareApp extends Application { } - private void initAppDir(String appDir) { + private void initAppDir(String appDir, boolean doClean) { Path dir = Paths.get(appDir); if (Files.exists(dir)) { - if (!Files.isWritable(dir)) { + if (!Files.isWritable(dir)) throw new BitsquareException("Application data directory '%s' is not writeable", dir); - } - return; + if (doClean) + FileSystemUtils.deleteRecursively(dir.toFile()); + else + return; } try { Files.createDirectory(dir); diff --git a/src/main/java/io/bitsquare/app/gui/BitsquareAppMain.java b/src/main/java/io/bitsquare/app/gui/BitsquareAppMain.java index 37b2502815..6a9560a778 100644 --- a/src/main/java/io/bitsquare/app/gui/BitsquareAppMain.java +++ b/src/main/java/io/bitsquare/app/gui/BitsquareAppMain.java @@ -41,13 +41,13 @@ public class BitsquareAppMain extends BitsquareExecutable { protected void customizeOptionParsing(OptionParser parser) { parser.accepts(USER_DATA_DIR_KEY, description("User data directory", DEFAULT_USER_DATA_DIR)) .withRequiredArg(); - parser.accepts(USER_DATA_CLEAN_DIR_KEY, description("Clean user data directory", false)) - .withRequiredArg() - .ofType(boolean.class); parser.accepts(APP_NAME_KEY, description("Application name", DEFAULT_APP_NAME)) .withRequiredArg(); parser.accepts(APP_DATA_DIR_KEY, description("Application data directory", DEFAULT_APP_DATA_DIR)) .withRequiredArg(); + parser.accepts(APP_DATA_DIR_CLEAN_KEY, description("Clean application data dir", DEFAULT_APP_DATA_DIR_CLEAN)) + .withRequiredArg() + .ofType(boolean.class); parser.accepts(NAME_KEY, description("Name of this node", null)) .withRequiredArg(); parser.accepts(PORT_KEY, description("Port to listen on", Node.DEFAULT_PORT))