Repository: karaf Updated Branches: refs/heads/karaf-3.0.x 3d4ed7416 -> 2cb406245
[KARAF-3705]put all bundles before FeaturesService available as startup feature Project: http://git-wip-us.apache.org/repos/asf/karaf/repo Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/2cb40624 Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/2cb40624 Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/2cb40624 Branch: refs/heads/karaf-3.0.x Commit: 2cb4062452f2ece63543d163f996ea1cd68ed09d Parents: 3d4ed74 Author: Freeman Fang <[email protected]> Authored: Mon Apr 27 15:45:47 2015 +0800 Committer: Freeman Fang <[email protected]> Committed: Mon Apr 27 15:45:47 2015 +0800 ---------------------------------------------------------------------- .../features/internal/FeaturesServiceImpl.java | 14 ++++++++++++ .../karaf/features/FeaturesServiceTest.java | 23 +++++++++++++++----- .../internal/FeaturesServiceImplTest.java | 11 +++++++--- 3 files changed, 39 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/karaf/blob/2cb40624/features/core/src/main/java/org/apache/karaf/features/internal/FeaturesServiceImpl.java ---------------------------------------------------------------------- diff --git a/features/core/src/main/java/org/apache/karaf/features/internal/FeaturesServiceImpl.java b/features/core/src/main/java/org/apache/karaf/features/internal/FeaturesServiceImpl.java index a24eeb9..0c7982b 100644 --- a/features/core/src/main/java/org/apache/karaf/features/internal/FeaturesServiceImpl.java +++ b/features/core/src/main/java/org/apache/karaf/features/internal/FeaturesServiceImpl.java @@ -31,6 +31,7 @@ import org.apache.karaf.features.Resolver; import org.apache.karaf.features.internal.BundleManager.BundleInstallerResult; import org.apache.karaf.util.collections.CopyOnWriteArrayIdentityList; import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; import org.osgi.framework.BundleException; import org.osgi.framework.Constants; import org.osgi.framework.Version; @@ -105,6 +106,19 @@ public class FeaturesServiceImpl implements FeaturesService { public FeaturesServiceImpl(BundleManager bundleManager, FeatureConfigInstaller configManager) { this.bundleManager = bundleManager; this.configManager = configManager; + // put all bundles before FeaturesService available as startup feature + // so those bundles are hold by this feature and won't be uninstalled by + // any other feature easily + Set<Long> startupBundleSet = new HashSet<Long>(); + if (bundleManager != null) { + BundleContext bundleContext = bundleManager.getBundleContext(); + if (bundleContext != null && bundleContext.getBundles() != null && installed.size() == 0) { + for (Bundle startupBundle : bundleContext.getBundles()) { + startupBundleSet.add(startupBundle.getBundleId()); + } + installed.put(new org.apache.karaf.features.internal.model.Feature("startup"), startupBundleSet); + } + } } public long getResolverTimeout() { http://git-wip-us.apache.org/repos/asf/karaf/blob/2cb40624/features/core/src/test/java/org/apache/karaf/features/FeaturesServiceTest.java ---------------------------------------------------------------------- diff --git a/features/core/src/test/java/org/apache/karaf/features/FeaturesServiceTest.java b/features/core/src/test/java/org/apache/karaf/features/FeaturesServiceTest.java index e158cc0..b86565f 100644 --- a/features/core/src/test/java/org/apache/karaf/features/FeaturesServiceTest.java +++ b/features/core/src/test/java/org/apache/karaf/features/FeaturesServiceTest.java @@ -82,6 +82,7 @@ public class FeaturesServiceTest extends TestBase { + "</features>"); BundleManager bundleManager = EasyMock.createMock(BundleManager.class); + expect(bundleManager.getBundleContext()).andReturn(null); Bundle installedBundle = createDummyBundle(12345L, "bundle-f1", headers()); FeaturesServiceImpl svc = testAddRepository("bundle-f1", uri, bundleManager, installedBundle); @@ -141,6 +142,7 @@ public class FeaturesServiceTest extends TestBase { BundleManager bundleManager = EasyMock.createMock(BundleManager.class); BundleContext bundleContext = EasyMock.createMock(BundleContext.class); expect(bundleManager.getDataFile(EasyMock.anyObject(String.class))).andReturn(dataFile).anyTimes(); + expect(bundleManager.getBundleContext()).andReturn(null); expect(bundleManager.installBundleIfNeeded("bundle-0.1", 0, null)).andReturn(new BundleInstallerResult(bundlef101, true)); expect(bundleManager.installBundleIfNeeded("bundle-0.1", 0, null)).andReturn(new BundleInstallerResult(bundlef101, false)); expect(bundleManager.isBundleInstalled("bundle-0.1")).andReturn(bundlef101).times(2); @@ -184,7 +186,7 @@ public class FeaturesServiceTest extends TestBase { BundleManager bundleManager = EasyMock.createMock(BundleManager.class); expect(bundleManager.getDataFile(EasyMock.<String>anyObject())).andReturn(dataFile).anyTimes(); - + expect(bundleManager.getBundleContext()).andReturn(null); replay(bundleManager); FeaturesServiceImpl svc = new FeaturesServiceImpl(bundleManager); EasyMock.verify(bundleManager); @@ -208,6 +210,7 @@ public class FeaturesServiceTest extends TestBase { BundleManager bundleManager = EasyMock.createMock(BundleManager.class); BundleContext bundleContext = EasyMock.createMock(BundleContext.class); + expect(bundleManager.getBundleContext()).andReturn(null); Bundle bundlef101 = createDummyBundle(12345L, "bundle-f1-0.1", headers()); Bundle bundlef201 = createDummyBundle(54321L, "bundle-f2-0.1", headers()); expect(bundleManager.getDataFile(EasyMock.<String> anyObject())).andReturn(dataFile).anyTimes(); @@ -257,7 +260,8 @@ public class FeaturesServiceTest extends TestBase { + "</features>"); BundleManager bundleManager = prepareBundleManagerForInstallUninstall("bundle-0.1", "bundle-0.1"); - + BundleContext bundleContext = EasyMock.createMock(BundleContext.class); + expect(bundleManager.getBundleContext()).andReturn(bundleContext); replay(bundleManager); FeaturesServiceImpl svc = new FeaturesServiceImpl(bundleManager); svc.addRepository(uri); @@ -276,7 +280,8 @@ public class FeaturesServiceTest extends TestBase { + "</features>"); BundleManager bundleManager = prepareBundleManagerForInstallUninstall("bundle-0.2", "bundle-0.2"); - + BundleContext bundleContext = EasyMock.createMock(BundleContext.class); + expect(bundleManager.getBundleContext()).andReturn(bundleContext); replay(bundleManager); FeaturesServiceImpl svc = new FeaturesServiceImpl(bundleManager); svc.addRepository(uri); @@ -298,6 +303,7 @@ public class FeaturesServiceTest extends TestBase { BundleManager bundleManager = EasyMock.createMock(BundleManager.class); BundleContext bundleContext = EasyMock.createMock(BundleContext.class); Bundle bundleVer02 = createDummyBundle(54321L, "bundleVer02", headers()); + expect(bundleManager.getBundleContext()).andReturn(null); expect(bundleManager.getDataFile(EasyMock.<String>anyObject())).andReturn(dataFile).anyTimes(); expect(bundleManager.installBundleIfNeeded("bundle-0.2", 0, null)).andReturn(new BundleInstallerResult(bundleVer02, true)); expect(bundleManager.getBundleContext()).andReturn(bundleContext); @@ -332,6 +338,7 @@ public class FeaturesServiceTest extends TestBase { BundleContext bundleContext = EasyMock.createMock(BundleContext.class); Bundle bundleVer01 = createDummyBundle(12345L, "bundleVer01", headers()); expect(bundleManager.getDataFile(EasyMock.<String>anyObject())).andReturn(dataFile).anyTimes(); + expect(bundleManager.getBundleContext()).andReturn(null); expect(bundleManager.installBundleIfNeeded(bundleVer01Uri, 0, null)).andReturn(new BundleInstallerResult(bundleVer01, true)); expect(bundleManager.installBundleIfNeeded(bundleVer01Uri, 0, null)).andReturn(new BundleInstallerResult(bundleVer01, false)); expect(bundleManager.isBundleInstalled("bundle-0.1")).andReturn(bundleVer01).times(2); @@ -361,7 +368,7 @@ public class FeaturesServiceTest extends TestBase { BundleManager bundleManager = EasyMock.createMock(BundleManager.class); expect(bundleManager.getDataFile(EasyMock.<String>anyObject())).andReturn(dataFile).anyTimes(); - + expect(bundleManager.getBundleContext()).andReturn(null); replay(bundleManager); FeaturesServiceImpl svc = new FeaturesServiceImpl(bundleManager); svc.addRepository(uri); @@ -384,6 +391,7 @@ public class FeaturesServiceTest extends TestBase { Bundle installedBundle1 = createDummyBundle(12345L, "bundle1", headers()); Bundle installedBundle2 = createDummyBundle(54321L, "bundle2", headers()); expect(bundleManager.getDataFile(EasyMock.<String>anyObject())).andReturn(dataFile).anyTimes(); + expect(bundleManager.getBundleContext()).andReturn(null); expect(bundleManager.installBundleIfNeeded(bundle1Uri, 0, null)).andReturn(new BundleInstallerResult(installedBundle1, true)); expect(bundleManager.installBundleIfNeeded(bundle2Uri, 0, null)).andReturn(new BundleInstallerResult(installedBundle2, true)); expect(bundleManager.installBundleIfNeeded("zfs:unknown", 0, null)).andThrow(new MalformedURLException()); @@ -410,6 +418,7 @@ public class FeaturesServiceTest extends TestBase { Bundle installedBundle1 = createDummyBundle(12345L, "bundle1", headers()); Bundle installedBundle2 = createDummyBundle(54321L, "bundle2", headers()); expect(bundleManager.getDataFile(EasyMock.<String>anyObject())).andReturn(dataFile).anyTimes(); + expect(bundleManager.getBundleContext()).andReturn(null); expect(bundleManager.installBundleIfNeeded(bundle1Uri, 0, null)).andReturn(new BundleInstallerResult(installedBundle1, true)); expect(bundleManager.installBundleIfNeeded(bundle2Uri, 0, null)).andReturn(new BundleInstallerResult(installedBundle2, true)); expect(bundleManager.installBundleIfNeeded("zfs:unknown", 0, null)).andThrow(new MalformedURLException()); @@ -434,6 +443,7 @@ public class FeaturesServiceTest extends TestBase { URI uri = createTempRepo(FEATURE_WITH_INVALID_BUNDLE, bundle1Uri, bundle2Uri); BundleManager bundleManager = EasyMock.createMock(BundleManager.class); + expect(bundleManager.getBundleContext()).andReturn(null); Bundle installedBundle1 = createDummyBundle(12345L, bundle1Uri, headers()); Bundle installedBundle2 = createDummyBundle(54321L, bundle2Uri, headers()); expect(bundleManager.getDataFile(EasyMock.<String>anyObject())).andReturn(dataFile).anyTimes(); @@ -466,6 +476,7 @@ public class FeaturesServiceTest extends TestBase { Bundle installedBundle1 = createDummyBundle(12345L, "bundle1", headers()); Bundle installedBundle2 = createDummyBundle(54321L, "bundle2", headers()); expect(bundleManager.getDataFile(EasyMock.<String>anyObject())).andReturn(dataFile).anyTimes(); + expect(bundleManager.getBundleContext()).andReturn(null); expect(bundleManager.installBundleIfNeeded(bundle1Uri, 0, null)).andReturn(new BundleInstallerResult(installedBundle1, true)); expect(bundleManager.installBundleIfNeeded(bundle2Uri, 0, null)).andReturn(new BundleInstallerResult(installedBundle2, true)); expect(bundleManager.installBundleIfNeeded("zfs:unknown", 0, null)).andThrow(new MalformedURLException()); @@ -497,7 +508,7 @@ public class FeaturesServiceTest extends TestBase { BundleManager bundleManager = EasyMock.createMock(BundleManager.class); expect(bundleManager.getDataFile(EasyMock.<String>anyObject())).andReturn(dataFile).anyTimes(); - + expect(bundleManager.getBundleContext()).andReturn(null); replay(bundleManager); FeaturesServiceImpl svc = new FeaturesServiceImpl(bundleManager); try { @@ -520,7 +531,7 @@ public class FeaturesServiceTest extends TestBase { BundleManager bundleManager = EasyMock.createMock(BundleManager.class); expect(bundleManager.getDataFile(EasyMock.<String>anyObject())).andReturn(dataFile).anyTimes(); - + expect(bundleManager.getBundleContext()).andReturn(null); replay(bundleManager); FeaturesServiceImpl svc = new FeaturesServiceImpl(bundleManager); svc.addRepository(uri); http://git-wip-us.apache.org/repos/asf/karaf/blob/2cb40624/features/core/src/test/java/org/apache/karaf/features/internal/FeaturesServiceImplTest.java ---------------------------------------------------------------------- diff --git a/features/core/src/test/java/org/apache/karaf/features/internal/FeaturesServiceImplTest.java b/features/core/src/test/java/org/apache/karaf/features/internal/FeaturesServiceImplTest.java index eab17f6..591505e 100644 --- a/features/core/src/test/java/org/apache/karaf/features/internal/FeaturesServiceImplTest.java +++ b/features/core/src/test/java/org/apache/karaf/features/internal/FeaturesServiceImplTest.java @@ -37,6 +37,8 @@ import org.apache.felix.utils.manifest.Clause; import org.apache.karaf.features.Feature; import org.apache.karaf.features.internal.BundleManager.BundleInstallerResult; import org.easymock.EasyMock; +import org.osgi.framework.BundleContext; + import org.junit.Before; import org.junit.Test; @@ -106,6 +108,7 @@ public class FeaturesServiceImplTest extends TestBase { public void testStartDoesNotFailWithOneInvalidUri() { BundleManager bundleManager = EasyMock.createMock(BundleManager.class); expect(bundleManager.getDataFile(EasyMock.<String>anyObject())).andReturn(dataFile).anyTimes(); + expect(bundleManager.getBundleContext()).andReturn(null); expect(bundleManager.createAndRegisterEventAdminListener()).andReturn(null); replay(bundleManager); FeaturesServiceImpl service = new FeaturesServiceImpl(bundleManager, null); @@ -128,6 +131,8 @@ public class FeaturesServiceImplTest extends TestBase { public void testNoDuplicateFeaturesInstallation() throws Exception { final List<Feature> installed = new LinkedList<Feature>(); BundleManager bundleManager = EasyMock.createMock(BundleManager.class); + BundleContext bundleContext = EasyMock.createMock(BundleContext.class); + expect(bundleManager.getBundleContext()).andReturn(bundleContext); expect(bundleManager.installBundleIfNeeded(EasyMock.anyObject(String.class), EasyMock.anyInt(), EasyMock.anyObject(String.class))) .andReturn(new BundleInstallerResult(createDummyBundle(1l, "", headers()), true)).anyTimes(); expect(bundleManager.isBundleInstalled("b1")).andReturn(createDummyBundle(1l, "", headers())); @@ -135,8 +140,8 @@ public class FeaturesServiceImplTest extends TestBase { expect(bundleManager.isBundleInstalled("b3")).andReturn(createDummyBundle(3l, "", headers())); expect(bundleManager.isBundleInstalled("b4")).andReturn(createDummyBundle(4l, "", headers())); bundleManager.refreshBundles(EasyMock.anyObject(Set.class), EasyMock.anyObject(Set.class), EasyMock.anyObject(EnumSet.class)); - - EasyMock.expectLastCall(); + replay(bundleManager); + final FeaturesServiceImpl impl = new FeaturesServiceImpl(bundleManager, null) { // override methods which refers to bundle context to avoid mocking everything @Override @@ -157,7 +162,7 @@ public class FeaturesServiceImplTest extends TestBase { } }; - replay(bundleManager); + impl.addRepository(getClass().getResource("repo2.xml").toURI()); impl.installFeature("all");
