You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:58:05 UTC

[sling-org-apache-sling-provisioning-model] 07/16: SLING-4880 Allow to get artifact versions from POM

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.provisioning.model-1.3.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-provisioning-model.git

commit bbf78730bd0de62c924fdf4896097b569368b35b
Author: Stefan Seifert <ss...@apache.org>
AuthorDate: Mon Jul 13 22:04:21 2015 +0000

    SLING-4880 Allow to get artifact versions from POM
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/tooling/support/provisioning-model@1690829 13f79535-47bb-0310-9956-ffa450edef68
---
 .../sling/provisioning/model/ModelUtility.java     | 114 +++++++++++++++++++--
 .../sling/provisioning/model/package-info.java     |   2 +-
 .../provisioning/model/CustomResolverTest.java     |  28 ++++-
 .../sling/provisioning/model/ModelUtilityTest.java |   8 +-
 .../apache/sling/provisioning/model/io/IOTest.java |   4 +-
 src/test/resources/example.txt                     |   4 +-
 6 files changed, 138 insertions(+), 22 deletions(-)

diff --git a/src/main/java/org/apache/sling/provisioning/model/ModelUtility.java b/src/main/java/org/apache/sling/provisioning/model/ModelUtility.java
index b68d53e..598e70e 100644
--- a/src/main/java/org/apache/sling/provisioning/model/ModelUtility.java
+++ b/src/main/java/org/apache/sling/provisioning/model/ModelUtility.java
@@ -267,13 +267,85 @@ public abstract class ModelUtility {
     }
 
     /**
+     * Optional artifact dependency version resolver
+     */
+    public interface ArtifactVersionResolver {
+
+        /**
+         * Setting a version for an artifact dependency in a Sling Provisioning file is optional.
+         * By default an artifact without a defined version gets "LATEST" as version.
+         * By defining an DependencyVersionResolver it is possible to plugin in an external dependency resolver
+         * which decides which version to use if no version is given in the provisioning file.
+         * If an exact version is given in the provisioning file this is always used.
+         * @param artifact Artifact without version (version is set to LATEST)
+         * @return New version, or null if the version should not be changed
+         */
+        String resolve(final Artifact artifact);
+    }
+    
+    /**
+     * Parameter builder class for {@link ModelUtility#getEffectiveModel(Model, ResolverOptions)} method.
+     */
+    public static final class ResolverOptions {
+        
+        private VariableResolver variableResolver;
+        private ArtifactVersionResolver artifactVersionResolver;
+        
+        public VariableResolver getVariableResolver() {
+            return variableResolver;
+        }
+        
+        public ResolverOptions variableResolver(VariableResolver variableResolver) {
+            this.variableResolver = variableResolver;
+            return this;
+        }
+        
+        public ArtifactVersionResolver getArtifactVersionResolver() {
+            return artifactVersionResolver;
+        }
+        
+        public ResolverOptions artifactVersionResolver(ArtifactVersionResolver dependencyVersionResolver) {
+            this.artifactVersionResolver = dependencyVersionResolver;
+            return this;
+        }
+        
+    }
+
+    /**
+     * Replace all variables in the model and return a new model with the replaced values.
+     * @param model The base model.
+     * @return The model with replaced variables.
+     * @throws IllegalArgumentException If a variable can't be replaced or configuration properties can't be parsed
+     */
+    public static Model getEffectiveModel(final Model model) {
+        return getEffectiveModel(model, new ResolverOptions());
+    }
+    
+    /**
      * Replace all variables in the model and return a new model with the replaced values.
      * @param model The base model.
      * @param resolver Optional variable resolver.
      * @return The model with replaced variables.
      * @throws IllegalArgumentException If a variable can't be replaced or configuration properties can't be parsed
+     * @deprecated Use {@link #getEffectiveModel(Model, ResolverOptions)} instead
      */
