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:09 UTC

[sling-org-apache-sling-provisioning-model] 11/16: SLING-4879 add ModelUtility.applyVariables method that allows to inject further variables from a custom variable resolver

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 5eea7de9718b79070b7d554f5bff80f67d66b6a1
Author: Stefan Seifert <ss...@apache.org>
AuthorDate: Tue Jul 14 10:05:28 2015 +0000

    SLING-4879 add ModelUtility.applyVariables method that allows to inject further variables from a custom variable resolver
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/tooling/support/provisioning-model@1690896 13f79535-47bb-0310-9956-ffa450edef68
---
 .../sling/provisioning/model/ModelProcessor.java   |   2 +-
 .../sling/provisioning/model/ModelUtility.java     |  53 +++++++++++
 .../model/ModelUtilityApplyVariablesTest.java      | 102 +++++++++++++++++++++
 3 files changed, 156 insertions(+), 1 deletion(-)

diff --git a/src/main/java/org/apache/sling/provisioning/model/ModelProcessor.java b/src/main/java/org/apache/sling/provisioning/model/ModelProcessor.java
index 59b952f..1f18624 100644
--- a/src/main/java/org/apache/sling/provisioning/model/ModelProcessor.java
+++ b/src/main/java/org/apache/sling/provisioning/model/ModelProcessor.java
@@ -86,7 +86,7 @@ class ModelProcessor {
         return result;
     }
     
-    protected KeyValueMap<String> processVariables(KeyValueMap<String> variables, Feature feature) {
+    protected KeyValueMap<String> processVariables(KeyValueMap<String> variables, Feature newFeature) {
         return variables;
     }
     
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 25d4602..f960d9f 100644
--- a/src/main/java/org/apache/sling/provisioning/model/ModelUtility.java
+++ b/src/main/java/org/apache/sling/provisioning/model/ModelUtility.java
@@ -415,5 +415,58 @@ public abstract class ModelUtility {
         }
         return errors;
     }
+    
+    /**
+     * Applies a set of variables to the given model.
+     * All variables that are referenced anywhere within the model are detected and passed to the given variable resolver.
+     * The variable resolver may look up variables on it's own, or fallback to the variables already defined for the feature.
+     * All resolved variable values are collected and put to the "variables" section of the resulting model.
+     * @param model Original model
+     * @param variableResolver Variable resolver
+     * @return Model with updated "variables" section.
+     * @throws IllegalArgumentException If a variable can't be replaced or configuration properties can't be parsed
+     * @since 1.3
+     */
+    public static Model applyVariables(final Model model, final VariableResolver variableResolver) {
+        
+        // define delegating resolver that collects all variable names and value per feature
+        final Map<String,Map<String,String>> collectedVars = new HashMap<String, Map<String,String>>();
+        VariableResolver variableCollector = new VariableResolver() {
+            @Override
+            public String resolve(Feature feature, String name) {
+                String value = variableResolver.resolve(feature, name);
+                if (value != null) {
+                    Map<String,String> featureVars = collectedVars.get(feature.getName());
+                    if (featureVars == null) {
+                        featureVars = new HashMap<String, String>();
+                        collectedVars.put(feature.getName(), featureVars);
+                    }
+                    featureVars.put(name, value);
+                }
+                return value;
+            }
+        };
+        
+        // use effective model processor to collect variables, but drop the resulting model
+        new EffectiveModelProcessor(new ResolverOptions().variableResolver(variableCollector)).process(model);
+        
+        // define a processor that updates the "variables" sections in the features
+        ModelProcessor variablesUpdater = new ModelProcessor() {
+            @Override
+            protected KeyValueMap<String> processVariables(KeyValueMap<String> variables, Feature newFeature) {
+                KeyValueMap<String> newVariables = new KeyValueMap<String>();
+                Map<String,String> featureVars = collectedVars.get(newFeature.getName());
+                if (featureVars != null) {
+                    for (Map.Entry<String, String> entry : featureVars.entrySet()) {
+                        newVariables.put(entry.getKey(), entry.getValue());
+                    }
+                }
+                return newVariables;
+            }
+        };
+        
+        // return model with replaced "variables" sections
+        return variablesUpdater.process(model);
+    }
 
 }
