You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by kw...@apache.org on 2021/06/06 18:56:44 UTC

[sling-org-apache-sling-feature] branch master updated: SLING-10451 cleanup cache directory (#24)

This is an automated email from the ASF dual-hosted git repository.

kwin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-feature.git


The following commit(s) were added to refs/heads/master by this push:
     new 94a5023  SLING-10451 cleanup cache directory (#24)
94a5023 is described below

commit 94a5023560c05f0bde2acc1617ca70846253c139
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Sun Jun 6 20:56:36 2021 +0200

    SLING-10451 cleanup cache directory (#24)
---
 pom.xml                                             |  7 ++++++-
 .../sling/feature/io/artifacts/ArtifactManager.java | 19 +++++++++++++++++++
 .../feature/io/artifacts/ArtifactManagerConfig.java | 21 ++++++++-------------
 .../sling/feature/io/artifacts/package-info.java    |  2 +-
 4 files changed, 34 insertions(+), 15 deletions(-)

diff --git a/pom.xml b/pom.xml
index 3d8bc23..71704eb 100644
--- a/pom.xml
+++ b/pom.xml
@@ -115,7 +115,12 @@
             <version>1.0.6</version>
             <scope>provided</scope>
         </dependency>
-
+        <dependency>
+            <groupId>org.jetbrains</groupId>
+            <artifactId>annotations</artifactId>
+            <scope>provided</scope>
+         </dependency>
+       
         <!-- Testing -->
         <dependency>
             <groupId>junit</groupId>
diff --git a/src/main/java/org/apache/sling/feature/io/artifacts/ArtifactManager.java b/src/main/java/org/apache/sling/feature/io/artifacts/ArtifactManager.java
index 021be9a..6f086d2 100644
--- a/src/main/java/org/apache/sling/feature/io/artifacts/ArtifactManager.java
+++ b/src/main/java/org/apache/sling/feature/io/artifacts/ArtifactManager.java
@@ -345,6 +345,7 @@ public class ArtifactManager
 
         private File cacheDir;
 
+        private boolean isNewlyCreatedCacheDir;
         private ArtifactProviderContext config;
 
         @Override
@@ -355,15 +356,33 @@ public class ArtifactManager
         @Override
         public void init(final ArtifactProviderContext config) throws IOException {
             this.cacheDir = config.getCacheDirectory();
+            if (cacheDir == null) {
+                this.cacheDir = Files.createTempDirectory("slingfeature").toFile();
+                isNewlyCreatedCacheDir = true;
+            }
             this.config = config;
         }
 
         @Override
         public void shutdown() {
             this.config = null;
+            if (isNewlyCreatedCacheDir) {
+                deleteDir(cacheDir);
+            }
             this.cacheDir = null;
         }
 
+        /** recursively delete directory */
+        private static void deleteDir(File dir) {
+            File[] files = dir.listFiles();
+            if(files != null) {
+                for (final File file : files) {
+                    deleteDir(file);
+                }
+            }
+            dir.delete();
+        }
+
         @Override
         public URL getArtifact(final String url, final String relativeCachePath) {
             logger.debug("Checking url to be local file {}", url);
diff --git a/src/main/java/org/apache/sling/feature/io/artifacts/ArtifactManagerConfig.java b/src/main/java/org/apache/sling/feature/io/artifacts/ArtifactManagerConfig.java
index 876d895..589205d 100644
--- a/src/main/java/org/apache/sling/feature/io/artifacts/ArtifactManagerConfig.java
+++ b/src/main/java/org/apache/sling/feature/io/artifacts/ArtifactManagerConfig.java
@@ -17,11 +17,11 @@
 package org.apache.sling.feature.io.artifacts;
 
 import java.io.File;
-import java.io.IOException;
-import java.nio.file.Files;
 import java.util.concurrent.atomic.AtomicLong;
 
 import org.apache.sling.feature.io.artifacts.spi.ArtifactProviderContext;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
 
 /**
  * This class holds the configuration of artifact manager.
@@ -50,7 +50,7 @@ public class ArtifactManagerConfig implements ArtifactProviderContext {
     /**
      * The .m2 directory.
      */
-    private final String repoHome;
+    private final @NotNull String repoHome;
 
     /**
      * Create a new configuration object. Set the default values
@@ -62,11 +62,6 @@ public class ArtifactManagerConfig implements ArtifactProviderContext {
                 "https://repo.maven.apache.org/maven2",
                 "https://repository.apache.org/content/groups/snapshots"
                 };
-        try {
-            this.cacheDirectory = Files.createTempDirectory("slingfeature").toFile();
-        } catch (IOException e) {
-            throw new RuntimeException(e);
-        }
         this.repoHome = System.getProperty("user.home") + "/.m2/repository/";
     }
 
@@ -76,7 +71,7 @@ public class ArtifactManagerConfig implements ArtifactProviderContext {
      */
     public void setRepositoryUrls(final String[] urls) {
         if ( urls == null || urls.length == 0 ) {
-            this.repositoryUrls = null;
+            this.repositoryUrls = new String[0];
         } else {
             this.repositoryUrls = new String[urls.length];
             System.arraycopy(urls, 0, this.repositoryUrls, 0, urls.length);
@@ -93,16 +88,16 @@ public class ArtifactManagerConfig implements ArtifactProviderContext {
      * A repository url does not end with a slash.
      * @return The repository urls.
      */
-    public String[] getRepositoryUrls() {
+    public @NotNull String[] getRepositoryUrls() {
         return repositoryUrls;
     }
 
     /**
      * Get the cache directory
-     * @return The cache directory.
+     * @return The cache directory or {@code null} if none has been set
      */
     @Override
-    public File getCacheDirectory() {
+    public @Nullable File getCacheDirectory() {
         return cacheDirectory;
     }
 
@@ -178,7 +173,7 @@ public class ArtifactManagerConfig implements ArtifactProviderContext {
      * 
      * @since 1.1.0
      */
-    String getMvnHome() {
+    @NotNull String getMvnHome() {
         return this.repoHome;
     }
 }
diff --git a/src/main/java/org/apache/sling/feature/io/artifacts/package-info.java b/src/main/java/org/apache/sling/feature/io/artifacts/package-info.java
index f2229fb..116ae96 100644
--- a/src/main/java/org/apache/sling/feature/io/artifacts/package-info.java
+++ b/src/main/java/org/apache/sling/feature/io/artifacts/package-info.java
@@ -17,7 +17,7 @@
  * under the License.
  */
 
-@org.osgi.annotation.versioning.Version("1.1.0")
+@org.osgi.annotation.versioning.Version("1.1.1")
 package org.apache.sling.feature.io.artifacts;