/jEdit/tags/jedit-4-0-pre9/installer/OperatingSystem.java

# · Java · 190 lines · 135 code · 27 blank · 28 comment · 13 complexity · b64a25e4aace2c4fdcc235d2a359eac4 MD5 · raw file

  1. /*
  2. * OperatingSystem.java
  3. * Copyright (C) 1999, 2000, 2001 Slava Pestov
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. package installer;
  20. import java.io.*;
  21. /*
  22. * Abstracts away operating-specific stuff, like finding out the installation
  23. * directory, creating a shortcut to start to program, and such.
  24. */
  25. public abstract class OperatingSystem
  26. {
  27. public abstract String getInstallDirectory(String name, String version);
  28. public String getShortcutDirectory(String name, String version)
  29. {
  30. return null;
  31. }
  32. public void createScript(Install installer, String installDir,
  33. String binDir, String name) throws IOException {}
  34. public void mkdirs(String directory) throws IOException
  35. {
  36. File file = new File(directory);
  37. if(!file.exists())
  38. file.mkdirs();
  39. }
  40. public static OperatingSystem getOperatingSystem()
  41. {
  42. if(os != null)
  43. return os;
  44. String osName = System.getProperty("os.name");
  45. if(osName.indexOf("Windows") != -1)
  46. os = new Windows();
  47. else if(osName.indexOf("Mac") != -1)
  48. os = new MacOS();
  49. else if(osName.indexOf("OS/2") != -1)
  50. os = new HalfAnOS();
  51. else
  52. os = new Unix();
  53. return os;
  54. }
  55. public static class MacOS extends OperatingSystem
  56. {
  57. public String getInstallDirectory(String name, String version)
  58. {
  59. return "/Applications/" + name + " " + version;
  60. }
  61. }
  62. public static class Unix extends OperatingSystem
  63. {
  64. public String getInstallDirectory(String name, String version)
  65. {
  66. return "/usr/local/share/" + name.toLowerCase()
  67. + "/" + version;
  68. }
  69. public String getShortcutDirectory(String name, String version)
  70. {
  71. return "/usr/local/bin";
  72. }
  73. public void createScript(Install installer,
  74. String installDir, String binDir, String name)
  75. throws IOException
  76. {
  77. // create app start script
  78. String script = binDir + File.separatorChar
  79. + name.toLowerCase();
  80. // Delete existing copy
  81. new File(script).delete();
  82. // Write simple script
  83. FileWriter out = new FileWriter(script);
  84. out.write("#!/bin/sh\n");
  85. out.write("# Java heap size, in megabytes\n");
  86. out.write("JAVA_HEAP_SIZE=32\n");
  87. out.write("exec "
  88. + System.getProperty("java.home")
  89. + "/bin/java -mx${JAVA_HEAP_SIZE}m ${"
  90. + name.toUpperCase() + "} ");
  91. String jar = installDir + File.separator
  92. + name.toLowerCase() + ".jar";
  93. out.write("-jar \"" + jar + "\" $@\n");
  94. out.close();
  95. // Make it executable
  96. String[] chmodArgs = { "chmod", "755", script };
  97. exec(chmodArgs);
  98. }
  99. public void mkdirs(String directory) throws IOException
  100. {
  101. File file = new File(directory);
  102. if(!file.exists())
  103. {
  104. String[] mkdirArgs = { "mkdir", "-m", "755",
  105. "-p", directory };
  106. exec(mkdirArgs);
  107. }
  108. }
  109. public void exec(String[] args) throws IOException
  110. {
  111. Process proc = Runtime.getRuntime().exec(args);
  112. proc.getInputStream().close();
  113. proc.getOutputStream().close();
  114. proc.getErrorStream().close();
  115. try
  116. {
  117. proc.waitFor();
  118. }
  119. catch(InterruptedException ie)
  120. {
  121. }
  122. }
  123. }
  124. public static class Windows extends OperatingSystem
  125. {
  126. public String getInstallDirectory(String name, String version)
  127. {
  128. return "C:\\Program Files\\" + name + " " + version;
  129. }
  130. public void createScript(Install installer,
  131. String installDir, String binDir, String name)
  132. throws IOException
  133. {
  134. // run jEditLauncher installation
  135. File executable = new File(installDir,"jedit.exe");
  136. if(!executable.exists())
  137. return;
  138. String[] args = { executable.getPath(), "/i",
  139. System.getProperty("java.home")
  140. + File.separator
  141. + "bin" };
  142. try
  143. {
  144. Runtime.getRuntime().exec(args).waitFor();
  145. }
  146. catch(IOException io)
  147. {
  148. }
  149. catch(InterruptedException ie)
  150. {
  151. }
  152. }
  153. }
  154. public static class HalfAnOS extends OperatingSystem
  155. {
  156. public String getInstallDirectory(String name, String version)
  157. {
  158. return "C:\\" + name + " " + version;
  159. }
  160. }
  161. // private members
  162. private static OperatingSystem os;
  163. }