PageRenderTime 33ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/edu/uncc/parsets/util/osabstraction/AbstractOS.java

https://code.google.com/p/parsets/
Java | 224 lines | 143 code | 26 blank | 55 comment | 29 complexity | 658637b3cdd7d05fa3b7771caa7b3444 MD5 | raw file
  1. package edu.uncc.parsets.util.osabstraction;
  2. import java.awt.FileDialog;
  3. import java.awt.Frame;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileNotFoundException;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.nio.channels.FileChannel;
  11. import javax.swing.JFileChooser;
  12. import com.sun.jna.Platform;
  13. import edu.uncc.parsets.ParallelSets;
  14. import edu.uncc.parsets.data.LocalDB;
  15. import edu.uncc.parsets.gui.CombinedFileNameFilter;
  16. import edu.uncc.parsets.util.PSLogging;
  17. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
  18. * Copyright (c) 2009, Robert Kosara, Caroline Ziemkiewicz,
  19. * and others (see Authors.txt for full list)
  20. * All rights reserved.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions are met:
  24. *
  25. * * Redistributions of source code must retain the above copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * * Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * * Neither the name of UNC Charlotte nor the names of its contributors
  31. * may be used to endorse or promote products derived from this software
  32. * without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY ITS AUTHORS ''AS IS'' AND ANY
  35. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  36. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  37. * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  38. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  39. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  40. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  41. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  43. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  45. public abstract class AbstractOS {
  46. protected static boolean javaStore = false;
  47. static {
  48. javaStore = System.getProperty("parsets.javastore", "false").equalsIgnoreCase("true");
  49. }
  50. private static AbstractOS currentOS;
  51. protected AbstractOS() {
  52. }
  53. /**
  54. * Install the DB when running as a regular application.
  55. * @param dbFile the location where to install the database
  56. */
  57. protected void installRegular(File dbFile) {
  58. }
  59. /**
  60. * Install the DB when running from the JavaStore. The difference is that
  61. * NIO classes can't be used, since the db is read from the jar file.
  62. * @param dbFile the location where to install the database
  63. */
  64. protected void installJavaStore(File dbFile) {
  65. PSLogging.logger.info("Installing new database at " + dbFile.getAbsolutePath());
  66. File parentDir = dbFile.getParentFile();
  67. if (!parentDir.exists())
  68. if (parentDir.mkdir() == false)
  69. PSLogging.logger.fatal("Could not create parent directory");
  70. PSLogging.logger.info("Source file: " + this.getClass().getResource("/"+LocalDB.LOCALDBFILENAME));
  71. InputStream fromStream = null;
  72. FileOutputStream toStream = null;
  73. try {
  74. fromStream = this.getClass().getResourceAsStream("/"+LocalDB.LOCALDBFILENAME);
  75. toStream = new FileOutputStream(dbFile);
  76. byte[] buffer = new byte[4096*4];
  77. int bytesRead;
  78. while ((bytesRead = fromStream.read(buffer)) != -1)
  79. toStream.write(buffer, 0, bytesRead);
  80. } catch (Exception e) {
  81. PSLogging.logger.error("Error installing DB", e);
  82. } finally {
  83. if (fromStream != null) {
  84. try {
  85. fromStream.close();
  86. } catch (IOException e) {
  87. }
  88. }
  89. if (toStream != null) {
  90. try {
  91. toStream.close();
  92. } catch (IOException e) {
  93. }
  94. }
  95. }
  96. }
  97. public String getLocalDBPath(String dbFileName) {
  98. String dbPath = getLocalDBDir()+File.separatorChar+ParallelSets.PROGRAMNAME+File.separatorChar+dbFileName;
  99. File dbFile = new File(dbPath);
  100. if (!dbFile.exists()) {
  101. if (javaStore)
  102. installJavaStore(dbFile);
  103. else
  104. installRegular(dbFile);
  105. }
  106. return dbPath;
  107. }
  108. public abstract String getLocalDBDir();
  109. /**
  110. * Call determineOS before calling this function.
  111. *
  112. * @return The current OS object, or null if not supported.
  113. */
  114. public static AbstractOS getCurrentOS() {
  115. return currentOS;
  116. }
  117. public static void determineOS() {
  118. if (Platform.isMac())
  119. currentOS = new MacOSX();
  120. else if (Platform.isWindows())
  121. currentOS = new Windows();
  122. else if (Platform.isLinux()) // may eventually include more Unices
  123. currentOS = new Linux();
  124. else {
  125. PSLogging.logger.fatal("OS not supported.");
  126. }
  127. }
  128. // Based on code from
  129. // http://stackoverflow.com/questions/106770/standard-concise-way-to-copy-a-file-in-java
  130. public static void copyFileNIO(File sourceFile, File destFile) {
  131. PSLogging.logger.info("Copying "+sourceFile.getAbsolutePath()+" to "+destFile.getAbsolutePath());
  132. if (!destFile.exists()) {
  133. try {
  134. destFile.createNewFile();
  135. } catch (IOException e) {
  136. PSLogging.logger.error("Could not create destination file.", e);
  137. }
  138. }
  139. FileChannel source = null;
  140. FileChannel destination = null;
  141. try {
  142. source = new FileInputStream(sourceFile).getChannel();
  143. destination = new FileOutputStream(destFile).getChannel();
  144. destination.transferFrom(source, 0, source.size());
  145. } catch (FileNotFoundException e) {
  146. PSLogging.logger.error("Source file not found.", e);
  147. } catch (IOException e) {
  148. PSLogging.logger.error("IO Error during copy.", e);
  149. } finally {
  150. if (source != null) {
  151. try {
  152. source.close();
  153. } catch (IOException e) {
  154. PSLogging.logger.error("Error closing source file.", e);
  155. }
  156. }
  157. if (destination != null) {
  158. try {
  159. destination.close();
  160. } catch (IOException e) {
  161. PSLogging.logger.error("Error closing destination file.", e);
  162. }
  163. }
  164. }
  165. }
  166. /**
  167. *
  168. * @param frame the window to block
  169. * @param fileFilter The {@link CombinedFileNameFilter} to use
  170. * @param mode Either {@link FileDialog#LOAD} or {@link FileDialog#SAVE}
  171. * @return
  172. */
  173. public String showDialog(Frame frame, CombinedFileNameFilter fileFilter, int mode) {
  174. // TODO: start in user's home directory
  175. JFileChooser fileChooser = new JFileChooser(new File("."));
  176. if (fileFilter != null)
  177. fileChooser.setFileFilter(fileFilter);
  178. int result = 0;
  179. if (mode == FileDialog.LOAD)
  180. result = fileChooser.showOpenDialog(frame);
  181. else
  182. result = fileChooser.showSaveDialog(frame);
  183. if (result == JFileChooser.APPROVE_OPTION) {
  184. if (fileFilter.accept(fileChooser.getSelectedFile()))
  185. return fileChooser.getSelectedFile().getAbsolutePath();
  186. else
  187. return fileChooser.getSelectedFile().getAbsolutePath()+fileFilter.getExtension();
  188. } else
  189. return null;
  190. }
  191. public abstract String shortName();
  192. /**
  193. * Shortcut, because we need to check in a few places.
  194. */
  195. public boolean isMacOSX() {
  196. return false;
  197. }
  198. }