SLING-4728 - refactor artifacts visit

git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1685532 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/sling/crankstart/launcher/ArtifactsVisitor.java b/src/main/java/org/apache/sling/crankstart/launcher/ArtifactsVisitor.java
new file mode 100644
index 0000000..dd3e0e6
--- /dev/null
+++ b/src/main/java/org/apache/sling/crankstart/launcher/ArtifactsVisitor.java
@@ -0,0 +1,67 @@
+/*
+ * 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.
+ */
+package org.apache.sling.crankstart.launcher;
+
+import org.apache.sling.provisioning.model.Artifact;
+import org.apache.sling.provisioning.model.ArtifactGroup;
+import org.apache.sling.provisioning.model.Feature;
+import org.apache.sling.provisioning.model.Model;
+import org.apache.sling.provisioning.model.RunMode;
+
+/** Visit the Artifacts of a Model */
+public abstract class ArtifactsVisitor {
+    private final Model model;
+    
+    public ArtifactsVisitor(Model m) {
+        model = m;
+    }
+    
+    protected abstract void visitArtifact(Feature f, RunMode rm, ArtifactGroup g, Artifact a) throws Exception;
+    
+    public void visit() throws Exception {
+        for(Feature f : model.getFeatures()) {
+            if(!acceptFeature(f)) {
+                continue;
+            }
+            for(RunMode rm : f.getRunModes()) {
+                if(!acceptRunMode(rm)) {
+                    continue;
+                }
+                for(ArtifactGroup g : rm.getArtifactGroups()) {
+                    if(!acceptArtifactGroup(g)) {
+                        continue;
+                    }
+                    for(Artifact a : g) {
+                        visitArtifact(f, rm, g, a);
+                    }
+                }
+            }
+        }
+    }
+    
+    protected boolean acceptFeature(Feature f) {
+        return true;
+    }
+    
+    protected boolean acceptRunMode(RunMode rm) {
+        return true;
+    }
+    
+    protected boolean acceptArtifactGroup(ArtifactGroup g) {
+        return true;
+    }   
+}
\ No newline at end of file
diff --git a/src/main/java/org/apache/sling/crankstart/launcher/BundlesInstaller.java b/src/main/java/org/apache/sling/crankstart/launcher/BundlesInstaller.java
index 2436ca2..89a2d5a 100644
--- a/src/main/java/org/apache/sling/crankstart/launcher/BundlesInstaller.java
+++ b/src/main/java/org/apache/sling/crankstart/launcher/BundlesInstaller.java
@@ -44,36 +44,44 @@
         this.rmFilter = rmFilter;
     }
     
-    public void installBundles(BundleContext ctx, FeatureFilter filter) throws IOException, BundleException {
-        for(Feature f : model.getFeatures()) {
-            if(filter.ignoreFeature(f)) {
-                log.info("Ignoring feature: {}", f.getName());
-                continue;
+    public void installBundles(final BundleContext ctx, final FeatureFilter filter) throws Exception {
+        final String JAR_TYPE = "jar";
+        
+        final ArtifactsVisitor v = new ArtifactsVisitor(model) {
+
+            @Override
+            protected void visitArtifact(Feature f, RunMode rm, ArtifactGroup g, Artifact a) throws Exception {
+                if(JAR_TYPE.equals(a.getType())) {
+                    installBundle(ctx, a, g.getStartLevel());
+                } else {
+                    log.info("Ignoring Artifact, not a bundle: {}", a);
+                }
             }
-            
-            log.info("Processing feature: {}", f.getName());
-            for(RunMode rm : f.getRunModes()) {
-                if(!rmFilter.runModeActive(rm)) {
+
+            @Override
+            protected boolean acceptFeature(Feature f) {
+                final boolean accept = !filter.ignoreFeature(f);
+                if(!accept) {
+                    log.info("Ignoring feature: {}", f.getName());
+                }
+                return accept;
+            }
+
+            @Override
+            protected boolean acceptRunMode(RunMode rm) {
+                final boolean accept = rmFilter.runModeActive(rm);
+                if(!accept) {
                     log.info("RunMode is not active, ignored: {}", Arrays.asList(rm.getNames()));
-                    continue;
                 }
-                for(ArtifactGroup g : rm.getArtifactGroups()) {
-                    final int startLevel = g.getStartLevel();
-                    for(Artifact a : g) {
-                        // TODO for now, naively assume a is a bundle, and mvn: protocol
-                        final String url = "mvn:" + a.getGroupId() + "/" + a.getArtifactId() + "/" + a.getVersion();
-                        installBundle(ctx, url, startLevel);
-                    }
-                }
+                return accept;
             }
-        }
+        };
+        
+        v.visit();
     }
     
-    protected boolean ignoreFeature(Feature f) {
-        return false;
-    }
-    
-    public void installBundle(BundleContext ctx, String bundleUrl, int startLevel) throws IOException, BundleException {
+    public void installBundle(BundleContext ctx, Artifact a, int startLevel) throws IOException, BundleException {
+        final String bundleUrl = "mvn:" + a.getGroupId() + "/" + a.getArtifactId() + "/" + a.getVersion();
         final URL url = new URL(bundleUrl);
         final InputStream bundleStream = url.openStream();
         try {