Skip to content
Open
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 @@ -64,6 +64,8 @@
import org.osgi.resource.Capability;
import org.osgi.resource.Requirement;
import org.osgi.resource.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static java.util.jar.JarFile.MANIFEST_NAME;
import static org.apache.karaf.features.internal.resolver.ResourceUtils.TYPE_FEATURE;
Expand All @@ -83,6 +85,8 @@
*/
public class Subsystem extends ResourceImpl {

private static final Logger LOGGER = LoggerFactory.getLogger(Subsystem.class);

private static final String ALL_FILTER = "(|(!(all=*))(all=*))";

private static final String SUBSYSTEM_FILTER = String.format("(%s=%s)", CAPABILITY_TYPE_ATTRIBUTE, TYPE_SUBSYSTEM);
Expand Down Expand Up @@ -398,7 +402,15 @@ private void doBuild(Map<String, List<Feature>> allFeatures, boolean mandatory)
VersionRange range = (VersionRange) requirement.getAttributes().get(CAPABILITY_VERSION_ATTRIBUTE);
if (TYPE_FEATURE.equals(type) && allFeatures.containsKey(name)) {
for (Feature feature : allFeatures.get(name)) {
if (range == null || range.contains(VersionTable.getVersion(feature.getVersion()))) {
Version version;
try {
version = VersionTable.getVersion(feature.getVersion());
} catch (IllegalArgumentException e) {
LOGGER.warn("Ignoring feature {}/{} with invalid OSGi version: {}",
feature.getName(), feature.getVersion(), e.getMessage());
continue;
}
if (range == null || range.contains(version)) {
if (feature != this.feature && !feature.isBlacklisted()) {
String ssName = this.name + "#" + (feature.hasVersion() ? feature.getName() + "-" + feature.getVersion() : feature.getName());
Subsystem fs = getChild(ssName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.karaf.features.internal.service;

import org.apache.felix.utils.version.VersionCleaner;
import org.apache.felix.utils.version.VersionRange;
import org.apache.felix.utils.version.VersionTable;
import org.apache.karaf.features.Feature;
Expand Down Expand Up @@ -52,6 +53,7 @@ public class FeatureReq {

private String name;
private VersionRange versionRange;
private String rawVersion;
private boolean blacklisted = false;

public static FeatureReq parseRequirement(String featureReq) {
Expand All @@ -69,12 +71,16 @@ public static FeatureReq parseNameAndRange(String nameAndRange) {
public FeatureReq(String nameAndRange) {
String[] parts = nameAndRange.trim().split(VERSION_SEPARATOR);
this.name = parts[0];
this.versionRange = (parts.length == 1) ? ANY_VERSION : range(parts[1]);
if (parts.length == 1) {
this.versionRange = ANY_VERSION;
} else {
initVersion(parts[1]);
}
}

public FeatureReq(String name, String versionRange) {
this.name = name;
this.versionRange = range(versionRange);
initVersion(versionRange);
}

public FeatureReq(String name, VersionRange versionRange, boolean blacklisted) {
Expand All @@ -84,7 +90,9 @@ public FeatureReq(String name, VersionRange versionRange, boolean blacklisted) {
}

public FeatureReq(Feature feature) {
this(feature.getName(), exactVersion(feature.getVersion()), feature.isBlacklisted());
this.name = feature.getName();
this.blacklisted = feature.isBlacklisted();
initExactVersion(feature.getVersion());
}

public String getName() {
Expand All @@ -98,7 +106,17 @@ public VersionRange getVersionRange() {
public Set<FeatureReq> getMatchingRequirements(Set<FeatureReq> reqs) {
Pattern pattern = Pattern.compile(name);
return filter(reqs, fr -> pattern.matcher(fr.getName()).matches()
&& versionRange.intersect(fr.getVersionRange()) != null);
&& matchesVersion(fr));
}

private boolean matchesVersion(FeatureReq other) {
if (rawVersion != null || other.rawVersion != null) {
if (rawVersion == null && ANY_VERSION.equals(versionRange)) {
return true;
}
return Objects.equals(rawVersion, other.rawVersion);
}
return versionRange.intersect(other.versionRange) != null;
}

public Stream<Feature> getMatchingFeatures(Map<String, Map<String, Feature>> allFeatures) {
Expand All @@ -107,28 +125,39 @@ public Stream<Feature> getMatchingFeatures(Map<String, Map<String, Feature>> all
Feature matchingFeature = null;
if (pattern.matcher(featureName).matches()) {
Map<String, Feature> versions = allFeatures.get(featureName);
matchingFeature = getLatestFeature(versions, versionRange);
matchingFeature = getLatestFeature(versions);
}
return Optional.ofNullable(matchingFeature);
};
return allFeatures.keySet().stream().map(func).filter(Optional::isPresent).map(Optional::get);
}

private static Feature getLatestFeature(Map<String, Feature> versions, VersionRange versionRange) {
private Feature getLatestFeature(Map<String, Feature> versions) {
if (versions == null || versions.isEmpty()) {
return null;
}
if (rawVersion != null) {
Feature direct = versions.get(rawVersion);
return direct != null ? direct : versions.get(VersionCleaner.clean(rawVersion));
}
Feature feature = null;
if (versions != null && !versions.isEmpty()) {
Version latest = Version.emptyVersion;
for (String available : versions.keySet()) {
Version availableVersion = VersionTable.getVersion(available);
if (availableVersion.compareTo(latest) >= 0 && versionRange.contains(availableVersion)) {
Feature possiblyBlacklisted = versions.get(available);
// return only if there are no more non-blaclisted features
if (feature == null || !possiblyBlacklisted.isBlacklisted()) {
feature = possiblyBlacklisted;
}
if (!possiblyBlacklisted.isBlacklisted()) {
latest = availableVersion;
}
Version latest = Version.emptyVersion;
for (String available : versions.keySet()) {
Version availableVersion;
try {
availableVersion = VersionTable.getVersion(available);
} catch (IllegalArgumentException e) {
// Skip features whose version cannot be parsed as an OSGi Version
continue;
}
if (availableVersion.compareTo(latest) >= 0 && versionRange.contains(availableVersion)) {
Feature possiblyBlacklisted = versions.get(available);
// return only if there are no more non-blaclisted features
if (feature == null || !possiblyBlacklisted.isBlacklisted()) {
feature = possiblyBlacklisted;
}
if (!possiblyBlacklisted.isBlacklisted()) {
latest = availableVersion;
}
}
}
Expand All @@ -137,7 +166,7 @@ private static Feature getLatestFeature(Map<String, Feature> versions, VersionRa

@Override
public String toString() {
return this.name + "/" + this.getVersionRange().toString();
return this.name + "/" + (this.rawVersion != null ? this.rawVersion : this.getVersionRange().toString());
}

public String toRequirement() {
Expand All @@ -154,31 +183,39 @@ public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
FeatureReq that = (FeatureReq) o;
return Objects.equals(name, that.name) &&
Objects.equals(versionRange, that.versionRange);
Objects.equals(versionRange, that.versionRange) &&
Objects.equals(rawVersion, that.rawVersion);
}

@Override
public int hashCode() {
return Objects.hash(name, versionRange);
return Objects.hash(name, versionRange, rawVersion);
}

private static VersionRange range(String versionRange) {
private void initVersion(String versionRange) {
if (versionRange == null) {
return ANY_VERSION;
this.versionRange = ANY_VERSION;
return;
}
versionRange = versionRange.trim();
if ("0.0.0".equals(versionRange)) {
return ANY_VERSION;
this.versionRange = ANY_VERSION;
return;
}
if (versionRange.contains(",")) {
return new VersionRange(versionRange, false, true);
this.versionRange = new VersionRange(versionRange, false, true);
} else {
return exactVersion(versionRange);
initExactVersion(versionRange);
}
}

private static VersionRange exactVersion(String versionRange) {
return new VersionRange(versionRange, true, true);
private void initExactVersion(String version) {
try {
this.versionRange = new VersionRange(version, true, true);
} catch (IllegalArgumentException e) {
// Keep the raw version when it is not a valid OSGi version even after cleaning
this.rawVersion = version;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,31 @@ public void testBlacklistedBundles() throws Exception {
verify(resolver, expected);
}

@Test
public void testIgnoreSameNameFeatureWithInvalidVersion() throws Exception {
org.apache.karaf.features.internal.model.Feature valid =
new org.apache.karaf.features.internal.model.Feature("f1", "1.0.0");
valid.getBundle().add(new Bundle("c"));
org.apache.karaf.features.internal.model.Feature invalid =
new org.apache.karaf.features.internal.model.Feature("f1", "1.0.0.beta+1");
invalid.getBundle().add(new Bundle("b"));

Map<String, Set<String>> features = new HashMap<>();
addToMapSet(features, "root", "f1");

Map<String, Set<String>> expected = new HashMap<>();
addToMapSet(expected, "root", "c/1.0.0");

SubsystemResolver resolver = new SubsystemResolver(this.resolver, new TestDownloadManager(getClass(), "data1"));
resolver.prepare(partitionByName(new Feature[] { valid, invalid }),
features,
Collections.emptyMap());
resolver.resolve(FeaturesService.DEFAULT_FEATURE_RESOLUTION_RANGE,
null, null, null);

verify(resolver, expected);
}

private void verify(SubsystemResolver resolver, Map<String, Set<String>> expected) {
Map<String, Set<String>> mapping = getBundleNamesPerRegions(resolver);
if (!expected.equals(mapping)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

Expand Down Expand Up @@ -359,10 +360,14 @@ protected DownloadManager createDownloadManager() throws IOException {
// the repository with the invalid version is added after f was installed
featureService.addRepository(URI.create("custom:invalidversion/bad.xml"));
assertEquals(1, featureService.listInstalledFeatures().length);
assertEquals(1, featureService.listRequiredFeatures().length);
assertEquals(FeatureState.Uninstalled, featureService.getState("bad/1.0.0.beta+1"));
Feature bad = Arrays.stream(featureService.listFeatures())
.filter(f -> "bad".equals(f.getName())).findFirst().orElseThrow(AssertionError::new);
Feature bad = featureService.getFeature("bad", "1.0.0.beta+1");
assertNotNull(bad);
assertNull(featureService.getFeature("bad", "1.0.0.missing+1"));
assertNull(featureService.getFeature("bad"));
assertFalse(featureService.isInstalled(bad));
assertFalse(featureService.isRequired(bad));

// stop
dummyInfo.bundles.put(1L, bundle);
Expand Down Expand Up @@ -390,6 +395,51 @@ protected DownloadManager createDownloadManager() throws IOException {
EasyMock.verify(installSupport);
}

/**
* A feature with the same name as a valid feature but an unparseable OSGi version must not break
* looking up or installing the valid feature.
*/
@Test
public void testFeatureWithSameNameAndInvalidVersionDoesNotBreakInstall() throws Exception {
Bundle bundle = EasyMock.niceMock(Bundle.class);
BundleStartLevel bundleStartLevel = EasyMock.niceMock(BundleStartLevel.class);
BundleRevision bundleRevision = EasyMock.niceMock(BundleRevision.class);

FeaturesServiceConfig cfg = new FeaturesServiceConfig();
BundleInstallSupport installSupport = EasyMock.niceMock(BundleInstallSupport.class);
FrameworkInfo dummyInfo = new FrameworkInfo();
expect(installSupport.getInfo()).andReturn(dummyInfo).atLeastOnce();
expect(installSupport.installBundle(EasyMock.eq("root"), EasyMock.eq("a100"), anyObject())).andReturn(bundle);
installSupport.startBundle(bundle);
expectLastCall();
expectBundleA(bundle, bundleStartLevel, bundleRevision);
EasyMock.replay(installSupport, bundle, bundleStartLevel, bundleRevision);
FeaturesService featureService = new FeaturesServiceImpl(new Storage(), null, null, this.resolver,
installSupport, null, cfg) {
@Override
protected DownloadManager createDownloadManager() throws IOException {
return new TestDownloadManager(FeaturesServiceImplTest.class, "data1");
}
};

featureService.addRepository(URI.create("custom:invalidversion/same-name.xml"));
Feature valid = featureService.getFeature("f", "1.0.0");
assertNotNull(valid);
assertEquals("1.0.0", valid.getVersion());
assertSame(valid, featureService.getFeature("f"));
Feature invalid = featureService.getFeature("f", "1.0.0.beta+1");
assertNotNull(invalid);
assertEquals("1.0.0.beta+1", invalid.getVersion());

featureService.installFeature("f");
waitInstalled(featureService, valid);
assertInstalled(featureService, valid);
assertFalse(featureService.isInstalled(invalid));
assertEquals(1, featureService.listInstalledFeatures().length);
assertEquals(1, featureService.listRequiredFeatures().length);
EasyMock.verify(installSupport);
}

private static void expectBundleA(Bundle bundle, BundleStartLevel bundleStartLevel,
BundleRevision bundleRevision) {
expect(bundle.getBundleId()).andReturn(1L).anyTimes();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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.
-->
<features name="test-same-name" xmlns="http://karaf.apache.org/xmlns/features/v1.3.0">

<feature name="f" version="1.0.0">
<bundle>a100</bundle>
</feature>

<!-- not a valid OSGi version, even after cleaning -->
<feature name="f" version="1.0.0.beta+1">
<bundle>a100</bundle>
</feature>

</features>
Loading