From bf15e9620433dab4787cc5b7dcb0034fe5221d4b Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Sun, 13 Mar 2011 13:12:44 +0100 Subject: [PATCH] improve os plugin condition --- .../src/main/java/sonia/scm/Platform.java | 30 +------------ .../src/main/java/sonia/scm/PlatformType.java | 44 +++++++++++++++++++ .../sonia/scm/plugin/PluginCondition.java | 25 +++++++---- .../sonia/scm/plugin/PluginConditionTest.java | 8 +++- 4 files changed, 68 insertions(+), 39 deletions(-) diff --git a/scm-core/src/main/java/sonia/scm/Platform.java b/scm-core/src/main/java/sonia/scm/Platform.java index 8c8d2e79ba..ab3e62523d 100644 --- a/scm-core/src/main/java/sonia/scm/Platform.java +++ b/scm-core/src/main/java/sonia/scm/Platform.java @@ -68,35 +68,7 @@ public class Platform arch = arch.toLowerCase(); x64 = "64".equals(arch) || "x86_64".equals(arch) || "ppc64".equals(arch) || "sparcv9".equals(arch) || "amd64".equals(arch); - - if (osName.startsWith("Linux")) - { - type = PlatformType.LINUX; - } - else if (osName.startsWith("Mac") || osName.startsWith("Darwin")) - { - type = PlatformType.MAC; - } - else if (osName.startsWith("Windows")) - { - type = PlatformType.WINDOWS; - } - else if (osName.startsWith("Solaris") || osName.startsWith("SunOS")) - { - type = PlatformType.SOLARIS; - } - else if (osName.startsWith("FreeBSD")) - { - type = PlatformType.FREEBSD; - } - else if (osName.startsWith("OpenBSD")) - { - type = PlatformType.OPENBSD; - } - else - { - type = PlatformType.UNSPECIFIED; - } + type = PlatformType.createPlatformType(osName); } //~--- methods -------------------------------------------------------------- diff --git a/scm-core/src/main/java/sonia/scm/PlatformType.java b/scm-core/src/main/java/sonia/scm/PlatformType.java index 4d46bc5cd5..dfda8940a9 100644 --- a/scm-core/src/main/java/sonia/scm/PlatformType.java +++ b/scm-core/src/main/java/sonia/scm/PlatformType.java @@ -56,6 +56,50 @@ public enum PlatformType this.posix = posix; } + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @param osName + * + * @return + */ + public static PlatformType createPlatformType(String osName) + { + osName = osName.toLowerCase(); + + PlatformType type = PlatformType.UNSPECIFIED; + + if (osName.startsWith("linux")) + { + type = PlatformType.LINUX; + } + else if (osName.startsWith("mac") || osName.startsWith("darwin")) + { + type = PlatformType.MAC; + } + else if (osName.startsWith("windows")) + { + type = PlatformType.WINDOWS; + } + else if (osName.startsWith("solaris") || osName.startsWith("sunos")) + { + type = PlatformType.SOLARIS; + } + else if (osName.startsWith("freebsd")) + { + type = PlatformType.FREEBSD; + } + else if (osName.startsWith("openbsd")) + { + type = PlatformType.OPENBSD; + } + + return type; + } + //~--- get methods ---------------------------------------------------------- /** diff --git a/scm-core/src/main/java/sonia/scm/plugin/PluginCondition.java b/scm-core/src/main/java/sonia/scm/plugin/PluginCondition.java index a58764bdd2..97890909ca 100644 --- a/scm-core/src/main/java/sonia/scm/plugin/PluginCondition.java +++ b/scm-core/src/main/java/sonia/scm/plugin/PluginCondition.java @@ -35,6 +35,7 @@ package sonia.scm.plugin; //~--- non-JDK imports -------------------------------------------------------- +import sonia.scm.PlatformType; import sonia.scm.SCMContext; import sonia.scm.util.SystemUtil; import sonia.scm.util.Util; @@ -149,11 +150,12 @@ public class PluginCondition if (supported && Util.isNotEmpty(this.os) && Util.isNotEmpty(os)) { supported = false; - os = os.toLowerCase(); + + PlatformType platformType = PlatformType.createPlatformType(os); for (String osType : this.os) { - supported = isOs(osType, os); + supported = isOs(osType, platformType); if (supported) { @@ -212,18 +214,23 @@ public class PluginCondition * * * @param osType - * @param os + * @param type * * @return */ - private boolean isOs(String osType, String os) + private boolean isOs(String osType, PlatformType type) { osType = osType.toLowerCase(); - - return (osType.startsWith("mac") && (os.indexOf("mac") >= 0)) - || (osType.startsWith("win") && (os.indexOf("win") >= 0)) - || ((osType.startsWith("unix") && (os.indexOf("nix") >= 0)) - || (os.indexOf("nux") >= 0)); + + return + ( osType.indexOf("win") >= 0 && PlatformType.WINDOWS == type ) || + ( osType.indexOf("unix") >= 0 && type.isUnix() ) || + ( osType.indexOf("posix") >= 0 && type.isPosix() ) || + ( osType.indexOf("mac") >= 0 && PlatformType.MAC == type) || + ( osType.indexOf("linux") >= 0 && PlatformType.LINUX == type ) || + ( osType.indexOf("solaris") >= 0 && PlatformType.SOLARIS == type ) || + ( osType.indexOf("openbsd") >= 0 && PlatformType.OPENBSD == type ) || + ( osType.indexOf("freebsd") >= 0 && PlatformType.FREEBSD == type ); } //~--- fields --------------------------------------------------------------- diff --git a/scm-core/src/test/java/sonia/scm/plugin/PluginConditionTest.java b/scm-core/src/test/java/sonia/scm/plugin/PluginConditionTest.java index e476e3a274..cc48001704 100644 --- a/scm-core/src/test/java/sonia/scm/plugin/PluginConditionTest.java +++ b/scm-core/src/test/java/sonia/scm/plugin/PluginConditionTest.java @@ -70,8 +70,14 @@ public class PluginConditionTest @Test public void testIsOsSupported() { - assertTrue(new PluginCondition(null, Arrays.asList("unix"), + assertTrue(new PluginCondition(null, Arrays.asList("linux"), null).isSupported(null, "linux", null)); + assertTrue(new PluginCondition(null, Arrays.asList("unix"), + null).isSupported(null, "Mac OS X", null)); + assertTrue(new PluginCondition(null, Arrays.asList("unix"), + null).isSupported(null, "Solaris", null)); + assertTrue(new PluginCondition(null, Arrays.asList("posix"), + null).isSupported(null, "Linux", null)); assertTrue(new PluginCondition(null, Arrays.asList("win"), null).isSupported(null, "Windows 2000", null));