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 @@ -94,10 +94,18 @@ public static String getFeatureId(Resource resource) {
/**
* Returns the feature ID in the same form as {@link #getFeatureId(Resource)}, which is the form used as key
* in the deployment state. The version is normalized the OSGi way, so <code>1.0.01</code> becomes
* <code>1.0.1</code>.
* <code>1.0.1</code>. A version that is not a valid OSGi version, even after cleaning, is kept as is.
*/
public static String getFeatureId(String name, String version) {
return version != null ? name + "/" + VersionTable.getVersion(version) : name;
if (version == null) {
return name;
}
try {
return name + "/" + VersionTable.getVersion(version);
} catch (IllegalArgumentException e) {
// not a valid OSGi version even after cleaning, keep it as is
return name + "/" + version;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -957,8 +957,9 @@ && isUpdateable(resource) && !deployment.bundleChecksums.containsKey(bundle.getB
//
if (!newFeatures.isEmpty()) {
Set<String> featureIds = flatten(newFeatures);
for (Feature feature : dstate.featuresById.values()) {
if (featureIds.contains(getFeatureId(feature.getName(), feature.getVersion()))) {
for (Map.Entry<String, Feature> entry : dstate.featuresById.entrySet()) {
Feature feature = entry.getValue();
if (featureIds.contains(entry.getKey())) {
callback.installConfigs(feature);
callback.installLibraries(feature);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.felix.utils.version.VersionCleaner;
import org.apache.karaf.features.BundleInfo;
import org.apache.karaf.features.DeploymentEvent;
import org.apache.karaf.features.DeploymentListener;
Expand Down Expand Up @@ -954,7 +953,7 @@ private String normalize(String feature) {
String name = feature.substring(0, idx);
String version = feature.substring(idx + 1);
// same form as the ids in the deployment state, so 1.0.01 matches 1.0.1
return ResourceUtils.getFeatureId(name, VersionCleaner.clean(version));
return ResourceUtils.getFeatureId(name, version);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.net.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
Expand Down Expand Up @@ -288,15 +289,7 @@ public void testInstallAndStopFeatureWithLeadingZeroInVersion() throws Exception
expect(installSupport.installBundle(EasyMock.eq("root"), EasyMock.eq("a100"), anyObject())).andReturn(bundle);
installSupport.startBundle(bundle);
expectLastCall();
expect(bundle.getBundleId()).andReturn(1L).anyTimes();
expect(bundle.getSymbolicName()).andReturn("a").anyTimes();
expect(bundle.getVersion()).andReturn(new Version("1.0.0")).anyTimes();
expect(bundle.getHeaders()).andReturn(new Hashtable<>()).anyTimes();
expect(bundle.adapt(BundleStartLevel.class)).andReturn(bundleStartLevel).anyTimes();
expect(bundle.adapt(BundleRevision.class)).andReturn(bundleRevision).anyTimes();
expect(bundleRevision.getBundle()).andReturn(bundle).anyTimes();
expect(bundleRevision.getCapabilities(null)).andReturn(Collections.emptyList()).anyTimes();
expect(bundleRevision.getRequirements(null)).andReturn(Collections.emptyList()).anyTimes();
expectBundleA(bundle, bundleStartLevel, bundleRevision);
EasyMock.replay(installSupport, bundle, bundleStartLevel, bundleRevision);
FeaturesService featureService = new FeaturesServiceImpl(new Storage(), null, null, this.resolver,
installSupport, null, cfg) {
Expand All @@ -321,6 +314,84 @@ protected DownloadManager createDownloadManager() throws IOException {
expect(installSupport.getInfo()).andReturn(dummyInfo).anyTimes();
installSupport.stopBundle(EasyMock.capture(stoppedBundle), EasyMock.anyInt());
expectLastCall();
expectBundleA(bundle, bundleStartLevel, bundleRevision);
EasyMock.replay(installSupport, bundle, bundleRevision, bundleStartLevel);

featureService.updateFeaturesState(states, EnumSet.noneOf(Option.class));
assertSame(bundle, stoppedBundle.getValue());
assertEquals(FeatureState.Resolved, featureService.getState(feature.getId()));
}

/**
* A feature whose version is not a valid OSGi version, even after cleaning, must not break listing, stopping and
* uninstalling a feature installed before its repository was added, nor querying its own state.
*/
@Test
public void testFeatureWithInvalidVersionDoesNotBreakOtherFeatures() throws Exception {
Capture<Bundle> stoppedBundle = Capture.newInstance();

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/f.xml"));
Feature feature = featureService.getFeature("f", "1.0.0");
installFeature(featureService, feature);
assertInstalled(featureService, feature);

// 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(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);
assertFalse(featureService.isInstalled(bad));

// stop
dummyInfo.bundles.put(1L, bundle);
Map<String, Map<String, FeatureState>> states = new HashMap<>();
states.computeIfAbsent("root", k -> new HashMap<>()).put(feature.getId(), FeatureState.Resolved);
EasyMock.reset(installSupport, bundle, bundleRevision, bundleStartLevel);
expect(installSupport.getInfo()).andReturn(dummyInfo).anyTimes();
installSupport.stopBundle(EasyMock.capture(stoppedBundle), EasyMock.anyInt());
expectLastCall();
expectBundleA(bundle, bundleStartLevel, bundleRevision);
EasyMock.replay(installSupport, bundle, bundleRevision, bundleStartLevel);
featureService.updateFeaturesState(states, EnumSet.noneOf(Option.class));
assertSame(bundle, stoppedBundle.getValue());
assertEquals(FeatureState.Resolved, featureService.getState(feature.getId()));

// uninstall
EasyMock.reset(installSupport, bundle, bundleRevision, bundleStartLevel);
expect(installSupport.getInfo()).andReturn(dummyInfo).anyTimes();
installSupport.uninstall(bundle);
expectLastCall();
expectBundleA(bundle, bundleStartLevel, bundleRevision);
EasyMock.replay(installSupport, bundle, bundleRevision, bundleStartLevel);
featureService.uninstallFeature("f", "1.0.0", EnumSet.noneOf(Option.class));
assertNotInstalled(featureService, feature);
EasyMock.verify(installSupport);
}

private static void expectBundleA(Bundle bundle, BundleStartLevel bundleStartLevel,
BundleRevision bundleRevision) {
expect(bundle.getBundleId()).andReturn(1L).anyTimes();
expect(bundle.getSymbolicName()).andReturn("a").anyTimes();
expect(bundle.getVersion()).andReturn(new Version("1.0.0")).anyTimes();
Expand All @@ -330,11 +401,6 @@ protected DownloadManager createDownloadManager() throws IOException {
expect(bundleRevision.getBundle()).andReturn(bundle).anyTimes();
expect(bundleRevision.getCapabilities(null)).andReturn(Collections.emptyList()).anyTimes();
expect(bundleRevision.getRequirements(null)).andReturn(Collections.emptyList()).anyTimes();
EasyMock.replay(installSupport, bundle, bundleRevision, bundleStartLevel);

featureService.updateFeaturesState(states, EnumSet.noneOf(Option.class));
assertSame(bundle, stoppedBundle.getValue());
assertEquals(FeatureState.Resolved, featureService.getState(feature.getId()));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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-bad" xmlns="http://karaf.apache.org/xmlns/features/v1.3.0">

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

</features>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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-f" xmlns="http://karaf.apache.org/xmlns/features/v1.3.0">

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

</features>
Loading