001: /*
002: * OperatingSystem.java
003: *
004: * Originally written by Slava Pestov for the jEdit installer project. This work
005: * has been placed into the public domain. You may use this work in any way and
006: * for any purpose you wish.
007: *
008: * THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND, NOT EVEN THE
009: * IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE, ASSUMES
010: * _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE RESULTING FROM THE USE, MODIFICATION,
011: * OR REDISTRIBUTION OF THIS SOFTWARE.
012: */
013:
014: package installer;
015:
016: import java.io.*;
017: import java.util.Vector;
018:
019: /*
020: * Abstracts away operating-specific stuff, like finding out the installation
021: * directory, creating a shortcut to start to program, and such.
022: */
023: public abstract class OperatingSystem {
024: public abstract String getInstallDirectory(String name,
025: String version);
026:
027: public abstract static class OSTask {
028: protected Install installer;
029: protected String name;
030: protected String label;
031: protected String directory;
032: protected boolean enabled;
033:
034: public OSTask(Install installer, String name) {
035: this .installer = installer;
036: this .name = name;
037: this .label = installer.getProperty("ostask." + name
038: + ".label");
039: this .directory = getDefaultDirectory(installer);
040:
041: // on by default
042: enabled = true;
043: }
044:
045: public String getName() {
046: return name;
047: }
048:
049: public String getLabel() {
050: return label;
051: }
052:
053: public String getDefaultDirectory(Install installer) {
054: return null;
055: }
056:
057: public String getDirectory() {
058: return directory;
059: }
060:
061: public boolean isEnabled() {
062: return enabled;
063: }
064:
065: public void setEnabled(boolean enabled) {
066: this .enabled = enabled;
067: }
068:
069: public void setDirectory(String directory) {
070: this .directory = directory;
071: }
072:
073: public abstract void perform(String installDir, Vector filesets)
074: throws IOException;
075: }
076:
077: public OSTask[] getOSTasks(Install installer) {
078: return new OSTask[0];
079: }
080:
081: public void mkdirs(String directory) throws IOException {
082: File file = new File(directory);
083: if (!file.exists())
084: file.mkdirs();
085: }
086:
087: public static OperatingSystem getOperatingSystem() {
088: if (os != null)
089: return os;
090:
091: if (System.getProperty("mrj.version") != null) {
092: os = new MacOS();
093: } else {
094: String osName = System.getProperty("os.name");
095: if (osName.indexOf("Windows") != -1)
096: os = new Windows();
097: else if (osName.indexOf("OS/2") != -1)
098: os = new HalfAnOS();
099: else if (osName.indexOf("VMS") != -1)
100: os = new VMS();
101: else
102: os = new Unix();
103: }
104:
105: return os;
106: }
107:
108: public static class Unix extends OperatingSystem {
109: public String getInstallDirectory(String name, String version) {
110: String dir = "/usr/local/share/";
111: if (!new File(dir).canWrite()) {
112: dir = System.getProperty("user.home");
113: }
114:
115: return new File(dir, name.toLowerCase() + "/" + version)
116: .getPath();
117: }
118:
119: public String getExtraClassPath() {
120: return "";
121: }
122:
123: public class ScriptOSTask extends OSTask {
124: public ScriptOSTask(Install installer) {
125: super (installer, "unix-script");
126: }
127:
128: public String getDefaultDirectory(Install installer) {
129: String dir = "/usr/local/";
130: if (!new File(dir).canWrite()) {
131: dir = System.getProperty("user.home");
132: }
133:
134: return new File(dir, "bin").getPath();
135: }
136:
137: public void perform(String installDir, Vector filesets)
138: throws IOException {
139: if (!enabled) {
140: return;
141: }
142:
143: mkdirs(directory);
144:
145: String name = installer.getProperty("app.name");
146:
147: // create app start script
148: String script = directory + File.separatorChar
149: + name.toLowerCase();
150:
151: // Delete existing copy
152: new File(script).delete();
153:
154: // Write simple script
155: FileWriter out = new FileWriter(script);
156: out.write("#!/bin/sh\n");
157: out.write("#\n");
158: out.write("# Runs jEdit - Programmer's Text Editor.\n");
159: out.write("#\n");
160: out.write("\n");
161: out
162: .write("# Set jvm heap initial and maximum sizes (in megabytes).\n");
163: out.write("JAVA_HEAP_INIT_SIZE=64\n");
164: out.write("JAVA_HEAP_MAX_SIZE=192\n");
165: out.write("\n");
166: out.write("DEFAULT_JAVA_HOME=\""
167: + System.getProperty("java.home") + "\"\n");
168: out.write("if [ -z \"$JAVA_HOME\" ]; then\n");
169: out.write("\tJAVA_HOME=\"$DEFAULT_JAVA_HOME\"\n");
170: out.write("fi\n");
171: out.write("\n");
172: out.write("# Launch application.\n");
173: out.write("\n");
174: out.write("while [ $# -gt 9 ]; do\n");
175: String jar = installDir + File.separator
176: + name.toLowerCase() + ".jar";
177: out.write("\texec \"$JAVA_HOME/bin/java\""
178: + " -Xms${JAVA_HEAP_INIT_SIZE}M"
179: + " -Xmx${JAVA_HEAP_MAX_SIZE}M -jar " + jar
180: + " -reuseview \"$1\" \"$2\""
181: + " \"$3\" \"$4\" \"$5\" \"$6\""
182: + " \"$7\" \"$8\" \"$9\"\n");
183: out.write("\tshift 9\n");
184: out.write("done\n");
185: out.write("exec \"$JAVA_HOME/bin/java\""
186: + " -Xms${JAVA_HEAP_INIT_SIZE}M"
187: + " -Xmx${JAVA_HEAP_MAX_SIZE}M -jar " + jar
188: + " -reuseview \"$1\" \"$2\""
189: + " \"$3\" \"$4\" \"$5\" \"$6\""
190: + " \"$7\" \"$8\" \"$9\"\n");
191: out.close();
192:
193: // Make it executable
194: String[] chmodArgs = { "chmod", "755", script };
195: exec(chmodArgs);
196: }
197: }
198:
199: public class ManPageOSTask extends OSTask {
200: public ManPageOSTask(Install installer) {
201: super (installer, "unix-man");
202: }
203:
204: public String getDefaultDirectory(Install installer) {
205: String dir = "/usr/local/";
206: if (!new File(dir).canWrite())
207: dir = System.getProperty("user.home");
208:
209: return new File(dir, "man/man1").getPath();
210: }
211:
212: public void perform(String installDir, Vector filesets)
213: throws IOException {
214: if (!enabled)
215: return;
216:
217: mkdirs(directory);
218:
219: String name = installer.getProperty("app.name");
220:
221: // install man page
222: String manpage = installer
223: .getProperty("ostask.unix-man.manpage");
224:
225: InputStream in = getClass().getResourceAsStream(
226: "/" + manpage);
227: installer.copy(in, new File(directory, manpage)
228: .getPath(), null);
229: }
230: }
231:
232: public OSTask[] getOSTasks(Install installer) {
233: return new OSTask[] { new ScriptOSTask(installer),
234: new ManPageOSTask(installer) };
235: }
236:
237: public void mkdirs(String directory) throws IOException {
238: File file = new File(directory);
239: if (!file.exists()) {
240: String[] mkdirArgs = { "mkdir", "-m", "755", "-p",
241: directory };
242: exec(mkdirArgs);
243: }
244: }
245:
246: public void exec(String[] args) throws IOException {
247: Process proc = Runtime.getRuntime().exec(args);
248: proc.getInputStream().close();
249: proc.getOutputStream().close();
250: proc.getErrorStream().close();
251: try {
252: proc.waitFor();
253: } catch (InterruptedException ie) {
254: }
255: }
256: }
257:
258: public static class MacOS extends Unix {
259: public String getInstallDirectory(String name, String version) {
260: return "/Applications/" + name + " " + version;
261: }
262:
263: public String getExtraClassPath() {
264: return "/System/Library/Java/:";
265: }
266: }
267:
268: public static class Windows extends OperatingSystem {
269: public String getInstallDirectory(String name, String version) {
270: String programDir = System.getenv("ProgramFiles");
271: // Here is a workaround for the case that the environment
272: // variable is not defined. Windows 98 and ME are known as
273: // such environments. This makes sense while jEdit supports
274: // JRE 5. JRE 6 doesn't support Windows 98 and ME.
275: if (programDir == null) {
276: // This is a hint for what is needed here.
277: programDir = "%ProgramFiles%";
278: }
279: return programDir + "\\" + name + " " + version;
280: }
281:
282: public class JEditLauncherOSTask extends OSTask {
283: public JEditLauncherOSTask(Install installer) {
284: super (installer, "jedit-launcher");
285: }
286:
287: public String getDefaultDirectory(Install installer) {
288: return null;
289: }
290:
291: public void perform(String installDir, Vector filesets) {
292: if (!enabled || !filesets.contains("jedit-windows"))
293: return;
294:
295: // run jEditLauncher installation
296: File executable = new File(installDir, "jedit.exe");
297: if (!executable.exists())
298: return;
299:
300: String[] args = {
301: executable.getPath(),
302: "/i",
303: System.getProperty("java.home")
304: + File.separator + "bin" };
305:
306: try {
307: Runtime.getRuntime().exec(args).waitFor();
308: } catch (IOException io) {
309: } catch (InterruptedException ie) {
310: }
311: }
312: }
313:
314: public OSTask[] getOSTasks(Install installer) {
315: return new OSTask[] { /* new JEditLauncherOSTask(installer) */};
316: }
317: }
318:
319: public static class HalfAnOS extends OperatingSystem {
320: public String getInstallDirectory(String name, String version) {
321: return "C:\\" + name + " " + version;
322: }
323: }
324:
325: public static class VMS extends OperatingSystem {
326: public String getInstallDirectory(String name, String version) {
327: return "./" + name.toLowerCase() + "/" + version;
328: }
329: }
330:
331: // private members
332: private static OperatingSystem os;
333: }
|