changeset 1339:714cbf75f775

Create directory structure on demand reviewed-by: omajid review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-November/008726.html
author Jon VanAlten <jon.vanalten@redhat.com>
date Fri, 15 Nov 2013 15:50:46 -0700
parents b13572b25f2a
children 6ab68f58f0a9
files config/src/main/java/com/redhat/thermostat/shared/config/Configuration.java config/src/test/java/com/redhat/thermostat/shared/config/ConfigurationTest.java web/server/src/main/webapp/WEB-INF/web.xml web/server/src/test/java/com/redhat/thermostat/web/server/WebStorageEndpointTest.java
diffstat 4 files changed, 103 insertions(+), 48 deletions(-) [+]
line wrap: on
line diff
--- a/config/src/main/java/com/redhat/thermostat/shared/config/Configuration.java	Tue Nov 19 12:06:01 2013 +0100
+++ b/config/src/main/java/com/redhat/thermostat/shared/config/Configuration.java	Fri Nov 15 15:50:46 2013 -0700
@@ -85,10 +85,21 @@
 
     private static final String THERMOSTAT_USER_DIR = ".thermostat";
 
-    private final String home;
+    private final File systemHome;
     private final UserDirectories userDirectories;
 
+    private static File defaultSystemUserPrefix;
+
     public Configuration() throws InvalidConfigurationException {
+        this(makeDir(null, "/"));
+    }
+
+    Configuration(String altTestingPrefix) {
+        this(makeDir(null, altTestingPrefix));
+    }
+
+    private Configuration(File defaultPrefix) {
+        Configuration.defaultSystemUserPrefix = defaultPrefix;
         // allow this to be specified also as a property, especially for
         // tests, this overrides the env setting
         String home = System.getProperty(THERMOSTAT_HOME);
@@ -99,7 +110,13 @@
         if (home == null) {
             throw new InvalidConfigurationException(THERMOSTAT_HOME + " not defined...");
         }
-        this.home = home;
+        this.systemHome = new File(home);
+        if (!systemHome.exists()) {
+            systemHome.mkdirs();
+        }
+        if (!systemHome.isDirectory()) {
+            throw new InvalidConfigurationException(THERMOSTAT_HOME + " is not a directory: " + home);
+        }
 
         String systemUser = System.getProperty(THERMOSTAT_SYSTEM_USER);
         if (systemUser == null) {
@@ -118,7 +135,7 @@
      */
 
     public File getSystemThermostatHome() throws InvalidConfigurationException {
-        return new File(home);
+        return systemHome;
     }
 
     public File getUserThermostatHome() throws InvalidConfigurationException {
@@ -126,23 +143,23 @@
     }
 
     public File getSystemPluginRoot() throws InvalidConfigurationException {
-        return new File(home, "plugins");
+        return makeDir(systemHome, "plugins");
     }
 
     public File getSystemLibRoot() throws InvalidConfigurationException {
-        return new File(home, "libs");
+        return makeDir(systemHome, "libs");
     }
     
     public File getSystemBinRoot() throws InvalidConfigurationException {
-        return new File(home, "bin");
+        return makeDir(systemHome, "bin");
     }
 
     public File getSystemNativeLibsRoot() throws InvalidConfigurationException {
-        return new File(getSystemLibRoot(), "native");
+        return makeDir(getSystemLibRoot(), "native");
     }
 
     public File getSystemConfigurationDirectory() throws InvalidConfigurationException {
-        return new File(getSystemThermostatHome(), "etc");
+        return makeDir(getSystemThermostatHome(), "etc");
     }
 
     public File getUserConfigurationDirectory() throws InvalidConfigurationException {
@@ -178,7 +195,7 @@
     }
 
     public File getUserStorageDirectory() throws InvalidConfigurationException {
-        return new File(getUserPersistentDataDirectory(), "db");
+        return makeDir(getUserPersistentDataDirectory(), "db");
     }
 
     public File getSystemStorageConfigurationFile() throws InvalidConfigurationException {
@@ -244,6 +261,21 @@
 
     }
 
+    private static File makeDir(File parent, String name) {
+        File dir = new File(parent, name);
+        boolean exists = dir.exists();
+        if (!exists) {
+            exists = dir.mkdirs();
+        }
+        if (!exists) {
+            throw new InvalidConfigurationException("Directory could not be created: " + dir.getAbsolutePath());
+        }
+        if (!dir.isDirectory()) {
+            throw new InvalidConfigurationException("File already exists but is not a directory: " + dir.getAbsolutePath());
+        }
+        return dir;
+    }
+
     /*
      * We need two different implementations because the paths are different. We
      * can't get clean paths by simply changing the prefix.
@@ -267,7 +299,7 @@
             if (userHome == null) {
                 userHome = System.getProperty("user.home") + File.separatorChar + THERMOSTAT_USER_DIR;
             }
-            this.userHome = new File(userHome);
+            this.userHome = makeDir(null, userHome);
         }
 
         public File getSystemRoot() throws InvalidConfigurationException {
@@ -276,27 +308,23 @@
 
 
         public File getUserConfigurationDirectory() throws InvalidConfigurationException {
-            return new File(getSystemRoot(), "etc");
+            return makeDir(getSystemRoot(), "etc");
         }
 
         public File getUserPersistentDataDirectory() throws InvalidConfigurationException {
-            File dataDir = new File(getSystemRoot(), "data");
-            return dataDir;
+            return makeDir(getSystemRoot(), "data");
         }
 
         public File getUserRuntimeDataDirectory() throws InvalidConfigurationException {
-            File runDir = new File(getSystemRoot(), "run");
-            return runDir;
+            return makeDir(getSystemRoot(), "run");
         }
 
         public File getUserLogDirectory() throws InvalidConfigurationException {
-            File logDir = new File(getSystemRoot(), "logs");
-            return logDir;
+            return makeDir(getSystemRoot(), "logs");
         }
 
         public File getUserCacheDirectory() throws InvalidConfigurationException {
-            File cacheDir = new File(getSystemRoot(), "cache");
-            return cacheDir;
+            return makeDir(getSystemRoot(), "cache");
         }
     }
 
@@ -311,9 +339,10 @@
                 userHome = System.getenv(USER_THERMOSTAT_HOME);
             }
             if (userHome == null) {
-                userHome = "/";
+                this.prefix = defaultSystemUserPrefix;
+            } else {
+                this.prefix = makeDir(null, userHome);
             }
-            this.prefix = new File(userHome);
         }
 
         public File getSystemRoot() throws InvalidConfigurationException {
@@ -322,27 +351,23 @@
 
 
         public File getUserConfigurationDirectory() throws InvalidConfigurationException {
-            return new File(getSystemRoot(), "etc/thermostat");
+            return makeDir(getSystemRoot(), "etc/thermostat");
         }
 
         public File getUserPersistentDataDirectory() throws InvalidConfigurationException {
-            File dataDir = new File(getSystemRoot(), "var/lib/thermostat");
-            return dataDir;
+            return makeDir(getSystemRoot(), "var/lib/thermostat");
         }
 
         public File getUserRuntimeDataDirectory() throws InvalidConfigurationException {
-            File runDir = new File(getSystemRoot(), "var/run/thermostat");
-            return runDir;
+            return makeDir(getSystemRoot(), "var/run/thermostat");
         }
 
         public File getUserLogDirectory() throws InvalidConfigurationException {
-            File logDir = new File(getSystemRoot(), "var/log/thermostat");
-            return logDir;
+            return makeDir(getSystemRoot(), "var/log/thermostat");
         }
 
         public File getUserCacheDirectory() throws InvalidConfigurationException {
-            File cacheDir = new File(getSystemRoot(), "var/cache/thermostat");
-            return cacheDir;
+            return makeDir(getSystemRoot(), "var/cache/thermostat");
         }
     }
 
--- a/config/src/test/java/com/redhat/thermostat/shared/config/ConfigurationTest.java	Tue Nov 19 12:06:01 2013 +0100
+++ b/config/src/test/java/com/redhat/thermostat/shared/config/ConfigurationTest.java	Fri Nov 15 15:50:46 2013 -0700
@@ -109,28 +109,28 @@
 
     @Test
     public void testPrivilegedUserLocations() throws InvalidConfigurationException, IOException {
-        String thermostatHome = "/tmp";
+        String thermostatHome = "/tmp/thermostat_test";
         System.setProperty("THERMOSTAT_HOME", thermostatHome);
         System.setProperty("THERMOSTAT_SYSTEM_USER", "");
-        Configuration config = new Configuration();
+        Configuration config = new Configuration(thermostatHome);
 
         // the paths are unix specific, but so are the paths in Configuration
 
-        Assert.assertEquals("/etc/thermostat/agent.properties", config.getUserAgentConfigurationFile().getCanonicalPath());
-        Assert.assertEquals("/etc/thermostat/agent.auth", config.getUserAgentAuthConfigFile().getCanonicalPath());
-        Assert.assertEquals("/etc/thermostat/db.properties", config.getUserStorageConfigurationFile().getCanonicalPath());
+        Assert.assertEquals("/tmp/thermostat_test/etc/thermostat/agent.properties", config.getUserAgentConfigurationFile().getCanonicalPath());
+        Assert.assertEquals("/tmp/thermostat_test/etc/thermostat/agent.auth", config.getUserAgentAuthConfigFile().getCanonicalPath());
+        Assert.assertEquals("/tmp/thermostat_test/etc/thermostat/db.properties", config.getUserStorageConfigurationFile().getCanonicalPath());
 
-        Assert.assertEquals("/var/lib/thermostat/db", config.getUserStorageDirectory().getCanonicalPath());
-        Assert.assertEquals("/var/run/thermostat/db.pid", config.getUserStoragePidFile().getAbsolutePath());
-        Assert.assertEquals("/var/log/thermostat/db.log", config.getUserStorageLogFile().getCanonicalPath());
+        Assert.assertEquals("/tmp/thermostat_test/var/lib/thermostat/db", config.getUserStorageDirectory().getCanonicalPath());
+        Assert.assertEquals("/tmp/thermostat_test/var/run/thermostat/db.pid", config.getUserStoragePidFile().getAbsolutePath());
+        Assert.assertEquals("/tmp/thermostat_test/var/log/thermostat/db.log", config.getUserStorageLogFile().getCanonicalPath());
 
-        Assert.assertEquals("/var/lib/thermostat/plugins", config.getUserPluginRoot().getCanonicalPath());
+        Assert.assertEquals("/tmp/thermostat_test/var/lib/thermostat/plugins", config.getUserPluginRoot().getCanonicalPath());
     }
 
     @Test
     public void testPrivilegedUserLocationsWithPrefix() throws InvalidConfigurationException, IOException {
         String thermostatHome = "/tmp";
-        String prefix = "/opt/custom/prefix";
+        String prefix = "/tmp/opt/custom/prefix";
         System.setProperty("THERMOSTAT_HOME", thermostatHome);
         System.setProperty("USER_THERMOSTAT_HOME", prefix);
         System.setProperty("THERMOSTAT_SYSTEM_USER", "");
--- a/web/server/src/main/webapp/WEB-INF/web.xml	Tue Nov 19 12:06:01 2013 +0100
+++ b/web/server/src/main/webapp/WEB-INF/web.xml	Fri Nov 15 15:50:46 2013 -0700
@@ -38,4 +38,15 @@
   <security-role>
     <role-name>thermostat-realm</role-name>
   </security-role>
+
+  <!-- THERMOSTAT_HOME is set via the listener below -->
+  <context-param>
+    <param-name>THERMOSTAT_HOME</param-name>
+    <param-value>/tmp/test-thermostat-home</param-value>
+  </context-param>
+
+  <listener>
+    <listener-class>com.redhat.thermostat.web.server.PropertySettingServletContextListener</listener-class>
+  </listener>
+
 </web-app>
--- a/web/server/src/test/java/com/redhat/thermostat/web/server/WebStorageEndpointTest.java	Tue Nov 19 12:06:01 2013 +0100
+++ b/web/server/src/test/java/com/redhat/thermostat/web/server/WebStorageEndpointTest.java	Fri Nov 15 15:50:46 2013 -0700
@@ -63,6 +63,8 @@
 import java.net.URL;
 import java.net.URLEncoder;
 import java.security.Principal;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -183,6 +185,22 @@
     private static Key<Integer> key2;
     private static Category<TestClass> category;
     private static String categoryName = "test";
+    private static String originalThermostatHome;
+
+    @BeforeClass
+    public static void setupThermostatHome() {
+        Path tempHomePath = null;
+        try {
+            tempHomePath = Files.createTempDirectory("WebStorageEndpointTest_THERMOSTAT_HOME");
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        File fakeHome = tempHomePath.toFile();
+        fakeHome.deleteOnExit();
+        assertTrue(fakeHome.canRead());
+        originalThermostatHome = System.getProperty("THERMOSTAT_HOME");
+        System.setProperty("THERMOSTAT_HOME", fakeHome.getAbsolutePath());
+    }
 
     @BeforeClass
     public static void setupCategory() {
@@ -192,6 +210,15 @@
     }
 
     @AfterClass
+    public static void cleanupThermostatHome() {
+        if (originalThermostatHome != null) {
+            System.setProperty("THERMOSTAT_HOME", originalThermostatHome);
+        } else {
+            System.clearProperty("THERMOSTAT_HOME");
+        }
+    }
+
+    @AfterClass
     public static void cleanupCategory() {
         Categories.remove(category);
         category = null;
@@ -201,13 +228,6 @@
 
     @Before
     public void setUp() throws Exception {
-
-        // Set thermostat home to something existing and readable
-        File fakeHome = new File(getClass().getResource("/broken_test_roles.properties").getFile());
-        // fakeHome does not need to be a real THERMOSTAT_HOME, but needs to
-        // be readable and must exist.
-        assertTrue(fakeHome.canRead());
-        System.setProperty("THERMOSTAT_HOME", fakeHome.getAbsolutePath());
         
         mockStorage = mock(BackingStorage.class);
         StorageWrapper.setStorage(mockStorage);
@@ -223,7 +243,6 @@
 
     @After
     public void tearDown() throws Exception {
-        System.clearProperty("THERMOSTAT_HOME");
         
         // some tests don't use server
         if (server != null) {