KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > launch4j > Util


1 /*
2     Launch4j (http://launch4j.sourceforge.net/)
3     Cross-platform Java application wrapper for creating Windows native executables.
4
5     Copyright (C) 2004, 2006 Grzegorz Kowal
6
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */

21
22 /*
23  * Created on 2005-04-24
24  */

25 package net.sf.launch4j;
26
27 import java.io.BufferedReader JavaDoc;
28 import java.io.File JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.io.InputStreamReader JavaDoc;
32 import java.io.OutputStream JavaDoc;
33 import java.io.Reader JavaDoc;
34 import java.io.Writer JavaDoc;
35 import java.util.regex.Matcher JavaDoc;
36 import java.util.regex.Pattern JavaDoc;
37
38 /**
39  * @author Copyright (C) 2005 Grzegorz Kowal
40  */

41 public class Util {
42     public static final boolean WINDOWS_OS = System.getProperty("os.name")
43             .toLowerCase().startsWith("windows");
44
45     private Util() {}
46
47     public static File JavaDoc createTempFile(String JavaDoc suffix) throws IOException JavaDoc {
48         String JavaDoc tmpdir = System.getProperty("launch4j.tmpdir");
49         if (tmpdir != null) {
50             if (tmpdir.indexOf(' ') != -1) {
51                 throw new IOException JavaDoc(Messages.getString("Util.tmpdir"));
52             }
53             return File.createTempFile("launch4j", suffix, new File JavaDoc(tmpdir));
54         } else {
55             return File.createTempFile("launch4j", suffix);
56         }
57     }
58
59     /**
60      * Returns the base directory of a jar file or null if the class is a standalone file.
61      * @return System specific path
62      *
63      * Based on a patch submitted by Josh Elsasser
64      */

65     public static File JavaDoc getJarBasedir() {
66         String JavaDoc url = Util.class.getClassLoader()
67                 .getResource(Util.class.getName().replace('.', '/') + ".class")
68                 .getFile()
69                 .replaceAll("%20", " ");
70         if (url.startsWith("file:")) {
71             String JavaDoc jar = url.substring(5, url.lastIndexOf('!'));
72             int x = jar.lastIndexOf('/');
73             if (x == -1) {
74                 x = jar.lastIndexOf('\\');
75             }
76             String JavaDoc basedir = jar.substring(0, x + 1);
77             return new File JavaDoc(basedir);
78         } else {
79             return new File JavaDoc(".");
80         }
81     }
82
83     public static File JavaDoc getAbsoluteFile(File JavaDoc basepath, File JavaDoc f) {
84         return f.isAbsolute() ? f : new File JavaDoc(basepath, f.getPath());
85     }
86
87     public static String JavaDoc getExtension(File JavaDoc f) {
88         String JavaDoc name = f.getName();
89         int x = name.lastIndexOf('.');
90         if (x != -1) {
91             return name.substring(x);
92         } else {
93             return "";
94         }
95     }
96
97     public static void exec(String JavaDoc cmd, Log log) throws ExecException {
98         BufferedReader JavaDoc is = null;
99         try {
100             if (WINDOWS_OS) {
101                 cmd = cmd.replaceAll("/", "\\\\");
102             }
103             Process JavaDoc p = Runtime.getRuntime().exec(cmd);
104             is = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(p.getErrorStream()));
105             String JavaDoc line;
106             int errLine = -1;
107             Pattern JavaDoc pattern = Pattern.compile(":\\d+:");
108             while ((line = is.readLine()) != null) {
109                 log.append(line);
110                 Matcher JavaDoc matcher = pattern.matcher(line);
111                 if (matcher.find()) {
112                     errLine = Integer.valueOf(
113                             line.substring(matcher.start() + 1, matcher.end() - 1))
114                             .intValue();
115                     if (line.matches("(?i).*unrecognized escape sequence")) {
116                         log.append(Messages.getString("Util.use.double.backslash"));
117                     }
118                     break;
119                 }
120             }
121             is.close();
122             p.waitFor();
123             if (errLine != -1) {
124                 throw new ExecException(Messages.getString("Util.exec.failed")
125                         + ": " + cmd, errLine);
126             }
127             if (p.exitValue() != 0) {
128                 throw new ExecException(Messages.getString("Util.exec.failed")
129                         + "(" + p.exitValue() + "): " + cmd);
130             }
131         } catch (IOException JavaDoc e) {
132             close(is);
133             throw new ExecException(e);
134         } catch (InterruptedException JavaDoc e) {
135             close(is);
136             throw new ExecException(e);
137         }
138     }
139
140     public static void close(final InputStream JavaDoc o) {
141         if (o != null) {
142             try {
143                 o.close();
144             } catch (IOException JavaDoc e) {
145                 System.err.println(e); // XXX log
146
}
147         }
148     }
149
150     public static void close(final OutputStream JavaDoc o) {
151         if (o != null) {
152             try {
153                 o.close();
154             } catch (IOException JavaDoc e) {
155                 System.err.println(e); // XXX log
156
}
157         }
158     }
159
160     public static void close(final Reader JavaDoc o) {
161         if (o != null) {
162             try {
163                 o.close();
164             } catch (IOException JavaDoc e) {
165                 System.err.println(e); // XXX log
166
}
167         }
168     }
169
170     public static void close(final Writer JavaDoc o) {
171         if (o != null) {
172             try {
173                 o.close();
174             } catch (IOException JavaDoc e) {
175                 System.err.println(e); // XXX log
176
}
177         }
178     }
179
180     public static boolean delete(File JavaDoc f) {
181         return (f != null) ? f.delete() : false;
182     }
183 }
184
Popular Tags