001: /*
002: Launch4j (http://launch4j.sourceforge.net/)
003: Cross-platform Java application wrapper for creating Windows native executables.
004:
005: Copyright (c) 2004, 2007 Grzegorz Kowal
006:
007: All rights reserved.
008:
009: Redistribution and use in source and binary forms, with or without modification,
010: are permitted provided that the following conditions are met:
011:
012: * Redistributions of source code must retain the above copyright notice,
013: this list of conditions and the following disclaimer.
014: * Redistributions in binary form must reproduce the above copyright notice,
015: this list of conditions and the following disclaimer in the documentation
016: and/or other materials provided with the distribution.
017: * Neither the name of the Launch4j nor the names of its contributors
018: may be used to endorse or promote products derived from this software without
019: specific prior written permission.
020:
021: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
022: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
023: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
024: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
025: CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
026: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
027: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
028: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
029: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
030: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
031: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
032: */
033:
034: /*
035: * Created on 2005-04-24
036: */
037: package net.sf.launch4j;
038:
039: import java.io.BufferedReader;
040: import java.io.File;
041: import java.io.IOException;
042: import java.io.InputStream;
043: import java.io.InputStreamReader;
044: import java.io.OutputStream;
045: import java.io.Reader;
046: import java.io.Writer;
047: import java.util.regex.Matcher;
048: import java.util.regex.Pattern;
049:
050: /**
051: * @author Copyright (C) 2005 Grzegorz Kowal
052: */
053: public class Util {
054: public static final boolean WINDOWS_OS = System.getProperty(
055: "os.name").toLowerCase().startsWith("windows");
056:
057: private Util() {
058: }
059:
060: public static File createTempFile(String suffix) throws IOException {
061: String tmpdir = System.getProperty("launch4j.tmpdir");
062: if (tmpdir != null) {
063: if (tmpdir.indexOf(' ') != -1) {
064: throw new IOException(Messages.getString("Util.tmpdir"));
065: }
066: return File.createTempFile("launch4j", suffix, new File(
067: tmpdir));
068: } else {
069: return File.createTempFile("launch4j", suffix);
070: }
071: }
072:
073: /**
074: * Returns the base directory of a jar file or null if the class is a standalone file.
075: * @return System specific path
076: *
077: * Based on a patch submitted by Josh Elsasser
078: */
079: public static File getJarBasedir() {
080: String url = Util.class.getClassLoader().getResource(
081: Util.class.getName().replace('.', '/') + ".class")
082: .getFile().replaceAll("%20", " ");
083: if (url.startsWith("file:")) {
084: String jar = url.substring(5, url.lastIndexOf('!'));
085: int x = jar.lastIndexOf('/');
086: if (x == -1) {
087: x = jar.lastIndexOf('\\');
088: }
089: String basedir = jar.substring(0, x + 1);
090: return new File(basedir);
091: } else {
092: return new File(".");
093: }
094: }
095:
096: public static File getAbsoluteFile(File basepath, File f) {
097: return f.isAbsolute() ? f : new File(basepath, f.getPath());
098: }
099:
100: public static String getExtension(File f) {
101: String name = f.getName();
102: int x = name.lastIndexOf('.');
103: if (x != -1) {
104: return name.substring(x);
105: } else {
106: return "";
107: }
108: }
109:
110: public static void exec(String[] cmd, Log log) throws ExecException {
111: BufferedReader is = null;
112: try {
113: if (WINDOWS_OS) {
114: for (int i = 0; i < cmd.length; i++) {
115: cmd[i] = cmd[i].replaceAll("/", "\\\\");
116: }
117: }
118: Process p = Runtime.getRuntime().exec(cmd);
119: is = new BufferedReader(new InputStreamReader(p
120: .getErrorStream()));
121: String line;
122: int errLine = -1;
123: Pattern pattern = Pattern.compile(":\\d+:");
124: while ((line = is.readLine()) != null) {
125: log.append(line);
126: Matcher matcher = pattern.matcher(line);
127: if (matcher.find()) {
128: errLine = Integer.valueOf(
129: line.substring(matcher.start() + 1, matcher
130: .end() - 1)).intValue();
131: if (line
132: .matches("(?i).*unrecognized escape sequence")) {
133: log
134: .append(Messages
135: .getString("Util.use.double.backslash"));
136: }
137: break;
138: }
139: }
140: is.close();
141: p.waitFor();
142: if (errLine != -1) {
143: throw new ExecException(Messages
144: .getString("Util.exec.failed")
145: + ": " + cmd, errLine);
146: }
147: if (p.exitValue() != 0) {
148: throw new ExecException(Messages
149: .getString("Util.exec.failed")
150: + "(" + p.exitValue() + "): " + cmd);
151: }
152: } catch (IOException e) {
153: close(is);
154: throw new ExecException(e);
155: } catch (InterruptedException e) {
156: close(is);
157: throw new ExecException(e);
158: }
159: }
160:
161: public static void close(final InputStream o) {
162: if (o != null) {
163: try {
164: o.close();
165: } catch (IOException e) {
166: System.err.println(e); // XXX log
167: }
168: }
169: }
170:
171: public static void close(final OutputStream o) {
172: if (o != null) {
173: try {
174: o.close();
175: } catch (IOException e) {
176: System.err.println(e); // XXX log
177: }
178: }
179: }
180:
181: public static void close(final Reader o) {
182: if (o != null) {
183: try {
184: o.close();
185: } catch (IOException e) {
186: System.err.println(e); // XXX log
187: }
188: }
189: }
190:
191: public static void close(final Writer o) {
192: if (o != null) {
193: try {
194: o.close();
195: } catch (IOException e) {
196: System.err.println(e); // XXX log
197: }
198: }
199: }
200:
201: public static boolean delete(File f) {
202: return (f != null) ? f.delete() : false;
203: }
204: }
|