You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2020/10/13 06:40:14 UTC

[sling-org-apache-sling-feature-analyser] branch master updated: SLING-9815 : Add analyser to check for unused bundles

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

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


The following commit(s) were added to refs/heads/master by this push:
     new e3195a2  SLING-9815 : Add analyser to check for unused bundles
e3195a2 is described below

commit e3195a236fe587f60f2773335aefd30467ad7bfa
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Tue Oct 13 08:39:54 2020 +0200

    SLING-9815 : Add analyser to check for unused bundles
---
 .../analyser/task/impl/CheckUnusedBundles.java     | 91 ++++++++++++++++++++++
 ...apache.sling.feature.analyser.task.AnalyserTask |  1 +
 2 files changed, 92 insertions(+)

diff --git a/src/main/java/org/apache/sling/feature/analyser/task/impl/CheckUnusedBundles.java b/src/main/java/org/apache/sling/feature/analyser/task/impl/CheckUnusedBundles.java
new file mode 100644
index 0000000..8dd1937
--- /dev/null
+++ b/src/main/java/org/apache/sling/feature/analyser/task/impl/CheckUnusedBundles.java
@@ -0,0 +1,91 @@
+/*
+ * 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.analyser.task.impl;
+
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+
+import org.apache.sling.feature.analyser.task.AnalyserTask;
+import org.apache.sling.feature.analyser.task.AnalyserTaskContext;
+import org.apache.sling.feature.scanner.BundleDescriptor;
+import org.apache.sling.feature.scanner.PackageInfo;
+
+public class CheckUnusedBundles implements AnalyserTask {
+
+    @Override
+    public String getName() {
+        return "Unused Bundle Check";
+    }
+
+    @Override
+    public String getId() {
+        return "check-unused-bundles";
+    }
+
+    @Override
+    public void execute(final AnalyserTaskContext ctx) throws IOException {
+        // iterate over all bundles
+        for(final BundleDescriptor info : ctx.getFeatureDescriptor().getBundleDescriptors()) {
+
+            if ( !info.getExportedPackages().isEmpty() ) {
+
+                final Set<PackageInfo> exports = new HashSet<>(info.getExportedPackages());
+                
+                // find importing bundle
+                for(final BundleDescriptor inner : ctx.getFeatureDescriptor().getBundleDescriptors()) {
+                    if ( inner == info ) {
+                        continue;
+                    }
+
+                    final Iterator<PackageInfo> iter = exports.iterator();
+                    while ( iter.hasNext() ) {
+                        final PackageInfo expPck = iter.next();
+
+                        boolean found = false;
+                        for(final PackageInfo impPck : inner.getImportedPackages()) {
+                            
+                            if ( expPck.getName().equals(impPck.getName())) {
+                                if ( impPck.getVersion() == null || impPck.getPackageVersionRange().includes(expPck.getPackageVersion()) ) {
+                                    found = true;
+                                    break;
+                                }
+                            }
+                        }
+
+                        if ( found ) {
+                            iter.remove();
+                        }
+     
+                    }
+
+                    if ( exports.isEmpty() ) {
+                        break;
+                    }
+                }
+
+                if ( exports.size() == info.getExportedPackages().size() ) {
+                    ctx.reportWarning("Exports from bundle ".concat(info.getArtifact().getId().toMvnId()).concat(" are not imported by any other bundle."));
+                }
+
+            }
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/main/resources/META-INF/services/org.apache.sling.feature.analyser.task.AnalyserTask b/src/main/resources/META-INF/services/org.apache.sling.feature.analyser.task.AnalyserTask
index a74ab89..b87b657 100644
--- a/src/main/resources/META-INF/services/org.apache.sling.feature.analyser.task.AnalyserTask
+++ b/src/main/resources/META-INF/services/org.apache.sling.feature.analyser.task.AnalyserTask
@@ -9,3 +9,4 @@ org.apache.sling.feature.analyser.task.impl.CheckContentPackagesDependencies
 org.apache.sling.feature.analyser.task.impl.CheckDuplicateSymbolicName
 org.apache.sling.feature.analyser.task.impl.CheckRepoinit
 org.apache.sling.feature.analyser.task.impl.CheckRequirementsCapabilities
+org.apache.sling.feature.analyser.task.impl.CheckUnusedBundles