lttng: Do not use java.io.tmpdir property (/tmp) when possible
authorMarc-Andre Laperle <marc-andre.laperle@ericsson.com>
Mon, 22 Sep 2014 19:57:28 +0000 (15:57 -0400)
committerMarc-Andre Laperle <marc-andre.laperle@ericsson.com>
Sat, 27 Sep 2014 18:19:15 +0000 (14:19 -0400)
This is problematic when multiple instances of Eclipse are running
(builds).
Instead, we can create a "temporary" folder under the Eclipse workspace
so
that each Eclipse instances are independant. This also has the advantage
of
the directory being cleared every time a mvn clean is executed or when a
JUnit launch configuration is used inside Eclipse.

Change-Id: I3c7fb17d4b36e7ba5fe054ea9d81667c465e75b6
Signed-off-by: Marc-Andre Laperle <marc-andre.laperle@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/33728
Tested-by: Hudson CI
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/CtfCoreTestPlugin.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/synthetictraces/LttngKernelTraceGenerator.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/trace/IOstructgenTest.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfTraceManager.java
org.eclipse.linuxtools.tmf.ctf.core.tests/src/org/eclipse/linuxtools/tmf/ctf/core/tests/tracemanager/TmfTraceManagerTest.java
org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/trace/CustomTxtIndexTest.java
org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/trace/CustomXmlIndexTest.java

index 40ee63ab3654c849b451ce9c32813dd9db625159..dbd4c11749177c2e48cc6f4eb3fe76aa4f1d0a2b 100644 (file)
 
 package org.eclipse.linuxtools.ctf.core.tests;
 
+import java.io.File;
+import java.net.URI;
+import java.net.URISyntaxException;
+
 import org.eclipse.core.runtime.Plugin;
+import org.eclipse.linuxtools.internal.ctf.core.Activator;
 import org.osgi.framework.BundleContext;
 
 /**
@@ -20,6 +25,8 @@ import org.osgi.framework.BundleContext;
  */
 public class CtfCoreTestPlugin extends Plugin {
 
+    private static final String TEMP_DIR_NAME = ".temp"; //$NON-NLS-1$
+
     // ------------------------------------------------------------------------
     // Attributes
     // ------------------------------------------------------------------------
@@ -76,4 +83,27 @@ public class CtfCoreTestPlugin extends Plugin {
         super.stop(context);
     }
 
+    /**
+     * Get the temporary directory path. If there is an instance of Eclipse
+     * running, the temporary directory will reside under the workspace.
+     *
+     * @return the temporary directory path suitable to be passed to the
+     *         java.io.File constructor without a trailing separator
+     */
+    public static String getTemporaryDirPath() {
+        String property = System.getProperty("osgi.instance.area"); //$NON-NLS-1$
+        if (property != null) {
+            try {
+                File dir = new File(new URI(property));
+                dir = new File(dir.getAbsolutePath() + File.separator + TEMP_DIR_NAME);
+                if (!dir.exists()) {
+                    dir.mkdirs();
+                }
+                return dir.getAbsolutePath();
+            } catch (URISyntaxException e) {
+                Activator.logError(e.getLocalizedMessage(), e);
+            }
+        }
+        return System.getProperty("java.io.tmpdir"); //$NON-NLS-1$
+    }
 }
