KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > installer > OperatingSystem


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
14 package installer;
15
16 import java.io.*;
17 import java.util.Vector JavaDoc;
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  */

23 public abstract class OperatingSystem
24 {
25     public abstract String JavaDoc getInstallDirectory(String JavaDoc name, String JavaDoc version);
26
27     public abstract static class OSTask
28     {
29         protected Install installer;
30         protected String JavaDoc name;
31         protected String JavaDoc label;
32         protected String JavaDoc directory;
33         protected boolean enabled;
34
35         public OSTask(Install installer, String JavaDoc 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 JavaDoc getName()
47         {
48             return name;
49         }
50
51         public String JavaDoc getLabel()
52         {
53             return label;
54         }
55
56         public String JavaDoc getDefaultDirectory(Install installer)
57         {
58             return null;
59         }
60
61         public String JavaDoc 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 JavaDoc directory)
77         {
78             this.directory = directory;
79         }
80
81         public abstract void perform(String JavaDoc installDir,
82             Vector JavaDoc filesets) throws IOException;
83     }
84
85     public OSTask[] getOSTasks(Install installer)
86     {
87         return new OSTask[0];
88     }
89
90     public void mkdirs(String JavaDoc 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 JavaDoc 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 JavaDoc getInstallDirectory(String JavaDoc name, String JavaDoc version)
123         {
124             String JavaDoc 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 String JavaDoc getExtraClassPath()
132         {
133             return "";
134         }
135
136         public class ScriptOSTask extends OSTask
137         {
138             public ScriptOSTask(Install installer)
139             {
140                 super(installer,"unix-script");
141             }
142
143             public String JavaDoc getDefaultDirectory(Install installer)
144             {
145                 String JavaDoc dir = "/usr/local/";
146                 if(!new File(dir).canWrite())
147                     dir = System.getProperty("user.home");
148
149                 return new File(dir,"bin").getPath();
150             }
151
152             public void perform(String JavaDoc installDir,
153                 Vector JavaDoc filesets) throws IOException
154             {
155                 if(!enabled)
156                     return;
157
158                 mkdirs(directory);
159
160                 String JavaDoc name = installer.getProperty("app.name");
161
162                 // create app start script
163
String JavaDoc script = directory + File.separatorChar
164                     + name.toLowerCase();
165
166                 // Delete existing copy
167
new File(script).delete();
168
169                 // Write simple script
170
FileWriter out = new FileWriter(script);
171                 out.write("#!/bin/sh\n");
172                 out.write("# Java heap size, in megabytes\n");
173                 out.write("JAVA_HEAP_SIZE=192\n");
174                 out.write("DEFAULT_JAVA_HOME=\""
175                     + System.getProperty("java.home")
176                     + "\"\n");
177                 out.write("if [ \"$JAVA_HOME\" = \"\" ]; then\n");
178                 out.write("JAVA_HOME=\"$DEFAULT_JAVA_HOME\"\n");
179                 out.write("fi\n");
180
181                 out.write("exec \"$JAVA_HOME"
182                     + "/bin/java\" -server -mx${JAVA_HEAP_SIZE}m ${"
183                     + name.toUpperCase() + "} ");
184
185                 String JavaDoc jar = installDir + File.separator
186                     + name.toLowerCase() + ".jar";
187
188                 out.write("-classpath \"" + getExtraClassPath()
189                     + jar + "\" org.gjt.sp.jedit.jEdit -reuseview $@\n");
190
191                 out.close();
192
193                 // Make it executable
194
String JavaDoc[] chmodArgs = { "chmod", "755", script };
195                 exec(chmodArgs);
196             }
197         }
198
199         public class ManPageOSTask extends OSTask
200         {
201             public ManPageOSTask(Install installer)
202             {
203                 super(installer,"unix-man");
204             }
205
206             public String JavaDoc getDefaultDirectory(Install installer)
207             {
208                 String JavaDoc dir = "/usr/local/";
209                 if(!new File(dir).canWrite())
210                     dir = System.getProperty("user.home");
211
212                 return new File(dir,"man/man1").getPath();
213             }
214
215             public void perform(String JavaDoc installDir,
216                 Vector JavaDoc filesets) throws IOException
217             {
218                 if(!enabled)
219                     return;
220
221                 mkdirs(directory);
222
223                 String JavaDoc name = installer.getProperty("app.name");
224
225                 // install man page
226
String JavaDoc manpage = installer.getProperty("ostask.unix-man.manpage");
227
228                 InputStream in = getClass().getResourceAsStream("/" + manpage);
229                 installer.copy(in,new File(directory,manpage).getPath(),
230                     null);
231             }
232         }
233
234         public OSTask[] getOSTasks(Install installer)
235         {
236             return new OSTask[] { new ScriptOSTask(installer),
237                 new ManPageOSTask(installer) };
238         }
239
240         public void mkdirs(String JavaDoc directory) throws IOException
241         {
242             File file = new File(directory);
243             if(!file.exists())
244             {
245                 String JavaDoc[] mkdirArgs = { "mkdir", "-m", "755",
246                     "-p", directory };
247                 exec(mkdirArgs);
248             }
249         }
250
251         public void exec(String JavaDoc[] args) throws IOException
252         {
253             Process JavaDoc proc = Runtime.getRuntime().exec(args);
254             proc.getInputStream().close();
255             proc.getOutputStream().close();
256             proc.getErrorStream().close();
257             try
258             {
259                 proc.waitFor();
260             }
261             catch(InterruptedException JavaDoc ie)
262             {
263             }
264         }
265     }
266
267     public static class MacOS extends Unix
268     {
269         public String JavaDoc getInstallDirectory(String JavaDoc name, String JavaDoc version)
270         {
271             return "/Applications/" + name + " " + version;
272         }
273
274         public String JavaDoc getExtraClassPath()
275         {
276             return "/System/Library/Java/:";
277         }
278     }
279
280     public static class Windows extends OperatingSystem
281     {
282         public String JavaDoc getInstallDirectory(String JavaDoc name, String JavaDoc version)
283         {
284             return "C:\\Program Files\\" + name + " " + version;
285         }
286
287         public class JEditLauncherOSTask extends OSTask
288         {
289             public JEditLauncherOSTask(Install installer)
290             {
291                 super(installer,"jedit-launcher");
292             }
293
294             public String JavaDoc getDefaultDirectory(Install installer)
295             {
296                 return null;
297             }
298
299             public void perform(String JavaDoc installDir,
300                 Vector JavaDoc filesets)
301             {
302                 if(!enabled
303                     || !filesets.contains("jedit-windows"))
304                     return;
305
306                 // run jEditLauncher installation
307
File executable = new File(installDir,"jedit.exe");
308                 if(!executable.exists())
309                     return;
310
311                 String JavaDoc[] args = { executable.getPath(), "/i",
312                     System.getProperty("java.home")
313                     + File.separator
314                     + "bin" };
315
316                 try
317                 {
318                     Runtime.getRuntime().exec(args).waitFor();
319                 }
320                 catch(IOException io)
321                 {
322                 }
323                 catch(InterruptedException JavaDoc ie)
324                 {
325                 }
326             }
327         }
328
329         public OSTask[] getOSTasks(Install installer)
330         {
331             return new OSTask[] { /* new JEditLauncherOSTask(installer) */ };
332         }
333     }
334
335     public static class HalfAnOS extends OperatingSystem
336     {
337         public String JavaDoc getInstallDirectory(String JavaDoc name, String JavaDoc version)
338         {
339             return "C:\\" + name + " " + version;
340         }
341     }
342
343     public static class VMS extends OperatingSystem
344     {
345         public String JavaDoc getInstallDirectory(String JavaDoc name, String JavaDoc version)
346         {
347             return "./" + name.toLowerCase() + "/" + version;
348         }
349     }
350
351     // private members
352
private static OperatingSystem os;
353 }
354
Popular Tags