PageRenderTime 95ms CodeModel.GetById 66ms app.highlight 24ms RepoModel.GetById 1ms app.codeStats 1ms

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

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