Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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<String, Attrs> 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)
Expand All @@ -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<String> headers )
{
InputStream in = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
<xsl:when test="@filter">
<xsl:choose>
<xsl:when test="@component-name">
<xsl:value-of select="concat(';filter=&quot;(&amp;', @filter, ')(osgi.service.blueprint.compname=', @component-name, ')&quot;')" />
<xsl:value-of select="concat(';filter=&quot;(&amp;', @filter, '(osgi.service.blueprint.compname=', @component-name, '))&quot;')" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat(';filter=&quot;', @filter, '&quot;')" />
Expand Down Expand Up @@ -170,7 +170,7 @@
<xsl:when test="@filter">
<xsl:choose>
<xsl:when test="@component-name">
<xsl:value-of select="concat(';filter=&quot;(&amp;', @filter, ')(osgi.service.blueprint.compname=', @component-name, ')&quot;')" />
<xsl:value-of select="concat(';filter=&quot;(&amp;', @filter, '(osgi.service.blueprint.compname=', @component-name, '))&quot;')" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat(';filter=&quot;', @filter, '&quot;')" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<String> 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<String> pkgs = Create.list();
Expand Down
Original file line number Diff line number Diff line change
@@ -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 = "<blueprint xmlns='http://www.osgi.org/xmlns/blueprint/v1.0.0'>\n";
private static final String BP_END = "</blueprint>";


@Test
public void testFilterWithBackslashIsKept() throws Exception
{
Analyzer analyzer = analyze( "service",
BP_START + "<reference interface='p.Foo' filter='(name=a\\*b)'/>\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 + "<reference interface='p.Foo' filter='(a=\"x\")'/>\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 + "<reference interface='p.Foo' filter='(a=b)' component-name='c'/>\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 + "<reference interface='p.Foo'/>\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 + "<reference interface='p.Foo'/>\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 + "<reference interface='p.Foo' filter='(a=b)'/>\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 + "<service interface='p.Foo' ref='b'/>\n" + BP_END,
"Export-Service", "p.Bar;k:List<String>=\"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<String>", attrs.getType( "k" ).toString() );
}


@Test
public void testGenericFilterWithBackslashIsKept() throws Exception
{
Analyzer analyzer = analyze( "generic",
BP_START + "<reference interface='p.Foo' filter='(name=a\\*b)'/>\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 + "<reference interface='p.Foo' filter='(a=\"x\")'/>\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
+ "<service interface='p.Foo' ref='b'>\n"
+ " <service-properties><entry key='k' value='say \"hi\"'/></service-properties>\n"
+ "</service>\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
+ "<service interface='p.Foo' ref='b'>\n"
+ " <service-properties><entry key='k' value='a\\b'/></service-properties>\n"
+ "</service>\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<String> 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<String> values( String header, String name, String key )
{
List<String> result = new ArrayList<>();
for ( Map.Entry<String, Attrs> clause : OSGiHeader.parseHeader( header ).entrySet() )
{
if ( Processor.removeDuplicateMarker( clause.getKey() ).equals( name ) && clause.getValue().containsKey( key ) )
{
result.add( clause.getValue().get( key ) );
}
}
return result;
}
}
Loading