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.BufferedReader;
|
---|
28 | import java.io.BufferedWriter;
|
---|
29 | import java.io.File;
|
---|
30 | import java.io.FileInputStream;
|
---|
31 | import java.io.FileReader;
|
---|
32 | import java.io.FileWriter;
|
---|
33 | import java.io.IOException;
|
---|
34 | import java.util.Properties;
|
---|
35 |
|
---|
36 | import net.sf.launch4j.Util;
|
---|
37 |
|
---|
38 | import com.thoughtworks.xstream.XStream;
|
---|
39 | import com.thoughtworks.xstream.io.xml.DomDriver;
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * @author Copyright (C) 2005 Grzegorz Kowal
|
---|
43 | */
|
---|
44 | public class ConfigPersister {
|
---|
45 |
|
---|
46 | private static final ConfigPersister _instance = new ConfigPersister();
|
---|
47 |
|
---|
48 | private final XStream _xstream;
|
---|
49 | private Config _config;
|
---|
50 | private File _configPath;
|
---|
51 |
|
---|
52 | private ConfigPersister() {
|
---|
53 | _xstream = new XStream(new DomDriver());
|
---|
54 | _xstream.alias("launch4jConfig", Config.class);
|
---|
55 | _xstream.alias("jre", Jre.class);
|
---|
56 | _xstream.alias("splash", Splash.class);
|
---|
57 | _xstream.alias("versionInfo", VersionInfo.class);
|
---|
58 | _xstream.alias("file", String.class);
|
---|
59 | }
|
---|
60 |
|
---|
61 | public static ConfigPersister getInstance() {
|
---|
62 | return _instance;
|
---|
63 | }
|
---|
64 |
|
---|
65 | public Config getConfig() {
|
---|
66 | return _config;
|
---|
67 | }
|
---|
68 |
|
---|
69 | public File getConfigPath() {
|
---|
70 | return _configPath;
|
---|
71 | }
|
---|
72 |
|
---|
73 | public File getOutputPath() throws IOException {
|
---|
74 | if (_config.getOutfile().isAbsolute()) {
|
---|
75 | return _config.getOutfile().getParentFile();
|
---|
76 | }
|
---|
77 | File parent = _config.getOutfile().getParentFile();
|
---|
78 | return (parent != null) ? new File(_configPath, parent.getPath()) : _configPath;
|
---|
79 | }
|
---|
80 |
|
---|
81 | public File getOutputFile() throws IOException {
|
---|
82 | return _config.getOutfile().isAbsolute()
|
---|
83 | ? _config.getOutfile()
|
---|
84 | : new File(getOutputPath(), _config.getOutfile().getName());
|
---|
85 | }
|
---|
86 |
|
---|
87 | public void createBlank() {
|
---|
88 | _config = new Config();
|
---|
89 | _config.setJre(new Jre());
|
---|
90 | _configPath = null;
|
---|
91 | }
|
---|
92 |
|
---|
93 | public void setAntConfig(Config c, File basedir) {
|
---|
94 | _config = c;
|
---|
95 | _configPath = basedir;
|
---|
96 | }
|
---|
97 |
|
---|
98 | public void load(File f) throws ConfigPersisterException {
|
---|
99 | try {
|
---|
100 | BufferedReader r = new BufferedReader(new FileReader(f));
|
---|
101 | _config = (Config) _xstream.fromXML(r);
|
---|
102 | r.close();
|
---|
103 | setConfigPath(f);
|
---|
104 | } catch (Exception e) {
|
---|
105 | throw new ConfigPersisterException(e);
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Imports launch4j 1.x.x config file.
|
---|
111 | */
|
---|
112 | public void loadVersion1(File f) throws ConfigPersisterException {
|
---|
113 | try {
|
---|
114 | Props props = new Props(f);
|
---|
115 | _config = new Config();
|
---|
116 | String header = props.getProperty(Config.HEADER);
|
---|
117 | _config.setHeaderType(header == null
|
---|
118 | || header.toLowerCase().equals("guihead.bin") ? 0 : 1);
|
---|
119 | _config.setJar(props.getFile(Config.JAR));
|
---|
120 | _config.setOutfile(props.getFile(Config.OUTFILE));
|
---|
121 | _config.setJre(new Jre());
|
---|
122 | _config.getJre().setPath(props.getProperty(Jre.PATH));
|
---|
123 | _config.getJre().setMinVersion(props.getProperty(Jre.MIN_VERSION));
|
---|
124 | _config.getJre().setMaxVersion(props.getProperty(Jre.MAX_VERSION));
|
---|
125 | _config.getJre().setArgs(props.getProperty(Jre.ARGS));
|
---|
126 | _config.setJarArgs(props.getProperty(Config.JAR_ARGS));
|
---|
127 | _config.setChdir("true".equals(props.getProperty(Config.CHDIR)) ? "." : null);
|
---|
128 | _config.setCustomProcName("true".equals(
|
---|
129 | props.getProperty("setProcName"))); // 1.x
|
---|
130 | _config.setStayAlive("true".equals(props.getProperty(Config.STAY_ALIVE)));
|
---|
131 | _config.setErrTitle(props.getProperty(Config.ERR_TITLE));
|
---|
132 | _config.setIcon(props.getFile(Config.ICON));
|
---|
133 | File splashFile = props.getFile(Splash.SPLASH_FILE);
|
---|
134 | if (splashFile != null) {
|
---|
135 | _config.setSplash(new Splash());
|
---|
136 | _config.getSplash().setFile(splashFile);
|
---|
137 | String waitfor = props.getProperty("waitfor"); // 1.x
|
---|
138 | _config.getSplash().setWaitForWindow(waitfor != null && !waitfor.equals(""));
|
---|
139 | String splashTimeout = props.getProperty(Splash.TIMEOUT);
|
---|
140 | if (splashTimeout != null) {
|
---|
141 | _config.getSplash().setTimeout(Integer.parseInt(splashTimeout));
|
---|
142 | }
|
---|
143 | _config.getSplash().setTimeoutErr("true".equals(
|
---|
144 | props.getProperty(Splash.TIMEOUT_ERR)));
|
---|
145 | } else {
|
---|
146 | _config.setSplash(null);
|
---|
147 | }
|
---|
148 | setConfigPath(f);
|
---|
149 | } catch (IOException e) {
|
---|
150 | throw new ConfigPersisterException(e);
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | public void save(File f) throws ConfigPersisterException {
|
---|
155 | try {
|
---|
156 | BufferedWriter w = new BufferedWriter(new FileWriter(f));
|
---|
157 | _xstream.toXML(_config, w);
|
---|
158 | w.close();
|
---|
159 | setConfigPath(f);
|
---|
160 | } catch (Exception e) {
|
---|
161 | throw new ConfigPersisterException(e);
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 | private void setConfigPath(File configFile) {
|
---|
166 | _configPath = configFile.getAbsoluteFile().getParentFile();
|
---|
167 | }
|
---|
168 |
|
---|
169 | private class Props {
|
---|
170 | final Properties _properties = new Properties();
|
---|
171 |
|
---|
172 | public Props(File f) throws IOException {
|
---|
173 | FileInputStream is = null;
|
---|
174 | try {
|
---|
175 | is = new FileInputStream(f);
|
---|
176 | _properties.load(is);
|
---|
177 | } finally {
|
---|
178 | Util.close(is);
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Get property and remove trailing # comments.
|
---|
184 | */
|
---|
185 | public String getProperty(String key) {
|
---|
186 | String p = _properties.getProperty(key);
|
---|
187 | if (p == null) {
|
---|
188 | return null;
|
---|
189 | }
|
---|
190 | int x = p.indexOf('#');
|
---|
191 | if (x == -1) {
|
---|
192 | return p;
|
---|
193 | }
|
---|
194 | do {
|
---|
195 | x--;
|
---|
196 | } while (x > 0 && (p.charAt(x) == ' ' || p.charAt(x) == '\t'));
|
---|
197 | return (x == 0) ? "" : p.substring(0, x + 1);
|
---|
198 | }
|
---|
199 |
|
---|
200 | public File getFile(String key) {
|
---|
201 | String value = getProperty(key);
|
---|
202 | return value != null ? new File(value) : null;
|
---|
203 | }
|
---|
204 | }
|
---|
205 | }
|
---|