changeset 2541:9c6f95f61014

Use java.io.tmpdir for default Unix sockets location Reviewed-by: jerboaa Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2016-November/021627.html
author Elliott Baron <ebaron@redhat.com>
date Mon, 05 Dec 2016 11:14:58 -0500
parents e2ad1b41260b
children 80c3708bd5e4
files agent/ipc/unix-socket/common/src/main/java/com/redhat/thermostat/agent/ipc/unixsocket/common/internal/UnixSocketIPCProperties.java agent/ipc/unix-socket/common/src/test/java/com/redhat/thermostat/agent/ipc/unixsocket/common/internal/UnixSocketIPCPropertiesTest.java
diffstat 2 files changed, 7 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/agent/ipc/unix-socket/common/src/main/java/com/redhat/thermostat/agent/ipc/unixsocket/common/internal/UnixSocketIPCProperties.java	Thu Dec 01 16:30:37 2016 -0500
+++ b/agent/ipc/unix-socket/common/src/main/java/com/redhat/thermostat/agent/ipc/unixsocket/common/internal/UnixSocketIPCProperties.java	Mon Dec 05 11:14:58 2016 -0500
@@ -81,29 +81,15 @@
     }
     
     /*
-     * Default socket directory is calculated using the first available from the following:
-     * 1. Environment variable "$XDG_RUNTIME_DIR" (e.g. /run/user/1000/)
-     * 2. System property "java.io.tmpdir", with a subdirectory named from
-     *    the value of system property "user.name" (e.g. /tmp/alice/)
+     * Socket directory should be accessible by all users, by default we use
+     * the value of "java.io.tmpdir".
      */
     private File getDefaultSocketDir() throws IOException {
-        File result;
-        // First check XDG_RUNTIME_DIR
-        String path = pathUtils.getEnvironmentVariable("XDG_RUNTIME_DIR");
-        if (path != null) {
-            result = new File(path);
-        } else {
-            // Fall back to java.io.tmpdir
-            path = pathUtils.getSystemProperty("java.io.tmpdir");
-            if (path == null) {
-                throw new IOException("Failed to build default socket directory");
-            }
-            String username = pathUtils.getSystemProperty("user.name");
-            if (username == null) {
-                throw new IOException("Unable to build socket directory path without username");
-            }
-            result = new File(path, username);
+        String path = pathUtils.getSystemProperty("java.io.tmpdir");
+        if (path == null) {
+            throw new IOException("Failed to build default socket directory");
         }
+        File result = new File(path);
         
         // Append our socket directory name
         result = new File(result, SOCKET_DIR_NAME);
@@ -115,8 +101,5 @@
         String getSystemProperty(String name) {
             return System.getProperty(name);
         }
-        String getEnvironmentVariable(String name) {
-            return System.getenv(name);
-        }
     }
 }
--- a/agent/ipc/unix-socket/common/src/test/java/com/redhat/thermostat/agent/ipc/unixsocket/common/internal/UnixSocketIPCPropertiesTest.java	Thu Dec 01 16:30:37 2016 -0500
+++ b/agent/ipc/unix-socket/common/src/test/java/com/redhat/thermostat/agent/ipc/unixsocket/common/internal/UnixSocketIPCPropertiesTest.java	Mon Dec 05 11:14:58 2016 -0500
@@ -80,20 +80,11 @@
     }
     
     @Test
-    public void testDefaultSocketDirectoryXDG() throws Exception {
-        when(jProps.getProperty(UnixSocketIPCProperties.PROP_UNIX_SOCKET_DIR)).thenReturn(null);
-        when(pathUtils.getEnvironmentVariable("XDG_RUNTIME_DIR")).thenReturn("/path/to/xdg/runtime");
-        UnixSocketIPCProperties props = new UnixSocketIPCProperties(jProps, propFile, pathUtils);
-        assertEquals("/path/to/xdg/runtime/thermostat-socks", props.getSocketDirectory().getAbsolutePath());
-    }
-    
-    @Test
     public void testDefaultSocketDirectoryTmp() throws Exception {
         when(jProps.getProperty(UnixSocketIPCProperties.PROP_UNIX_SOCKET_DIR)).thenReturn(null);
         when(pathUtils.getSystemProperty("java.io.tmpdir")).thenReturn("/path/to/tmp");
-        when(pathUtils.getSystemProperty("user.name")).thenReturn("myUserName");
         UnixSocketIPCProperties props = new UnixSocketIPCProperties(jProps, propFile, pathUtils);
-        assertEquals("/path/to/tmp/myUserName/thermostat-socks", props.getSocketDirectory().getAbsolutePath());
+        assertEquals("/path/to/tmp/thermostat-socks", props.getSocketDirectory().getAbsolutePath());
     }
     
     @Test(expected=IOException.class)
@@ -102,11 +93,4 @@
         new UnixSocketIPCProperties(jProps, propFile, pathUtils);
     }
     
-    @Test(expected=IOException.class)
-    public void testNoUsername() throws Exception {
-        when(jProps.getProperty(UnixSocketIPCProperties.PROP_UNIX_SOCKET_DIR)).thenReturn(null);
-        when(pathUtils.getSystemProperty("java.io.tmpdir")).thenReturn("/path/to/tmp");
-        new UnixSocketIPCProperties(jProps, propFile, pathUtils);
-    }
-    
 }