You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by ol...@apache.org on 2019/01/09 08:53:02 UTC

svn commit: r1850828 - in /felix/sandbox/olli: ./ org.apache.felix.bnd.plugins/ org.apache.felix.bnd.plugins/src/ org.apache.felix.bnd.plugins/src/main/ org.apache.felix.bnd.plugins/src/main/java/ org.apache.felix.bnd.plugins/src/main/java/org/ org.apa...

Author: olli
Date: Wed Jan  9 08:53:02 2019
New Revision: 1850828

URL: http://svn.apache.org/viewvc?rev=1850828&view=rev
Log:
FELIX-6019 Provide a bnd plugin to remove header parameters

initial commit

Added:
    felix/sandbox/olli/
    felix/sandbox/olli/org.apache.felix.bnd.plugins/
    felix/sandbox/olli/org.apache.felix.bnd.plugins/README.md
    felix/sandbox/olli/org.apache.felix.bnd.plugins/pom.xml
    felix/sandbox/olli/org.apache.felix.bnd.plugins/src/
    felix/sandbox/olli/org.apache.felix.bnd.plugins/src/main/
    felix/sandbox/olli/org.apache.felix.bnd.plugins/src/main/java/
    felix/sandbox/olli/org.apache.felix.bnd.plugins/src/main/java/org/
    felix/sandbox/olli/org.apache.felix.bnd.plugins/src/main/java/org/apache/
    felix/sandbox/olli/org.apache.felix.bnd.plugins/src/main/java/org/apache/felix/
    felix/sandbox/olli/org.apache.felix.bnd.plugins/src/main/java/org/apache/felix/bnd/
    felix/sandbox/olli/org.apache.felix.bnd.plugins/src/main/java/org/apache/felix/bnd/plugins/
    felix/sandbox/olli/org.apache.felix.bnd.plugins/src/main/java/org/apache/felix/bnd/plugins/header/
    felix/sandbox/olli/org.apache.felix.bnd.plugins/src/main/java/org/apache/felix/bnd/plugins/header/RemoveParametersPlugin.java
    felix/sandbox/olli/org.apache.felix.bnd.plugins/src/test/
    felix/sandbox/olli/org.apache.felix.bnd.plugins/src/test/java/
    felix/sandbox/olli/org.apache.felix.bnd.plugins/src/test/java/org/
    felix/sandbox/olli/org.apache.felix.bnd.plugins/src/test/java/org/apache/
    felix/sandbox/olli/org.apache.felix.bnd.plugins/src/test/java/org/apache/felix/
    felix/sandbox/olli/org.apache.felix.bnd.plugins/src/test/java/org/apache/felix/bnd/
    felix/sandbox/olli/org.apache.felix.bnd.plugins/src/test/java/org/apache/felix/bnd/plugins/
    felix/sandbox/olli/org.apache.felix.bnd.plugins/src/test/java/org/apache/felix/bnd/plugins/header/
    felix/sandbox/olli/org.apache.felix.bnd.plugins/src/test/java/org/apache/felix/bnd/plugins/header/RemoveParametersPluginTest.java

