/flink-core/src/main/java/org/apache/flink/util/OperatingSystem.java

https://github.com/tongcheng-elong/flink · Java · 154 lines · 58 code · 22 blank · 74 comment · 11 complexity · f4b07f2f7d3e5a3d0d59ab0c9e16c937 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 org.apache.flink.util;
  19. import org.apache.flink.annotation.Internal;
  20. /** An enumeration indicating the operating system that the JVM runs on. */
  21. @Internal
  22. public enum OperatingSystem {
  23. LINUX,
  24. WINDOWS,
  25. MAC_OS,
  26. FREE_BSD,
  27. SOLARIS,
  28. UNKNOWN;
  29. // ------------------------------------------------------------------------
  30. /**
  31. * Gets the operating system that the JVM runs on from the java system properties. this method
  32. * returns <tt>UNKNOWN</tt>, if the operating system was not successfully determined.
  33. *
  34. * @return The enum constant for the operating system, or <tt>UNKNOWN</tt>, if it was not
  35. * possible to determine.
  36. */
  37. public static OperatingSystem getCurrentOperatingSystem() {
  38. return os;
  39. }
  40. /**
  41. * Checks whether the operating system this JVM runs on is Windows.
  42. *
  43. * @return <code>true</code> if the operating system this JVM runs on is Windows, <code>false
  44. * </code> otherwise
  45. */
  46. public static boolean isWindows() {
  47. return getCurrentOperatingSystem() == WINDOWS;
  48. }
  49. /**
  50. * Checks whether the operating system this JVM runs on is Linux.
  51. *
  52. * @return <code>true</code> if the operating system this JVM runs on is Linux, <code>false
  53. * </code> otherwise
  54. */
  55. public static boolean isLinux() {
  56. return getCurrentOperatingSystem() == LINUX;
  57. }
  58. /**
  59. * Checks whether the operating system this JVM runs on is Windows.
  60. *
  61. * @return <code>true</code> if the operating system this JVM runs on is Windows, <code>false
  62. * </code> otherwise
  63. */
  64. public static boolean isMac() {
  65. return getCurrentOperatingSystem() == MAC_OS;
  66. }
  67. /**
  68. * Checks whether the operating system this JVM runs on is FreeBSD.
  69. *
  70. * @return <code>true</code> if the operating system this JVM runs on is FreeBSD, <code>false
  71. * </code> otherwise
  72. */
  73. public static boolean isFreeBSD() {
  74. return getCurrentOperatingSystem() == FREE_BSD;
  75. }
  76. /**
  77. * Checks whether the operating system this JVM runs on is Solaris.
  78. *
  79. * @return <code>true</code> if the operating system this JVM runs on is Solaris, <code>false
  80. * </code> otherwise
  81. */
  82. public static boolean isSolaris() {
  83. return getCurrentOperatingSystem() == SOLARIS;
  84. }
  85. /** The enum constant for the operating system. */
  86. private static final OperatingSystem os = readOSFromSystemProperties();
  87. /**
  88. * Parses the operating system that the JVM runs on from the java system properties. If the
  89. * operating system was not successfully determined, this method returns {@code UNKNOWN}.
  90. *
  91. * @return The enum constant for the operating system, or {@code UNKNOWN}, if it was not
  92. * possible to determine.
  93. */
  94. private static OperatingSystem readOSFromSystemProperties() {
  95. String osName = System.getProperty(OS_KEY);
  96. if (osName.startsWith(LINUX_OS_PREFIX)) {
  97. return LINUX;
  98. }
  99. if (osName.startsWith(WINDOWS_OS_PREFIX)) {
  100. return WINDOWS;
  101. }
  102. if (osName.startsWith(MAC_OS_PREFIX)) {
  103. return MAC_OS;
  104. }
  105. if (osName.startsWith(FREEBSD_OS_PREFIX)) {
  106. return FREE_BSD;
  107. }
  108. String osNameLowerCase = osName.toLowerCase();
  109. if (osNameLowerCase.contains(SOLARIS_OS_INFIX_1)
  110. || osNameLowerCase.contains(SOLARIS_OS_INFIX_2)) {
  111. return SOLARIS;
  112. }
  113. return UNKNOWN;
  114. }
  115. // --------------------------------------------------------------------------------------------
  116. // Constants to extract the OS type from the java environment
  117. // --------------------------------------------------------------------------------------------
  118. /** The key to extract the operating system name from the system properties. */
  119. private static final String OS_KEY = "os.name";
  120. /** The expected prefix for Linux operating systems. */
  121. private static final String LINUX_OS_PREFIX = "Linux";
  122. /** The expected prefix for Windows operating systems. */
  123. private static final String WINDOWS_OS_PREFIX = "Windows";
  124. /** The expected prefix for Mac OS operating systems. */
  125. private static final String MAC_OS_PREFIX = "Mac";
  126. /** The expected prefix for FreeBSD. */
  127. private static final String FREEBSD_OS_PREFIX = "FreeBSD";
  128. /** One expected infix for Solaris. */
  129. private static final String SOLARIS_OS_INFIX_1 = "sunos";
  130. /** One expected infix for Solaris. */
  131. private static final String SOLARIS_OS_INFIX_2 = "solaris";
  132. }