Author: jbonofre
Date: Fri Jun 15 14:06:44 2012
New Revision: 1350629

URL: http://svn.apache.org/viewvc?rev=1350629&view=rev
Log:
[KARAF-1546] Add the --install-all/--uninstall-all option to feature:repo-add 
and feature:repo-remove commands

Modified:
    
karaf/trunk/features/command/src/main/java/org/apache/karaf/features/command/RepoAddCommand.java
    
karaf/trunk/features/command/src/main/java/org/apache/karaf/features/command/RepoRemoveCommand.java
    
karaf/trunk/features/core/src/main/java/org/apache/karaf/features/FeaturesService.java
    
karaf/trunk/features/core/src/main/java/org/apache/karaf/features/internal/FeaturesServiceImpl.java
    
karaf/trunk/features/core/src/test/java/org/apache/karaf/features/FeaturesServiceTest.java
    
karaf/trunk/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/InstallKarsMojo.java

Modified: 
karaf/trunk/features/command/src/main/java/org/apache/karaf/features/command/RepoAddCommand.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/features/command/src/main/java/org/apache/karaf/features/command/RepoAddCommand.java?rev=1350629&r1=1350628&r2=1350629&view=diff
==============================================================================
--- 
karaf/trunk/features/command/src/main/java/org/apache/karaf/features/command/RepoAddCommand.java
 (original)
+++ 
karaf/trunk/features/command/src/main/java/org/apache/karaf/features/command/RepoAddCommand.java
 Fri Jun 15 14:06:44 2012
@@ -21,9 +21,10 @@ import java.net.URI;
 import org.apache.karaf.features.FeaturesService;
 import org.apache.karaf.shell.commands.Argument;
 import org.apache.karaf.shell.commands.Command;
+import org.apache.karaf.shell.commands.Option;
 import org.apache.karaf.shell.console.AbstractAction;
 