index f1cb365e705e39eb6aa683f2a03d51267848078a..07d3ecab17668d2c15555456150f718c08e00595 100644 (file)
@@ -180,7 +180,7 @@ public class LttngKernelTraceGenerator {
      */
     public static void main(String[] args) {
         // not using createTempFile as this is a directory
-        String path = System.getProperty("java.io.tmpdir") + File.separator + TRACE_NAME;
+        String path = CtfCoreTestPlugin.getTemporaryDirPath() + File.separator + TRACE_NAME;
         generateLttngKernelTrace(new File(path));
     }
 
index 9b2f7a17037ec3cfaf50d4c9c5b02f4db3f217e2..af166cb16cf8fd6ca278f63bf4b62d45c7ff5c6b 100644 (file)
@@ -25,6 +25,7 @@ import java.util.ArrayList;
 import java.util.List;
 
 import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration;
+import org.eclipse.linuxtools.ctf.core.tests.CtfCoreTestPlugin;
 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
 import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
 import org.eclipse.linuxtools.internal.ctf.core.event.EventDeclaration;
@@ -273,7 +274,7 @@ public class IOstructgenTest {
     private static final String allDressedTSDL = metadataDecs + environmentMD + clockMD
             + ctfStart + ctfHeaders + ctfBody + enumMd + callsiteMD;
 
-    static final String tempTraceDir = System.getProperty("java.io.tmpdir")
+    static final String tempTraceDir = CtfCoreTestPlugin.getTemporaryDirPath()
             + File.separator + "tempTrace";
 
     private static final int DATA_SIZE = 4096;
index 3536a0c66962a419cd7bc2a6b0823a27a4cb5aa0..fc54fd05542cdf3b17843efef46bcda576665e3c 100644 (file)
@@ -15,6 +15,8 @@
 package org.eclipse.linuxtools.tmf.core.trace;
 
 import java.io.File;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.LinkedHashMap;
@@ -66,6 +68,8 @@ public final class TmfTraceManager {
     /** The currently-selected trace. Should always be part of the trace map */
     private ITmfTrace fCurrentTrace = null;
 
+    private static final String TEMP_DIR_NAME = ".temp"; //$NON-NLS-1$
+
     // ------------------------------------------------------------------------
     // Constructor
     // ------------------------------------------------------------------------
@@ -476,12 +480,38 @@ public final class TmfTraceManager {
         return new TmfTimeRange(start, end);
     }
 
+    /**
+     * Get the temporary directory path. If there is an instance of Eclipse
+     * running, the temporary directory will reside under the workspace.
+     *
+     * @return the temporary directory path suitable to be passed to the
+     *         java.io.File constructor without a trailing separator
+     * @since 3.1
+     */
+    public static String getTemporaryDirPath() {
+        // Get the workspace path from the properties
+        String property = System.getProperty("osgi.instance.area"); //$NON-NLS-1$
+        if (property != null) {
+            try {
+                File dir = new File(new URI(property));
+                dir = new File(dir.getAbsolutePath() + File.separator + TEMP_DIR_NAME);
+                if (!dir.exists()) {
+                    dir.mkdirs();
+                }
+                return dir.getAbsolutePath();
+            } catch (URISyntaxException e) {
+                Activator.logError(e.getLocalizedMessage(), e);
+            }
+        }
+        return System.getProperty("java.io.tmpdir"); //$NON-NLS-1$
+    }
+
     /**
      * Get a temporary directory based on a trace's name. We will create the
      * directory if it doesn't exist, so that it's ready to be used.
      */
     private static String getTemporaryDir(ITmfTrace trace) {
-        String pathName = System.getProperty("java.io.tmpdir") + //$NON-NLS-1$
+        String pathName = getTemporaryDirPath() +
             File.separator +
             trace.getName() +
             File.separator;
index 68c994960cfd01a29d9cf769f749c517d4f936cd..5919d827cc4bb279f175177f325ea9902e7c5a72 100644 (file)
@@ -222,7 +222,7 @@ public class TmfTraceManagerTest {
     public void testSupplementaryFileDir() {
         String name1 = trace1.getName();
         String name2 = trace2.getName();
-        String basePath = System.getProperty("java.io.tmpdir") + File.separator;
+        String basePath = TmfTraceManager.getTemporaryDirPath() + File.separator;
 
         String expected1 = basePath + name1 + File.separator;
         String expected2 = basePath + name2 + File.separator;
index d398ba841d21b151d9cadc028ffa893076693e25..ef2910881793deb6be033b2458eddf4b35bc7ee0 100644 (file)
@@ -24,6 +24,7 @@ import java.util.Date;
 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
 import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomTxtTrace;
 import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomTxtTraceDefinition;
+import org.eclipse.linuxtools.tmf.core.trace.TmfTraceManager;
 import org.eclipse.linuxtools.tmf.core.trace.indexer.ITmfTraceIndexer;
 
 /**
@@ -33,7 +34,7 @@ import org.eclipse.linuxtools.tmf.core.trace.indexer.ITmfTraceIndexer;
  */
 public class CustomTxtIndexTest extends AbstractCustomTraceIndexTest {
 
-    private static final String TRACE_DIRECTORY = System.getProperty("java.io.tmpdir") + File.separator + "dummyTxtTrace";
+    private static final String TRACE_DIRECTORY = TmfTraceManager.getTemporaryDirPath() + File.separator + "dummyTxtTrace";
     private static final String TRACE_PATH = TRACE_DIRECTORY + File.separator + "test.txt";
     private static final String DEFINITION_PATH = "tracesets" + File.separator + "txt" + File.separator + "testTxtDefinition.xml";
 
index ed603802872a9eb94d0f7ed86c85962db2a6863a..bc1edebb8a1b9eae7ee092bf64c9a54e25bdad64 100644 (file)
@@ -24,6 +24,7 @@ import java.util.Date;
 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
 import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomXmlTrace;
 import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomXmlTraceDefinition;
+import org.eclipse.linuxtools.tmf.core.trace.TmfTraceManager;
 import org.eclipse.linuxtools.tmf.core.trace.indexer.ITmfTraceIndexer;
 
 /**
@@ -33,7 +34,7 @@ import org.eclipse.linuxtools.tmf.core.trace.indexer.ITmfTraceIndexer;
  */
 public class CustomXmlIndexTest extends AbstractCustomTraceIndexTest {
 
-    private static final String TRACE_DIRECTORY = System.getProperty("java.io.tmpdir") + File.separator + "dummyXmlTrace";
+    private static final String TRACE_DIRECTORY = TmfTraceManager.getTemporaryDirPath() + File.separator + "dummyXmlTrace";
     private static final String TRACE_PATH = TRACE_DIRECTORY + File.separator + "test.xml";
     private static final String DEFINITION_PATH = "tracesets" + File.separator + "xml" + File.separator + "testDefinition.xml";
 
This page took 0.032662 seconds and 5 git commands to generate.