KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > launch4j > config > ConfigPersister


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 Apr 22, 2005
24  */

25 package net.sf.launch4j.config;
26
27 import java.io.BufferedWriter JavaDoc;
28 import java.io.File JavaDoc;
29 import java.io.FileInputStream JavaDoc;
30 import java.io.FileReader JavaDoc;
31 import java.io.FileWriter JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Properties JavaDoc;
36
37 import net.sf.launch4j.Util;
38
39 import com.thoughtworks.xstream.XStream;
40 import com.thoughtworks.xstream.io.xml.DomDriver;
41
42 /**
43  * @author Copyright (C) 2005 Grzegorz Kowal
44  */

45 public class ConfigPersister {
46
47     private static final ConfigPersister _instance = new ConfigPersister();
48
49     private final XStream _xstream;
50     private Config _config;
51     private File JavaDoc _configPath;
52
53     private ConfigPersister() {
54         _xstream = new XStream(new DomDriver());
55         _xstream.alias("launch4jConfig", Config.class);
56         _xstream.alias("classPath", ClassPath.class);
57         _xstream.alias("jre", Jre.class);
58         _xstream.alias("splash", Splash.class);
59         _xstream.alias("versionInfo", VersionInfo.class);
60
61         _xstream.addImplicitCollection(Config.class, "headerObjects", "obj",
62                 String JavaDoc.class);
63         _xstream.addImplicitCollection(Config.class, "libs", "lib", String JavaDoc.class);
64         _xstream.addImplicitCollection(Config.class, "variables", "var", String JavaDoc.class);
65         _xstream.addImplicitCollection(ClassPath.class, "paths", "cp", String JavaDoc.class);
66         _xstream.addImplicitCollection(Jre.class, "options", "opt", String JavaDoc.class);
67     }
68
69     public static ConfigPersister getInstance() {
70         return _instance;
71     }
72     
73     public Config getConfig() {
74         return _config;
75     }
76
77     public File JavaDoc getConfigPath() {
78         return _configPath;
79     }
80     
81     public File JavaDoc getOutputPath() throws IOException JavaDoc {
82         if (_config.getOutfile().isAbsolute()) {
83             return _config.getOutfile().getParentFile();
84         }
85         File JavaDoc parent = _config.getOutfile().getParentFile();
86         return (parent != null) ? new File JavaDoc(_configPath, parent.getPath()) : _configPath;
87     }
88     
89     public File JavaDoc getOutputFile() throws IOException JavaDoc {
90         return _config.getOutfile().isAbsolute()
91             ? _config.getOutfile()
92             : new File JavaDoc(getOutputPath(), _config.getOutfile().getName());
93     }
94
95     public void createBlank() {
96         _config = new Config();
97         _config.setJre(new Jre());
98         _configPath = null;
99     }
100
101     public void setAntConfig(Config c, File JavaDoc basedir) {
102         _config = c;
103         _configPath = basedir;
104     }
105
106     public void load(File JavaDoc f) throws ConfigPersisterException {
107         try {
108             FileReader JavaDoc r = new FileReader JavaDoc(f);
109             char[] buf = new char[(int) f.length()];
110             r.read(buf);
111             r.close();
112             // Convert 2.x config to 3.x
113
String JavaDoc s = String.valueOf(buf)
114                     .replaceAll("<headerType>0<", "<headerType>gui<")
115                     .replaceAll("<headerType>1<", "<headerType>console<")
116                     .replaceAll("jarArgs>", "cmdLine>")
117                     .replaceAll("<jarArgs[ ]*/>", "<cmdLine/>")
118                     .replaceAll("args>", "opt>")
119                     .replaceAll("<args[ ]*/>", "<opt/>");
120             _config = (Config) _xstream.fromXML(s);
121             setConfigPath(f);
122         } catch (Exception JavaDoc e) {
123             throw new ConfigPersisterException(e);
124         }
125     }
126
127     /**
128      * Imports launch4j 1.x.x config file.
129      */

130     public void loadVersion1(File JavaDoc f) throws ConfigPersisterException {
131         try {
132             Props props = new Props(f);
133             _config = new Config();
134             String JavaDoc header = props.getProperty(Config.HEADER);
135             _config.setHeaderType(header == null
136                     || header.toLowerCase().equals("guihead.bin") ? Config.GUI_HEADER
137                                                             : Config.CONSOLE_HEADER);
138             _config.setJar(props.getFile(Config.JAR));
139             _config.setOutfile(props.getFile(Config.OUTFILE));
140             _config.setJre(new Jre());
141             _config.getJre().setPath(props.getProperty(Jre.PATH));
142             _config.getJre().setMinVersion(props.getProperty(Jre.MIN_VERSION));
143             _config.getJre().setMaxVersion(props.getProperty(Jre.MAX_VERSION));
144             List JavaDoc args = new ArrayList JavaDoc();
145             args.add(props.getProperty(Jre.ARGS));
146             _config.getJre().setOptions(args);
147             _config.setCmdLine(props.getProperty(Config.JAR_ARGS));
148             _config.setChdir("true".equals(props.getProperty(Config.CHDIR))
149                     ? "." : null);
150             _config.setCustomProcName("true".equals(
151                     props.getProperty("setProcName"))); // 1.x
152
_config.setStayAlive("true".equals(props.getProperty(Config.STAY_ALIVE)));
153             _config.setErrTitle(props.getProperty(Config.ERR_TITLE));
154             _config.setIcon(props.getFile(Config.ICON));
155             File JavaDoc splashFile = props.getFile(Splash.SPLASH_FILE);
156             if (splashFile != null) {
157                 _config.setSplash(new Splash());
158                 _config.getSplash().setFile(splashFile);
159                 String JavaDoc waitfor = props.getProperty("waitfor"); // 1.x
160
_config.getSplash().setWaitForWindow(waitfor != null
161                                                     && !waitfor.equals(""));
162                 String JavaDoc splashTimeout = props.getProperty(Splash.TIMEOUT);
163                 if (splashTimeout != null) {
164                     _config.getSplash().setTimeout(Integer.parseInt(splashTimeout));
165                 }
166                 _config.getSplash().setTimeoutErr("true".equals(
167                         props.getProperty(Splash.TIMEOUT_ERR)));
168             } else {
169                 _config.setSplash(null);
170             }
171             setConfigPath(f);
172         } catch (IOException JavaDoc e) {
173             throw new ConfigPersisterException(e);
174         }
175     }
176
177     public void save(File JavaDoc f) throws ConfigPersisterException {
178         try {
179             BufferedWriter JavaDoc w = new BufferedWriter JavaDoc(new FileWriter JavaDoc(f));
180             _xstream.toXML(_config, w);
181             w.close();
182             setConfigPath(f);
183         } catch (Exception JavaDoc e) {
184             throw new ConfigPersisterException(e);
185         }
186     }
187
188     private void setConfigPath(File JavaDoc configFile) {
189         _configPath = configFile.getAbsoluteFile().getParentFile();
190     }
191
192     private class Props {
193         final Properties JavaDoc _properties = new Properties JavaDoc();
194
195         public Props(File JavaDoc f) throws IOException JavaDoc {
196             FileInputStream JavaDoc is = null;
197             try {
198                 is = new FileInputStream JavaDoc(f);
199                 _properties.load(is);
200             } finally {
201                 Util.close(is);
202             }
203         }
204
205         /**
206          * Get property and remove trailing # comments.
207          */

208         public String JavaDoc getProperty(String JavaDoc key) {
209             String JavaDoc p = _properties.getProperty(key);
210             if (p == null) {
211                 return null;
212             }
213             int x = p.indexOf('#');
214             if (x == -1) {
215                 return p;
216             }
217             do {
218                 x--;
219             } while (x > 0 && (p.charAt(x) == ' ' || p.charAt(x) == '\t'));
220             return (x == 0) ? "" : p.substring(0, x + 1);
221         }
222
223         public File JavaDoc getFile(String JavaDoc key) {
224             String JavaDoc value = getProperty(key);
225             return value != null ? new File JavaDoc(value) : null;
226         }
227     }
228 }
229
Popular Tags