openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1766225 - in /openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave: Microwave.java io/IO.java
Date Sat, 22 Oct 2016 19:29:59 GMT
Author: rmannibucau
Date: Sat Oct 22 19:29:59 2016
New Revision: 1766225

URL: http://svn.apache.org/viewvc?rev=1766225&view=rev
Log:
avoiding to leak directories

Modified:
    openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/Microwave.java
    openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/io/IO.java

Modified: openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/Microwave.java
URL: http://svn.apache.org/viewvc/openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/Microwave.java?rev=1766225&r1=1766224&r2=1766225&view=diff
==============================================================================
--- openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/Microwave.java
(original)
+++ openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/Microwave.java
Sat Oct 22 19:29:59 2016
@@ -89,15 +89,18 @@ import static java.util.Optional.ofNulla
 public class Microwave implements AutoCloseable {
     private final Builder configuration;
     protected File base;
+    protected final File ownedTempDir;
     protected InternalTomcat tomcat;
     protected volatile Thread hook;
 
     // we can undeploy webapps with that later
     private final Map<String, Runnable> contexts = new HashMap<>();
     private Runnable postTask;
+    private boolean clearCatalinaSystemProperties;
 
     public Microwave(final Builder builder) {
         this.configuration = builder;
+        this.ownedTempDir = new File(configuration.tempDir, "microwave_" + System.nanoTime());
     }
 
     public Builder getConfiguration() {
@@ -155,7 +158,7 @@ public class Microwave implements AutoCl
 
 
         final File dir = ofNullable(meta.docBase).orElseGet(() -> {
-            final File d = new File(configuration.tempDir, "classpath/fake-" + meta.context.replace("/",
""));
+            final File d = new File(ownedTempDir, "classpath/fake-" + meta.context.replace("/",
""));
             IO.mkdirs(d);
             return d;
         });
@@ -230,6 +233,8 @@ public class Microwave implements AutoCl
     }
 
     public Microwave start() {
+        clearCatalinaSystemProperties = System.getProperty("catalina.base") == null &&
System.getProperty("catalina.home") == null;
+
         if (configuration.loggingGlobalSetup) {
             final String[] toRestore = new String[]{
                     System.getProperty("openwebbeans.logging.factory"),
@@ -478,12 +483,18 @@ public class Microwave implements AutoCl
             } catch (final LifecycleException e) {
                 throw new IllegalStateException(e);
             } finally {
+                if (clearCatalinaSystemProperties) {
+                    Stream.of("catalina.base", "catalina.home").forEach(System::clearProperty);
+                }
                 ofNullable(postTask).ifPresent(Runnable::run);
                 postTask = null;
                 try {
                     IO.delete(base);
+                    IO.delete(ownedTempDir);
                 } catch (final IllegalArgumentException /*does not exist from the hook*/
e) {
                     // no-op
+                } finally {
+                    base = null;
                 }
             }
         }
@@ -541,9 +552,7 @@ public class Microwave implements AutoCl
 
     private File createDirectory(final File parent, final String directory) {
         final File dir = new File(parent, directory);
-        if (!dir.exists() && !dir.mkdirs()) {
-            throw new IllegalStateException("Unable to make dir " + dir.getAbsolutePath());
-        }
+        IO.mkdirs(dir);
         return dir;
     }
 
@@ -671,7 +680,7 @@ public class Microwave implements AutoCl
         private final Collection<Connector> connectors = new ArrayList<>();
 
         @CliOption(name = "tmp-dir", description = "Temporary directory")
-        private String tempDir = new File(System.getProperty("java.io.tmpdir"), "microwave_"
+ System.nanoTime()).getAbsolutePath();
+        private String tempDir = System.getProperty("java.io.tmpdir");
 
         @CliOption(name = "web-resource-cached", description = "Cache web resources")
         private boolean webResourceCached = true;
@@ -1402,14 +1411,9 @@ public class Microwave implements AutoCl
         private static final String DEFAULT_HTTPS_PORT = "8443";
         private static final String DEFAULT_STOP_PORT = "8005";
         private static final String DEFAULT_HOST = "localhost";
-        private static final String DEFAULT_APP_BASE = "webapps";
 
         private final Map<String, String> values = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
 
-        private QuickServerXmlParser() { // ensure defaults are present
-            this(true);
-        }
-
         private QuickServerXmlParser(final boolean useDefaults) {
             if (useDefaults) {
                 values.put(STOP_KEY, DEFAULT_STOP_PORT);

Modified: openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/io/IO.java
URL: http://svn.apache.org/viewvc/openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/io/IO.java?rev=1766225&r1=1766224&r2=1766225&view=diff
==============================================================================
--- openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/io/IO.java
(original)
+++ openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/io/IO.java
Sat Oct 22 19:29:59 2016
@@ -44,15 +44,15 @@ public final class IO {
                     if (f.isFile()) {
                         retryDelete(f);
                     } else {
-                        delete(f);
+                        retryDelete(f);
                     }
-                    retryDelete(f);
                 });
+        retryDelete(dir);
     }
 
     private static void retryDelete(final File f) {
         for (int i = 0; i < 3; i++) {
-            if (f.isFile() && !f.delete()) {
+            if (f.exists() && !f.delete()) {
                 System.gc(); // win
                 try {
                     Thread.sleep(50);



Mime
View raw message