/nephele/nephele-common/src/main/java/eu/stratosphere/nephele/os/OperatingSystem.java

https://github.com/bjoernlohrmann/nephele-streaming · Java · 143 lines · 41 code · 22 blank · 80 comment · 8 complexity · e643a1153e3093bb32d3e4f329486210 MD5 · raw file

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