/src/main/java/io/mycat/memory/environment/OperatingSystem.java

https://github.com/MyCATApache/Mycat-Server · Java · 145 lines · 45 code · 22 blank · 78 comment · 8 complexity · 67c6baa8e8cef8956a8d90b08db0778e MD5 · raw file

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package io.mycat.memory.environment;
  19. /**
  20. * An enumeration indicating the operating system that the JVM runs on.
  21. */
  22. public enum OperatingSystem {
  23. LINUX,
  24. WINDOWS,
  25. MAC_OS,
  26. FREE_BSD,
  27. UNKNOWN;
  28. // ------------------------------------------------------------------------
  29. /**
  30. * Gets the operating system that the JVM runs on from the java system properties.
  31. * this method returns <tt>UNKNOWN</tt>, if the operating system was not successfully determined.
  32. *
  33. * @return The enum constant for the operating system, or <tt>UNKNOWN</tt>, if it was not possible to determine.
  34. */
  35. public static OperatingSystem getCurrentOperatingSystem() {
  36. return os;
  37. }
  38. /**
  39. * Checks whether the operating system this JVM runs on is Windows.
  40. *
  41. * @return <code>true</code> if the operating system this JVM runs on is
  42. * Windows, <code>false</code> otherwise
  43. */
  44. public static boolean isWindows() {
  45. return getCurrentOperatingSystem() == WINDOWS;
  46. }
  47. /**
  48. * Checks whether the operating system this JVM runs on is Linux.
  49. *
  50. * @return <code>true</code> if the operating system this JVM runs on is
  51. * Linux, <code>false</code> otherwise
  52. */
  53. public static boolean isLinux() {
  54. return getCurrentOperatingSystem() == LINUX;
  55. }
  56. /**
  57. * Checks whether the operating system this JVM runs on is Windows.
  58. *
  59. * @return <code>true</code> if the operating system this JVM runs on is
  60. * Windows, <code>false</code> otherwise
  61. */
  62. public static boolean isMac() {
  63. return getCurrentOperatingSystem() == MAC_OS;
  64. }
  65. /**
  66. * Checks whether the operating system this JVM runs on is FreeBSD.
  67. *
  68. * @return <code>true</code> if the operating system this JVM runs on is
  69. * FreeBSD, <code>false</code> otherwise
  70. */
  71. public static boolean isFreeBSD() {
  72. return getCurrentOperatingSystem() == FREE_BSD;
  73. }
  74. /**
  75. * The enum constant for the operating system.
  76. */
  77. private static final OperatingSystem os = readOSFromSystemProperties();
  78. /**
  79. * Parses the operating system that the JVM runs on from the java system properties.
  80. * If the operating system was not successfully determined, this method returns {@code UNKNOWN}.
  81. *
  82. * @return The enum constant for the operating system, or {@code UNKNOWN}, if it was not possible to determine.
  83. */
  84. private static OperatingSystem readOSFromSystemProperties() {
  85. String osName = System.getProperty(OS_KEY);
  86. if (osName.startsWith(LINUX_OS_PREFIX)) {
  87. return LINUX;
  88. }
  89. if (osName.startsWith(WINDOWS_OS_PREFIX)) {
  90. return WINDOWS;
  91. }
  92. if (osName.startsWith(MAC_OS_PREFIX)) {
  93. return MAC_OS;
  94. }
  95. if (osName.startsWith(FREEBSD_OS_PREFIX)) {
  96. return FREE_BSD;
  97. }
  98. return UNKNOWN;
  99. }
  100. // --------------------------------------------------------------------------------------------
  101. // Constants to extract the OS type from the java environment
  102. // --------------------------------------------------------------------------------------------
  103. /**
  104. * The key to extract the operating system name from the system properties.
  105. */
  106. private static final String OS_KEY = "os.name";
  107. /**
  108. * The expected prefix for Linux operating systems.
  109. */
  110. private static final String LINUX_OS_PREFIX = "Linux";
  111. /**
  112. * The expected prefix for Windows operating systems.
  113. */
  114. private static final String WINDOWS_OS_PREFIX = "Windows";
  115. /**
  116. * The expected prefix for Mac OS operating systems.
  117. */
  118. private static final String MAC_OS_PREFIX = "Mac";
  119. /**
  120. * The expected prefix for FreeBSD.
  121. */
  122. private static final String FREEBSD_OS_PREFIX = "FreeBSD";
  123. }