PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/snodes/fs/PathManager.java

https://github.com/mdippery/snodes
Java | 154 lines | 67 code | 16 blank | 71 comment | 12 complexity | f19026c69ff7e90bbae26b2ca7f834f3 MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. * Copyright (c) 2007-2008 Michael Schoonmaker <michael.r.schoonmaker@gmail.com>
  3. * Copyright (c) 2007-2008 Michael Dippery <michael@monkey-robot.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 2 of the License, or (at your option)
  8. * any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. package snodes.fs;
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.awt.Image;
  23. import java.awt.Toolkit;
  24. /**
  25. * A set of utilities for interacting with the standard directory structure
  26. * in a platform-independent way. This class provides a number of static
  27. * methods to finding standard paths to application data directories, temporary
  28. * file storage locations, and preference files.
  29. *
  30. * <p>PathManager is a singleton class; use {@link #getManager} to get
  31. * instances of this class.</p>
  32. *
  33. * @author <a href="mailto:michael@monkey-robot.com">Michael Dippery</a>
  34. * @author Michael Schoonmaker
  35. * @version 0.1
  36. */
  37. public final class PathManager
  38. {
  39. /** The singleton instance of the class. */
  40. private static PathManager singleton = null;
  41. /** The cached data directory. */
  42. private File dataDirectory;
  43. /** Creates a new <tt>PathManager</tt>. */
  44. private PathManager()
  45. {
  46. dataDirectory = null;
  47. }
  48. /**
  49. * Gets the singleton instance of the path manager.
  50. *
  51. * @return The path manager.
  52. */
  53. public static synchronized PathManager getManager()
  54. {
  55. if (singleton == null) singleton = new PathManager();
  56. return singleton;
  57. }
  58. /**
  59. * Returns a file reference representing a path to a directory in which
  60. * application data, such as cache files, can be stored.
  61. *
  62. * <p>The returned file object can be prepended to a data file, like so:</p>
  63. *
  64. * <pre>
  65. * File dataDir = PathUtils.getDataDirectory();
  66. * File data = new File(dataDir, "data.dat");
  67. * </pre>
  68. *
  69. * @return A path to a directory in which application data can be stored.
  70. */
  71. public File getDataDirectory()
  72. {
  73. if (dataDirectory == null) {
  74. final String OS = System.getProperty("os.name");
  75. final String HOME = System.getProperty("user.home");
  76. final String FSEP = System.getProperty("file.separator");
  77. final String TEST = "test.txt";
  78. File tmpFile = null;
  79. String path = HOME;
  80. boolean createdTestFile = false;
  81. // The goal is to get the path to a part of the file system in which
  82. // application data can be stored. Start first by constructing the
  83. // path to standard locations.
  84. if (OS.equals("Mac OS X")) {
  85. path += FSEP + "Library";
  86. path += FSEP + "Application Support";
  87. path += FSEP + "Spaghetti Nodes";
  88. } else if (OS.startsWith("Windows")) {
  89. path += FSEP + "Application Data";
  90. path += FSEP + "Spaghetti Nodes";
  91. } else {
  92. // Most other platforms are likely just UNIX-like platforms.
  93. // Warning: This isn't true of Mac OS 9 and below, but is anyone
  94. // actually using that platform anymore?
  95. path += FSEP + ".snodes";
  96. }
  97. // We should now have a directory in which to store app data. Check to
  98. // make sure it exists; if it doesn't, create it.
  99. tmpFile = new File(path);
  100. if (!tmpFile.exists()) {
  101. boolean success = tmpFile.mkdir();
  102. if (!success) {
  103. System.err.println("Could not create directory: " + tmpFile);
  104. // Fall back to using the local directory
  105. path = System.getProperty("user.dir");
  106. }
  107. }
  108. // At this point, we should have created a directory to store our
  109. // application data. Let's test if we have read/write access.
  110. tmpFile = new File(path, TEST);
  111. try {
  112. createdTestFile = tmpFile.createNewFile();
  113. } catch (IOException e) {
  114. createdTestFile = false;
  115. }
  116. if (!createdTestFile) {
  117. System.err.println("Cannot write to directory: " + path);
  118. // Fall back to using local directory
  119. path = System.getProperty("user.dir");
  120. } else {
  121. // Delete the temporary test file
  122. tmpFile.delete();
  123. }
  124. dataDirectory = new File(path);
  125. }
  126. return dataDirectory;
  127. }
  128. /**
  129. * Returns the application's plugins directory. Any plugin stored in this
  130. * directory will be loaded at startup.
  131. *
  132. * @return The application plugin directory.
  133. */
  134. public File getPluginDirectory()
  135. {
  136. return new File(getDataDirectory(), "Plugins");
  137. }
  138. }