+    @Deprecated
     public static Model getEffectiveModel(final Model model, final VariableResolver resolver) {
+        return getEffectiveModel(model, new ResolverOptions().variableResolver(resolver));
+    }
+    
+    /**
+     * Replace all variables in the model and return a new model with the replaced values.
+     * @param model The base model.
+     * @param options Resolver options.
+     * @return The model with replaced variables.
+     * @throws IllegalArgumentException If a variable can't be replaced or configuration properties can't be parsed
+     */
+    public static Model getEffectiveModel(final Model model, final ResolverOptions options) {
+        if (options == null) {
+            throw new IllegalArgumentException("Resolver options is null");
+        }
+        
         final Model result = new Model();
         result.setLocation(model.getLocation());
 
@@ -296,11 +368,14 @@ public abstract class ModelUtility {
                     newGroup.setLocation(group.getLocation());
 
                     for(final Artifact artifact : group) {
-                        final Artifact newArtifact = new Artifact(replace(newFeature, artifact.getGroupId(), resolver),
-                                replace(newFeature, artifact.getArtifactId(), resolver),
-                                replace(newFeature, artifact.getVersion(), resolver),
-                                replace(newFeature, artifact.getClassifier(), resolver),
-                                replace(newFeature, artifact.getType(), resolver));
+                        final String groupId = replace(newFeature, artifact.getGroupId(), options.getVariableResolver());
+                        final String artifactId = replace(newFeature, artifact.getArtifactId(), options.getVariableResolver());
+                        final String version = replace(newFeature, artifact.getVersion(), options.getVariableResolver());
+                        final String classifier = replace(newFeature, artifact.getClassifier(), options.getVariableResolver());
+                        final String type = replace(newFeature, artifact.getType(), options.getVariableResolver());
+                        final String resolvedVersion = resolveArtifactVersion(groupId, artifactId, version, classifier, type,
+                                options.getArtifactVersionResolver());
+                        final Artifact newArtifact = new Artifact(groupId, artifactId, resolvedVersion, classifier, type);
                         newArtifact.setComment(artifact.getComment());
                         newArtifact.setLocation(artifact.getLocation());
 
@@ -313,7 +388,7 @@ public abstract class ModelUtility {
                 for(final Configuration config : runMode.getConfigurations()) {
                     final Configuration newConfig = newRunMode.getOrCreateConfiguration(config.getPid(), config.getFactoryPid());
 
-                    getProcessedConfiguration(newFeature, newConfig, config, resolver);
+                    getProcessedConfiguration(newFeature, newConfig, config, options.getVariableResolver());
                 }
 
                 newRunMode.getSettings().setComment(runMode.getSettings().getComment());
@@ -327,8 +402,8 @@ public abstract class ModelUtility {
                                     if ( "sling.home".equals(name) ) {
                                         return "${sling.home}";
                                     }
-                                    if ( resolver != null ) {
-                                        return resolver.resolve(newFeature, name);
+                                    if ( options.getVariableResolver() != null ) {
+                                        return options.getVariableResolver().resolve(newFeature, name);
                                     }
                                     return newFeature.getVariables().get(name);
                                 }
@@ -382,7 +457,28 @@ public abstract class ModelUtility {
         }
         return msg;
     }
-
+    
+    /**
+     * Tries to resolves artifact version via {@link ArtifactVersionResolver} if no version was defined in provisioning file.
+     * @param groupId Group ID
+     * @param artifactId Artifact ID
+     * @param version Version
+     * @param classifier Classifier
+     * @param type Type
+     * @param artifactVersionResolver Artifact Version Resolver (may be null)
+     * @return Version to use for this artifact
+     */
+    static String resolveArtifactVersion(final String groupId, final String artifactId, final String version,
+            final String classifier, final String type, ArtifactVersionResolver artifactVersionResolver) {
+        if (artifactVersionResolver != null && "LATEST".equals(version)) {
+            String newVersion = artifactVersionResolver.resolve(new Artifact(groupId, artifactId, version, classifier, type));
+            if (newVersion != null) {
+                return newVersion;
+            }
+        }
+        return version;       
+    }
+    
     /**
      * Validates the model.
      * @param model The model to validate
diff --git a/src/main/java/org/apache/sling/provisioning/model/package-info.java b/src/main/java/org/apache/sling/provisioning/model/package-info.java
index 552f509..46ea4e8 100644
--- a/src/main/java/org/apache/sling/provisioning/model/package-info.java
+++ b/src/main/java/org/apache/sling/provisioning/model/package-info.java
@@ -17,7 +17,7 @@
  * under the License.
  */
 
-@Version("1.2.0")
+@Version("1.3.0")
 package org.apache.sling.provisioning.model;
 
 import aQute.bnd.annotation.Version;
diff --git a/src/test/java/org/apache/sling/provisioning/model/CustomResolverTest.java b/src/test/java/org/apache/sling/provisioning/model/CustomResolverTest.java
index 4f7598d..dd7bece 100644
--- a/src/test/java/org/apache/sling/provisioning/model/CustomResolverTest.java
+++ b/src/test/java/org/apache/sling/provisioning/model/CustomResolverTest.java
@@ -16,7 +16,9 @@
  */
 package org.apache.sling.provisioning.model;
 
+import org.apache.sling.provisioning.model.ModelUtility.ArtifactVersionResolver;
 import org.apache.sling.provisioning.model.ModelUtility.VariableResolver;
+import org.apache.sling.provisioning.model.ModelUtility.ResolverOptions;
 import org.junit.Test;
 
 /** Read and merge our test models, write and read them again
@@ -24,18 +26,36 @@ import org.junit.Test;
  */
 public class CustomResolverTest {
 
-    public static class CustomResolver implements VariableResolver {
+    public static class CustomVariableResolver implements VariableResolver {
         @Override
         public String resolve(Feature feature, String name) {
             return "#" + feature.getName() + "#" + name + "#";
         }
     }
     
-    @Test public void testCustomResolverNoFilter() throws Exception {
+    @Test
+    public void testCustomVariableResolverNoFilter() throws Exception {
         final Model m = U.readCompleteTestModel();
-        final VariableResolver r = new CustomResolver();
-        final Model effective = ModelUtility.getEffectiveModel(m, r);
+        final VariableResolver r = new CustomVariableResolver();
+        final Model effective = ModelUtility.getEffectiveModel(m, new ResolverOptions().variableResolver(r));
         final ArtifactGroup g = U.getGroup(effective, "example", "jackrabbit", 15);
         U.assertArtifact(g, "mvn:org.apache.sling/org.apache.sling.jcr.jackrabbit.server/#example#jackrabbit.version#/jar");
     }
+
+    public static class CustomArtifactVersionResolver implements ArtifactVersionResolver {
+        @Override
+        public String resolve(Artifact artifact) {
+            return "9.9.9";
+        }
+    }
+    
+    @Test
+    public void testCustomArtifactVersionResolver() throws Exception {
+        final Model m = U.readCompleteTestModel();
+        final ArtifactVersionResolver r = new CustomArtifactVersionResolver();
+        final Model effective = ModelUtility.getEffectiveModel(m, new ResolverOptions().artifactVersionResolver(r));
+        final ArtifactGroup g = U.getGroup(effective, "example", null, 0);
+        U.assertArtifact(g, "mvn:org.example/jar-without-version/9.9.9");
+    }
+
 }
diff --git a/src/test/java/org/apache/sling/provisioning/model/ModelUtilityTest.java b/src/test/java/org/apache/sling/provisioning/model/ModelUtilityTest.java
index d5d18ef..7440d0a 100644
--- a/src/test/java/org/apache/sling/provisioning/model/ModelUtilityTest.java
+++ b/src/test/java/org/apache/sling/provisioning/model/ModelUtilityTest.java
@@ -113,8 +113,8 @@ public class ModelUtilityTest {
         final Model baseRaw = U.readCompleteTestModel(new String[] {"merge/config-base.txt"});
         final Model mergeRaw = U.readCompleteTestModel(new String[] {"merge/config-merge.txt"});
 
-        final Model baseEffective = ModelUtility.getEffectiveModel(baseRaw, null);
-        final Model mergeEffective = ModelUtility.getEffectiveModel(mergeRaw, null);
+        final Model baseEffective = ModelUtility.getEffectiveModel(baseRaw);
+        final Model mergeEffective = ModelUtility.getEffectiveModel(mergeRaw);
 
         ModelUtility.merge(baseEffective, mergeEffective);
 
@@ -147,7 +147,7 @@ public class ModelUtilityTest {
     @Test public void mergeBaseRawTest() throws Exception {
         final Model baseRaw = U.readCompleteTestModel(new String[] {"merge/config-base.txt"});
         final Model mergeRaw = U.readCompleteTestModel(new String[] {"merge/config-merge.txt"});
-        final Model mergeEffective = ModelUtility.getEffectiveModel(mergeRaw, null);
+        final Model mergeEffective = ModelUtility.getEffectiveModel(mergeRaw);
 
         ModelUtility.merge(baseRaw, mergeEffective);
 
@@ -180,7 +180,7 @@ public class ModelUtilityTest {
     @Test public void mergeBaseEffectiveTest() throws Exception {
         final Model baseRaw = U.readCompleteTestModel(new String[] {"merge/config-base.txt"});
         final Model mergeRaw = U.readCompleteTestModel(new String[] {"merge/config-merge.txt"});
-        final Model baseEffective = ModelUtility.getEffectiveModel(baseRaw, null);
+        final Model baseEffective = ModelUtility.getEffectiveModel(baseRaw);
 
         ModelUtility.merge(baseEffective, mergeRaw);
 
diff --git a/src/test/java/org/apache/sling/provisioning/model/io/IOTest.java b/src/test/java/org/apache/sling/provisioning/model/io/IOTest.java
index e271a00..5fa6a1e 100644
--- a/src/test/java/org/apache/sling/provisioning/model/io/IOTest.java
+++ b/src/test/java/org/apache/sling/provisioning/model/io/IOTest.java
@@ -64,7 +64,7 @@ public class IOTest {
         U.verifyTestModel(readModel, false);
 
         // Resolve variables and verify the result
-        final Model effective = ModelUtility.getEffectiveModel(readModel, null);
+        final Model effective = ModelUtility.getEffectiveModel(readModel);
         U.verifyTestModel(effective, true);
 
         // write effective model
@@ -84,7 +84,7 @@ public class IOTest {
     }
 
     @Test public void testMultilineConfiguration() throws Exception {
-        final Model m = ModelUtility.getEffectiveModel(U.readCompleteTestModel(new String[] {"configadmin.txt"}), null);
+        final Model m = ModelUtility.getEffectiveModel(U.readCompleteTestModel(new String[] {"configadmin.txt"}));
 
         final List<Configuration> configs = new ArrayList<Configuration>();
         for(final Configuration c :  m.getFeature("configadmin").getRunMode().getConfigurations()) {
diff --git a/src/test/resources/example.txt b/src/test/resources/example.txt
index 0264693..a409266 100644
--- a/src/test/resources/example.txt
+++ b/src/test/resources/example.txt
@@ -43,7 +43,8 @@
     commons-lang/commons-lang/2.6/jar
     org.apache.commons/commons-math/2.2/jar
     org.example/jar-is-default/1.2
-    
+    org.example/jar-without-version
+
 # Artifacts can have additional information like a SHA1 etc.
 #
     org.apache.commons/commons-math/2.2/jar [sha1=2353750701ABE]
@@ -82,4 +83,3 @@ org.apache.sling.another.config [format=properties]
     org.apache.derby/derby/10.5.3.0_1/jar
     org.apache.sling/org.apache.sling.jcr.jackrabbit.server/${jackrabbit.version}/jar
 
-

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.