diff --git a/src/test/java/org/apache/sling/provisioning/model/ModelUtilityApplyVariablesTest.java b/src/test/java/org/apache/sling/provisioning/model/ModelUtilityApplyVariablesTest.java
new file mode 100644
index 0000000..a25c206
--- /dev/null
+++ b/src/test/java/org/apache/sling/provisioning/model/ModelUtilityApplyVariablesTest.java
@@ -0,0 +1,102 @@
+/*
+ * 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.provisioning.model;
+
+import static org.junit.Assert.assertEquals;
+
+import org.apache.sling.provisioning.model.ModelUtility.VariableResolver;
+import org.junit.Before;
+import org.junit.Test;
+
+public class ModelUtilityApplyVariablesTest {
+
+    private Model testModel;
+    private VariableResolver testVariableResolver;
+    
+    @Before
+    public void setUp() {
+        testModel = new Model();
+        
+        Feature feature = testModel.getOrCreateFeature("feature1");
+        feature.getVariables().put("param1", "v1");
+        feature.getVariables().put("extparam2", "v2");
+        
+        RunMode runMode = feature.getOrCreateRunMode(new String[] { "rm1"});
+        ArtifactGroup group = runMode.getOrCreateArtifactGroup(10);
+        
+        group.add(new Artifact("g1", "a1", "${param1}", "c1", "t1"));
+        group.add(new Artifact("g2", "a2", "${extparam2}", null, null));
+        
+        Configuration conf = runMode.getOrCreateConfiguration("pid1", null);
+        conf.getProperties().put(ModelConstants.CFG_UNPROCESSED, "conf1=\"${extparam1}\"\n"
+                + "conf2=\"${extparam2}\"");
+        
+        runMode.getSettings().put("set1", "${param1}");
+        
+        
+        // dummy variable resolver that simulates external resolving of some variables
+        testVariableResolver = new VariableResolver() {
+            @Override
+            public String resolve(Feature feature, String name) {
+                if ("extparam1".equals(name)) {
+                    return "extvalue1";
+                }
+                if ("extparam2".equals(name)) {
+                    return "extvalue2";
+                }
+                return feature.getVariables().get(name);
+            }
+        };
+    }
+
+    @Test
+    public void testApplyVariables() {
+        Model model = ModelUtility.applyVariables(testModel, testVariableResolver);
+
+        Feature feature = model.getFeature("feature1");
+        assertEquals("v1", feature.getVariables().get("param1"));
+        assertEquals("extvalue1", feature.getVariables().get("extparam1"));
+        assertEquals("extvalue2", feature.getVariables().get("extparam2"));
+        assertEquals(3, feature.getVariables().size());
+        
+        Model effectiveModel = ModelUtility.getEffectiveModel(model);
+        Feature effectiveFeature = effectiveModel.getFeature("feature1");
+
+        RunMode runMode = effectiveFeature.getRunMode("rm1");
+        ArtifactGroup group = runMode.getArtifactGroup(10);
+        
+        group.add(new Artifact("g1", "a1", "${param1}", "c1", "t1"));
+        group.add(new Artifact("g2", "a2", "${extparam2}", null, null));
+        
+        Configuration conf = runMode.getConfiguration("pid1", null);
+        assertEquals("extvalue1", conf.getProperties().get("conf1"));
+        assertEquals("extvalue2", conf.getProperties().get("conf2"));
+
+        assertEquals("v1", runMode.getSettings().get("set1"));
+    }
+    
+    @Test(expected=IllegalArgumentException.class)
+    public void testApplyVariablesInvalidVariable() {
+        ModelUtility.applyVariables(testModel, new VariableResolver() {
+            @Override
+            public String resolve(Feature feature, String name) {
+                return feature.getVariables().get(name);
+            }
+        });
+    }
+    
+}

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