KickJava   Java API By Example, From Geeks To Geeks.

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


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.File JavaDoc;
28 import java.io.FileInputStream JavaDoc;
29 import java.io.FileOutputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33
34 import net.sf.launch4j.binding.InvariantViolationException;
35 import net.sf.launch4j.config.Config;
36 import net.sf.launch4j.config.ConfigPersister;
37
38 /**
39  * @author Copyright (C) 2005 Grzegorz Kowal
40  */

41 public class Builder {
42     private final Log _log;
43     private final File JavaDoc _basedir;
44
45     public Builder(Log log) {
46         _log = log;
47         _basedir = Util.getJarBasedir();
48     }
49
50     public Builder(Log log, File JavaDoc basedir) {
51         _log = log;
52         _basedir = basedir;
53     }
54
55     /**
56      * @return Output file path.
57      */

58     public File JavaDoc build() throws BuilderException {
59         final Config c = ConfigPersister.getInstance().getConfig();
60         try {
61             c.validate();
62         } catch (InvariantViolationException e) {
63             throw new BuilderException(e.getMessage());
64         }
65         File JavaDoc rc = null;
66         File JavaDoc ro = null;
67         File JavaDoc outfile = null;
68         FileInputStream JavaDoc is = null;
69         FileOutputStream JavaDoc os = null;
70         final RcBuilder rcb = new RcBuilder();
71         try {
72             rc = rcb.build(c);
73             ro = Util.createTempFile("o");
74             outfile = ConfigPersister.getInstance().getOutputFile();
75
76             Cmd resCmd = new Cmd(_basedir);
77             resCmd.addExe("windres")
78                     .add(Util.WINDOWS_OS ? "--preprocessor=type" : "--preprocessor=cat")
79                     .add("-J rc -O coff -F pe-i386")
80                     .addAbsFile(rc)
81                     .addAbsFile(ro);
82             _log.append(Messages.getString("Builder.compiling.resources"));
83             resCmd.exec(_log);
84
85             Cmd ldCmd = new Cmd(_basedir);
86             ldCmd.addExe("ld")
87                     .add("-mi386pe")
88                     .add("--oformat pei-i386")
89                     .add((c.getHeaderType().equals(Config.GUI_HEADER))
90                             ? "--subsystem windows" : "--subsystem console")
91                     .add("-s") // strip symbols
92
.addFiles(c.getHeaderObjects())
93                     .addAbsFile(ro)
94                     .addFiles(c.getLibs())
95                     .add("-o")
96                     .addAbsFile(outfile);
97             _log.append(Messages.getString("Builder.linking"));
98             ldCmd.exec(_log);
99
100             if (!c.isDontWrapJar()) {
101                 _log.append(Messages.getString("Builder.wrapping"));
102                 int len;
103                 byte[] buffer = new byte[1024];
104                 is = new FileInputStream JavaDoc(Util.getAbsoluteFile(
105                         ConfigPersister.getInstance().getConfigPath(), c.getJar()));
106                 os = new FileOutputStream JavaDoc(outfile, true);
107                 while ((len = is.read(buffer)) > 0) {
108                     os.write(buffer, 0, len);
109                 }
110             }
111             _log.append(Messages.getString("Builder.success") + outfile.getPath());
112             return outfile;
113         } catch (IOException JavaDoc e) {
114             Util.delete(outfile);
115             _log.append(e.getMessage());
116             throw new BuilderException(e);
117         } catch (ExecException e) {
118             Util.delete(outfile);
119             String JavaDoc msg = e.getMessage();
120             if (msg != null && msg.indexOf("windres") != -1) {
121                 if (e.getErrLine() != -1) {
122                     _log.append(Messages.getString("Builder.line.has.errors",
123                             String.valueOf(e.getErrLine())));
124                     _log.append(rcb.getLine(e.getErrLine()));
125                 } else {
126                     _log.append(Messages.getString("Builder.generated.resource.file"));
127                     _log.append(rcb.getContent());
128                 }
129             }
130             throw new BuilderException(e);
131         } finally {
132             Util.close(is);
133             Util.close(os);
134             Util.delete(rc);
135             Util.delete(ro);
136         }
137     }
138 }
139
140 class Cmd {
141     private final StringBuffer JavaDoc _sb = new StringBuffer JavaDoc();
142     private final File JavaDoc _basedir;
143     private final File JavaDoc _bindir;
144     private final boolean _quote;
145
146     public Cmd(File JavaDoc basedir) {
147         _basedir = basedir;
148         _quote = basedir.getPath().indexOf(' ') != -1;
149         String JavaDoc path = System.getProperty("launch4j.bindir");
150         if (path == null) {
151             _bindir = new File JavaDoc(basedir, "bin");
152         } else {
153             File JavaDoc bindir = new File JavaDoc(path);
154             _bindir = bindir.isAbsolute() ? bindir : new File JavaDoc(basedir, path);
155         }
156     }
157
158     public Cmd add(String JavaDoc s) {
159         space();
160         _sb.append(s);
161         return this;
162     }
163
164     public Cmd addAbsFile(File JavaDoc file) {
165         space();
166         boolean quote = file.getPath().indexOf(' ') != -1;
167         if (quote) {
168             _sb.append('"');
169         }
170         _sb.append(file.getPath());
171         if (quote) {
172             _sb.append('"');
173         }
174         return this;
175     }
176
177     public Cmd addFile(String JavaDoc pathname) {
178         space();
179         if (_quote) {
180             _sb.append('"');
181         }
182         _sb.append(new File JavaDoc(_basedir, pathname).getPath());
183         if (_quote) {
184             _sb.append('"');
185         }
186         return this;
187     }
188
189     public Cmd addExe(String JavaDoc pathname) {
190         space();
191         if (_quote) {
192             _sb.append('"');
193         }
194         if (Util.WINDOWS_OS) {
195             pathname += ".exe";
196         }
197         _sb.append(new File JavaDoc(_bindir, pathname).getPath());
198         if (_quote) {
199             _sb.append('"');
200         }
201         return this;
202     }
203
204     public Cmd addFiles(List JavaDoc files) {
205         for (Iterator JavaDoc iter = files.iterator(); iter.hasNext();) {
206             addFile((String JavaDoc) iter.next());
207         }
208         return this;
209     }
210
211     private void space() {
212         if (_sb.length() > 0) {
213             _sb.append(' ');
214         }
215     }
216     
217     public String JavaDoc toString() {
218         return _sb.toString();
219     }
220
221     public void exec(Log log) throws ExecException {
222         Util.exec(_sb.toString(), log);
223     }
224 }
225
Popular Tags