You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2015/10/21 10:36:50 UTC

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

Author: cziegeler
Date: Wed Oct 21 08:36:50 2015
New Revision: 1709753

URL: http://svn.apache.org/viewvc?rev=1709753&view=rev
Log:
SLING-5174 : Refactor merging of models into separate utility class and add merge options

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

Added: sling/trunk/tooling/support/provisioning-model/src/main/java/org/apache/sling/provisioning/model/MergeUtility.java
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/support/provisioning-model/src/main/java/org/apache/sling/provisioning/model/MergeUtility.java?rev=1709753&view=auto
==============================================================================
--- sling/trunk/tooling/support/provisioning-model/src/main/java/org/apache/sling/provisioning/model/MergeUtility.java (added)
+++ sling/trunk/tooling/support/provisioning-model/src/main/java/org/apache/sling/provisioning/model/MergeUtility.java Wed Oct 21 08:36:50 2015
@@ -0,0 +1,315 @@
+/*
+ * 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.apache.sling.provisioning.model.ModelResolveUtility.getProcessedConfiguration;
+
+import java.util.Enumeration;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Utility for merging two model.
+ *
+ * @since 1.4
+ */
+public abstract class MergeUtility {
+
+    /**
+     * Options for specifying some parts of the merge operation.
+     */
+    public static class MergeOptions {
+
+        private boolean handleRemoveRunMode = true;
+
+        private boolean latestArtifactWins = true;
+
+        /**
+         * Returns {@code true} if the remove run mode should be respected.
+         * @return {@code true} or {@code false}
+         */
+        public boolean isHandleRemoveRunMode() {
+            return handleRemoveRunMode;
+        }
+
+        /**
+         * Set to {@code true} if the remove run mode should be respected.
+         * @param handleRemoveRunMode
+         */
+        public void setHandleRemoveRunMode(boolean handleRemoveRunMode) {
+            this.handleRemoveRunMode = handleRemoveRunMode;
+        }
+
+        /**
+         * Returns {@code true} if the latest artifact should win on a merge.
+         * @return {@code true} or {@code false} if the artifact with the
+         *         highest version should win
+         */
+        public boolean isLatestArtifactWins() {
+            return latestArtifactWins;
+        }
+
+        /**
+         * Set to {@code true} if the latest artifact should win on a merge.
+         * Set to {@code false} if the artifact with the highest version should win
+         */
+        public void setLatestArtifactWins(boolean latestArtifactWins) {
+            this.latestArtifactWins = latestArtifactWins;
+        }
+    }
+
+    /**
+     * Merge the additional model into the base model.
+     * @param base The base model.
+     * @param additional The additional model.
+     */
+    public static void merge(final Model base, final Model additional) {
+        merge(base, additional, new MergeOptions());
+    }
+
+    /**
+     * Merge the additional model into the base model.
+     * @param base The base model.
+     * @param additional The additional model.
+     * @param options The merge options
+     */
+    public static void merge(final Model base, final Model additional, final MergeOptions options) {
+        // features
+        for(final Feature feature : additional.getFeatures()) {
+            final Feature baseFeature = base.getOrCreateFeature(feature.getName());
+            baseFeature.setType(feature.getType());
+
+            // additional sections
+            baseFeature.getAdditionalSections().addAll(feature.getAdditionalSections());
+
+            // variables
+            baseFeature.getVariables().putAll(feature.getVariables());
+
+            // run modes
+            for(final RunMode runMode : feature.getRunModes()) {
+                // check for special remove run mode
+                final String names[] = runMode.getNames();
+                if ( options.isHandleRemoveRunMode() && names != null ) {
+                    if ( handleRemoveRunMode(baseFeature, runMode) ) {
+                        continue;
+                    }
+                }
+                final RunMode baseRunMode = baseFeature.getOrCreateRunMode(names);
+
+                // artifact groups
+                for(final ArtifactGroup group : runMode.getArtifactGroups()) {
+                    final ArtifactGroup baseGroup = baseRunMode.getOrCreateArtifactGroup(group.getStartLevel());
+
+                    for(final Artifact artifact : group) {
+                        boolean addArtifact = true;
+                        for(final ArtifactGroup searchGroup : baseRunMode.getArtifactGroups()) {
+                            final Artifact found = searchGroup.search(artifact);
+                            if ( found != null ) {
+                                if ( options.isLatestArtifactWins() ) {
+                                    searchGroup.remove(found);
+                                } else {
+                                    final Version baseVersion = new Version(found.getVersion());
+                                    final Version mergeVersion = new Version(artifact.getVersion());
+                                    if ( baseVersion.compareTo(mergeVersion) <= 0 ) {
+                                        searchGroup.remove(found);
+                                    } else {
+                                        addArtifact = false;
+                                    }
+                                }
+                            }
+                        }
+                        if ( addArtifact ) {
+                            baseGroup.add(artifact);
+                        }
+                    }
+                }
+
+                // configurations
+                for(final Configuration config : runMode.getConfigurations()) {
+                    final Configuration found = baseRunMode.getOrCreateConfiguration(config.getPid(), config.getFactoryPid());
+
+                    mergeConfiguration(found, config);
+                }
+
+                // settings
+                for(final Map.Entry<String, String> entry : runMode.getSettings() ) {
+                    baseRunMode.getSettings().put(entry.getKey(), entry.getValue());
+                }
+            }
+
+        }
+    }
+
+    /**
+     * Handle the remove run mode
+     * @param baseFeature The base feature
+     * @param runMode The current run mode
+     * @return {@code true} if the current run mode is a remove run mode
+     */
+    private static boolean handleRemoveRunMode(final Feature baseFeature, final RunMode runMode) {
+        String names[] = runMode.getNames();
+        int removeIndex = -1;
+        int index = 0;
+        for(final String name : names) {
+            if ( name.equals(ModelConstants.RUN_MODE_REMOVE) ) {
+                removeIndex = index;
+                break;
+            }
+            index++;
+        }
+        if ( removeIndex != -1 ) {
+            String[] newNames = null;
+            if ( names.length > 1 ) {
+                newNames = new String[names.length - 1];
+                index = 0;
+                for(final String name : names) {
+                    if ( !name.equals(ModelConstants.RUN_MODE_REMOVE) ) {
+                        newNames[index++] = name;
+                    }
+                }
+            }
+            names = newNames;
+            final RunMode baseRunMode = baseFeature.getRunMode(names);
+            if ( baseRunMode != null ) {
+
+                // artifact groups
+                for(final ArtifactGroup group : runMode.getArtifactGroups()) {
+                    for(final Artifact artifact : group) {
+                        for(final ArtifactGroup searchGroup : baseRunMode.getArtifactGroups()) {
+                            final Artifact found = searchGroup.search(artifact);
+                            if ( found != null ) {
+                                searchGroup.remove(found);
+                            }
+                        }
+                    }
+                }
+
+                // configurations
+                for(final Configuration config : runMode.getConfigurations()) {
+                    final Configuration found = baseRunMode.getConfiguration(config.getPid(), config.getFactoryPid());
+                    if ( found != null ) {
+                        baseRunMode.getConfigurations().remove(found);
+                    }
+                }
+
+                // settings
+                for(final Map.Entry<String, String> entry : runMode.getSettings() ) {
+                    baseRunMode.getSettings().remove(entry.getKey());
+                }
+            }
+            return true;
+        }
+        return false;
+    }
+
+    /**
+     * Merge two configurations
+     * @param baseConfig The base configuration.
+     * @param mergeConfig The merge configuration.
+     */
+    private static void mergeConfiguration(final Configuration baseConfig, final Configuration mergeConfig) {
+        // check for merge mode
+        final boolean isNew = baseConfig.getProperties().isEmpty();
+        if ( isNew ) {
+            copyConfigurationProperties(baseConfig, mergeConfig);
+            final Object mode = mergeConfig.getProperties().get(ModelConstants.CFG_UNPROCESSED_MODE);
+            if ( mode != null ) {
+                baseConfig.getProperties().put(ModelConstants.CFG_UNPROCESSED_MODE, mode);
+            }
+        } else {
+            final boolean baseIsRaw = baseConfig.getProperties().get(ModelConstants.CFG_UNPROCESSED) != null;
+            final boolean mergeIsRaw = mergeConfig.getProperties().get(ModelConstants.CFG_UNPROCESSED) != null;
+            // simplest case, both are raw
+            if ( baseIsRaw && mergeIsRaw ) {
+                final String cfgMode = (String)mergeConfig.getProperties().get(ModelConstants.CFG_UNPROCESSED_MODE);
+                if ( cfgMode == null || ModelConstants.CFG_MODE_OVERWRITE.equals(cfgMode) ) {
+                    copyConfigurationProperties(baseConfig, mergeConfig);
+                } else {
+                    final Configuration newConfig = new Configuration(baseConfig.getPid(), baseConfig.getFactoryPid());
+                    getProcessedConfiguration(null, newConfig, baseConfig, false, null);
+                    clearConfiguration(baseConfig);
+                    copyConfigurationProperties(baseConfig, newConfig);
+
+                    clearConfiguration(newConfig);
+                    getProcessedConfiguration(null, newConfig, mergeConfig, false, null);
+
+                    if ( baseConfig.isSpecial() ) {
+                        final String baseValue = baseConfig.getProperties().get(baseConfig.getPid()).toString();
+                        final String mergeValue = newConfig.getProperties().get(baseConfig.getPid()).toString();
+                        baseConfig.getProperties().put(baseConfig.getPid(), baseValue + "\n" + mergeValue);
+                    } else {
+                        copyConfigurationProperties(baseConfig, newConfig);
+                    }
+                }
+
+            // another simple case, both are not raw
+            } else if ( !baseIsRaw && !mergeIsRaw ) {
+                // merge mode is always overwrite
+                clearConfiguration(baseConfig);
+                copyConfigurationProperties(baseConfig, mergeConfig);
+
+            // base is not raw but merge is
+            } else if ( !baseIsRaw && mergeIsRaw ) {
+                final String cfgMode = (String)mergeConfig.getProperties().get(ModelConstants.CFG_UNPROCESSED_MODE);
+                if ( cfgMode == null || ModelConstants.CFG_MODE_OVERWRITE.equals(cfgMode) ) {
+                    clearConfiguration(baseConfig);
+                    copyConfigurationProperties(baseConfig, mergeConfig);
+                } else {
+                    final Configuration newMergeConfig = new Configuration(mergeConfig.getPid(), mergeConfig.getFactoryPid());
+                    getProcessedConfiguration(null, newMergeConfig, mergeConfig, false, null);
+
+                    if ( baseConfig.isSpecial() ) {
+                        final String baseValue = baseConfig.getProperties().get(baseConfig.getPid()).toString();
+                        final String mergeValue = newMergeConfig.getProperties().get(baseConfig.getPid()).toString();
+                        baseConfig.getProperties().put(baseConfig.getPid(), baseValue + "\n" + mergeValue);
+                    } else {
+                        copyConfigurationProperties(baseConfig, newMergeConfig);
+                    }
+                }
+
+                // base is raw, but merge is not raw
+            } else {
+                // merge mode is always overwrite
+                clearConfiguration(baseConfig);
+                copyConfigurationProperties(baseConfig, mergeConfig);
+            }
+        }
+    }
+
+    private static void clearConfiguration(final Configuration cfg) {
+        final Set<String> keys = new HashSet<String>();
+        final Enumeration<String> e = cfg.getProperties().keys();
+        while ( e.hasMoreElements() ) {
+            keys.add(e.nextElement());
+        }
+
+        for(final String key : keys) {
+            cfg.getProperties().remove(key);
+        }
+    }
+
+    private static void copyConfigurationProperties(final Configuration baseConfig, final Configuration mergeConfig) {
+        final Enumeration<String> e = mergeConfig.getProperties().keys();
+        while ( e.hasMoreElements() ) {
+            final String key = e.nextElement();
+            if ( !key.equals(ModelConstants.CFG_UNPROCESSED_MODE) ) {
+                baseConfig.getProperties().put(key, mergeConfig.getProperties().get(key));
+            }
+        }
+    }
+}

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

