/revolsys-core/src/main/java/com/revolsys/util/OS.java

https://github.com/revolsys/com.revolsys.open · Java · 181 lines · 154 code · 27 blank · 0 comment · 36 complexity · fa205acdad7c7eb3f14a05679d114819 MD5 · raw file

  1. package com.revolsys.util;
  2. import java.io.File;
  3. import java.util.LinkedHashMap;
  4. import java.util.Map;
  5. import com.revolsys.io.FileUtil;
  6. import com.revolsys.record.io.format.json.Json;
  7. public class OS {
  8. public static final String OS_ARCH = System.getProperty("os.arch");
  9. public final static String OS_NAME = System.getProperty("os.name");
  10. public final static boolean IS_LINUX = OS_NAME.equals("Linux");
  11. public final static boolean IS_MAC = OS_NAME.contains("OS X") || OS_NAME.equals("Darwin");
  12. public final static boolean IS_SOLARIS = OS_NAME.equals("SunOS");
  13. public final static boolean IS_WINDOWS = OS_NAME.startsWith("Windows");
  14. public static File getApplicationDataDirectory() {
  15. String path;
  16. if (OS.isWindows()) {
  17. path = System.getenv("APPDATA");
  18. } else if (OS.isMac()) {
  19. path = System.getProperty("user.home") + "/Library/Application Support";
  20. } else {
  21. path = System.getProperty("user.home") + "/.config";
  22. }
  23. final File directory = FileUtil.getDirectory(path);
  24. return directory;
  25. }
  26. public static File getApplicationDataDirectory(final String applicationName) {
  27. final File appsDirectory = getApplicationDataDirectory();
  28. final File directory = FileUtil.getDirectory(appsDirectory, applicationName);
  29. return directory;
  30. }
  31. public static String getArch() {
  32. final String osArch = OS_ARCH.toLowerCase();
  33. if (osArch.equals("i386")) {
  34. return "x86";
  35. } else if (osArch.startsWith("amd64") || osArch.startsWith("x86_64")) {
  36. return "x86_64";
  37. } else if (osArch.equals("ppc")) {
  38. return "ppc";
  39. } else if (osArch.startsWith("ppc")) {
  40. return "ppc_64";
  41. } else if (osArch.startsWith("sparc")) {
  42. return "sparc";
  43. } else {
  44. return OS_ARCH;
  45. }
  46. }
  47. @SuppressWarnings("unchecked")
  48. public static <T> T getPreference(final String applicationName, final String path,
  49. final String propertyName) {
  50. final Map<String, Object> preferences = getPreferences(applicationName, path);
  51. return (T)Property.get(preferences, propertyName);
  52. }
  53. public static <T> T getPreference(final String applicationName, final String path,
  54. final String propertyName, final T defaultValue) {
  55. final T value = getPreference(applicationName, path, propertyName);
  56. if (value == null) {
  57. return defaultValue;
  58. } else {
  59. return value;
  60. }
  61. }
  62. public static boolean getPreferenceBoolean(final String applicationName, final String path,
  63. final String propertyName) {
  64. final Map<String, Object> preferences = getPreferences(applicationName, path);
  65. final Object value = preferences.get(propertyName);
  66. return !Booleans.isFalse(value);
  67. }
  68. public static boolean getPreferenceBoolean(final String applicationName, final String path,
  69. final String propertyName, final boolean defaultValue) {
  70. final Map<String, Object> preferences = getPreferences(applicationName, path);
  71. final Object value = preferences.get(propertyName);
  72. if (value == null) {
  73. return defaultValue;
  74. } else {
  75. return !Booleans.isFalse(value);
  76. }
  77. }
  78. public static File getPreferenceFile(final String applicationName, final String path) {
  79. if (path.contains("..")) {
  80. throw new IllegalArgumentException(
  81. "Path cannot contain the '..' character sequernce: " + path);
  82. }
  83. final File preferencesDirectory = getPreferencesDirectory(applicationName);
  84. final File file = FileUtil.getFile(preferencesDirectory, path + ".rgobject");
  85. file.getParentFile().mkdirs();
  86. return file;
  87. }
  88. public static Integer getPreferenceInt(final String applicationName, final String path,
  89. final String propertyName) {
  90. final Map<String, Object> preferences = getPreferences(applicationName, path);
  91. final Object value = preferences.get(propertyName);
  92. if (value == null) {
  93. return null;
  94. } else {
  95. return Integer.valueOf(value.toString());
  96. }
  97. }
  98. public static int getPreferenceInt(final String applicationName, final String path,
  99. final String propertyName, final int defaultValue) {
  100. final Map<String, Object> preferences = getPreferences(applicationName, path);
  101. final Object value = preferences.get(propertyName);
  102. if (value == null) {
  103. return defaultValue;
  104. } else {
  105. return Integer.valueOf(value.toString());
  106. }
  107. }
  108. public static Map<String, Object> getPreferences(final String applicationName,
  109. final String path) {
  110. final File file = getPreferenceFile(applicationName, path);
  111. if (file.exists()) {
  112. return Json.toMap(file);
  113. } else {
  114. return new LinkedHashMap<>();
  115. }
  116. }
  117. public static File getPreferencesDirectory(final String applicationName) {
  118. String path;
  119. if (OS.isWindows()) {
  120. path = System.getenv("APPDATA") + "/" + applicationName + "/Preferences";
  121. } else if (OS.isMac()) {
  122. path = System.getProperty("user.home") + "/Library/Preferences/" + applicationName;
  123. } else {
  124. path = System.getProperty("user.home") + "/.config/" + applicationName + "/Preferences";
  125. }
  126. final File directory = FileUtil.getFile(path);
  127. directory.mkdirs();
  128. return directory;
  129. }
  130. public static File getUserDirectory() {
  131. final String home = System.getProperty("user.home");
  132. return FileUtil.getFile(home);
  133. }
  134. public static File getUserDirectory(final String path) {
  135. final File userDirectory = getUserDirectory();
  136. return FileUtil.getFile(userDirectory, path);
  137. }
  138. public static boolean isMac() {
  139. return IS_MAC;
  140. }
  141. public static boolean isUnix() {
  142. return IS_SOLARIS || IS_LINUX;
  143. }
  144. public static boolean isWindows() {
  145. return IS_WINDOWS;
  146. }
  147. public static void setPreference(final String applicationName, final String path,
  148. final String propertyName, final Object value) {
  149. final Map<String, Object> preferences = getPreferences(applicationName, path);
  150. preferences.put(propertyName, value);
  151. final File file = getPreferenceFile(applicationName, path);
  152. Json.writeMap(preferences, file, true);
  153. }
  154. }