/jEdit/tags/jedit-4-1-final/installer/OperatingSystem.java

# · Java · 320 lines · 240 code · 56 blank · 24 comment · 18 complexity · 978acf64c96a8cb10ed2e81db4827ccd MD5 · raw file

  1. /*
  2. * OperatingSystem.java
  3. *
  4. * Originally written by Slava Pestov for the jEdit installer project. This work
  5. * has been placed into the public domain. You may use this work in any way and
  6. * for any purpose you wish.
  7. *
  8. * THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND, NOT EVEN THE
  9. * IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE, ASSUMES
  10. * _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE RESULTING FROM THE USE, MODIFICATION,
  11. * OR REDISTRIBUTION OF THIS SOFTWARE.
  12. */
  13. package installer;
  14. import java.io.*;
  15. /*
  16. * Abstracts away operating-specific stuff, like finding out the installation
  17. * directory, creating a shortcut to start to program, and such.
  18. */
  19. public abstract class OperatingSystem
  20. {
  21. public abstract String getInstallDirectory(String name, String version);
  22. public abstract static class OSTask
  23. {
  24. protected Install installer;
  25. protected String name;
  26. protected String label;
  27. protected String directory;
  28. protected boolean enabled;
  29. public OSTask(Install installer, String name)
  30. {
  31. this.installer = installer;
  32. this.name = name;
  33. this.label = installer.getProperty("ostask." + name + ".label");
  34. this.directory = getDefaultDirectory(installer);
  35. // on by default
  36. enabled = true;
  37. }
  38. public String getName()
  39. {
  40. return name;
  41. }
  42. public String getLabel()
  43. {
  44. return label;
  45. }
  46. public String getDefaultDirectory(Install installer)
  47. {
  48. return null;
  49. }
  50. public String getDirectory()
  51. {
  52. return directory;
  53. }
  54. public boolean isEnabled()
  55. {
  56. return enabled;
  57. }
  58. public void setEnabled(boolean enabled)
  59. {
  60. this.enabled = enabled;
  61. }
  62. public void setDirectory(String directory)
  63. {
  64. this.directory = directory;
  65. }
  66. public abstract void perform(String installDir) throws IOException;
  67. }
  68. public OSTask[] getOSTasks(Install installer)
  69. {
  70. return new OSTask[0];
  71. }
  72. public void mkdirs(String directory) throws IOException
  73. {
  74. File file = new File(directory);
  75. if(!file.exists())
  76. file.mkdirs();
  77. }
  78. public static OperatingSystem getOperatingSystem()
  79. {
  80. if(os != null)
  81. return os;
  82. if(System.getProperty("mrj.version") != null)
  83. os = new MacOS();
  84. else
  85. {
  86. String osName = System.getProperty("os.name");
  87. if(osName.indexOf("Windows") != -1)
  88. os = new Windows();
  89. else if(osName.indexOf("OS/2") != -1)
  90. os = new HalfAnOS();
  91. else
  92. os = new Unix();
  93. }
  94. return os;
  95. }
  96. public static class Unix extends OperatingSystem
  97. {
  98. public String getInstallDirectory(String name, String version)
  99. {
  100. String dir = "/usr/local/share/";
  101. if(!new File(dir).canWrite())
  102. dir = System.getProperty("user.home");
  103. return new File(dir,name.toLowerCase() + "/" + version).getPath();
  104. }
  105. public class ScriptOSTask extends OSTask
  106. {
  107. public ScriptOSTask(Install installer)
  108. {
  109. super(installer,"unix-script");
  110. }
  111. public String getDefaultDirectory(Install installer)
  112. {
  113. String dir = "/usr/local/";
  114. if(!new File(dir).canWrite())
  115. dir = System.getProperty("user.home");
  116. return new File(dir,"bin").getPath();
  117. }
  118. public void perform(String installDir) throws IOException
  119. {
  120. if(!enabled)
  121. return;
  122. mkdirs(directory);
  123. String name = installer.getProperty("app.name");
  124. // create app start script
  125. String script = directory + File.separatorChar
  126. + name.toLowerCase();
  127. // Delete existing copy
  128. new File(script).delete();
  129. // Write simple script
  130. FileWriter out = new FileWriter(script);
  131. out.write("#!/bin/sh\n");
  132. out.write("# Java heap size, in megabytes\n");
  133. out.write("JAVA_HEAP_SIZE=32\n");
  134. out.write("exec "
  135. + System.getProperty("java.home")
  136. + "/bin/java -mx${JAVA_HEAP_SIZE}m ${"
  137. + name.toUpperCase() + "} ");
  138. String jar = installDir + File.separator
  139. + name.toLowerCase() + ".jar";
  140. out.write("-jar \"" + jar + "\" $@\n");
  141. out.close();
  142. // Make it executable
  143. String[] chmodArgs = { "chmod", "755", script };
  144. exec(chmodArgs);
  145. }
  146. }
  147. public class ManPageOSTask extends OSTask
  148. {
  149. public ManPageOSTask(Install installer)
  150. {
  151. super(installer,"unix-man");
  152. }
  153. public String getDefaultDirectory(Install installer)
  154. {
  155. String dir = "/usr/local/";
  156. if(!new File(dir).canWrite())
  157. dir = System.getProperty("user.home");
  158. return new File(dir,"man/man1").getPath();
  159. }
  160. public void perform(String installDir) throws IOException
  161. {
  162. if(!enabled)
  163. return;
  164. mkdirs(directory);
  165. String name = installer.getProperty("app.name");
  166. // install man page
  167. String manpage = installer.getProperty("ostask.unix-man.manpage");
  168. InputStream in = getClass().getResourceAsStream("/" + manpage);
  169. installer.copy(in,new File(directory,manpage).getPath(),
  170. null);
  171. }
  172. }
  173. public OSTask[] getOSTasks(Install installer)
  174. {
  175. return new OSTask[] { new ScriptOSTask(installer),
  176. new ManPageOSTask(installer) };
  177. }
  178. public void mkdirs(String directory) throws IOException
  179. {
  180. File file = new File(directory);
  181. if(!file.exists())
  182. {
  183. String[] mkdirArgs = { "mkdir", "-m", "755",
  184. "-p", directory };
  185. exec(mkdirArgs);
  186. }
  187. }
  188. public void exec(String[] args) throws IOException
  189. {
  190. Process proc = Runtime.getRuntime().exec(args);
  191. proc.getInputStream().close();
  192. proc.getOutputStream().close();
  193. proc.getErrorStream().close();
  194. try
  195. {
  196. proc.waitFor();
  197. }
  198. catch(InterruptedException ie)
  199. {
  200. }
  201. }
  202. }
  203. public static class MacOS extends Unix
  204. {
  205. public String getInstallDirectory(String name, String version)
  206. {
  207. return "/Applications/" + name + " " + version;
  208. }
  209. }
  210. public static class Windows extends OperatingSystem
  211. {
  212. public String getInstallDirectory(String name, String version)
  213. {
  214. return "C:\\Program Files\\" + name + " " + version;
  215. }
  216. public class JEditLauncherOSTask extends OSTask
  217. {
  218. public JEditLauncherOSTask(Install installer)
  219. {
  220. super(installer,"jedit-launcher");
  221. }
  222. public String getDefaultDirectory(Install installer)
  223. {
  224. return null;
  225. }
  226. public void perform(String installDir)
  227. {
  228. if(!enabled)
  229. return;
  230. // run jEditLauncher installation
  231. File executable = new File(installDir,"jedit.exe");
  232. if(!executable.exists())
  233. return;
  234. String[] args = { executable.getPath(), "/i",
  235. System.getProperty("java.home")
  236. + File.separator
  237. + "bin" };
  238. try
  239. {
  240. Runtime.getRuntime().exec(args).waitFor();
  241. }
  242. catch(IOException io)
  243. {
  244. }
  245. catch(InterruptedException ie)
  246. {
  247. }
  248. }
  249. }
  250. public OSTask[] getOSTasks(Install installer)
  251. {
  252. return new OSTask[] { new JEditLauncherOSTask(installer) };
  253. }
  254. }
  255. public static class HalfAnOS extends OperatingSystem
  256. {
  257. public String getInstallDirectory(String name, String version)
  258. {
  259. return "C:\\" + name + " " + version;
  260. }
  261. }
  262. // private members
  263. private static OperatingSystem os;
  264. }