/overthere/src/main/java/com/xebialabs/overthere/OperatingSystemFamily.java

https://github.com/bmoussaud/overthere · Java · 136 lines · 63 code · 12 blank · 61 comment · 19 complexity · 3386483c7216c855f9d5981aa69786d1 MD5 · raw file

  1. /*
  2. * This file is part of Overthere.
  3. *
  4. * Overthere is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * Overthere is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with Overthere. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package com.xebialabs.overthere;
  18. import java.io.BufferedReader;
  19. import java.io.IOException;
  20. import java.io.StringReader;
  21. /**
  22. * The family (flavour) of the operating system running on a host.
  23. */
  24. public enum OperatingSystemFamily {
  25. /**
  26. * An operating system from the Windows family: NT, XP, Server 2003, Vista, etc.
  27. */
  28. WINDOWS,
  29. /**
  30. * An operating system from the Unix family: Linux, AIX, MacOS, etc.
  31. */
  32. UNIX;
  33. /**
  34. * Returns the {@link OperatingSystemFamily} that corresponds to the local host
  35. */
  36. public static OperatingSystemFamily getLocalHostOperatingSystemFamily() {
  37. return System.getProperty("os.name").startsWith("Windows") ? WINDOWS : UNIX;
  38. }
  39. /**
  40. * Returns the extension for scripts used by the operating system family, e.g. <tt>.bat</tt> or <tt>.sh</tt>
  41. *
  42. *
  43. * @return the script extension including the preceding dot
  44. */
  45. public String getScriptExtension() {
  46. if (this == WINDOWS) {
  47. return ".bat";
  48. } else {
  49. return ".sh";
  50. }
  51. }
  52. /**
  53. * Returns the characters used by the operating system family to separate line in a text file, e.g. <tt>\r\n</tt> or <tt>\n</tt>
  54. *
  55. * @return the line separator
  56. */
  57. public String getLineSeparator() {
  58. if (this == WINDOWS) {
  59. return "\r\n";
  60. } else {
  61. return "\n";
  62. }
  63. }
  64. /**
  65. * Returns the default path of the temporary directory for this operating system family, i.e. <tt>C:\temp</tt> or <tt>/tmp</tt>.
  66. *
  67. * @return the path
  68. */
  69. public String getDefaultTemporaryDirectoryPath() {
  70. if (this == WINDOWS) {
  71. return "C:\\windows\\temp";
  72. } else {
  73. return "/tmp";
  74. }
  75. }
  76. /**
  77. * Returns the character used by the operating system family to separate components of a file path, e.g. <tt>\</tt> or <tt>/<tt>.
  78. *
  79. * @return the file separator.
  80. */
  81. public String getFileSeparator() {
  82. if (this == WINDOWS) {
  83. return "\\";
  84. } else {
  85. return "/";
  86. }
  87. }
  88. /**
  89. * Returns the character used by the operating system family to separate components of a path, e.g. <tt>;</tt> or <tt>:<tt>.
  90. *
  91. * @return the file separator.
  92. */
  93. public String getPathSeparator() {
  94. if (this == WINDOWS) {
  95. return ";";
  96. } else {
  97. return ":";
  98. }
  99. }
  100. /**
  101. * Converts a text to use the {@link #getLineSeparator() line separator} of this operating system family.
  102. *
  103. * @param text
  104. * the text to convert.
  105. * @return the converted text.
  106. */
  107. public String convertText(String text) {
  108. if (text == null) {
  109. return null;
  110. }
  111. String lineSep = getLineSeparator();
  112. try {
  113. StringBuilder converted = new StringBuilder();
  114. BufferedReader r = new BufferedReader(new StringReader(text));
  115. String line;
  116. while ((line = r.readLine()) != null) {
  117. converted.append(line).append(lineSep);
  118. }
  119. return converted.toString();
  120. } catch (IOException exc) {
  121. throw new RuntimeIOException("Unable to read String", exc);
  122. }
  123. }
  124. }