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 2019/05/09 11:00:15 UTC

[sling-org-apache-sling-feature-analyser] branch master updated: SLING-8410 : Add analyser to scan for potential problems when using Apache Felix Connect

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 efb1675  SLING-8410 : Add analyser to scan for potential problems when using Apache Felix Connect
efb1675 is described below

commit efb16759336fcbab909fa3bf5f317113f532c42f
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Thu May 9 12:59:01 2019 +0200

    SLING-8410 : Add analyser to scan for potential problems when using Apache Felix Connect
---
 .../analyser/task/impl/CheckBundlesForConnect.java | 116 +++++++++++++++++++++
 ...apache.sling.feature.analyser.task.AnalyserTask |   1 +
 2 files changed, 117 insertions(+)

diff --git a/src/main/java/org/apache/sling/feature/analyser/task/impl/CheckBundlesForConnect.java b/src/main/java/org/apache/sling/feature/analyser/task/impl/CheckBundlesForConnect.java
new file mode 100644
index 0000000..8480dee
--- /dev/null
+++ b/src/main/java/org/apache/sling/feature/analyser/task/impl/CheckBundlesForConnect.java
@@ -0,0 +1,116 @@
+/*
+ * 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.FileInputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.jar.JarEntry;
+import java.util.jar.JarInputStream;
+
+import org.apache.sling.feature.Artifact;
+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.osgi.framework.Constants;
+
+public class CheckBundlesForConnect implements AnalyserTask {
+
+    @Override
+    public String getName() {
+        return "Bundle Check For Connect";
+    }
+
+    @Override
+    public String getId() {
+        return "bundle-connect";
+    }
+
+    @Override
+    public void execute(final AnalyserTaskContext ctx) {
+        final Map<String, List<Artifact>> packageMap = new HashMap<>();
+        for (final BundleDescriptor bd : ctx.getFeatureDescriptor().getBundleDescriptors()) {
+            if (bd.getManifest() != null) {
+                final String cp = bd.getManifest().getMainAttributes().getValue(Constants.BUNDLE_CLASSPATH);
+                String[] jars = null;
+                if (cp != null) {
+                    jars = cp.split(",");
+                    ctx.reportWarning("Found bundle classpath in " + bd.getArtifact() + " : " + cp);
+                }
+
+                final Set<String> packages = new HashSet<>();
+                try (final JarInputStream jis = new JarInputStream(new FileInputStream(bd.getArtifactFile()))) {
+                    JarEntry entry;
+                    while ((entry = jis.getNextJarEntry()) != null) {
+                        if (entry.getName().endsWith(".class")) {
+                            final int lastPos = entry.getName().lastIndexOf('/');
+                            if (lastPos == -1) {
+                                ctx.reportError("Bundle contains classes in the default package: " + bd.getArtifact());
+                            } else {
+                                packages.add(entry.getName().substring(0, lastPos));
+                            }
+                        } else if (!entry.isDirectory() && jars != null) {
+                            for (final String jar : jars) {
+                                if (jar.equals(entry.getName())) {
+                                    final JarInputStream is = new JarInputStream(jis);
+                                    JarEntry inner;
+                                    while ((inner = is.getNextJarEntry()) != null) {
+                                        if (inner.getName().endsWith(".class")) {
+                                            final int lastPos = inner.getName().lastIndexOf('/');
+                                            if (lastPos == -1) {
+                                                ctx.reportError(
+                                                        "Bundle contains (embedded) classes in the default package: "
+                                                                + bd.getArtifact());
+                                            } else {
+                                                packages.add(inner.getName().substring(0, lastPos));
+                                            }
+                                        }
+                                        is.closeEntry();
+                                    }
+                                }
+                            }
+                        }
+                        jis.closeEntry();
+                    }
+                } catch (final IOException ioe) {
+                    ctx.reportError("Unable to scan bundle " + bd.getArtifact() + " : " + ioe.getMessage());
+                }
+                for (final String p : packages) {
+                    List<Artifact> list = packageMap.get(p);
+                    if (list == null) {
+                        list = new ArrayList<>();
+                        packageMap.put(p, list);
+                    }
+                    list.add(bd.getArtifact());
+                }
+            }
+        }
+
+        for (final Map.Entry<String, List<Artifact>> entry : packageMap.entrySet()) {
+            if (entry.getValue().size() > 1) {
+                ctx.reportWarning("Duplicate package " + entry.getKey() + " in " + entry.getValue());
+            }
+        }
+    }
+}
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 005b143..8c8f112 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
@@ -1,4 +1,5 @@
 org.apache.sling.feature.analyser.task.impl.CheckBundleExportsImports
+org.apache.sling.feature.analyser.task.impl.CheckBundlesForConnect
 org.apache.sling.feature.analyser.task.impl.CheckBundlesForInitialContent
 org.apache.sling.feature.analyser.task.impl.CheckBundlesForResources
 org.apache.sling.feature.analyser.task.impl.CheckRequirementsCapabilities