Repository: brooklyn-server Updated Branches: refs/heads/master 664ee0ea7 -> 1068d905b
Osgis.bundleFinder: support version range Useful for `brooklyn.libraries` when name:version of pre-installed bundle is specified. Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/2b6479f1 Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/2b6479f1 Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/2b6479f1 Branch: refs/heads/master Commit: 2b6479f1c6a8ec1f4828994cdc0888beb80ae6dc Parents: 2fea6d1 Author: Aled Sage <[email protected]> Authored: Fri Aug 25 18:09:32 2017 +0100 Committer: Aled Sage <[email protected]> Committed: Tue Aug 29 14:37:27 2017 +0100 ---------------------------------------------------------------------- .../apache/brooklyn/util/core/osgi/Osgis.java | 31 +++- .../brooklyn/util/core/osgi/OsgisTest.java | 140 +++++++++++++++++++ 2 files changed, 165 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/2b6479f1/core/src/main/java/org/apache/brooklyn/util/core/osgi/Osgis.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/util/core/osgi/Osgis.java b/core/src/main/java/org/apache/brooklyn/util/core/osgi/Osgis.java index 96a30c8..db459ae 100644 --- a/core/src/main/java/org/apache/brooklyn/util/core/osgi/Osgis.java +++ b/core/src/main/java/org/apache/brooklyn/util/core/osgi/Osgis.java @@ -47,6 +47,7 @@ import org.apache.brooklyn.util.text.VersionComparator; import org.osgi.framework.Bundle; import org.osgi.framework.BundleException; import org.osgi.framework.Version; +import org.osgi.framework.VersionRange; import org.osgi.framework.launch.Framework; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -173,19 +174,31 @@ public class Osgis { boolean urlMatched = false; List<Bundle> result = MutableList.of(); String v=null, vDep = null; + VersionRange vRange = null; if (version!=null) { - v = BrooklynVersionSyntax.toValidOsgiVersion(version); - vDep = OsgiUtils.toOsgiVersion(version); + if (isVersionRange(version)) { + vRange = VersionRange.valueOf(version); + } else { + v = BrooklynVersionSyntax.toValidOsgiVersion(version); + vDep = OsgiUtils.toOsgiVersion(version); + } } for (Bundle b: framework.getBundleContext().getBundles()) { if (symbolicName!=null && !symbolicName.equals(b.getSymbolicName())) continue; if (version!=null) { - String bv = b.getVersion().toString(); - if (!v.equals(bv)) { - if (!vDep.equals(bv)) { + Version bv = b.getVersion(); + if (vRange != null) { + if (!vRange.includes(bv)) { continue; } - LOG.warn("Legacy inferred OSGi version string '"+vDep+"' found to match "+symbolicName+":"+version+"; switch to '"+v+"' format to avoid issues with deprecated version syntax"); + } else { + String bvString = bv.toString(); + if (!v.equals(bvString)) { + if (!vDep.equals(bvString)) { + continue; + } + LOG.warn("Legacy inferred OSGi version string '"+vDep+"' found to match "+symbolicName+":"+version+"; switch to '"+v+"' format to avoid issues with deprecated version syntax"); + } } } if (!Predicates.and(predicates).apply(b)) continue; @@ -262,6 +275,12 @@ public class Osgis { predicates.add(predicate); return this; } + + private boolean isVersionRange(String version) { + return (version != null) && (version.length() > 2) + && (version.charAt(0) == VersionRange.LEFT_OPEN || version.charAt(0) == VersionRange.LEFT_CLOSED) + && (version.charAt(version.length()-1) == VersionRange.RIGHT_OPEN || version.charAt(version.length()-1) == VersionRange.RIGHT_CLOSED); + } } public static BundleFinder bundleFinder(Framework framework) { http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/2b6479f1/core/src/test/java/org/apache/brooklyn/util/core/osgi/OsgisTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/util/core/osgi/OsgisTest.java b/core/src/test/java/org/apache/brooklyn/util/core/osgi/OsgisTest.java new file mode 100644 index 0000000..fbcf0d0 --- /dev/null +++ b/core/src/test/java/org/apache/brooklyn/util/core/osgi/OsgisTest.java @@ -0,0 +1,140 @@ +/* + * 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.brooklyn.util.core.osgi; + +import static org.testng.Assert.assertEquals; + +import java.util.List; + +import org.mockito.Mockito; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.Version; +import org.osgi.framework.launch.Framework; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableList; + +public class OsgisTest { + + private Framework framework; + private BundleContext bundleContext; + private Bundle myBundle1_0_0; + private Bundle myBundle1_1_0; + private Bundle myBundle2_0_0; + private Bundle myBundle2_0_0_snapshot; + private Bundle otherBundle1_0_0; + private Bundle snapshotBundle1_0_0_snapshot; + + @BeforeMethod(alwaysRun=true) + public void setUp() throws Exception { + myBundle1_0_0 = Mockito.mock(Bundle.class); + Mockito.when(myBundle1_0_0.getSymbolicName()).thenReturn("mybundle"); + Mockito.when(myBundle1_0_0.getVersion()).thenReturn(Version.parseVersion("1.0.0")); + + myBundle1_1_0 = Mockito.mock(Bundle.class); + Mockito.when(myBundle1_1_0.getSymbolicName()).thenReturn("mybundle"); + Mockito.when(myBundle1_1_0.getVersion()).thenReturn(Version.parseVersion("1.1.0")); + + myBundle2_0_0 = Mockito.mock(Bundle.class); + Mockito.when(myBundle2_0_0.getSymbolicName()).thenReturn("mybundle"); + Mockito.when(myBundle2_0_0.getVersion()).thenReturn(Version.parseVersion("2.0.0")); + + myBundle2_0_0_snapshot = Mockito.mock(Bundle.class); + Mockito.when(myBundle2_0_0_snapshot.getSymbolicName()).thenReturn("mybundle"); + Mockito.when(myBundle2_0_0_snapshot.getVersion()).thenReturn(Version.parseVersion("2.0.0.SNAPSHOT")); + + otherBundle1_0_0 = Mockito.mock(Bundle.class); + Mockito.when(otherBundle1_0_0.getSymbolicName()).thenReturn("otherbundle"); + Mockito.when(otherBundle1_0_0.getVersion()).thenReturn(Version.parseVersion("1.0.0")); + + snapshotBundle1_0_0_snapshot = Mockito.mock(Bundle.class); + Mockito.when(snapshotBundle1_0_0_snapshot.getSymbolicName()).thenReturn("snapshotbundle"); + Mockito.when(snapshotBundle1_0_0_snapshot.getVersion()).thenReturn(Version.parseVersion("1.0.0.SNAPSHOT")); + + bundleContext = Mockito.mock(BundleContext.class); + Mockito.when(bundleContext.getBundles()).thenReturn(new Bundle[] {myBundle1_0_0, myBundle1_1_0, myBundle2_0_0, + myBundle2_0_0_snapshot, otherBundle1_0_0, snapshotBundle1_0_0_snapshot}); + + framework = Mockito.mock(Framework.class); + Mockito.when(framework.getBundleContext()).thenReturn(bundleContext); + + } + + @Test + public void testFindByNameAndVersion() throws Exception { + Bundle result = Osgis.bundleFinder(framework) + .symbolicName("mybundle") + .version("1.0.0") + .find() + .get(); + assertEquals(result, myBundle1_0_0); + } + + @Test + public void testFindAllByNameAndVersion() throws Exception { + List<Bundle> result = Osgis.bundleFinder(framework) + .symbolicName("mybundle") + .version("1.0.0") + .findAll(); + assertEquals(result, ImmutableList.of(myBundle1_0_0)); + } + + @Test + public void testFindByNameAndVersionRange() throws Exception { + List<Bundle> result = Osgis.bundleFinder(framework) + .symbolicName("mybundle") + .version("[1.0.0, 1.1.0)") + .findAll(); + assertEquals(result, ImmutableList.of(myBundle1_0_0)); + + List<Bundle> result2 = Osgis.bundleFinder(framework) + .symbolicName("mybundle") + .version("[1.0.0, 2.0.0)") + .findAll(); + assertEquals(result2, ImmutableList.of(myBundle1_0_0, myBundle1_1_0)); + + List<Bundle> result3 = Osgis.bundleFinder(framework) + .symbolicName("mybundle") + .version("[1.0.0, 2.0.0]") + .findAll(); + assertEquals(result3, ImmutableList.of(myBundle1_0_0, myBundle1_1_0, myBundle2_0_0)); + + List<Bundle> result4 = Osgis.bundleFinder(framework) + .symbolicName("mybundle") + .version("[2.0.0, 3.0.0)") + .findAll(); + assertEquals(result4, ImmutableList.of(myBundle2_0_0_snapshot, myBundle2_0_0)); + + List<Bundle> result5 = Osgis.bundleFinder(framework) + .symbolicName("snapshotbundle") + .version("[1.0.0, 2.0.0)") + .findAll(); + assertEquals(result5, ImmutableList.of(snapshotBundle1_0_0_snapshot)); + } + + @Test + public void testFindAllByNameOnly() throws Exception { + List<Bundle> result = Osgis.bundleFinder(framework) + .symbolicName("mybundle") + .findAll(); + assertEquals(result, ImmutableList.of(myBundle2_0_0_snapshot, myBundle1_0_0, myBundle1_1_0, myBundle2_0_0)); + } +}