Added: felix/sandbox/olli/org.apache.felix.bnd.plugins/README.md
URL: http://svn.apache.org/viewvc/felix/sandbox/olli/org.apache.felix.bnd.plugins/README.md?rev=1850828&view=auto
==============================================================================
--- felix/sandbox/olli/org.apache.felix.bnd.plugins/README.md (added)
+++ felix/sandbox/olli/org.apache.felix.bnd.plugins/README.md Wed Jan  9 08:53:02 2019
@@ -0,0 +1,15 @@
+# Apache Felix bnd Plugins
+
+This module provides plugins for [bnd](https://bnd.bndtools.org).
+
+## RemoveParametersPlugin
+
+Removes parameters from bundle headers.
+
+Example instruction (for bnd file):
+
+````
+-plugin:\
+  org.apache.felix.bnd.plugins.header.RemoveParametersPlugin;\
+    'Require-Capability'='osgi.service;filter:="(objectClass=org.osgi.service.event.EventAdmin)";effective:=active,osgi.service;filter:="(objectClass=org.osgi.service.event.EventHandler)";effective:=active;cardinality:=multiple'
+````

Added: felix/sandbox/olli/org.apache.felix.bnd.plugins/pom.xml
URL: http://svn.apache.org/viewvc/felix/sandbox/olli/org.apache.felix.bnd.plugins/pom.xml?rev=1850828&view=auto
==============================================================================
--- felix/sandbox/olli/org.apache.felix.bnd.plugins/pom.xml (added)
+++ felix/sandbox/olli/org.apache.felix.bnd.plugins/pom.xml Wed Jan  9 08:53:02 2019
@@ -0,0 +1,113 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
+
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <groupId>org.apache.felix</groupId>
+    <artifactId>felix-parent</artifactId>
+    <version>6</version>
+    <relativePath/>
+  </parent>
+
+  <artifactId>org.apache.felix.bnd.plugins</artifactId>
+  <version>0.0.1-SNAPSHOT</version>
+
+  <name>Apache Felix bnd Plugins</name>
+  <description>This module provides plugins for bnd.</description>
+
+  <properties>
+    <felix.java.version>8</felix.java.version>
+  </properties>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-dependency-plugin</artifactId>
+        <executions>
+          <execution>
+            <phase>generate-test-resources</phase>
+            <goals>
+              <goal>copy</goal>
+            </goals>
+          </execution>
+        </executions>
+        <configuration>
+          <artifactItems>
+            <artifactItem>
+              <groupId>org.apache.sling</groupId>
+              <artifactId>org.apache.sling.resourceresolver</artifactId>
+              <version>1.6.4</version>
+              <type>jar</type>
+            </artifactItem>
+          </artifactItems>
+          <outputDirectory>${project.build.directory}/test-bundles</outputDirectory>
+        </configuration>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-surefire-plugin</artifactId>
+        <configuration>
+          <systemPropertyVariables>
+            <test.bundles.sling.resourceresolver>${project.build.directory}/test-bundles/org.apache.sling.resourceresolver-1.6.4.jar</test.bundles.sling.resourceresolver>
+          </systemPropertyVariables>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+
+  <dependencies>
+    <dependency>
+      <groupId>biz.aQute.bnd</groupId>
+      <artifactId>biz.aQute.bndlib</artifactId>
+      <version>4.1.0</version>
+      <scope>provided</scope>
+    </dependency>
+    <!-- logging -->
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>slf4j-simple</artifactId>
+      <version>1.7.25</version>
+      <scope>test</scope>
+    </dependency>
+    <!-- testing -->
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>4.12</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.mockito</groupId>
+      <artifactId>mockito-core</artifactId>
+      <version>2.23.4</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>com.google.truth</groupId>
+      <artifactId>truth</artifactId>
+      <version>0.42</version>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+
+</project>

Added: felix/sandbox/olli/org.apache.felix.bnd.plugins/src/main/java/org/apache/felix/bnd/plugins/header/RemoveParametersPlugin.java
URL: http://svn.apache.org/viewvc/felix/sandbox/olli/org.apache.felix.bnd.plugins/src/main/java/org/apache/felix/bnd/plugins/header/RemoveParametersPlugin.java?rev=1850828&view=auto
==============================================================================
--- felix/sandbox/olli/org.apache.felix.bnd.plugins/src/main/java/org/apache/felix/bnd/plugins/header/RemoveParametersPlugin.java (added)
+++ felix/sandbox/olli/org.apache.felix.bnd.plugins/src/main/java/org/apache/felix/bnd/plugins/header/RemoveParametersPlugin.java Wed Jan  9 08:53:02 2019
@@ -0,0 +1,102 @@
+/*
+ * 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.felix.bnd.plugins.header;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+
+import aQute.bnd.header.Attrs;
+import aQute.bnd.header.OSGiHeader;
+import aQute.bnd.header.Parameters;
+import aQute.bnd.osgi.Analyzer;
+import aQute.bnd.service.AnalyzerPlugin;
+import aQute.bnd.service.Plugin;
+import aQute.service.reporter.Reporter;
+
+public class RemoveParametersPlugin implements Plugin, AnalyzerPlugin {
+
+    /**
+     * the header parameters, e.g.
+     * key: 'Require-Capability'
+     * value: 'osgi.service;filter:="(objectClass=org.osgi.service.event.EventAdmin)";effective:=active,osgi.service;filter:="(objectClass=org.osgi.service.event.EventHandler)";effective:=active;cardinality:=multiple'
+     */
+    private Map<String, String> properties;
+
+    private Reporter reporter;
+
+    @Override
+    public void setProperties(final Map<String, String> properties) throws Exception {
+        this.properties = properties;
+    }
+
+    @Override
+    public void setReporter(final Reporter reporter) {
+        this.reporter = reporter;
+    }
+
+    @Override
+    public boolean analyzeJar(final Analyzer analyzer) throws Exception {
+        process(analyzer, properties, reporter);
+        return false;
+    }
+
+    private void process(final Analyzer analyzer, final Map<String, String> headers, final Reporter reporter) {
+        for (final Entry<String, String> header : headers.entrySet()) {
+            reporter.trace("Processing header '%s'", header.getKey());
+
+            // create mapping, e.g. 'osgi.service' → ['filter:="(objectClass=org.apache.sling.api.resource.ResourceResolverFactory)";effective:=active']
+            final Map<String, Set<String>> mapping = new HashMap<>();
+            for (final Entry<String, Attrs> parameter : OSGiHeader.parseHeader(header.getValue()).entrySet()) {
+                final String key = cleanKey(parameter.getKey());
+                mapping.computeIfAbsent(key, k -> new HashSet<>());
+                reporter.trace("Mapping '%s' → '%s'", key, parameter.getValue());
+                mapping.get(key).add(parameter.getValue().toString());
+            }
+
+            // collect parameters for removal
+            final Parameters parameters = analyzer.getParameters(header.getKey());
+            final Set<String> remove = new HashSet<>();
+            for (final Entry<String, Attrs> parameter : parameters.entrySet()) {
+                final String key = cleanKey(parameter.getKey());
+                if (mapping.containsKey(key) && mapping.get(key).contains(parameter.getValue().toString())) {
+                    reporter.trace("Parameter for removal found: %s;%s", key, parameter.getValue());
+                    remove.add(parameter.getKey());
+                }
+            }
+
+            // finally remove parameters
+            if (!remove.isEmpty()) {
+                for (final String key : remove) {
+                    final Attrs attrs = parameters.remove(key);
+                    reporter.trace("Parameter removed: %s;%s", cleanKey(key), attrs);
+                }
+                reporter.trace("Setting header '%s'", header.getKey());
+                analyzer.set(header.getKey(), parameters.toString());
+            }
+        }
+    }
+
+    private String cleanKey(final String key) {
+        return key.replace("~", "");
+    }
+
+}

