95 lines
2.8 KiB
Java
95 lines
2.8 KiB
Java
package envoy.server.util;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
/**
|
|
* Implements a comparison algorithm between Envoy versions and defines minimal
|
|
* and maximal client versions compatible with this server.
|
|
* <p>
|
|
* Project: <strong>envoy-server-standalone</strong><br>
|
|
* File: <strong>VersionUtil.java</strong><br>
|
|
* Created: <strong>23.06.2020</strong><br>
|
|
*
|
|
* @author Kai S. K. Engelbart
|
|
* @since Envoy Server Standalone v0.1-beta
|
|
*/
|
|
public class VersionUtil {
|
|
|
|
/**
|
|
* The minimal client version compatible with this server.
|
|
*
|
|
* @since Envoy Server Standalone v0.1-beta
|
|
*/
|
|
public static final String MIN_CLIENT_VERSION = "0.1-beta";
|
|
|
|
/**
|
|
* The maximal client version compatible with this server.
|
|
*
|
|
* @since Envoy Server Standalone v0.1-beta
|
|
*/
|
|
public static final String MAX_CLIENT_VERSION = "0.1-beta";
|
|
|
|
private static final Pattern versionPattern = Pattern.compile("(?<major>\\d).(?<minor>\\d)(?:-(?<suffix>\\w+))?");
|
|
|
|
private VersionUtil() {}
|
|
|
|
/**
|
|
* Parses an Envoy Client version string and checks whether that version is
|
|
* compatible with this server.
|
|
*
|
|
* @param version the version string to parse
|
|
* @return {@code true} if the given version is compatible with this server
|
|
* @since Envoy Server Standalone v0.1-beta
|
|
*/
|
|
public static boolean verifyCompatibility(String version) {
|
|
final var currentMatcher = versionPattern.matcher(version);
|
|
|
|
if (!currentMatcher.matches()) return false;
|
|
|
|
final var minMatcher = versionPattern.matcher(MIN_CLIENT_VERSION);
|
|
final var maxMatcher = versionPattern.matcher(MAX_CLIENT_VERSION);
|
|
|
|
if (!minMatcher.matches() || !maxMatcher.matches()) throw new RuntimeException("Invalid min or max client version configured!");
|
|
|
|
// Compare suffixes
|
|
{
|
|
final var currentSuffix = convertSuffix(currentMatcher.group("suffix"));
|
|
final var minSuffix = convertSuffix(minMatcher.group("suffix"));
|
|
final var maxSuffix = convertSuffix(maxMatcher.group("suffix"));
|
|
|
|
if (currentSuffix < minSuffix || currentSuffix > maxSuffix) return false;
|
|
}
|
|
|
|
// Compare major
|
|
{
|
|
final var currentMajor = Integer.parseInt(currentMatcher.group("major"));
|
|
final var minMajor = Integer.parseInt(minMatcher.group("major"));
|
|
final var maxMajor = Integer.parseInt(maxMatcher.group("major"));
|
|
|
|
if (currentMajor < minMajor || currentMajor > maxMajor) return false;
|
|
}
|
|
|
|
// Compare minor
|
|
{
|
|
final var currentMinor = Integer.parseInt(currentMatcher.group("minor"));
|
|
final var minMinor = Integer.parseInt(minMatcher.group("minor"));
|
|
final var maxMinor = Integer.parseInt(maxMatcher.group("minor"));
|
|
|
|
if (currentMinor < minMinor || currentMinor > maxMinor) return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
private static int convertSuffix(String suffix) {
|
|
switch (suffix == null ? "" : suffix) {
|
|
case "alpha":
|
|
return 0;
|
|
case "beta":
|
|
return 1;
|
|
default:
|
|
return 2;
|
|
}
|
|
}
|
|
}
|