Propchange: sling/trunk/tooling/support/provisioning-model/src/main/java/org/apache/sling/provisioning/model/MergeUtility.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

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=1709753&r1=1709752&r2=1709753&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 Wed Oct 21 08:36:50 2015
@@ -16,18 +16,16 @@
  */
 package org.apache.sling.provisioning.model;
 
-import static org.apache.sling.provisioning.model.ModelResolveUtility.getProcessedConfiguration;
 import static org.apache.sling.provisioning.model.ModelResolveUtility.resolveArtifactVersion;
 
 import java.util.Arrays;
-import java.util.Enumeration;
 import java.util.HashMap;
-import java.util.HashSet;
 import java.util.Map;
-import java.util.Set;
+
+import org.apache.sling.provisioning.model.MergeUtility.MergeOptions;
 
 /**
- * Merge two models
+ * Model utility
  */
 public abstract class ModelUtility {
 
@@ -35,9 +33,11 @@ public abstract class ModelUtility {
      * Merge the additional model into the base model.
      * @param base The base model.
      * @param additional The additional model.
+     * @deprecated Use {link {@link MergeUtility#merge(Model, Model)}
      */
+    @Deprecated
     public static void merge(final Model base, final Model additional) {
-        merge(base, additional, true);
+        MergeUtility.merge(base, additional);
     }
 
     /**
@@ -46,205 +46,14 @@ public abstract class ModelUtility {
      * @param additional The additional model.
      * @param handleRemove Handle special remove run mode
      * @since 1.2
+     * @deprecated Use {link {@link MergeUtility#merge(Model, Model, org.apache.sling.provisioning.model.MergeUtility.MergeOptions)}
      */
+    @Deprecated
     public static void merge(final Model base, final Model additional, final boolean handleRemove) {
-        // features
-        for(final Feature feature : additional.getFeatures()) {
-            final Feature baseFeature = base.getOrCreateFeature(feature.getName());
-            baseFeature.setType(feature.getType());
-
-            // additional sections
-            baseFeature.getAdditionalSections().addAll(feature.getAdditionalSections());
-
-            // variables
-            baseFeature.getVariables().putAll(feature.getVariables());
-
-            // run modes
-            for(final RunMode runMode : feature.getRunModes()) {
-                // check for special remove run mode
-                String names[] = runMode.getNames();
-                if ( handleRemove ) {
-                    if ( names != null ) {
-                        int removeIndex = -1;
-                        int index = 0;
-                        for(final String name : names) {
-                            if ( name.equals(ModelConstants.RUN_MODE_REMOVE) ) {
-                                removeIndex = index;
-                                break;
-                            }
-                            index++;
-                        }
-                        if ( removeIndex != -1 ) {
-                            String[] newNames = null;
-                            if ( names.length > 1 ) {
-                                newNames = new String[names.length - 1];
-                                index = 0;
-                                for(final String name : names) {
-                                    if ( !name.equals(ModelConstants.RUN_MODE_REMOVE) ) {
-                                        newNames[index++] = name;
-                                    }
-                                }
-                            }
-                            names = newNames;
-                            final RunMode baseRunMode = baseFeature.getRunMode(names);
-                            if ( baseRunMode != null ) {
-
-                                // artifact groups
-                                for(final ArtifactGroup group : runMode.getArtifactGroups()) {
-                                    for(final Artifact artifact : group) {
-                                        for(final ArtifactGroup searchGroup : baseRunMode.getArtifactGroups()) {
-                                            final Artifact found = searchGroup.search(artifact);
-                                            if ( found != null ) {
-                                                searchGroup.remove(found);
-                                            }
-                                        }
-                                    }
-                                }
-
-                                // configurations
-                                for(final Configuration config : runMode.getConfigurations()) {
-                                    final Configuration found = baseRunMode.getConfiguration(config.getPid(), config.getFactoryPid());
-                                    if ( found != null ) {
-                                        baseRunMode.getConfigurations().remove(found);
-                                    }
-                                }
-
-                                // settings
-                                for(final Map.Entry<String, String> entry : runMode.getSettings() ) {
-                                    baseRunMode.getSettings().remove(entry.getKey());
-                                }
-                            }
-                            continue;
-                        }
-                    }
-                }
-                final RunMode baseRunMode = baseFeature.getOrCreateRunMode(names);
-
-                // artifact groups
-                for(final ArtifactGroup group : runMode.getArtifactGroups()) {
-                    final ArtifactGroup baseGroup = baseRunMode.getOrCreateArtifactGroup(group.getStartLevel());
-
-                    for(final Artifact artifact : group) {
-                        for(final ArtifactGroup searchGroup : baseRunMode.getArtifactGroups()) {
-                            final Artifact found = searchGroup.search(artifact);
-                            if ( found != null ) {
-                                searchGroup.remove(found);
-                            }
-                        }
-                        baseGroup.add(artifact);
-                    }
-                }
-
-                // configurations
-                for(final Configuration config : runMode.getConfigurations()) {
-                    final Configuration found = baseRunMode.getOrCreateConfiguration(config.getPid(), config.getFactoryPid());
-
-                    mergeConfiguration(found, config);
-                }
-
-                // settings
-                for(final Map.Entry<String, String> entry : runMode.getSettings() ) {
-                    baseRunMode.getSettings().put(entry.getKey(), entry.getValue());
-                }
-            }
-
-        }
-    }
-
-    /**
-     * Merge two configurations
-     * @param baseConfig The base configuration.
-     * @param mergeConfig The merge configuration.
-     */
-    private static void mergeConfiguration(final Configuration baseConfig, final Configuration mergeConfig) {
-        // check for merge mode
-        final boolean isNew = baseConfig.getProperties().isEmpty();
-        if ( isNew ) {
-            copyConfigurationProperties(baseConfig, mergeConfig);
-            final Object mode = mergeConfig.getProperties().get(ModelConstants.CFG_UNPROCESSED_MODE);
-            if ( mode != null ) {
-                baseConfig.getProperties().put(ModelConstants.CFG_UNPROCESSED_MODE, mode);
-            }
-        } else {
-            final boolean baseIsRaw = baseConfig.getProperties().get(ModelConstants.CFG_UNPROCESSED) != null;
-            final boolean mergeIsRaw = mergeConfig.getProperties().get(ModelConstants.CFG_UNPROCESSED) != null;
-            // simplest case, both are raw
-            if ( baseIsRaw && mergeIsRaw ) {
-                final String cfgMode = (String)mergeConfig.getProperties().get(ModelConstants.CFG_UNPROCESSED_MODE);
-                if ( cfgMode == null || ModelConstants.CFG_MODE_OVERWRITE.equals(cfgMode) ) {
-                    copyConfigurationProperties(baseConfig, mergeConfig);
-                } else {
-                    final Configuration newConfig = new Configuration(baseConfig.getPid(), baseConfig.getFactoryPid());
-                    getProcessedConfiguration(null, newConfig, baseConfig, false, null);
-                    clearConfiguration(baseConfig);
-                    copyConfigurationProperties(baseConfig, newConfig);
-
-                    clearConfiguration(newConfig);
-                    getProcessedConfiguration(null, newConfig, mergeConfig, false, null);
-
-                    if ( baseConfig.isSpecial() ) {
-                        final String baseValue = baseConfig.getProperties().get(baseConfig.getPid()).toString();
-                        final String mergeValue = newConfig.getProperties().get(baseConfig.getPid()).toString();
-                        baseConfig.getProperties().put(baseConfig.getPid(), baseValue + "\n" + mergeValue);
-                    } else {
-                        copyConfigurationProperties(baseConfig, newConfig);
-                    }
-                }
-
-            // another simple case, both are not raw
-            } else if ( !baseIsRaw && !mergeIsRaw ) {
-                // merge mode is always overwrite
-                clearConfiguration(baseConfig);
-                copyConfigurationProperties(baseConfig, mergeConfig);
-
-            // base is not raw but merge is
-            } else if ( !baseIsRaw && mergeIsRaw ) {
-                final String cfgMode = (String)mergeConfig.getProperties().get(ModelConstants.CFG_UNPROCESSED_MODE);
-                if ( cfgMode == null || ModelConstants.CFG_MODE_OVERWRITE.equals(cfgMode) ) {
-                    clearConfiguration(baseConfig);
-                    copyConfigurationProperties(baseConfig, mergeConfig);
-                } else {
-                    final Configuration newMergeConfig = new Configuration(mergeConfig.getPid(), mergeConfig.getFactoryPid());
-                    getProcessedConfiguration(null, newMergeConfig, mergeConfig, false, null);
-
-                    if ( baseConfig.isSpecial() ) {
-                        final String baseValue = baseConfig.getProperties().get(baseConfig.getPid()).toString();
-                        final String mergeValue = newMergeConfig.getProperties().get(baseConfig.getPid()).toString();
-                        baseConfig.getProperties().put(baseConfig.getPid(), baseValue + "\n" + mergeValue);
-                    } else {
-                        copyConfigurationProperties(baseConfig, newMergeConfig);
-                    }
-                }
-
-                // base is raw, but merge is not raw
-            } else {
-                // merge mode is always overwrite
-                clearConfiguration(baseConfig);
-                copyConfigurationProperties(baseConfig, mergeConfig);
-            }
-        }
-    }
-
-    private static void clearConfiguration(final Configuration cfg) {
-        final Set<String> keys = new HashSet<String>();
-        final Enumeration<String> e = cfg.getProperties().keys();
-        while ( e.hasMoreElements() ) {
-            keys.add(e.nextElement());
-        }
-
-        for(final String key : keys) {
-            cfg.getProperties().remove(key);
-        }
-    }
+        final MergeOptions opts = new MergeOptions();
+        opts.setHandleRemoveRunMode(handleRemove);
+        MergeUtility.merge(base, additional, opts);
 
-    private static void copyConfigurationProperties(final Configuration baseConfig, final Configuration mergeConfig) {
-        final Enumeration<String> e = mergeConfig.getProperties().keys();
-        while ( e.hasMoreElements() ) {
-            final String key = e.nextElement();
-            if ( !key.equals(ModelConstants.CFG_UNPROCESSED_MODE) ) {
-                baseConfig.getProperties().put(key, mergeConfig.getProperties().get(key));
-            }
-        }
     }
 
     /**

Added: sling/trunk/tooling/support/provisioning-model/src/main/java/org/apache/sling/provisioning/model/Version.java
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/support/provisioning-model/src/main/java/org/apache/sling/provisioning/model/Version.java?rev=1709753&view=auto
==============================================================================
--- sling/trunk/tooling/support/provisioning-model/src/main/java/org/apache/sling/provisioning/model/Version.java (added)
+++ sling/trunk/tooling/support/provisioning-model/src/main/java/org/apache/sling/provisioning/model/Version.java Wed Oct 21 08:36:50 2015
@@ -0,0 +1,131 @@
+/*
+ * 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;
+
+/**
+ * Version object supporting Maven versions.
+ *
+ */
+public class Version implements Comparable<Version> {
+
+    private final int majorVersion;
+	private final int minorVersion;
+	private final int microVersion;
+	private final String qualifier;
+
+	/**
+	 * Creates a version identifier from the specified string.
+	 * @throws IllegalArgumentException if the version string can't be parsed
+	 */
+	public Version(final String version) {
+	    String parts[] = version.split("\\.");
+	    if ( parts.length > 3 ) {
+	        throw new IllegalArgumentException("Invalid version " + version);
+	    }
+	    final int pos = parts[parts.length - 1].indexOf('-');
+	    if ( pos != -1 ) {
+	        final String[] newParts = new String[4];
+	        newParts[0] = parts.length > 1 ? parts[0] : parts[0].substring(0, pos);
+            newParts[1] = parts.length > 2 ? parts[1] : (parts.length > 1 ? parts[1].substring(0, pos) : "0");
+            newParts[2] = parts.length > 3 ? parts[2] : (parts.length > 2 ? parts[2].substring(0, pos) : "0");
+            newParts[3] = parts[parts.length - 1].substring(pos + 1);
+            parts = newParts;
+	    }
+	    this.majorVersion = parseInt(parts[0], version);
+	    if ( parts.length > 1 ) {
+	        this.minorVersion = parseInt(parts[1], version);
+	    } else {
+	        this.minorVersion = 0;
+	    }
+        if ( parts.length > 2 ) {
+            this.microVersion = parseInt(parts[2], version);
+        } else {
+            this.microVersion = 0;
+        }
+        this.qualifier = (parts.length > 3 ? parts[3] : "");
+
+	}
+
+	/**
+	 * Parse an integer.
+	 */
+	private static int parseInt(final String value, final String version) {
+		try {
+			return Integer.parseInt(value);
+		} catch (NumberFormatException e) {
+			throw new IllegalArgumentException("Invalid version " + version);
+		}
+	}
+
+	@Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + majorVersion;
+        result = prime * result + microVersion;
+        result = prime * result + minorVersion;
+        result = prime * result + ((qualifier == null) ? 0 : qualifier.hashCode());
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj)
+            return true;
+        if (obj == null)
+            return false;
+        if (getClass() != obj.getClass())
+            return false;
+        Version other = (Version) obj;
+        if (majorVersion != other.majorVersion)
+            return false;
+        if (microVersion != other.microVersion)
+            return false;
+        if (minorVersion != other.minorVersion)
+            return false;
+        if (qualifier == null) {
+            if (other.qualifier != null)
+                return false;
+        } else if (!qualifier.equals(other.qualifier))
+            return false;
+        return true;
+    }
+
+    /**
+	 * Compares this {@code Version} object to another {@code Version}.
+	 */
+	@Override
+    public int compareTo(final Version other) {
+	    int result = 0;
+		if (other != this) {
+
+	        result = majorVersion - other.majorVersion;
+	        if (result == 0) {
+	            result = minorVersion - other.minorVersion;
+	            if (result == 0) {
+	                result = microVersion - other.microVersion;
+	                if (result == 0) {
+	                    result = other.qualifier.compareTo(qualifier);
+	                }
+	            }
+
+	        }
+
+		}
+		return result;
+	}
+}

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

Propchange: sling/trunk/tooling/support/provisioning-model/src/main/java/org/apache/sling/provisioning/model/Version.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Added: sling/trunk/tooling/support/provisioning-model/src/test/java/org/apache/sling/provisioning/model/VersionTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/support/provisioning-model/src/test/java/org/apache/sling/provisioning/model/VersionTest.java?rev=1709753&view=auto
==============================================================================
--- sling/trunk/tooling/support/provisioning-model/src/test/java/org/apache/sling/provisioning/model/VersionTest.java (added)
+++ sling/trunk/tooling/support/provisioning-model/src/test/java/org/apache/sling/provisioning/model/VersionTest.java Wed Oct 21 08:36:50 2015
@@ -0,0 +1,72 @@
+/*
+ * 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 static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+public class VersionTest {
+
+    @Test
+    public void testSameVersion() {
+        final String v1 = "1";
+        final String v10 = "1.0";
+        final String v100 = "1.0.0";
+
+        final Version ve1 = new Version(v1);
+        final Version ve10 = new Version(v10);
+        final Version ve100 = new Version(v100);
+
+        assertEquals(0, ve1.compareTo(ve10));
+        assertEquals(0, ve10.compareTo(ve100));
+        assertEquals(0, ve1.compareTo(ve100));
+        assertEquals(0, ve10.compareTo(ve1));
+        assertEquals(0, ve100.compareTo(ve10));
+        assertEquals(0, ve100.compareTo(ve1));
+    }
+
+    @Test
+    public void testVersions() {
+        final String v1 = "1";
+        final String v20 = "2.0";
+        final String v150 = "1.5.0";
+
+        final Version ve1 = new Version(v1);
+        final Version ve20 = new Version(v20);
+        final Version ve150 = new Version(v150);
+
+        assertTrue(ve1.compareTo(ve20) < 0);
+        assertTrue(ve20.compareTo(ve150) > 0);
+        assertTrue(ve1.compareTo(ve150) < 0);
+        assertTrue(ve20.compareTo(ve1) > 0);
+        assertTrue(ve150.compareTo(ve20) < 0);
+        assertTrue(ve150.compareTo(ve1) > 0);
+    }
+
+    @Test
+    public void testQualifier() {
+        final String v1 = "1";
+        final String v1snapshot = "1-SNAPSHOT";
+
+        final Version ve1 = new Version(v1);
+        final Version ve1snapshot = new Version(v1snapshot);
+
+        assertTrue(ve1.compareTo(ve1snapshot) > 0);
+    }
+}

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

Propchange: sling/trunk/tooling/support/provisioning-model/src/test/java/org/apache/sling/provisioning/model/VersionTest.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url