PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/installer/OperatingSystem.java

#
Java | 353 lines | 278 code | 52 blank | 23 comment | 19 complexity | 916609c3bbe2905209a0d80dcda56057 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 String getExtraClassPath()
  110. {
  111. return "";
  112. }
  113. public class ScriptOSTask extends OSTask
  114. {
  115. public ScriptOSTask(Install installer)
  116. {
  117. super(installer,"unix-script");
  118. }
  119. public String getDefaultDirectory(Install installer)
  120. {
  121. String dir = "/usr/local/";
  122. if(!new File(dir).canWrite())
  123. dir = System.getProperty("user.home");
  124. return new File(dir,"bin").getPath();
  125. }
  126. public void perform(String installDir,
  127. Vector filesets) throws IOException
  128. {
  129. if(!enabled)
  130. return;
  131. mkdirs(directory);
  132. String name = installer.getProperty("app.name");
  133. // create app start script
  134. String script = directory + File.separatorChar
  135. + name.toLowerCase();
  136. // Delete existing copy
  137. new File(script).delete();
  138. // Write simple script
  139. FileWriter out = new FileWriter(script);
  140. out.write("#!/bin/sh\n");
  141. out.write("# Java heap size, in megabytes\n");
  142. out.write("JAVA_HEAP_SIZE=128\n");
  143. out.write("DEFAULT_JAVA_HOME=\""
  144. + System.getProperty("java.home")
  145. + "\"\n");
  146. out.write("if [ \"$JAVA_HOME\" = \"\" ]; then\n");
  147. out.write("JAVA_HOME=\"$DEFAULT_JAVA_HOME\"\n");
  148. out.write("fi\n");
  149. out.write("exec \"$JAVA_HOME"
  150. + "/bin/java\" -server -mx${JAVA_HEAP_SIZE}m ${"
  151. + name.toUpperCase() + "} ");
  152. String jar = installDir + File.separator
  153. + name.toLowerCase() + ".jar";
  154. out.write("-classpath \"" + getExtraClassPath()
  155. + jar + "\" org.gjt.sp.jedit.jEdit $@\n");
  156. out.close();
  157. // Make it executable
  158. String[] chmodArgs = { "chmod", "755", script };
  159. exec(chmodArgs);
  160. }
  161. }
  162. public class ManPageOSTask extends OSTask
  163. {
  164. public ManPageOSTask(Install installer)
  165. {
  166. super(installer,"unix-man");
  167. }
  168. public String getDefaultDirectory(Install installer)
  169. {
  170. String dir = "/usr/local/";
  171. if(!new File(dir).canWrite())
  172. dir = System.getProperty("user.home");
  173. return new File(dir,"man/man1").getPath();
  174. }
  175. public void perform(String installDir,
  176. Vector filesets) throws IOException
  177. {
  178. if(!enabled)
  179. return;
  180. mkdirs(directory);
  181. String name = installer.getProperty("app.name");
  182. // install man page
  183. String manpage = installer.getProperty("ostask.unix-man.manpage");
  184. InputStream in = getClass().getResourceAsStream("/" + manpage);
  185. installer.copy(in,new File(directory,manpage).getPath(),
  186. null);
  187. }
  188. }
  189. public OSTask[] getOSTasks(Install installer)
  190. {
  191. return new OSTask[] { new ScriptOSTask(installer),
  192. new ManPageOSTask(installer) };
  193. }
  194. public void mkdirs(String directory) throws IOException
  195. {
  196. File file = new File(directory);
  197. if(!file.exists())
  198. {
  199. String[] mkdirArgs = { "mkdir", "-m", "755",
  200. "-p", directory };
  201. exec(mkdirArgs);
  202. }
  203. }
  204. public void exec(String[] args) throws IOException
  205. {
  206. Process proc = Runtime.getRuntime().exec(args);
  207. proc.getInputStream().close();
  208. proc.getOutputStream().close();
  209. proc.getErrorStream().close();
  210. try
  211. {
  212. proc.waitFor();
  213. }
  214. catch(InterruptedException ie)
  215. {
  216. }
  217. }
  218. }
  219. public static class MacOS extends Unix
  220. {
  221. public String getInstallDirectory(String name, String version)
  222. {
  223. return "/Applications/" + name + " " + version;
  224. }
  225. public String getExtraClassPath()
  226. {
  227. return "/System/Library/Java/:";
  228. }
  229. }
  230. public static class Windows extends OperatingSystem
  231. {
  232. public String getInstallDirectory(String name, String version)
  233. {
  234. return "C:\\Program Files\\" + name + " " + version;
  235. }
  236. public class JEditLauncherOSTask extends OSTask
  237. {
  238. public JEditLauncherOSTask(Install installer)
  239. {
  240. super(installer,"jedit-launcher");
  241. }
  242. public String getDefaultDirectory(Install installer)
  243. {
  244. return null;
  245. }
  246. public void perform(String installDir,
  247. Vector filesets)
  248. {
  249. if(!enabled
  250. || !filesets.contains("jedit-windows"))
  251. return;
  252. // run jEditLauncher installation
  253. File executable = new File(installDir,"jedit.exe");
  254. if(!executable.exists())
  255. return;
  256. String[] args = { executable.getPath(), "/i",
  257. System.getProperty("java.home")
  258. + File.separator
  259. + "bin" };
  260. try
  261. {
  262. Runtime.getRuntime().exec(args).waitFor();
  263. }
  264. catch(IOException io)
  265. {
  266. }
  267. catch(InterruptedException ie)
  268. {
  269. }
  270. }
  271. }
  272. public OSTask[] getOSTasks(Install installer)
  273. {
  274. return new OSTask[] { /* new JEditLauncherOSTask(installer) */ };
  275. }
  276. }
  277. public static class HalfAnOS extends OperatingSystem
  278. {
  279. public String getInstallDirectory(String name, String version)
  280. {
  281. return "C:\\" + name + " " + version;
  282. }
  283. }
  284. public static class VMS extends OperatingSystem
  285. {
  286. public String getInstallDirectory(String name, String version)
  287. {
  288. return "./" + name.toLowerCase() + "/" + version;
  289. }
  290. }
  291. // private members
  292. private static OperatingSystem os;
  293. }