Added: felix/sandbox/olli/org.apache.felix.bnd.plugins/src/test/java/org/apache/felix/bnd/plugins/header/RemoveParametersPluginTest.java
URL: http://svn.apache.org/viewvc/felix/sandbox/olli/org.apache.felix.bnd.plugins/src/test/java/org/apache/felix/bnd/plugins/header/RemoveParametersPluginTest.java?rev=1850828&view=auto
==============================================================================
--- felix/sandbox/olli/org.apache.felix.bnd.plugins/src/test/java/org/apache/felix/bnd/plugins/header/RemoveParametersPluginTest.java (added)
+++ felix/sandbox/olli/org.apache.felix.bnd.plugins/src/test/java/org/apache/felix/bnd/plugins/header/RemoveParametersPluginTest.java Wed Jan  9 08:53:02 2019
@@ -0,0 +1,68 @@
+/*
+ * 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.felix.bnd.plugins.header;
+
+import java.io.File;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import aQute.bnd.header.Attrs;
+import aQute.bnd.osgi.Analyzer;
+import aQute.bnd.osgi.Jar;
+import aQute.service.reporter.Reporter;
+import org.junit.Test;
+
+import static com.google.common.truth.Truth.assertThat;
+import static org.mockito.Mockito.mock;
+
+public class RemoveParametersPluginTest {
+
+    @Test
+    public void testSlingResourceresolverRequireCapability() throws Exception {
+        final File bundle = new File(System.getProperty("test.bundles.sling.resourceresolver"));
+        final Jar jar = new Jar(bundle);
+        final Analyzer analyzer = new Analyzer(jar);
+        analyzer.analyze();
+
+        final Reporter reporter = mock(Reporter.class);
+        final Map<String, String> properties = Collections.singletonMap("Require-Capability", "osgi.service;filter:=\"(objectClass=org.osgi.service.event.EventAdmin)\";effective:=active,osgi.service;filter:=\"(objectClass=org.osgi.service.event.EventHandler)\";effective:=active;cardinality:=multiple");
+
+        final RemoveParametersPlugin plugin = new RemoveParametersPlugin();
+        plugin.setProperties(properties);
+        plugin.setReporter(reporter);
+
+        final List<String> before = analyzer.getRequireCapability().values().stream().map(Attrs::toString).collect(Collectors.toList());
+        assertThat(before).containsAllOf(
+            "filter:=\"(objectClass=org.osgi.service.event.EventAdmin)\";effective:=active",
+            "filter:=\"(objectClass=org.osgi.service.event.EventHandler)\";effective:=active;cardinality:=multiple"
+        );
+
+        final boolean result = plugin.analyzeJar(analyzer);
+        assertThat(result).isFalse();
+
+        final List<String> after = analyzer.getRequireCapability().values().stream().map(Attrs::toString).collect(Collectors.toList());
+        assertThat(after).containsNoneOf(
+            "filter:=\"(objectClass=org.osgi.service.event.EventAdmin)\";effective:=active",
+            "filter:=\"(objectClass=org.osgi.service.event.EventHandler)\";effective:=active;cardinality:=multiple"
+        );
+    }
+
+}