-@Command(scope = "feature", name = "repo-add", description = "Add a repository 
for well known features.")
+@Command(scope = "feature", name = "repo-add", description = "Add a features 
repository")
 public class RepoAddCommand extends AbstractAction {
 
     @Argument(index = 0, name = "Feature name or uri", description = "Shortcut 
name of the feature repository or the full URI", required = true, multiValued = 
false)
@@ -31,6 +32,9 @@ public class RepoAddCommand extends Abst
     
     @Argument(index = 1, name = "Feature version", description = "The version 
of the feature if using the feature name. Should be empty if using the uri", 
required = false, multiValued = false)
     private String version;
+
+    @Option(name = "-i", aliases = { "--install" }, description = "Install all 
features contained in the repository", required = false, multiValued = false)
+    private boolean install;
     
     private FeatureFinder featureFinder;
     private FeaturesService featuresService;
@@ -47,10 +51,10 @@ public class RepoAddCommand extends Abst
         String effectiveVersion = (version == null) ? "LATEST" : version;
         URI uri = featureFinder.getUriFor(nameOrUrl, effectiveVersion);
         if (uri == null) {
-            featuresService.addRepository(new URI(nameOrUrl));
+            uri = new URI(nameOrUrl);
         }
         System.out.println("Adding feature url " + uri);
-        featuresService.addRepository(uri);
+        featuresService.addRepository(uri, install);
         return null;
     }
 

Modified: 
karaf/trunk/features/command/src/main/java/org/apache/karaf/features/command/RepoRemoveCommand.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/features/command/src/main/java/org/apache/karaf/features/command/RepoRemoveCommand.java?rev=1350629&r1=1350628&r2=1350629&view=diff
==============================================================================
--- 
karaf/trunk/features/command/src/main/java/org/apache/karaf/features/command/RepoRemoveCommand.java
 (original)
+++ 
karaf/trunk/features/command/src/main/java/org/apache/karaf/features/command/RepoRemoveCommand.java
 Fri Jun 15 14:06:44 2012
@@ -20,6 +20,7 @@ import java.net.URI;
 
 import org.apache.karaf.shell.commands.Argument;
 import org.apache.karaf.shell.commands.Command;
+import org.apache.karaf.shell.commands.Option;
 import org.apache.karaf.features.FeaturesService;
 import org.apache.karaf.features.Repository;
 
@@ -29,6 +30,9 @@ public class RepoRemoveCommand extends F
     @Argument(index = 0, name = "repository", description = "Name or url of 
the repository to remove.", required = true, multiValued = false)
     private String repository;
 
+    @Option(name = "-u", aliases = { "--uninstall-all" }, description = 
"Uninstall all features from the repository", required = false, multiValued = 
false)
+    private boolean uninstall;
+
     protected void doExecute(FeaturesService featuresService) throws Exception 
{
        URI uri = null;
        for (Repository r : featuresService.listRepositories()) {
@@ -42,6 +46,6 @@ public class RepoRemoveCommand extends F
            uri = new URI(repository);
        }
 
-       featuresService.removeRepository(uri);
+       featuresService.removeRepository(uri, uninstall);
     }
 }

Modified: 
karaf/trunk/features/core/src/main/java/org/apache/karaf/features/FeaturesService.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/features/core/src/main/java/org/apache/karaf/features/FeaturesService.java?rev=1350629&r1=1350628&r2=1350629&view=diff
==============================================================================
--- 
karaf/trunk/features/core/src/main/java/org/apache/karaf/features/FeaturesService.java
 (original)
+++ 
karaf/trunk/features/core/src/main/java/org/apache/karaf/features/FeaturesService.java
 Fri Jun 15 14:06:44 2012
@@ -41,11 +41,15 @@ public interface FeaturesService {
      */
     void validateRepository(URI uri) throws Exception;
 
-    void addRepository(URI url) throws Exception;
+    void addRepository(URI uri) throws Exception;
 
-    void removeRepository(URI url);
+    void addRepository(URI uri, boolean install) throws Exception;
+
+    void removeRepository(URI uri) throws Exception;
+
+    void removeRepository(URI uri, boolean uninstall) throws Exception;
     
-    void restoreRepository(URI url) throws Exception;
+    void restoreRepository(URI uri) throws Exception;
 
     Repository[] listRepositories();
 

Modified: 
karaf/trunk/features/core/src/main/java/org/apache/karaf/features/internal/FeaturesServiceImpl.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/features/core/src/main/java/org/apache/karaf/features/internal/FeaturesServiceImpl.java?rev=1350629&r1=1350628&r2=1350629&view=diff
==============================================================================
--- 
karaf/trunk/features/core/src/main/java/org/apache/karaf/features/internal/FeaturesServiceImpl.java
 (original)
+++ 
karaf/trunk/features/core/src/main/java/org/apache/karaf/features/internal/FeaturesServiceImpl.java
 Fri Jun 15 14:06:44 2012
@@ -94,7 +94,6 @@ import static java.lang.String.format;
  * Adding a repository url will load the features contained in this repository 
and
  * create dummy sub shells.  When invoked, these commands will prompt the user 
for
  * installing the needed bundles.
- *
  */
 public class FeaturesServiceImpl implements FeaturesService, FrameworkListener 
{
 
@@ -170,7 +169,6 @@ public class FeaturesServiceImpl impleme
         listeners.remove(listener);
     }
 
-
     /**
      * Returns a RegionsPersistence service.
      * @param timeout
@@ -214,30 +212,66 @@ public class FeaturesServiceImpl impleme
     }
 
     /**
-     * Validate repository.
+     * Validate a features repository XML.
+     *
+     * @param uri the features repository URI.
      */
     public void validateRepository(URI uri) throws Exception {
         FeatureValidationUtil.validate(uri);
     }
 
+    /**
+     * Add a features repository.
+     *
+     * @param uri the features repository URI.
+     * @throws Exception in case of adding failure.
+     */
     public void addRepository(URI uri) throws Exception {
+        this.addRepository(uri, false);
+    }
+
+    /**
+     * Add a features repository.
+     *
+     * @param uri the features repository URI.
+     * @param install if true, install all features contained in the features 
repository.
+     * @throws Exception in case of adding failure.
+     */
+    public void addRepository(URI uri, boolean install) throws Exception {
         if (!repositories.containsKey(uri)) {
-            internalAddRepository(uri);
+            RepositoryImpl repositoryImpl = this.internalAddRepository(uri);
             saveState();
+            if (install) {
+                for (Feature f : repositoryImpl.getFeatures()) {
+                    installFeature(f.getName(), f.getVersion());
+                }
+            }
         } else {
-            refreshRepository(uri);
+            refreshRepository(uri, install);
         }
     }
     
     /**
-     * Refreshes the url.
-     * @param url
-     * @throws Exception
+     * Refresh a features repository.
+     *
+     * @param uri the features repository URI.
+     * @throws Exception in case of refresh failure.
      */
     protected void refreshRepository(URI uri) throws Exception {
+        this.refreshRepository(uri, false);
+    }
+
+    /**
+     * Refresh a features repository.
+     *
+     * @param uri the features repository URI.
+     * @param install if true, install all features in the features repository.
+     * @throws Exception in case of refresh failure.
+     */
+    protected void refreshRepository(URI uri, boolean install) throws 
Exception {
         try {
-            removeRepository(uri);
-            addRepository(uri);
+            removeRepository(uri, install);
+            addRepository(uri, install);
         } catch (Exception e) {
             //get chance to restore previous, fix for KARAF-4
             restoreRepository(uri);
@@ -245,6 +279,13 @@ public class FeaturesServiceImpl impleme
         }
     }
 
+    /**
+     * Add a features repository into the internal container.
+     *
+     * @param uri the features repository URI.
+     * @return the internal <code>RepositoryImpl</code> representation.
+     * @throws Exception in case of adding failure.
+     */
     protected RepositoryImpl internalAddRepository(URI uri) throws Exception {
         validateRepository(uri);
         RepositoryImpl repo = new RepositoryImpl(uri);
@@ -256,55 +297,99 @@ public class FeaturesServiceImpl impleme
         
     }
 
-    public void removeRepository(URI uri) {
+    /**
+     * Remove a features repository.
+     *
+     * @param uri the features repository URI.
+     * @throws Exception in case of remove failure.
+     */
+    public void removeRepository(URI uri) throws Exception {
+        this.removeRepository(uri, false);
+    }
+
+    /**
+     * Remove a features repository.
+     *
+     * @param uri the features repository URI.
+     * @param uninstall if true, uninstall all features from the features 
repository.
+     * @throws Exception in case of remove failure.
+     */
+    public void removeRepository(URI uri, boolean uninstall) throws Exception {
         if (repositories.containsKey(uri)) {
+            if (uninstall) {
+                RepositoryImpl repositoryImpl = repositories.get(uri);
+                for (Feature feature : repositoryImpl.getFeatures()) {
+                    this.uninstallFeature(feature.getName(), 
feature.getVersion());
+                }
+            }
             internalRemoveRepository(uri);
             saveState();
         }
     }
 
-    public void internalRemoveRepository(URI uri) {
+    /**
+     * Remove a features repository from the internal container.
+     *
+     * @param uri the features repository URI.
+     */
+    protected void internalRemoveRepository(URI uri) {
         Repository repo = repositories.remove(uri);
         this.repo.set(repo);
         callListeners(new RepositoryEvent(repo, 
RepositoryEvent.EventType.RepositoryRemoved, false));
         features = null;
     }
-    
+
+    /**
+     * Restore a features repository.
+     *
+     * @param uri the features repository URI.
+     * @throws Exception in case of restore failure.
+     */
     public void restoreRepository(URI uri) throws Exception {
        repositories.put(uri, (RepositoryImpl)repo.get());
        callListeners(new RepositoryEvent(repo.get(), 
RepositoryEvent.EventType.RepositoryAdded, false));
         features = null;
     }
 
+    /**
+     * Get the list of features repository.
+     *
+     * @return the list of features repository.
+     */
     public Repository[] listRepositories() {
         Collection<RepositoryImpl> repos = repositories.values();
         return repos.toArray(new Repository[repos.size()]);
     }
 
-    public void installAllFeatures(URI uri) throws Exception {
-        RepositoryImpl repo = internalAddRepository(uri);
-        for (Feature f : repo.getFeatures()) {
-            installFeature(f.getName(), f.getVersion());
-        }
-        internalRemoveRepository(uri);            
-    }
-
-    public void uninstallAllFeatures(URI uri) throws Exception {
-        RepositoryImpl repo = internalAddRepository(uri);
-        for (Feature f : repo.getFeatures()) {
-            uninstallFeature(f.getName(), f.getVersion());
-        }
-        internalRemoveRepository(uri);            
-    }
-
+    /**
+     * Install a feature identified by a name.
+     *
+     * @param name the name of the feature.
+     * @throws Exception in case of install failure.
+     */
     public void installFeature(String name) throws Exception {
        installFeature(name, 
org.apache.karaf.features.internal.model.Feature.DEFAULT_VERSION);
     }
 
+    /**
+     * Install a feature identified by a name and a version.
+     *
+     * @param name the name of the feature.
+     * @param version the version of the feature.
+     * @throws Exception in case of install failure.
+     */
     public void installFeature(String name, String version) throws Exception {
         installFeature(name, version, EnumSet.noneOf(Option.class));
     }
 
+    /**
+     * Install a feature identified by a name and a version, including a set 
of options.
+     *
+     * @param name the name of the feature.
+     * @param version the version of the feature.
+     * @param options the installation options.
+     * @throws Exception in case of install failure.
+     */
     public void installFeature(String name, String version, EnumSet<Option> 
options) throws Exception {
         Feature f = getFeature(name, version);
         if (f == null) {
@@ -314,10 +399,24 @@ public class FeaturesServiceImpl impleme
         installFeature(f, options);
     }
 
-    public void installFeature(Feature f, EnumSet<Option> options) throws 
Exception {
-        installFeatures(Collections.singleton(f), options);
+    /**
+     * Install a feature including a set of options.
+     *
+     * @param feature the <code>Feature</code> to install.
+     * @param options the installation options set.
+     * @throws Exception in case of install failure.
+     */
+    public void installFeature(Feature feature, EnumSet<Option> options) 
throws Exception {
+        installFeatures(Collections.singleton(feature), options);
     }
 
+    /**
+     * Install a set of features, including a set of options.
+     *
+     * @param features a set of <code>Feature</code>.
+     * @param options the installation options set.
+     * @throws Exception in case of install failure.
+     */
     public void installFeatures(Set<Feature> features, EnumSet<Option> 
options) throws Exception {
         InstallationState state = new InstallationState();
         InstallationState failure = new InstallationState();
@@ -399,24 +498,30 @@ public class FeaturesServiceImpl impleme
         }
     }
 
-       private void startBundle(InstallationState state, Bundle b)
-                       throws Exception {
+    /**
+     * Start a bundle.
+     *
+     * @param state the current bundle installation state.
+     * @param bundle the bundle to start.
+     * @throws Exception in case of start failure.
+     */
+       private void startBundle(InstallationState state, Bundle bundle) throws 
Exception {
                // do not start fragment bundles
-               Dictionary d = b.getHeaders();
+               Dictionary d = bundle.getHeaders();
                String fragmentHostHeader = (String) 
d.get(Constants.FRAGMENT_HOST);
                if (fragmentHostHeader == null || 
fragmentHostHeader.trim().length() == 0) {
                    // do not start bundles that are persistently stopped
-                   if (state.installed.contains(b)
-                           || (b.getState() != Bundle.STARTING && b.getState() 
!= Bundle.ACTIVE
-                                   && 
b.adapt(BundleStartLevel.class).isPersistentlyStarted())) {
+                   if (state.installed.contains(bundle)
+                           || (bundle.getState() != Bundle.STARTING && 
bundle.getState() != Bundle.ACTIVE
+                                   && 
bundle.adapt(BundleStartLevel.class).isPersistentlyStarted())) {
                        // do no start bundles when user request it
-                       Long bundleId = b.getBundleId();
+                       Long bundleId = bundle.getBundleId();
                        BundleInfo bundleInfo = state.bundleInfos.get(bundleId);
                        if (bundleInfo == null || bundleInfo.isStart()) {
                            try {
-                               b.start();
+                               bundle.start();
                            } catch (BundleException be) {
-                               String msg = format("Could not start bundle %s 
in feature(s) %s: %s", b.getLocation(), getFeaturesContainingBundleList(b), 
be.getMessage());
+                               String msg = format("Could not start bundle %s 
in feature(s) %s: %s", bundle.getLocation(), 
getFeaturesContainingBundleList(bundle), be.getMessage());
                                throw new Exception(msg, be);
                            }
                        }
@@ -424,8 +529,7 @@ public class FeaturesServiceImpl impleme
                }
        }
 
-       private void cleanUpOnFailure(InstallationState state,
-                       InstallationState failure, boolean noCleanIfFailure) {
+       private void cleanUpOnFailure(InstallationState state, 
InstallationState failure, boolean noCleanIfFailure) {
                // cleanup on error
                if (!noCleanIfFailure) {
                    // Uninstall everything

Modified: 
karaf/trunk/features/core/src/test/java/org/apache/karaf/features/FeaturesServiceTest.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/features/core/src/test/java/org/apache/karaf/features/FeaturesServiceTest.java?rev=1350629&r1=1350628&r2=1350629&view=diff
==============================================================================
--- 
karaf/trunk/features/core/src/test/java/org/apache/karaf/features/FeaturesServiceTest.java
 (original)
+++ 
karaf/trunk/features/core/src/test/java/org/apache/karaf/features/FeaturesServiceTest.java
 Fri Jun 15 14:06:44 2012
@@ -254,100 +254,6 @@ public class FeaturesServiceTest extends
         svc.removeRepository(uri);        
     }
 
-    // Tests installing all features in a repo and uninstalling
-    // all features in a repo
-    public void testInstallUninstallAllFeatures() throws Exception {
-
-        String name = getJarUrl(BlueprintContainer.class);
-
-        File tmp = File.createTempFile("karaf", ".feature");
-        PrintWriter pw = new PrintWriter(new FileWriter(tmp));
-        pw.println("<features name=\"test\" 
xmlns=\"http://karaf.apache.org/xmlns/features/v1.0.0\";>");
-        pw.println("  <feature name=\"f1\" version=\"0.1\">");
-        pw.println("    <bundle>" + name + "</bundle>");
-        pw.println("  </feature>");
-        pw.println("  <feature name=\"f1\" version=\"0.2\">");
-        pw.println("    <bundle>" + name + "</bundle>");
-        pw.println("  </feature>");
-        pw.println("  <feature name=\"f2\" version=\"0.2\">");
-        pw.println("    <bundle>" + name + "</bundle>");
-        pw.println("  </feature>");
-        pw.println("</features>");
-        pw.close();
-
-        URI uri = tmp.toURI();
-
-        BundleContext bundleContext = EasyMock.createMock(BundleContext.class);
-        Bundle installedBundle = EasyMock.createMock(Bundle.class);
-        Bundle framework = EasyMock.createMock(Bundle.class);
-
-        // Installs first feature name = f1, version = 0.1
-        expect(bundleContext.getBundles()).andReturn(new Bundle[0]);
-        expect(bundleContext.installBundle(isA(String.class),
-                                           
isA(InputStream.class))).andReturn(installedBundle);
-        expect(installedBundle.getBundleId()).andReturn(12345L);
-        expect(installedBundle.getBundleId()).andReturn(12345L);
-        expect(installedBundle.getBundleId()).andReturn(12345L);
-        expect(bundleContext.getBundle(12345L)).andReturn(installedBundle);
-        expect(installedBundle.getHeaders()).andReturn(new Hashtable());
-        expect(installedBundle.getSymbolicName()).andReturn("bundle");
-
-        installedBundle.start();
-        
-        // Installs second feature name = f1, version = 0.2
-        expect(bundleContext.getBundles()).andReturn(new Bundle[0]);
-        expect(bundleContext.installBundle(isA(String.class),
-                                           
isA(InputStream.class))).andReturn(installedBundle);
-        expect(installedBundle.getBundleId()).andReturn(123456L);
-        expect(installedBundle.getBundleId()).andReturn(123456L);
-        expect(installedBundle.getBundleId()).andReturn(123456L);
-        expect(bundleContext.getBundle(123456L)).andReturn(installedBundle);
-        expect(installedBundle.getHeaders()).andReturn(new Hashtable());
-        installedBundle.start();
-        
-        // Installs third feature name = f2, version = 0.2
-        expect(bundleContext.getBundles()).andReturn(new Bundle[0]);
-        expect(bundleContext.installBundle(isA(String.class),
-                                           
isA(InputStream.class))).andReturn(installedBundle);
-        expect(installedBundle.getBundleId()).andReturn(1234567L);
-        expect(installedBundle.getBundleId()).andReturn(1234567L);
-        expect(installedBundle.getBundleId()).andReturn(1234567L);
-        expect(bundleContext.getBundle(1234567L)).andReturn(installedBundle);
-        expect(installedBundle.getHeaders()).andReturn(new Hashtable());
-        installedBundle.start();
-        
-        expect(installedBundle.getHeaders()).andReturn(new 
Hashtable()).anyTimes();
-
-        // uninstallAllFeatures 
-        
-        // uninstalls first feature name = f1, version = 0.1
-        expect(bundleContext.getBundle(12345)).andReturn(installedBundle);
-        installedBundle.uninstall();
-
-        // uninstalls third feature name = f2, version = 0.2
-        expect(bundleContext.getBundle(1234567)).andReturn(installedBundle);
-        installedBundle.uninstall();
-
-        // uninstalls second feature name = f1, version = 0.2
-        expect(bundleContext.getBundle(123456)).andReturn(installedBundle);
-        installedBundle.uninstall();
-
-        
expect(bundleContext.getDataFile(EasyMock.<String>anyObject())).andReturn(dataFile).anyTimes();
-
-        expect(bundleContext.getBundle()).andReturn(framework).anyTimes();
-        
expect(framework.adapt(FrameworkWiring.class)).andReturn(null).anyTimes();
-
-        replay(bundleContext, installedBundle, framework);
-
-        FeaturesServiceImpl svc = new FeaturesServiceImpl();
-        svc.setBundleContext(bundleContext);
-        svc.installAllFeatures(uri);
-        
-        // Uninstalls features with versions.
-        svc.uninstallAllFeatures(uri);    
-    }    
-
-
     // Tests install of a Repository that includes a feature
     // with a feature dependency
     // The dependant feature is in the same repository

Modified: 
karaf/trunk/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/InstallKarsMojo.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/InstallKarsMojo.java?rev=1350629&r1=1350628&r2=1350629&view=diff
==============================================================================
--- 
karaf/trunk/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/InstallKarsMojo.java
 (original)
+++ 
karaf/trunk/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/InstallKarsMojo.java
 Fri Jun 15 14:06:44 2012
@@ -431,6 +431,10 @@ public class InstallKarsMojo extends Moj
             }
         }
 
+        @Override
+        public void addRepository(URI uri, boolean install) throws Exception {
+        }
+
         private String retrieveProperty(Properties properties, String key) {
             return properties.containsKey(key) && properties.get(key) != null 
?  properties.get(key) + "," : "";
         }
@@ -454,11 +458,15 @@ public class InstallKarsMojo extends Moj
         }
 
         @Override
-        public void removeRepository(URI url) {
+        public void removeRepository(URI uri) {
+        }
+
+        @Override
+        public void removeRepository(URI uri, boolean install) {
         }
 
         @Override
-        public void restoreRepository(URI url) throws Exception {
+        public void restoreRepository(URI uri) throws Exception {
         }
 
         @Override


Reply via email to