You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ss...@apache.org on 2015/07/14 12:05:29 UTC

svn commit: r1690896 - in /sling/trunk/tooling/support/provisioning-model/src: main/java/org/apache/sling/provisioning/model/ test/java/org/apache/sling/provisioning/model/

Author: sseifert
Date: Tue Jul 14 10:05:28 2015
New Revision: 1690896

URL: http://svn.apache.org/r1690896
Log:
SLING-4879 add ModelUtility.applyVariables method that allows to inject further variables from a custom variable resolver

Added:
    sling/trunk/tooling/support/provisioning-model/src/test/java/org/apache/sling/provisioning/model/ModelUtilityApplyVariablesTest.java   (with props)
Modified:
    sling/trunk/tooling/support/provisioning-model/src/main/java/org/apache/sling/provisioning/model/ModelProcessor.java
    sling/trunk/tooling/support/provisioning-model/src/main/java/org/apache/sling/provisioning/model/ModelUtility.java

Modified: sling/trunk/tooling/support/provisioning-model/src/main/java/org/apache/sling/provisioning/model/ModelProcessor.java
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/support/provisioning-model/src/main/java/org/apache/sling/provisioning/model/ModelProcessor.java?rev=1690896&r1=1690895&r2=1690896&view=diff
==============================================================================
--- sling/trunk/tooling/support/provisioning-model/src/main/java/org/apache/sling/provisioning/model/ModelProcessor.java (original)
+++ sling/trunk/tooling/support/provisioning-model/src/main/java/org/apache/sling/provisioning/model/ModelProcessor.java Tue Jul 14 10:05:28 2015
@@ -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;
     }
     

Modified: sling/trunk/tooling/support/provisioning-model/src/main/java/org/apache/sling/provisioning/model/ModelUtility.java
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/support/provisioning-model/src/main/java/org/apache/sling/provisioning/model/ModelUtility.java?rev=1690896&r1=1690895&r2=1690896&view=diff
==============================================================================
--- sling/trunk/tooling/support/provisioning-model/src/main/java/org/apache/sling/provisioning/model/ModelUtility.java (original)
+++ sling/trunk/tooling/support/provisioning-model/src/main/java/org/apache/sling/provisioning/model/ModelUtility.java Tue Jul 14 10:05:28 2015
@@ -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);
+    }
 
 }

Added: sling/trunk/tooling/support/provisioning-model/src/test/java/org/apache/sling/provisioning/model/ModelUtilityApplyVariablesTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/support/provisioning-model/src/test/java/org/apache/sling/provisioning/model/ModelUtilityApplyVariablesTest.java?rev=1690896&view=auto
==============================================================================
--- sling/trunk/tooling/support/provisioning-model/src/test/java/org/apache/sling/provisioning/model/ModelUtilityApplyVariablesTest.java (added)
+++ sling/trunk/tooling/support/provisioning-model/src/test/java/org/apache/sling/provisioning/model/ModelUtilityApplyVariablesTest.java Tue Jul 14 10:05:28 2015
@@ -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);
+            }
+        });
+    }
+    
+}

Propchange: sling/trunk/tooling/support/provisioning-model/src/test/java/org/apache/sling/provisioning/model/ModelUtilityApplyVariablesTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/tooling/support/provisioning-model/src/test/java/org/apache/sling/provisioning/model/ModelUtilityApplyVariablesTest.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Tue Jul 14 10:05:28 2015
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author

Propchange: sling/trunk/tooling/support/provisioning-model/src/test/java/org/apache/sling/provisioning/model/ModelUtilityApplyVariablesTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain