/rio/src/main/java/org/rioproject/system/OperatingSystemType.java

https://github.com/jramsdale/Rio · Java · 167 lines · 60 code · 13 blank · 94 comment · 3 complexity · 43d0144f451b6f1dff60eba635f5ffe5 MD5 · raw file

  1. /*
  2. * Copyright 2008 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * 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
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.rioproject.system;
  17. import java.io.IOException;
  18. import java.io.InputStreamReader;
  19. import java.io.BufferedReader;
  20. import java.util.Vector;
  21. /**
  22. * Utilities to help determine operating system type
  23. *
  24. * @author Dennis Reedy
  25. */
  26. public class OperatingSystemType {
  27. /**
  28. * Linux identifier
  29. */
  30. public static final String LINUX = "linux";
  31. /**
  32. * Solaris identifier
  33. */
  34. public static final String SOLARIS = "sunos";
  35. /**
  36. * Mac identifier
  37. */
  38. public static final String MACINTOSH = "Mac";
  39. /**
  40. * Windows identifier
  41. */
  42. public static final String WINDOWS = "Windows";
  43. /**
  44. * Windows 2000 identifier
  45. */
  46. public static final String WINDOWS_2K = WINDOWS+" 2000";
  47. /**
  48. * Windows XP identifier
  49. */
  50. public static final String WINDOWS_XP = WINDOWS+" XP";
  51. /**
  52. * HP-UX identifier
  53. */
  54. public static final String HP_UX = "HP-UX";
  55. /**
  56. * Check if running on Linux
  57. *
  58. * @return If running on Linux return <code>true</code>, otherwise return
  59. * <code>false</code>
  60. */
  61. public static boolean isLinux() {
  62. String opSys = System.getProperty("os.name");
  63. return (opSys.equalsIgnoreCase(LINUX));
  64. }
  65. /**
  66. * Check if running on Solaris
  67. *
  68. * @return If running on Solaris return <code>true</code>, otherwise return
  69. * <code>false</code>
  70. */
  71. public static boolean isSolaris() {
  72. String opSys = System.getProperty("os.name");
  73. return (opSys.equalsIgnoreCase(SOLARIS));
  74. }
  75. /**
  76. * Check if running on Mac
  77. *
  78. * @return If running on Mac return <code>true</code>, otherwise return
  79. * <code>false</code>
  80. */
  81. public static boolean isMac() {
  82. String opSys = System.getProperty("os.name");
  83. return (opSys.startsWith(MACINTOSH));
  84. }
  85. /**
  86. * Check if running on HP-UX
  87. *
  88. * @return If running on HP-UX return <code>true</code>, otherwise return
  89. * <code>false</code>
  90. */
  91. public static boolean isHP() {
  92. String opSys = System.getProperty("os.name");
  93. return (opSys.equals(HP_UX));
  94. }
  95. /**
  96. * Check if running on Windows
  97. *
  98. * @return If running on Windows return <code>true</code>, otherwise return
  99. * <code>false</code>
  100. */
  101. public static boolean isWindows() {
  102. String opSys = System.getProperty("os.name");
  103. return (opSys.startsWith(WINDOWS));
  104. }
  105. /**
  106. * Check if running on Windows 2000
  107. *
  108. * @return If running on Windows 2000 return <code>true</code>, otherwise
  109. * return <code>false</code>
  110. */
  111. public static boolean isWindows2K() {
  112. String opSys = System.getProperty("os.name");
  113. return (opSys.startsWith(WINDOWS_2K));
  114. }
  115. /**
  116. * Check if running on Windows XP
  117. *
  118. * @return If running on Windows XP return <code>true</code>, otherwise
  119. * return <code>false</code>
  120. */
  121. public static boolean isWindowsXP() {
  122. String opSys = System.getProperty("os.name");
  123. return (opSys.startsWith(WINDOWS_XP));
  124. }
  125. /**
  126. * Gets a string representing the pid of this program - Java VM
  127. *
  128. * @return A String that corresponds to the process ID (PID) of the JVM.
  129. * This method will create a shell using /bin/bash and obtain the $PPID
  130. * environment variable. If the operating system type is Windows, an
  131. * empty string is returned
  132. *
  133. * @throws IOException If the shell cannot be created
  134. * @throws InterruptedException If the process wait is interrupted
  135. */
  136. public static String getPid() throws IOException,InterruptedException {
  137. if(isWindows())
  138. return "";
  139. Vector<String> commands=new Vector<String>();
  140. commands.add("/bin/bash");
  141. commands.add("-c");
  142. commands.add("echo $PPID");
  143. ProcessBuilder pb=new ProcessBuilder(commands);
  144. Process pr=pb.start();
  145. pr.waitFor();
  146. if (pr.exitValue()==0) {
  147. BufferedReader outReader=new BufferedReader(new InputStreamReader(pr.getInputStream()));
  148. return outReader.readLine().trim();
  149. } else {
  150. System.out.println("Error while getting PID");
  151. return "";
  152. }
  153. }
  154. }