Repository: karaf
Updated Branches:
  refs/heads/karaf-3.0.x 5340c128e -> 44f638419


[KARAF-3642]bundles mistaken got unstalled even though it has a depending 
feature with it.


Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/2a7bd9a5
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/2a7bd9a5
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/2a7bd9a5

Branch: refs/heads/karaf-3.0.x
Commit: 2a7bd9a5ac244fd63cad3e2e4bbb9ea5a2219c36
Parents: d5b0f4a
Author: Freeman Fang <[email protected]>
Authored: Fri Apr 3 11:39:46 2015 +0800
Committer: Freeman Fang <[email protected]>
Committed: Fri Apr 3 11:39:46 2015 +0800

----------------------------------------------------------------------
 .../karaf/features/internal/BundleManager.java  | 42 ++++++++++++++++++++
 .../features/internal/FeaturesServiceImpl.java  |  8 ++++
 .../karaf/features/FeaturesServiceTest.java     | 13 +++++-
 .../internal/FeaturesServiceImplTest.java       |  5 +++
 4 files changed, 67 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/2a7bd9a5/features/core/src/main/java/org/apache/karaf/features/internal/BundleManager.java
----------------------------------------------------------------------
diff --git 
a/features/core/src/main/java/org/apache/karaf/features/internal/BundleManager.java
 
b/features/core/src/main/java/org/apache/karaf/features/internal/BundleManager.java
index 1eb0d06..086b3e2 100644
--- 
a/features/core/src/main/java/org/apache/karaf/features/internal/BundleManager.java
+++ 
b/features/core/src/main/java/org/apache/karaf/features/internal/BundleManager.java
@@ -157,6 +157,48 @@ public class BundleManager {
             is.close();
         }
     }
+    
+    public Bundle isBundleInstalled(String bundleLocation) throws IOException, 
BundleException {
+        InputStream is = getInputStreamForBundle(bundleLocation);
+        try {
+            is.mark(256 * 1024);
+            @SuppressWarnings("resource")
+            JarInputStream jar = new JarInputStream(is);
+            Manifest m = jar.getManifest();
+            if (m == null) {
+                ZipEntry entry;
+                while ((entry = jar.getNextEntry()) != null) {
+                    if (MANIFEST_NAME.equals(entry.getName())) {
+                        m = new Manifest(jar);
+                        break;
+                    }
+                }
+                if (m == null) {
+                    throw new BundleException("Manifest not present in the zip 
" + bundleLocation);
+                }
+            }
+            String sn = 
m.getMainAttributes().getValue(Constants.BUNDLE_SYMBOLICNAME);
+            if (sn == null) {
+                throw new BundleException("Jar is not a bundle, no 
Bundle-SymbolicName " + bundleLocation);
+            }
+            // remove attributes from the symbolic name (like
+            // ;blueprint.graceperiod:=false suffix)
+            int attributeIndexSep = sn.indexOf(';');
+            if (attributeIndexSep != -1) {
+                sn = sn.substring(0, attributeIndexSep);
+            }
+            String vStr = 
m.getMainAttributes().getValue(Constants.BUNDLE_VERSION);
+            Version v = vStr == null ? Version.emptyVersion : 
Version.parseVersion(vStr);
+            Bundle existingBundle = findInstalled(sn, v);
+            if (existingBundle != null) {
+                LOGGER.debug("Found installed bundle: " + existingBundle);
+                return existingBundle;
+            }
+            return null;
+        } finally {
+            is.close();
+        }
+    }
 
     private Bundle findInstalled(String symbolicName, Version version) {
         String vStr;

http://git-wip-us.apache.org/repos/asf/karaf/blob/2a7bd9a5/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 17691b4..b79f110 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
@@ -573,6 +573,14 @@ public class FeaturesServiceImpl implements 
FeaturesService {
                 }
 
             }
+            
+            for (BundleInfo bInfo : feature.getBundles()) {
+                Bundle bundle = 
bundleManager.isBundleInstalled(bInfo.getLocation());
+                if (bundle != null && !bundles.contains(bundle.getBundleId())) 
{
+                    bundles.add(bundle.getBundleId());
+                }
+            }
+
             state.features.put(feature, bundles);
         }
     }

