diff --git a/tools/maven-bundle-plugin/src/main/java/org/apache/felix/bundleplugin/BlueprintPlugin.java b/tools/maven-bundle-plugin/src/main/java/org/apache/felix/bundleplugin/BlueprintPlugin.java index 3535c96579..5d18ec7ce0 100644 --- a/tools/maven-bundle-plugin/src/main/java/org/apache/felix/bundleplugin/BlueprintPlugin.java +++ b/tools/maven-bundle-plugin/src/main/java/org/apache/felix/bundleplugin/BlueprintPlugin.java @@ -41,9 +41,11 @@ import org.apache.felix.utils.manifest.Attribute; import org.apache.felix.utils.manifest.Clause; +import org.apache.felix.utils.manifest.Directive; import org.osgi.framework.Constants; import aQute.bnd.header.Attrs; +import aQute.bnd.header.OSGiHeader; import aQute.bnd.osgi.Analyzer; import aQute.bnd.osgi.Descriptors.PackageRef; import aQute.bnd.osgi.Jar; @@ -162,7 +164,8 @@ public boolean analyzeJar( Analyzer analyzer ) throws Exception } else { filter = "(&(" + Constants.OBJECTCLASS + "=" + clause.getName() + ")" + filter + ")"; } - sb.append("filter:=\"").append(filter).append("\""); + sb.append("filter:="); + OSGiHeader.quote(sb, filter); reqs.add(sb.toString()); } else if (!"generic".equals(mode)) @@ -199,9 +202,8 @@ else if ("Export-Service".equals(h)) { sb.append(";"); sb.append(hc[0].getAttributes()[i].getName()); - sb.append("=\""); - sb.append(hc[0].getAttributes()[i].getValue()); - sb.append("\""); + sb.append("="); + OSGiHeader.quote(sb, hc[0].getAttributes()[i].getValue()); } caps.add(sb.toString()); } @@ -296,14 +298,15 @@ else if (!"generic".equals(mode)) String org = analyzer.getProperty(header); if (org != null && !org.isEmpty()) { - for (Clause clause : parseHeader(org)) + // the configured header is bnd syntax (single quotes, \" escapes), so parse it with bnd + for (Map.Entry clause : OSGiHeader.parseHeader(org, analyzer).entrySet()) { - merge.add(clause.toString()); + merge.add(toHeaderClause(Processor.removeDuplicateMarker(clause.getKey()), clause.getValue())); } } for (Clause clause : hdrs.get(header)) { - merge.add(clause.toString()); + merge.add(toHeaderClause(clause)); } StringBuilder sb = new StringBuilder(); for (String clause : merge) @@ -322,6 +325,43 @@ else if (!"generic".equals(mode)) } + /** + * Serializes a clause in bnd header syntax. {@link Clause#toString()} only quotes values containing + * ',', so a service filter such as {@code (ns=http://...)} would be written unquoted, and bnd + * truncates such a value at the first '=' when it parses the header again. bnd's own quoting is + * used instead, so bnd reads every value back unchanged. + */ + static String toHeaderClause( Clause clause ) + { + Attrs attrs = new Attrs(); + for ( Directive directive : clause.getDirectives() ) + { + attrs.put( directive.getName() + ":", directive.getValue() ); + } + for ( Attribute attribute : clause.getAttributes() ) + { + attrs.put( attribute.getName(), attribute.getValue() ); + } + return toHeaderClause( clause.getName(), attrs ); + } + + + static String toHeaderClause( String name, Attrs attrs ) + { + // directives first, like Clause.toString(), so that equal clauses are merged + Attrs ordered = new Attrs(); + for ( String key : attrs.keySet() ) + { + if ( key.endsWith( ":" ) ) + { + ordered.put( key, attrs.get( key ) ); + } + } + ordered.mergeWith( attrs, false ); + return ordered.isEmpty() ? name : name + ";" + ordered; + } + + private void process( Analyzer analyzer, String path, Resource resource, Set headers ) { InputStream in = null; diff --git a/tools/maven-bundle-plugin/src/main/resources/org/apache/felix/bundleplugin/blueprint.xsl b/tools/maven-bundle-plugin/src/main/resources/org/apache/felix/bundleplugin/blueprint.xsl index 89e6551cd9..bc04b7c1db 100644 --- a/tools/maven-bundle-plugin/src/main/resources/org/apache/felix/bundleplugin/blueprint.xsl +++ b/tools/maven-bundle-plugin/src/main/resources/org/apache/felix/bundleplugin/blueprint.xsl @@ -133,7 +133,7 @@ - + @@ -170,7 +170,7 @@ - + diff --git a/tools/maven-bundle-plugin/src/test/java/org/apache/felix/bundleplugin/BlueprintComponentTest.java b/tools/maven-bundle-plugin/src/test/java/org/apache/felix/bundleplugin/BlueprintComponentTest.java index f63b875b92..40c5bb1b46 100644 --- a/tools/maven-bundle-plugin/src/test/java/org/apache/felix/bundleplugin/BlueprintComponentTest.java +++ b/tools/maven-bundle-plugin/src/test/java/org/apache/felix/bundleplugin/BlueprintComponentTest.java @@ -137,6 +137,19 @@ public File getBasedir() } assertNotNull( impSvc ); + // namespace handler filters contain '=' and must be quoted, otherwise bnd (>= 7.2 re-parses + // the header when writing the manifest) truncates them at the first '=' + if ("service".equals(mode)) { + List filters = BlueprintPluginTest.values( impSvc, "foo.bar.Namespace", "filter" ); + assertTrue( filters.toString(), filters.contains("(ns=http://foo.bar/tx)") ); + assertTrue( filters.toString(), filters.contains("(ns=http://foo.bar/t2)") ); + } else { + String reqCap = manifest.getMainAttributes().getValue( Constants.REQUIRE_CAPABILITY ); + assertNotNull( reqCap ); + List filters = BlueprintPluginTest.values( reqCap, "osgi.service", "filter:" ); + assertTrue( filters.toString(), filters.contains("(&(objectClass=foo.bar.Namespace)(ns=http://foo.bar/tx))") ); + assertTrue( filters.toString(), filters.contains("(&(objectClass=foo.bar.Namespace)(ns=http://foo.bar/t2))") ); + } String impPkg = manifest.getMainAttributes().getValue( Constants.IMPORT_PACKAGE ); List pkgs = Create.list(); diff --git a/tools/maven-bundle-plugin/src/test/java/org/apache/felix/bundleplugin/BlueprintPluginTest.java b/tools/maven-bundle-plugin/src/test/java/org/apache/felix/bundleplugin/BlueprintPluginTest.java new file mode 100644 index 0000000000..fb53676465 --- /dev/null +++ b/tools/maven-bundle-plugin/src/test/java/org/apache/felix/bundleplugin/BlueprintPluginTest.java @@ -0,0 +1,226 @@ +/* + * 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.bundleplugin; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +import org.junit.Test; + +import aQute.bnd.header.Attrs; +import aQute.bnd.header.OSGiHeader; +import aQute.bnd.osgi.AbstractResource; +import aQute.bnd.osgi.Analyzer; +import aQute.bnd.osgi.Jar; +import aQute.bnd.osgi.Processor; + +/** + * Checks that the headers BlueprintPlugin writes are read back unchanged by bnd's header parser. + */ +public class BlueprintPluginTest +{ + + private static final String BP_START = "\n"; + private static final String BP_END = ""; + + + @Test + public void testFilterWithBackslashIsKept() throws Exception + { + Analyzer analyzer = analyze( "service", + BP_START + "\n" + BP_END ); + + assertEquals( Arrays.asList( "(name=a\\*b)" ), values( analyzer, "Import-Service", "p.Foo", "filter" ) ); + } + + + @Test + public void testFilterWithQuoteIsKept() throws Exception + { + Analyzer analyzer = analyze( "service", + BP_START + "\n" + BP_END ); + + assertEquals( Arrays.asList( "(a=\"x\")" ), values( analyzer, "Import-Service", "p.Foo", "filter" ) ); + } + + + @Test + public void testFilterWithComponentName() throws Exception + { + Analyzer analyzer = analyze( "service", + BP_START + "\n" + BP_END ); + + assertEquals( Arrays.asList( "(&(a=b)(osgi.service.blueprint.compname=c))" ), + values( analyzer, "Import-Service", "p.Foo", "filter" ) ); + } + + + @Test + public void testConfiguredHeaderWithSingleQuotes() throws Exception + { + Analyzer analyzer = analyze( "service", + BP_START + "\n" + BP_END, + "Import-Service", "p.Bar;filter='(a=b)'" ); + + assertEquals( Arrays.asList( "(a=b)" ), values( analyzer, "Import-Service", "p.Bar", "filter" ) ); + assertEquals( 1, values( analyzer, "Import-Service", "p.Foo", "multiple:" ).size() ); + } + + + @Test + public void testConfiguredHeaderWithEscapedQuote() throws Exception + { + Analyzer analyzer = analyze( "service", + BP_START + "\n" + BP_END, + "Import-Service", "p.Bar;filter=\"(a=\\\"x\\\")\"" ); + + assertEquals( Arrays.asList( "(a=\"x\")" ), values( analyzer, "Import-Service", "p.Bar", "filter" ) ); + } + + + @Test + public void testConfiguredClauseIsMerged() throws Exception + { + Analyzer analyzer = analyze( "service", + BP_START + "\n" + BP_END, + "Import-Service", "p.Foo;filter=\"(a=b)\";multiple:=false" ); + + assertEquals( Arrays.asList( "(a=b)" ), values( analyzer, "Import-Service", "p.Foo", "filter" ) ); + } + + + @Test + public void testConfiguredHeaderKeepsTypedAttribute() throws Exception + { + Analyzer analyzer = analyze( "service", + BP_START + "\n" + BP_END, + "Export-Service", "p.Bar;k:List=\"x,y\"" ); + + String header = analyzer.getProperty( "Export-Service" ); + Attrs attrs = OSGiHeader.parseHeader( header ).get( "p.Bar" ); + assertNotNull( header, attrs ); + assertEquals( "x,y", attrs.get( "k" ) ); + assertEquals( "List", attrs.getType( "k" ).toString() ); + } + + + @Test + public void testGenericFilterWithBackslashIsKept() throws Exception + { + Analyzer analyzer = analyze( "generic", + BP_START + "\n" + BP_END ); + + assertEquals( Arrays.asList( "(&(objectClass=p.Foo)(name=a\\*b))" ), + values( analyzer, "Require-Capability", "osgi.service", "filter:" ) ); + } + + + @Test + public void testGenericFilterWithQuoteIsKept() throws Exception + { + Analyzer analyzer = analyze( "generic", + BP_START + "\n" + BP_END ); + + assertEquals( Arrays.asList( "(&(objectClass=p.Foo)(a=\"x\"))" ), + values( analyzer, "Require-Capability", "osgi.service", "filter:" ) ); + } + + + @Test + public void testGenericServicePropertyWithQuoteIsKept() throws Exception + { + Analyzer analyzer = analyze( "generic", + BP_START + + "\n" + + " \n" + + "\n" + + BP_END ); + + assertEquals( Arrays.asList( "say \"hi\"" ), values( analyzer, "Provide-Capability", "osgi.service", "k" ) ); + } + + + @Test + public void testGenericServicePropertyWithBackslashIsKept() throws Exception + { + Analyzer analyzer = analyze( "generic", + BP_START + + "\n" + + " \n" + + "\n" + + BP_END ); + + assertEquals( Arrays.asList( "a\\b" ), values( analyzer, "Provide-Capability", "osgi.service", "k" ) ); + } + + + private static Analyzer analyze( String mode, final String blueprint, String... properties ) throws Exception + { + Analyzer analyzer = new Analyzer(); + Jar jar = new Jar( "the-jar" ); + jar.putResource( "OSGI-INF/blueprint/bp.xml", new AbstractResource( 0 ) + { + @Override + protected byte[] getBytes() throws Exception + { + return blueprint.getBytes( "UTF-8" ); + } + } ); + analyzer.setJar( jar ); + analyzer.setProperty( "service_mode", mode ); + for ( int i = 0; i < properties.length; i += 2 ) + { + analyzer.setProperty( properties[i], properties[i + 1] ); + } + + new BlueprintPlugin().analyzeJar( analyzer ); + return analyzer; + } + + + private static List values( Analyzer analyzer, String header, String name, String key ) + { + String value = analyzer.getProperty( header ); + assertNotNull( header + " not set", value ); + return values( value, name, key ); + } + + + /** + * Parses the header with bnd and returns the given attribute (or directive, with a trailing ':') + * of every clause with the given name. + */ + static List values( String header, String name, String key ) + { + List result = new ArrayList<>(); + for ( Map.Entry clause : OSGiHeader.parseHeader( header ).entrySet() ) + { + if ( Processor.removeDuplicateMarker( clause.getKey() ).equals( name ) && clause.getValue().containsKey( key ) ) + { + result.add( clause.getValue().get( key ) ); + } + } + return result; + } +}