svn-gvsig-desktop / trunk / build / distribution / IzPack / src / lib / com / izforge / izpack / event / AntAction.java @ 21757
History | View | Annotate | Download (11.1 KB)
1 |
/*
|
---|---|
2 |
* $Id: AntAction.java 5819 2006-06-14 07:29:09Z cesar $
|
3 |
* IzPack
|
4 |
* Copyright (C) 2004 Thomas Guenter, Klaus Bartz
|
5 |
*
|
6 |
* File : AntAction.java
|
7 |
* Description : Data class for ant action listeners.
|
8 |
* Author's email : bartzkau@users.berlios.de
|
9 |
* Website : http://www.izforge.com
|
10 |
*
|
11 |
* This program is free software; you can redistribute it and/or
|
12 |
* modify it under the terms of the GNU General Public License
|
13 |
* as published by the Free Software Foundation; either version 2
|
14 |
* of the License, or any later version.
|
15 |
*
|
16 |
* This program is distributed in the hope that it will be useful,
|
17 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
19 |
* GNU General Public License for more details.
|
20 |
*
|
21 |
* You should have received a copy of the GNU General Public License
|
22 |
* along with this program; if not, write to the Free Software
|
23 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
24 |
*/
|
25 |
|
26 |
package com.izforge.izpack.event; |
27 |
|
28 |
import java.io.File; |
29 |
import java.io.FileInputStream; |
30 |
import java.io.PrintStream; |
31 |
import java.util.ArrayList; |
32 |
import java.util.Iterator; |
33 |
import java.util.List; |
34 |
import java.util.Properties; |
35 |
|
36 |
import org.apache.tools.ant.BuildLogger; |
37 |
import org.apache.tools.ant.DefaultLogger; |
38 |
import org.apache.tools.ant.DemuxOutputStream; |
39 |
import org.apache.tools.ant.Project; |
40 |
import org.apache.tools.ant.Target; |
41 |
import org.apache.tools.ant.input.DefaultInputHandler; |
42 |
import org.apache.tools.ant.taskdefs.Ant; |
43 |
import org.apache.tools.ant.util.JavaEnvUtils; |
44 |
|
45 |
/**
|
46 |
* This class contains data and 'perform' logic for ant action listeners.
|
47 |
*
|
48 |
* @author Thomas Guenter
|
49 |
* @author Klaus Bartz
|
50 |
*
|
51 |
*/
|
52 |
public class AntAction extends ActionBase |
53 |
{ |
54 |
// --------AntAction specific String constants for ------------
|
55 |
// --- parsing the XML specification ------------
|
56 |
|
57 |
public static final String ANTACTIONS = "antactions"; |
58 |
public static final String ANTACTION = "antaction"; |
59 |
public static final String ANTCALL = "antcall"; |
60 |
|
61 |
private boolean quiet = false; |
62 |
private boolean verbose = false; |
63 |
private Properties properties = null; |
64 |
private List targets = null; |
65 |
private List uninstallTargets = null; |
66 |
private String logFile = null; |
67 |
private String buildFile = null; |
68 |
private List propertyFiles = null; |
69 |
|
70 |
/**
|
71 |
* Default constructor
|
72 |
*/
|
73 |
public AntAction()
|
74 |
{ |
75 |
super();
|
76 |
properties = new Properties(); |
77 |
targets = new ArrayList(); |
78 |
uninstallTargets = new ArrayList(); |
79 |
propertyFiles = new ArrayList(); |
80 |
} |
81 |
|
82 |
/**
|
83 |
* Performs all defined install actions.
|
84 |
*
|
85 |
* Calls {#performAction performAction(false)}.
|
86 |
* @throws Exception
|
87 |
*/
|
88 |
public void performInstallAction() |
89 |
throws Exception |
90 |
{ |
91 |
performAction(false);
|
92 |
} |
93 |
|
94 |
/**
|
95 |
* Performs all defined uninstall actions.
|
96 |
*
|
97 |
* Calls {#performAction performAction(true)}.
|
98 |
*
|
99 |
* @throws Exception
|
100 |
*/
|
101 |
public void performUninstallAction() |
102 |
throws Exception |
103 |
{ |
104 |
performAction(true);
|
105 |
} |
106 |
|
107 |
/**
|
108 |
* Performs all defined actions.
|
109 |
*
|
110 |
* @param uninstall An install/uninstall switch. If this is <tt>true</tt>
|
111 |
* only the uninstall actions, otherwise only the install actions are
|
112 |
* being performed.
|
113 |
*
|
114 |
* @see #performInstallAction() for calling all install actions.
|
115 |
* @see #performUninstallAction() for calling all uninstall actions.
|
116 |
*
|
117 |
* @throws Exception
|
118 |
*/
|
119 |
public void performAction(boolean uninstall) |
120 |
throws Exception |
121 |
{ |
122 |
if (verbose)
|
123 |
System.out.println("Calling ANT with buildfile: " + buildFile); |
124 |
SecurityManager oldsm = null; |
125 |
if (!JavaEnvUtils.isJavaVersion("1.0") |
126 |
&& !JavaEnvUtils.isJavaVersion("1.1"))
|
127 |
oldsm = System.getSecurityManager();
|
128 |
PrintStream err = System.err; |
129 |
PrintStream out = System.out; |
130 |
try
|
131 |
{ |
132 |
Project antProj = new Project();
|
133 |
antProj.setName("antcallproject");
|
134 |
antProj.addBuildListener(createLogger()); |
135 |
antProj.setInputHandler(new DefaultInputHandler());
|
136 |
antProj.setSystemProperties(); |
137 |
addProperties( antProj, getProperties() ); |
138 |
addPropertiesFromPropertyFiles( antProj ); |
139 |
// TODO: propertyfiles, logFile
|
140 |
antProj.fireBuildStarted(); |
141 |
antProj.init(); |
142 |
List antcalls = new ArrayList(); |
143 |
List choosenTargets = (uninstall) ? uninstallTargets : targets;
|
144 |
if (choosenTargets.size() > 0) |
145 |
{ |
146 |
Ant antcall = null;
|
147 |
for (int i = 0; i < choosenTargets.size(); i++) |
148 |
{ |
149 |
antcall = (Ant) antProj.createTask("ant");
|
150 |
antcall.setAntfile(getBuildFile()); |
151 |
antcall.setTarget((String) choosenTargets.get(i));
|
152 |
antcalls.add(antcall); |
153 |
} |
154 |
} |
155 |
Target target = new Target(); |
156 |
target.setName("calltarget");
|
157 |
|
158 |
for (int i = 0; i < antcalls.size(); i++) |
159 |
{ |
160 |
target.addTask((Ant) antcalls.get(i)); |
161 |
} |
162 |
antProj.addTarget(target); |
163 |
System.setOut(new PrintStream(new DemuxOutputStream(antProj, false))); |
164 |
System.setErr(new PrintStream(new DemuxOutputStream(antProj, true))); |
165 |
antProj.executeTarget("calltarget");
|
166 |
} |
167 |
finally
|
168 |
{ |
169 |
if(oldsm != null) |
170 |
System.setSecurityManager(oldsm);
|
171 |
System.setOut(out);
|
172 |
System.setErr(err);
|
173 |
} |
174 |
} |
175 |
|
176 |
/**
|
177 |
* Returns the build file.
|
178 |
* @return the build file
|
179 |
*/
|
180 |
public String getBuildFile() |
181 |
{ |
182 |
return buildFile;
|
183 |
} |
184 |
|
185 |
/**
|
186 |
* Sets the build file to be used to the given string.
|
187 |
* @param buildFile build file path to be used
|
188 |
*/
|
189 |
public void setBuildFile(String buildFile) |
190 |
{ |
191 |
this.buildFile = buildFile;
|
192 |
} |
193 |
|
194 |
/**
|
195 |
* Returns the current logfile path as string.
|
196 |
* @return current logfile path
|
197 |
*/
|
198 |
public String getLogFile() |
199 |
{ |
200 |
return logFile;
|
201 |
} |
202 |
|
203 |
/**
|
204 |
* Sets the logfile path to the given string.
|
205 |
* @param logFile to be set
|
206 |
*/
|
207 |
public void setLogFile(String logFile) |
208 |
{ |
209 |
this.logFile = logFile;
|
210 |
} |
211 |
|
212 |
/**
|
213 |
* Returns the property file paths as list of strings.
|
214 |
* @return the property file paths
|
215 |
*/
|
216 |
public List getPropertyFiles() |
217 |
{ |
218 |
return propertyFiles;
|
219 |
} |
220 |
|
221 |
/**
|
222 |
* Adds one property file path to the internal list
|
223 |
* of property file paths.
|
224 |
* @param propertyFile to be added
|
225 |
*/
|
226 |
public void addPropertyFile(String propertyFile) |
227 |
{ |
228 |
this.propertyFiles.add( propertyFile );
|
229 |
} |
230 |
|
231 |
/**
|
232 |
* Sets the property file path list to the given list.
|
233 |
* Old settings will be lost.
|
234 |
* @param propertyFiles list of property file paths to be set
|
235 |
*/
|
236 |
public void setPropertyFiles(List propertyFiles) |
237 |
{ |
238 |
this.propertyFiles = propertyFiles;
|
239 |
} |
240 |
|
241 |
/**
|
242 |
* Returns the properties.
|
243 |
* @return the properties
|
244 |
*/
|
245 |
public Properties getProperties() |
246 |
{ |
247 |
return properties;
|
248 |
} |
249 |
|
250 |
/**
|
251 |
* Sets the internal properties to the given properties.
|
252 |
* Old settings will be lost.
|
253 |
* @param properties properties to be set
|
254 |
*/
|
255 |
public void setProperties(Properties properties) |
256 |
{ |
257 |
this.properties = properties;
|
258 |
} |
259 |
|
260 |
/**
|
261 |
* Sets the given value to the property identified
|
262 |
* by the given name.
|
263 |
* @param name key of the property
|
264 |
* @param value value to be used for the property
|
265 |
*/
|
266 |
public void setProperty(String name, String value) |
267 |
{ |
268 |
this.properties.put(name, value);
|
269 |
} |
270 |
|
271 |
/**
|
272 |
* Returns the value for the property identified
|
273 |
* by the given name.
|
274 |
* @param name name of the property
|
275 |
* @return value of the property
|
276 |
*/
|
277 |
public String getProperty(String name) |
278 |
{ |
279 |
return this.properties.getProperty(name); |
280 |
} |
281 |
|
282 |
/**
|
283 |
* Returns the quiet state.
|
284 |
* @return quiet state
|
285 |
*/
|
286 |
public boolean isQuiet() |
287 |
{ |
288 |
return quiet;
|
289 |
} |
290 |
|
291 |
|
292 |
/**
|
293 |
* Sets whether the associated ant task should be
|
294 |
* performed quiet or not.
|
295 |
* @param quiet quiet state to set
|
296 |
*/
|
297 |
public void setQuiet(boolean quiet) |
298 |
{ |
299 |
this.quiet = quiet;
|
300 |
} |
301 |
|
302 |
/**
|
303 |
* Returns the targets.
|
304 |
* @return the targets
|
305 |
*/
|
306 |
public List getTargets() |
307 |
{ |
308 |
return targets;
|
309 |
} |
310 |
|
311 |
/**
|
312 |
* Sets the targets which should be performed
|
313 |
* at installation time.
|
314 |
* Old settings are lost.
|
315 |
* @param targets list of targets
|
316 |
*/
|
317 |
public void setTargets(ArrayList targets) |
318 |
{ |
319 |
this.targets = targets;
|
320 |
} |
321 |
|
322 |
/**
|
323 |
* Adds the given target to the target list
|
324 |
* which should be performed at installation time.
|
325 |
* @param target target to be add
|
326 |
*/
|
327 |
public void addTarget(String target) |
328 |
{ |
329 |
this.targets.add(target);
|
330 |
} |
331 |
|
332 |
|
333 |
/**
|
334 |
* Returns the uninstaller targets.
|
335 |
* @return the uninstaller targets
|
336 |
*/
|
337 |
public List getUninstallTargets() |
338 |
{ |
339 |
return uninstallTargets;
|
340 |
} |
341 |
|
342 |
/**
|
343 |
* Sets the targets which should be performed
|
344 |
* at uninstallation time.
|
345 |
* Old settings are lost.
|
346 |
* @param targets list of targets
|
347 |
*/
|
348 |
public void setUninstallTargets(ArrayList targets) |
349 |
{ |
350 |
this.uninstallTargets = targets;
|
351 |
} |
352 |
|
353 |
/**
|
354 |
* Adds the given target to the target list
|
355 |
* which should be performed at uninstallation time.
|
356 |
* @param target target to be add
|
357 |
*/
|
358 |
public void addUninstallTarget(String target) |
359 |
{ |
360 |
this.uninstallTargets.add(target);
|
361 |
} |
362 |
|
363 |
|
364 |
|
365 |
/**
|
366 |
* Returns the verbose state.
|
367 |
* @return verbose state
|
368 |
*/
|
369 |
public boolean isVerbose() |
370 |
{ |
371 |
return verbose;
|
372 |
} |
373 |
|
374 |
/**
|
375 |
* Sets the verbose state.
|
376 |
* @param verbose state to be set
|
377 |
*/
|
378 |
public void setVerbose(boolean verbose) |
379 |
{ |
380 |
this.verbose = verbose;
|
381 |
} |
382 |
|
383 |
private BuildLogger createLogger()
|
384 |
{ |
385 |
int msgOutputLevel = 2; |
386 |
if (verbose)
|
387 |
msgOutputLevel = 4;
|
388 |
else if (quiet) |
389 |
msgOutputLevel = 1;
|
390 |
BuildLogger logger = new DefaultLogger();
|
391 |
logger.setMessageOutputLevel(msgOutputLevel); |
392 |
logger.setOutputPrintStream(System.out);
|
393 |
logger.setErrorPrintStream(System.err);
|
394 |
return logger;
|
395 |
} |
396 |
|
397 |
private void addProperties( Project proj, Properties props ) |
398 |
{ |
399 |
if (proj == null) |
400 |
return;
|
401 |
if (props.size() > 0) |
402 |
{ |
403 |
Iterator iter = props.keySet().iterator();
|
404 |
String key = null; |
405 |
while (iter.hasNext())
|
406 |
{ |
407 |
key = (String) iter.next();
|
408 |
proj.setProperty(key, props.getProperty(key)); |
409 |
} |
410 |
} |
411 |
} |
412 |
|
413 |
private void addPropertiesFromPropertyFiles( Project proj ) |
414 |
throws Exception |
415 |
{ |
416 |
if (proj == null) |
417 |
return;
|
418 |
Properties props = new Properties(); |
419 |
File pf = null; |
420 |
FileInputStream fis = null; |
421 |
try
|
422 |
{ |
423 |
for (int i = 0; i < propertyFiles.size(); i++) |
424 |
{ |
425 |
pf = new File( (String)propertyFiles.get(i) ); |
426 |
if (pf.exists())
|
427 |
{ |
428 |
fis = new FileInputStream(pf); |
429 |
props.load(fis); |
430 |
fis.close(); |
431 |
} |
432 |
else
|
433 |
{ |
434 |
throw new Exception("Required propertyfile " + pf + " for antcall doesn't exist."); |
435 |
} |
436 |
} |
437 |
} |
438 |
finally
|
439 |
{ |
440 |
if (fis != null) |
441 |
fis.close(); |
442 |
} |
443 |
addProperties( proj, props ); |
444 |
} |
445 |
|
446 |
|
447 |
} |