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

simonetripodi pushed a commit to branch SLING-8251
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-feature-analyser.git


The following commit(s) were added to refs/heads/SLING-8251 by this push:
     new c9c4852  SLING-8251 - Support checking dependencies for content 
packages
c9c4852 is described below

commit c9c4852b719dcb12b561f5748d0f7ebc93c8229a
Author: Simo Tripodi <[email protected]>
AuthorDate: Thu Jun 13 09:18:29 2019 +0200

    SLING-8251 - Support checking dependencies for content packages
    
    temporary directories containing previously extracted content-package(s)
    have to be removed later that AnalyzerTask(s) execution, otherwise it
    happens that  there are few
    org.apache.sling.feature.scanner.ArtifactDescriptor pointing to files
    that do not exist anymore
---
 .../impl/CheckContentPackagesDependencies.java     |  10 +
 .../scanner/impl/ContentPackageScanner.java        | 224 ++++++++++-----------
 .../feature/scanner/impl/DeleteDirectoryHook.java  |  73 +++++++
 3 files changed, 187 insertions(+), 120 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/feature/analyser/task/impl/CheckContentPackagesDependencies.java
 
b/src/main/java/org/apache/sling/feature/analyser/task/impl/CheckContentPackagesDependencies.java
index 1e78744..6daf26d 100644
--- 
a/src/main/java/org/apache/sling/feature/analyser/task/impl/CheckContentPackagesDependencies.java
+++ 
b/src/main/java/org/apache/sling/feature/analyser/task/impl/CheckContentPackagesDependencies.java
@@ -33,11 +33,15 @@ import 
org.apache.jackrabbit.vault.packaging.impl.PackageManagerImpl;
 import org.apache.sling.feature.analyser.task.AnalyserTask;
 import org.apache.sling.feature.analyser.task.AnalyserTaskContext;
 import org.apache.sling.feature.scanner.ArtifactDescriptor;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class CheckContentPackagesDependencies implements AnalyserTask {
 
     private final PackageManager packageManager = new PackageManagerImpl();
 
+    private final Logger logger = LoggerFactory.getLogger(getClass());
+
     @Override
     public String getId() {
         return "content-packages-dependencies";
@@ -76,11 +80,17 @@ public class CheckContentPackagesDependencies implements 
AnalyserTask {
         try (VaultPackage vaultPackage = packageManager.open(artifactFile, 
true)) {
             PackageId packageId = vaultPackage.getId();
 
+            logger.debug("Collecting " + packageId + " dependencies...");
+
             dependenciesMap.put(packageId, vaultPackage.getDependencies());
+
+            logger.debug(packageId + " dependencies collected.");
         }
     }
 
     private void verifyDependenciesTree(AnalyserTaskContext ctx, PackageId 
root, Map<PackageId, Dependency[]> dependenciesMap) {
+        logger.debug("Verifying " + root + " transitive dependencies...");
+
         Queue<Dependency> toBeVisited = new LinkedList<>();
         enqueue(toBeVisited, dependenciesMap.get(root));
 
diff --git 
a/src/main/java/org/apache/sling/feature/scanner/impl/ContentPackageScanner.java
 
b/src/main/java/org/apache/sling/feature/scanner/impl/ContentPackageScanner.java
index e5d2878..9b01c77 100644
--- 
a/src/main/java/org/apache/sling/feature/scanner/impl/ContentPackageScanner.java
+++ 
b/src/main/java/org/apache/sling/feature/scanner/impl/ContentPackageScanner.java
@@ -42,6 +42,8 @@ public class ContentPackageScanner {
 
     private final Logger logger = LoggerFactory.getLogger(this.getClass());
 
+    private final DeleteDirectoryHook deleteDirectoryHook = new 
DeleteDirectoryHook();
+
     private final byte[] buffer = new byte[65536];
 
     private enum FileType {
@@ -50,6 +52,10 @@ public class ContentPackageScanner {
         PACKAGE
     }
 
+    public ContentPackageScanner() {
+        Runtime.getRuntime().addShutdownHook(deleteDirectoryHook);
+    }
+
     public Set<ContentPackageDescriptor> scan(final Artifact desc, final File 
file) throws IOException {
         if (!file.getName().endsWith(".zip") ) {
             throw new IOException("Artifact seems to be no content package 
(not a zip file): " + desc.getId().toMvnId());
@@ -77,39 +83,55 @@ public class ContentPackageScanner {
         logger.debug("Analyzing Content Package {}", archive.getName());
 
         final File tempDir = Files.createTempDirectory(null).toFile();
-        try {
-            final File toDir = new File(tempDir, archive.getName());
-            toDir.mkdirs();
-
-            final List<File> toProcess = new ArrayList<>();
-
-            try (final ZipInputStream zis = new ZipInputStream(new 
FileInputStream(archive)) ) {
-                boolean done = false;
-                while ( !done ) {
-                    final ZipEntry entry = zis.getNextEntry();
-                    if ( entry == null ) {
-                        done = true;
-                    } else {
-                        final String entryName = entry.getName();
-                        if ( !entryName.endsWith("/") && 
entryName.startsWith("jcr_root/") ) {
-                            final String contentPath = entryName.substring(8);
-
-                            FileType fileType = null;
-
-                            if ( entryName.endsWith(".zip") ) {
-                                // embedded content package
-                                fileType = FileType.PACKAGE;
-
-                                // check for libs or apps
-                            } else if ( entryName.startsWith("jcr_root/libs/") 
|| entryName.startsWith("jcr_root/apps/") ) {
-
-                                // check if this is an install folder (I)
-                                // install folders are either named:
-                                // "install" or
-                                // "install.{runmode}"
-                                boolean isInstall = 
entryName.indexOf("/install/") != -1;
+        deleteDirectoryHook.markToBeDeleted(tempDir);
+
+        final File toDir = new File(tempDir, archive.getName());
+        toDir.mkdirs();
+
+        final List<File> toProcess = new ArrayList<>();
+
+        try (final ZipInputStream zis = new ZipInputStream(new 
FileInputStream(archive)) ) {
+            boolean done = false;
+            while ( !done ) {
+                final ZipEntry entry = zis.getNextEntry();
+                if ( entry == null ) {
+                    done = true;
+                } else {
+                    final String entryName = entry.getName();
+                    if ( !entryName.endsWith("/") && 
entryName.startsWith("jcr_root/") ) {
+                        final String contentPath = entryName.substring(8);
+
+                        FileType fileType = null;
+
+                        if ( entryName.endsWith(".zip") ) {
+                            // embedded content package
+                            fileType = FileType.PACKAGE;
+
+                            // check for libs or apps
+                        } else if ( entryName.startsWith("jcr_root/libs/") || 
entryName.startsWith("jcr_root/apps/") ) {
+
+                            // check if this is an install folder (I)
+                            // install folders are either named:
+                            // "install" or
+                            // "install.{runmode}"
+                            boolean isInstall = entryName.indexOf("/install/") 
!= -1;
+                            if ( !isInstall ) {
+                                final int pos = entryName.indexOf("/install.");
+                                if ( pos != -1 ) {
+                                    final int endSlashPos = 
entryName.indexOf('/', pos + 1);
+                                    if ( endSlashPos != -1 ) {
+                                        isInstall = true;
+                                    }
+                                }
+                            }
+                            if ( !isInstall ) {
+                                // check if this is an install folder (II)
+                                // config folders are either named:
+                                // "config" or
+                                // "config.{runmode}"
+                                isInstall = entryName.indexOf("/config/") != 
-1;
                                 if ( !isInstall ) {
-                                    final int pos = 
entryName.indexOf("/install.");
+                                    final int pos = 
entryName.indexOf("/config.");
                                     if ( pos != -1 ) {
                                         final int endSlashPos = 
entryName.indexOf('/', pos + 1);
                                         if ( endSlashPos != -1 ) {
@@ -117,117 +139,79 @@ public class ContentPackageScanner {
                                         }
                                     }
                                 }
-                                if ( !isInstall ) {
-                                    // check if this is an install folder (II)
-                                    // config folders are either named:
-                                    // "config" or
-                                    // "config.{runmode}"
-                                    isInstall = entryName.indexOf("/config/") 
!= -1;
-                                    if ( !isInstall ) {
-                                        final int pos = 
entryName.indexOf("/config.");
-                                        if ( pos != -1 ) {
-                                            final int endSlashPos = 
entryName.indexOf('/', pos + 1);
-                                            if ( endSlashPos != -1 ) {
-                                                isInstall = true;
-                                            }
-                                        }
-                                    }
-                                }
+                            }
 
-                                if (isInstall ) {
+                            if (isInstall ) {
 
-                                   if ( entryName.endsWith(".jar") ) {
-                                       fileType = FileType.BUNDLE;
-                                   } else if ( entryName.endsWith(".xml") || 
entryName.endsWith(".config") ) {
-                                       fileType = FileType.CONFIG;
-                                   }
-                                }
+                               if ( entryName.endsWith(".jar") ) {
+                                   fileType = FileType.BUNDLE;
+                               } else if ( entryName.endsWith(".xml") || 
entryName.endsWith(".config") ) {
+                                   fileType = FileType.CONFIG;
+                               }
                             }
+                        }
 
-                            if ( fileType != null ) {
-                                logger.debug("- extracting : {}", entryName);
-                                final File newFile = new File(toDir, 
entryName.replace('/', File.separatorChar));
-                                newFile.getParentFile().mkdirs();
+                        if ( fileType != null ) {
+                            logger.debug("- extracting : {}", entryName);
+                            final File newFile = new File(toDir, 
entryName.replace('/', File.separatorChar));
+                            newFile.getParentFile().mkdirs();
 
-                                try (final FileOutputStream fos = new 
FileOutputStream(newFile)) {
-                                    int len;
-                                    while ((len = zis.read(buffer)) > -1) {
-                                        fos.write(buffer, 0, len);
-                                    }
+                            try (final FileOutputStream fos = new 
FileOutputStream(newFile)) {
+                                int len;
+                                while ((len = zis.read(buffer)) > -1) {
+                                    fos.write(buffer, 0, len);
                                 }
+                            }
 
-                                if ( fileType == FileType.BUNDLE ) {
-                                    int startLevel = 20;
-                                    final int lastSlash = 
contentPath.lastIndexOf('/');
-                                    final int nextSlash = 
contentPath.lastIndexOf('/', lastSlash - 1);
-                                    final String part = 
contentPath.substring(nextSlash + 1, lastSlash);
-                                    try {
-                                        startLevel = Integer.valueOf(part);
-                                    } catch ( final NumberFormatException 
ignore ) {
-                                        // ignore
-                                    }
-
-                                    final Artifact bundle = new 
Artifact(extractArtifactId(tempDir, newFile));
-                                    final BundleDescriptor info = new 
BundleDescriptorImpl(bundle, newFile, startLevel);
-                                    
bundle.getMetadata().put("content-package", cp.getArtifact().getId().toMvnId());
-                                    bundle.getMetadata().put("content-path", 
contentPath);
+                            if ( fileType == FileType.BUNDLE ) {
+                                int startLevel = 20;
+                                final int lastSlash = 
contentPath.lastIndexOf('/');
+                                final int nextSlash = 
contentPath.lastIndexOf('/', lastSlash - 1);
+                                final String part = 
contentPath.substring(nextSlash + 1, lastSlash);
+                                try {
+                                    startLevel = Integer.valueOf(part);
+                                } catch ( final NumberFormatException ignore ) 
{
+                                    // ignore
+                                }
 
-                                    cp.bundles.add(info);
+                                final Artifact bundle = new 
Artifact(extractArtifactId(tempDir, newFile));
+                                final BundleDescriptor info = new 
BundleDescriptorImpl(bundle, newFile, startLevel);
+                                bundle.getMetadata().put("content-package", 
cp.getArtifact().getId().toMvnId());
+                                bundle.getMetadata().put("content-path", 
contentPath);
 
-                                } else if ( fileType == FileType.CONFIG ) {
+                                cp.bundles.add(info);
 
-                                    final Configuration configEntry = 
this.process(newFile, cp.getArtifact(), contentPath);
-                                    if ( configEntry != null ) {
+                            } else if ( fileType == FileType.CONFIG ) {
 
-                                        cp.configs.add(configEntry);
-                                    }
+                                final Configuration configEntry = 
this.process(newFile, cp.getArtifact(), contentPath);
+                                if ( configEntry != null ) {
 
-                                } else if ( fileType == FileType.PACKAGE ) {
-                                    toProcess.add(newFile);
+                                    cp.configs.add(configEntry);
                                 }
 
+                            } else if ( fileType == FileType.PACKAGE ) {
+                                toProcess.add(newFile);
                             }
 
                         }
-                        zis.closeEntry();
+
                     }
+                    zis.closeEntry();
                 }
-
             }
 
-            for(final File f : toProcess) {
-                extractContentPackage(cp, infos, f);
-                final ContentPackageDescriptor i = new 
ContentPackageDescriptor(f.getName());
-                final int lastDot = f.getName().lastIndexOf(".");
-                i.setName(f.getName().substring(0, lastDot));
-                i.setArtifactFile(f);
-                i.setContentPackageInfo(cp.getArtifact(), f.getName());
-                infos.add(i);
-
-                i.lock();
-            }
-        } finally {
-            deleteRecursive(tempDir);
         }
-    }
 
-    private boolean deleteRecursive(File file) {
-        if (file.isDirectory()) {
-            File[] childs = file.listFiles();
-            if (childs != null) {
-                for (File child : childs) {
-                    if (!deleteRecursive(child)) {
-                        return false;
-                    }
-                }
-                return file.delete();
-            }
-            else {
-                return false;
-            }
-        }
-        else {
-            return file.delete();
+        for(final File f : toProcess) {
+            extractContentPackage(cp, infos, f);
+            final ContentPackageDescriptor i = new 
ContentPackageDescriptor(f.getName());
+            final int lastDot = f.getName().lastIndexOf(".");
+            i.setName(f.getName().substring(0, lastDot));
+            i.setArtifactFile(f);
+            i.setContentPackageInfo(cp.getArtifact(), f.getName());
+            infos.add(i);
+
+            i.lock();
         }
     }
 
diff --git 
a/src/main/java/org/apache/sling/feature/scanner/impl/DeleteDirectoryHook.java 
b/src/main/java/org/apache/sling/feature/scanner/impl/DeleteDirectoryHook.java
new file mode 100644
index 0000000..bfea678
--- /dev/null
+++ 
b/src/main/java/org/apache/sling/feature/scanner/impl/DeleteDirectoryHook.java
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.feature.scanner.impl;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+final class DeleteDirectoryHook extends Thread {
+
+    private final Logger logger = LoggerFactory.getLogger(getClass());
+
+    private final List<File> toBeDeletedList = new ArrayList<>();
+
+    public void markToBeDeleted(File toBeDeleted) {
+        if (toBeDeleted != null && toBeDeleted.exists()) {
+            toBeDeletedList.add(toBeDeleted);
+        }
+    }
+
+    @Override
+    public void run() {
+        for (File toBeDeleted : toBeDeletedList) {
+            logger.debug("Deleting recursively " + toBeDeleted + "...");
+
+            if (!deleteRecursive(toBeDeleted)) {
+                logger.warn("Somethign went wrong when deleting directory "
+                            + toBeDeleted
+                            + " please verify the current user has enough 
rights to delete such dir.");
+            } else {
+                logger.debug(toBeDeleted + " successfully deleted");
+            }
+        }
+    }
+
+    private static boolean deleteRecursive(File file) {
+        if (file.isDirectory()) {
+            File[] childs = file.listFiles();
+            if (childs != null) {
+                for (File child : childs) {
+                    if (!deleteRecursive(child)) {
+                        return false;
+                    }
+                }
+                return file.delete();
+            }
+            else {
+                return false;
+            }
+        }
+        else {
+            return file.delete();
+        }
+    }
+
+}

Reply via email to