|
@@ -17,12 +17,12 @@
|
|
*/
|
|
*/
|
|
package org.apache.ambari.server.utils;
|
|
package org.apache.ambari.server.utils;
|
|
|
|
|
|
-import org.apache.ambari.server.bootstrap.BootStrapImpl;
|
|
|
|
-import org.apache.commons.lang.StringUtils;
|
|
|
|
-
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
|
|
|
+import org.apache.ambari.server.bootstrap.BootStrapImpl;
|
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Provides various utility functions to be used for version handling.
|
|
* Provides various utility functions to be used for version handling.
|
|
* The compatibility matrix between server, agent, store can also be maintained
|
|
* The compatibility matrix between server, agent, store can also be maintained
|
|
@@ -30,28 +30,35 @@ import java.util.List;
|
|
*/
|
|
*/
|
|
public class VersionUtils {
|
|
public class VersionUtils {
|
|
/**
|
|
/**
|
|
- * Compares two versions strings of the form N.N.N.N or even N.N.N.N-### (which should ignore everything after the dash).
|
|
|
|
- * If the user has a custom stack, e.g., 2.3.MYNAME or MYNAME.2.3, then any segment that contains letters should be ignored.
|
|
|
|
|
|
+ * Compares two versions strings of the form N.N.N.N or even N.N.N.N-###
|
|
|
|
+ * (which should ignore everything after the dash). If the user has a custom
|
|
|
|
+ * stack, e.g., 2.3.MYNAME or MYNAME.2.3, then any segment that contains
|
|
|
|
+ * letters should be ignored.
|
|
*
|
|
*
|
|
* @param version1
|
|
* @param version1
|
|
|
|
+ * the first operand. If set to {@value BootStrapImpl#DEV_VERSION}
|
|
|
|
+ * then this will always return {@code 0)}
|
|
* @param version2
|
|
* @param version2
|
|
- * @param maxLengthToCompare The maximum length to compare - 2 means only Major and Minor
|
|
|
|
- * 0 to compare the whole version strings
|
|
|
|
- * @return 0 if both are equal up to the length compared, -1 if first one is lower, +1 otherwise
|
|
|
|
|
|
+ * the second operand.
|
|
|
|
+ * @param maxLengthToCompare
|
|
|
|
+ * The maximum length to compare - 2 means only Major and Minor 0 to
|
|
|
|
+ * compare the whole version strings
|
|
|
|
+ * @return 0 if both are equal up to the length compared, -1 if first one is
|
|
|
|
+ * lower, +1 otherwise
|
|
*/
|
|
*/
|
|
public static int compareVersions(String version1, String version2, int maxLengthToCompare)
|
|
public static int compareVersions(String version1, String version2, int maxLengthToCompare)
|
|
throws IllegalArgumentException {
|
|
throws IllegalArgumentException {
|
|
if (version1 == null){
|
|
if (version1 == null){
|
|
throw new IllegalArgumentException("version1 cannot be null");
|
|
throw new IllegalArgumentException("version1 cannot be null");
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
if (version2 == null){
|
|
if (version2 == null){
|
|
throw new IllegalArgumentException("version2 cannot be null");
|
|
throw new IllegalArgumentException("version2 cannot be null");
|
|
- }
|
|
|
|
-
|
|
|
|
|
|
+ }
|
|
|
|
+
|
|
version1 = StringUtils.trim(version1);
|
|
version1 = StringUtils.trim(version1);
|
|
version2 = StringUtils.trim(version2);
|
|
version2 = StringUtils.trim(version2);
|
|
-
|
|
|
|
|
|
+
|
|
if (version1.indexOf('-') >=0) {
|
|
if (version1.indexOf('-') >=0) {
|
|
version1 = version1.substring(0, version1.indexOf('-'));
|
|
version1 = version1.substring(0, version1.indexOf('-'));
|
|
}
|
|
}
|
|
@@ -69,7 +76,9 @@ public class VersionUtils {
|
|
throw new IllegalArgumentException("maxLengthToCompare cannot be less than 0");
|
|
throw new IllegalArgumentException("maxLengthToCompare cannot be less than 0");
|
|
}
|
|
}
|
|
|
|
|
|
- if(BootStrapImpl.DEV_VERSION.equals(version1.trim())) return 0;
|
|
|
|
|
|
+ if(BootStrapImpl.DEV_VERSION.equals(version1.trim())) {
|
|
|
|
+ return 0;
|
|
|
|
+ }
|
|
|
|
|
|
String[] version1Parts = version1.split("\\.");
|
|
String[] version1Parts = version1.split("\\.");
|
|
String[] version2Parts = version2.split("\\.");
|
|
String[] version2Parts = version2.split("\\.");
|
|
@@ -118,13 +127,20 @@ public class VersionUtils {
|
|
* Compares two versions strings of the form N.N.N.N
|
|
* Compares two versions strings of the form N.N.N.N
|
|
*
|
|
*
|
|
* @param version1
|
|
* @param version1
|
|
|
|
+ * the first operand. If set to {@value BootStrapImpl#DEV_VERSION}
|
|
|
|
+ * then this will always return {@code 0)}
|
|
* @param version2
|
|
* @param version2
|
|
- * @param allowEmptyVersions Allow one or both version values to be null or empty string
|
|
|
|
- * @return 0 if both are equal up to the length compared, -1 if first one is lower, +1 otherwise
|
|
|
|
|
|
+ * the second operand.
|
|
|
|
+ * @param allowEmptyVersions
|
|
|
|
+ * Allow one or both version values to be null or empty string
|
|
|
|
+ * @return 0 if both are equal up to the length compared, -1 if first one is
|
|
|
|
+ * lower, +1 otherwise
|
|
*/
|
|
*/
|
|
public static int compareVersions(String version1, String version2, boolean allowEmptyVersions) {
|
|
public static int compareVersions(String version1, String version2, boolean allowEmptyVersions) {
|
|
if (allowEmptyVersions) {
|
|
if (allowEmptyVersions) {
|
|
- if (version1 != null && version1.equals(BootStrapImpl.DEV_VERSION)) return 0;
|
|
|
|
|
|
+ if (version1 != null && version1.equals(BootStrapImpl.DEV_VERSION)) {
|
|
|
|
+ return 0;
|
|
|
|
+ }
|
|
if (version1 == null && version2 == null) {
|
|
if (version1 == null && version2 == null) {
|
|
return 0;
|
|
return 0;
|
|
} else {
|
|
} else {
|
|
@@ -155,7 +171,10 @@ public class VersionUtils {
|
|
* Compares two versions strings of the form N.N.N.N
|
|
* Compares two versions strings of the form N.N.N.N
|
|
*
|
|
*
|
|
* @param version1
|
|
* @param version1
|
|
|
|
+ * the first operand. If set to {@value BootStrapImpl#DEV_VERSION}
|
|
|
|
+ * then this will always return {@code 0)}
|
|
* @param version2
|
|
* @param version2
|
|
|
|
+ * the second operand.
|
|
* @return 0 if both are equal, -1 if first one is lower, +1 otherwise
|
|
* @return 0 if both are equal, -1 if first one is lower, +1 otherwise
|
|
*/
|
|
*/
|
|
public static int compareVersions(String version1, String version2) {
|
|
public static int compareVersions(String version1, String version2) {
|
|
@@ -166,8 +185,12 @@ public class VersionUtils {
|
|
* Compares two version for equality, allows empty versions
|
|
* Compares two version for equality, allows empty versions
|
|
*
|
|
*
|
|
* @param version1
|
|
* @param version1
|
|
|
|
+ * the first operand. If set to {@value BootStrapImpl#DEV_VERSION}
|
|
|
|
+ * then this will always return {@code 0)}
|
|
* @param version2
|
|
* @param version2
|
|
- * @param allowEmptyVersions Allow one or both version values to be null or empty string
|
|
|
|
|
|
+ * the second operand.
|
|
|
|
+ * @param allowEmptyVersions
|
|
|
|
+ * Allow one or both version values to be null or empty string
|
|
* @return true if versions are equal; false otherwise
|
|
* @return true if versions are equal; false otherwise
|
|
*/
|
|
*/
|
|
public static boolean areVersionsEqual(String version1, String version2, boolean allowEmptyVersions) {
|
|
public static boolean areVersionsEqual(String version1, String version2, boolean allowEmptyVersions) {
|