PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/installer/OperatingSystem.java

#
Java | 342 lines | 269 code | 50 blank | 23 comment | 19 complexity | 3aba41371c5a454cde66c5c51862bb68 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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. import java.util.Vector;
  16. /*
  17. * Abstracts away operating-specific stuff, like finding out the installation
  18. * directory, creating a shortcut to start to program, and such.
  19. */
  20. public abstract class OperatingSystem
  21. {
  22. public abstract String getInstallDirectory(String name, String version);
  23. public abstract static class OSTask
  24. {
  25. protected Install installer;
  26. protected String name;
  27. protected String label;
  28. protected String directory;
  29. protected boolean enabled;
  30. public OSTask(Install installer, String name)
  31. {
  32. this.installer = installer;
  33. this.name = name;
  34. this.label = installer.getProperty("ostask." + name + ".label");
  35. this.directory = getDefaultDirectory(installer);
  36. // on by default
  37. enabled = true;
  38. }
  39. public String getName()
  40. {
  41. return name;
  42. }
  43. public String getLabel()
  44. {
  45. return label;
  46. }
  47. public String getDefaultDirectory(Install installer)
  48. {
  49. return null;
  50. }
  51. public String getDirectory()
  52. {
  53. return directory;
  54. }
  55. public boolean isEnabled()
  56. {
  57. return enabled;
  58. }
  59. public void setEnabled(boolean enabled)
  60. {
  61. this.enabled = enabled;
  62. }
  63. public void setDirectory(String directory)
  64. {
  65. this.directory = directory;
  66. }
  67. public abstract void perform(String installDir,
  68. Vector filesets) throws IOException;
  69. }
  70. public OSTask[] getOSTasks(Install installer)
  71. {
  72. return new OSTask[0];
  73. }
  74. public void mkdirs(String directory) throws IOException
  75. {
  76. File file = new File(directory);
  77. if(!file.exists())
  78. file.mkdirs();
  79. }
  80. public static OperatingSystem getOperatingSystem()
  81. {
  82. if(os != null)
  83. return os;
  84. if(System.getProperty("mrj.version") != null)
  85. os = new MacOS();
  86. else
  87. {
  88. String osName = System.getProperty("os.name");
  89. if(osName.indexOf("Windows") != -1)
  90. os = new Windows();
  91. else if(osName.indexOf("OS/2") != -1)
  92. os = new HalfAnOS();
  93. else if(osName.indexOf("VMS") != -1)
  94. os = new VMS();
  95. else
  96. os = new Unix();
  97. }
  98. return os;
  99. }
  100. public static class Unix extends OperatingSystem
  101. {
  102. public String getInstallDirectory(String name, String version)
  103. {
  104. String dir = "/usr/local/share/";
  105. if(!new File(dir).canWrite())
  106. dir = System.getProperty("user.home");
  107. return new File(dir,name.toLowerCase() + "/" + version).getPath();
  108. }
  109. public class ScriptOSTask extends OSTask
  110. {
  111. public ScriptOSTask(Install installer)
  112. {
  113. super(installer,"unix-script");
  114. }
  115. public String getDefaultDirectory(Install installer)
  116. {
  117. String dir = "/usr/local/";
  118. if(!new File(dir).canWrite())
  119. dir = System.getProperty("user.home");
  120. return new File(dir,"bin").getPath();
  121. }
  122. public void perform(String installDir,
  123. Vector filesets) throws IOException
  124. {
  125. if(!enabled)
  126. return;
  127. mkdirs(directory);
  128. String name = installer.getProperty("app.name");
  129. // create app start script
  130. String script = directory + File.separatorChar
  131. + name.toLowerCase();
  132. // Delete existing copy
  133. new File(script).delete();
  134. // Write simple script
  135. FileWriter out = new FileWriter(script);
  136. out.write("#!/bin/sh\n");
  137. out.write("# Java heap size, in megabytes\n");
  138. out.write("JAVA_HEAP_SIZE=32\n");
  139. out.write("DEFAULT_JAVA_HOME=\""
  140. + System.getProperty("java.home")
  141. + "\"\n");
  142. out.write("if [ \"$JAVA_HOME\" = \"\" ]; then\n");
  143. out.write("JAVA_HOME=\"$DEFAULT_JAVA_HOME\"\n");
  144. out.write("fi\n");
  145. out.write("exec \"$JAVA_HOME"
  146. + "/bin/java\" -mx${JAVA_HEAP_SIZE}m ${"
  147. + name.toUpperCase() + "} ");
  148. String jar = installDir + File.separator
  149. + name.toLowerCase() + ".jar";
  150. out.write("-jar \"" + jar + "\" $@\n");
  151. out.close();
  152. // Make it executable
  153. String[] chmodArgs = { "chmod", "755", script };
  154. exec(chmodArgs);
  155. }
  156. }
  157. public class ManPageOSTask extends OSTask
  158. {
  159. public ManPageOSTask(Install installer)
  160. {
  161. super(installer,"unix-man");
  162. }
  163. public String getDefaultDirectory(Install installer)
  164. {
  165. String dir = "/usr/local/";
  166. if(!new File(dir).canWrite())
  167. dir = System.getProperty("user.home");
  168. return new File(dir,"man/man1").getPath();
  169. }
  170. public void perform(String installDir,
  171. Vector filesets) throws IOException
  172. {
  173. if(!enabled)
  174. return;
  175. mkdirs(directory);
  176. String name = installer.getProperty("app.name");
  177. // install man page
  178. String manpage = installer.getProperty("ostask.unix-man.manpage");
  179. InputStream in = getClass().getResourceAsStream("/" + manpage);
  180. installer.copy(in,new File(directory,manpage).getPath(),
  181. null);
  182. }
  183. }
  184. public OSTask[] getOSTasks(Install installer)
  185. {
  186. return new OSTask[] { new ScriptOSTask(installer),
  187. new ManPageOSTask(installer) };
  188. }
  189. public void mkdirs(String directory) throws IOException
  190. {
  191. File file = new File(directory);
  192. if(!file.exists())
  193. {
  194. String[] mkdirArgs = { "mkdir", "-m", "755",
  195. "-p", directory };
  196. exec(mkdirArgs);
  197. }
  198. }
  199. public void exec(String[] args) throws IOException
  200. {
  201. Process proc = Runtime.getRuntime().exec(args);
  202. proc.getInputStream().close();
  203. proc.getOutputStream().close();
  204. proc.getErrorStream().close();
  205. try
  206. {
  207. proc.waitFor();
  208. }
  209. catch(InterruptedException ie)
  210. {
  211. }
  212. }
  213. }
  214. public static class MacOS extends Unix
  215. {
  216. public String getInstallDirectory(String name, String version)
  217. {
  218. return "/Applications/" + name + " " + version;
  219. }
  220. }
  221. public static class Windows extends OperatingSystem
  222. {
  223. public String getInstallDirectory(String name, String version)
  224. {
  225. return "C:\\Program Files\\" + name + " " + version;
  226. }
  227. public class JEditLauncherOSTask extends OSTask
  228. {
  229. public JEditLauncherOSTask(Install installer)
  230. {
  231. super(installer,"jedit-launcher");
  232. }
  233. public String getDefaultDirectory(Install installer)
  234. {
  235. return null;
  236. }
  237. public void perform(String installDir,
  238. Vector filesets)
  239. {
  240. if(!enabled
  241. || !filesets.contains("jedit-windows"))
  242. return;
  243. // run jEditLauncher installation
  244. File executable = new File(installDir,"jedit.exe");
  245. if(!executable.exists())
  246. return;
  247. String[] args = { executable.getPath(), "/i",
  248. System.getProperty("java.home")
  249. + File.separator
  250. + "bin" };
  251. try
  252. {
  253. Runtime.getRuntime().exec(args).waitFor();
  254. }
  255. catch(IOException io)
  256. {
  257. }
  258. catch(InterruptedException ie)
  259. {
  260. }
  261. }
  262. }
  263. public OSTask[] getOSTasks(Install installer)
  264. {
  265. return new OSTask[] { new JEditLauncherOSTask(installer) };
  266. }
  267. }
  268. public static class HalfAnOS extends OperatingSystem
  269. {
  270. public String getInstallDirectory(String name, String version)
  271. {
  272. return "C:\\" + name + " " + version;
  273. }
  274. }
  275. public static class VMS extends OperatingSystem
  276. {
  277. public String getInstallDirectory(String name, String version)
  278. {
  279. return "./" + name.toLowerCase() + "/" + version;
  280. }
  281. }
  282. // private members
  283. private static OperatingSystem os;
  284. }