http://git-wip-us.apache.org/repos/asf/karaf/blob/2a7bd9a5/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 41dcdf7..e158cc0 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
@@ -88,6 +88,7 @@ public class FeaturesServiceTest extends TestBase {
         reset(bundleManager);
         
         expect(bundleManager.installBundleIfNeeded(eq("bundle-f1"), eq(0), 
eq((String)null))).andReturn(new BundleInstallerResult(installedBundle, true));
+        
expect(bundleManager.isBundleInstalled("bundle-f1")).andReturn(installedBundle);
         
expect(bundleManager.getDataFile(EasyMock.anyObject(String.class))).andReturn(dataFile);
         ignoreRefreshes(bundleManager);
         replay(bundleManager);
@@ -142,6 +143,7 @@ public class FeaturesServiceTest extends TestBase {
         
expect(bundleManager.getDataFile(EasyMock.anyObject(String.class))).andReturn(dataFile).anyTimes();
         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);
         expect(bundleManager.getBundleContext()).andReturn(bundleContext);
         ignoreRefreshes(bundleManager);
         bundleManager.uninstall(Collections.EMPTY_LIST, true);
@@ -213,6 +215,8 @@ public class FeaturesServiceTest extends TestBase {
             .andReturn(new BundleInstallerResult(bundlef101, true));
         expect(bundleManager.installBundleIfNeeded("bundle-f2-0.1", 0, null))
             .andReturn(new BundleInstallerResult(bundlef201, true));
+        
expect(bundleManager.isBundleInstalled("bundle-f2-0.1")).andReturn(bundlef201);
+        
expect(bundleManager.isBundleInstalled("bundle-f1-0.1")).andReturn(bundlef101);
         
expect(bundleManager.getBundleContext()).andReturn(bundleContext).anyTimes();
         
expect(bundleContext.getBundle(12345)).andReturn(bundlef101).anyTimes();
         ignoreRefreshes(bundleManager);
@@ -236,6 +240,7 @@ public class FeaturesServiceTest extends TestBase {
         Bundle installedBundle = createDummyBundle(12345L, bundlename, 
headers());
         
expect(bundleManager.getDataFile(EasyMock.<String>anyObject())).andReturn(dataFile).anyTimes();
         expect(bundleManager.installBundleIfNeeded(bundleUri, 0, 
null)).andReturn(new BundleInstallerResult(installedBundle, true));
+        
expect(bundleManager.isBundleInstalled(bundlename)).andReturn(installedBundle);
         expect(bundleManager.getBundleContext()).andReturn(bundleContext);
         ignoreRefreshes(bundleManager);
         bundleManager.uninstall(Collections.EMPTY_LIST, true);
@@ -296,6 +301,7 @@ public class FeaturesServiceTest extends TestBase {
         
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);
+        
expect(bundleManager.isBundleInstalled("bundle-0.2")).andReturn(bundleVer02);
         ignoreRefreshes(bundleManager);
         bundleManager.uninstall(Collections.EMPTY_LIST, true);
 
@@ -328,6 +334,8 @@ public class FeaturesServiceTest extends TestBase {
         
expect(bundleManager.getDataFile(EasyMock.<String>anyObject())).andReturn(dataFile).anyTimes();
         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);
+        expect(bundleManager.getBundleContext()).andReturn(bundleContext);
         ignoreRefreshes(bundleManager);
         bundleManager.uninstall(Collections.EMPTY_LIST, true);
         
@@ -379,6 +387,7 @@ public class FeaturesServiceTest extends TestBase {
         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());
+        
expect(bundleManager.isBundleInstalled("bundle2")).andReturn(installedBundle2);
         EasyMock.expectLastCall();
         ignoreRefreshes(bundleManager);
 
@@ -404,6 +413,7 @@ public class FeaturesServiceTest extends TestBase {
         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());
+        
expect(bundleManager.isBundleInstalled("file:bundle2")).andReturn(installedBundle2);
         bundleManager.uninstall(setOf(installedBundle1));
         EasyMock.expectLastCall();
         ignoreRefreshes(bundleManager);
@@ -430,7 +440,7 @@ public class FeaturesServiceTest extends TestBase {
         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());
-        
+        
expect(bundleManager.isBundleInstalled("file:bundle2")).andReturn(installedBundle2);
         replay(bundleManager);
         FeaturesServiceImpl svc = new FeaturesServiceImpl(bundleManager);
         svc.addRepository(uri);
@@ -459,6 +469,7 @@ public class FeaturesServiceTest extends TestBase {
         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());
+        
expect(bundleManager.isBundleInstalled("file:bundle2")).andReturn(installedBundle2);
         bundleManager.uninstall(setOf(installedBundle1, installedBundle2));
         EasyMock.expectLastCall();
 

http://git-wip-us.apache.org/repos/asf/karaf/blob/2a7bd9a5/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 120588b..eab17f6 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
@@ -130,7 +130,12 @@ public class FeaturesServiceImplTest extends TestBase {
         BundleManager bundleManager = EasyMock.createMock(BundleManager.class);
         
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()));
+        
expect(bundleManager.isBundleInstalled("b2")).andReturn(createDummyBundle(2l, 
"", headers()));
+        
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();
         final FeaturesServiceImpl impl = new 
FeaturesServiceImpl(bundleManager, null) {
             // override methods which refers to bundle context to avoid 
mocking everything

Reply via email to