Repository: karaf
Updated Branches:
  refs/heads/karaf-3.0.x 3107ca0e9 -> d846cdf9a


[KARAF-2104] Add exceptions when some features are not found


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

Branch: refs/heads/karaf-3.0.x
Commit: d846cdf9a7aab5845367607c40ddfd364eafb4e0
Parents: 3107ca0
Author: Jean-Baptiste Onofré <[email protected]>
Authored: Fri Dec 19 16:00:01 2014 +0100
Committer: Jean-Baptiste Onofré <[email protected]>
Committed: Fri Dec 19 16:00:01 2014 +0100

----------------------------------------------------------------------
 .../features/internal/FeaturesServiceImpl.java  | 35 ++++++++++++++++++--
 .../karaf/features/FeaturesServiceTest.java     | 14 ++++++--
 2 files changed, 44 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/d846cdf9/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 3098a8e..167c487 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
@@ -369,8 +369,23 @@ public class FeaturesServiceImpl implements 
FeaturesService {
      */
     public void installFeature(String name, String version, EnumSet<Option> 
options) throws Exception {
         Feature[] features = getFeatures(name, version);
+        ArrayList<Exception> exceptions = new ArrayList<Exception>();
+        if (features.length < 1) {
+            throw new IllegalStateException("No feature matching " + name + 
"/" + version);
+        }
         for (Feature feature : features) {
-            installFeature(feature, options);
+            try {
+                installFeature(feature, options);
+            } catch (Exception e) {
+                exceptions.add(e);
+            }
+        }
+        if (!exceptions.isEmpty()) {
+            StringBuilder builder = new StringBuilder();
+            for (Exception exception : exceptions) {
+                builder.append("\t\n").append(exception.getMessage());
+            }
+            throw new IllegalStateException("Can't install feature " + name + 
"/" + version + ": " + builder.toString());
         }
     }
 
@@ -393,7 +408,6 @@ public class FeaturesServiceImpl implements FeaturesService 
{
      * @throws Exception in case of install failure.
      */
     public void installFeatures(Set<Feature> features, EnumSet<Option> 
options) throws Exception {
-
         final InstallationState state = new InstallationState();
         final InstallationState failure = new InstallationState();
         boolean verbose = options.contains(FeaturesService.Option.Verbose);
@@ -653,8 +667,23 @@ public class FeaturesServiceImpl implements 
FeaturesService {
                 toRemove.add(f);
             }
         }
+        if (toRemove.isEmpty()) {
+            throw new IllegalStateException("No installed feature matching " + 
name);
+        }
+        ArrayList<Exception> exceptions = new ArrayList<Exception>();
         for (Feature f : toRemove) {
-            uninstallFeature(f.getName(), f.getVersion(), options);
+            try {
+                uninstallFeature(f.getName(), f.getVersion(), options);
+            } catch (Exception e) {
+                exceptions.add(e);
+            }
+        }
+        if (!exceptions.isEmpty()) {
+            StringBuilder builder = new StringBuilder();
+            for (Exception exception : exceptions) {
+                builder.append("\t\n").append(exception);
+            }
+            throw new IllegalStateException("Can't uninstall feature " + name 
+ ": " + builder.toString());
         }
     }
 

http://git-wip-us.apache.org/repos/asf/karaf/blob/d846cdf9/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 be563e3..6b01021 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
@@ -152,14 +152,24 @@ public class FeaturesServiceTest extends TestBase {
         FeaturesServiceImpl svc = new FeaturesServiceImpl(bundleManager);
         svc.addRepository(uri);
 
-        svc.uninstallFeature("f1");
+        try {
+            svc.uninstallFeature("f1");
+            fail("Exception expected as feature f1 is not installed");
+        } catch (Exception e) {
+            // normal
+        }
 
         svc.installFeature("f1", "0.1", 
EnumSet.of(FeaturesService.Option.NoAutoRefreshBundles));
         svc.installFeature("f1", "0.2", 
EnumSet.of(FeaturesService.Option.NoAutoRefreshBundles));
 
         svc.uninstallFeature("f1");
         svc.uninstallFeature("f1", "0.1");
-        svc.uninstallFeature("f1");
+        try {
+            svc.uninstallFeature("f1");
+            fail("Exception expected as feature f1 has already been 
uninstalled");
+        } catch (Exception e) {
+            // normal
+        }
 
         verify(bundleManager);
     }    

Reply via email to