KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > event > AntAction


1 /*
2  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
3  *
4  * http://www.izforge.com/izpack/
5  * http://developer.berlios.de/projects/izpack/
6  *
7  * Copyright 2004 Klaus Bartz
8  * Copyright 2004 Thomas Guenter
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */

22
23 package com.izforge.izpack.event;
24
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.FileNotFoundException JavaDoc;
28 import java.io.FileOutputStream JavaDoc;
29 import java.io.PrintStream JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Properties JavaDoc;
34
35 import org.apache.tools.ant.BuildLogger;
36 import org.apache.tools.ant.DefaultLogger;
37 import org.apache.tools.ant.DemuxOutputStream;
38 import org.apache.tools.ant.Project;
39 import org.apache.tools.ant.Target;
40 import org.apache.tools.ant.input.DefaultInputHandler;
41 import org.apache.tools.ant.taskdefs.Ant;
42 import org.apache.tools.ant.util.JavaEnvUtils;
43
44 /**
45  * This class contains data and 'perform' logic for ant action listeners.
46  *
47  * @author Thomas Guenter
48  * @author Klaus Bartz
49  *
50  */

51 public class AntAction extends ActionBase
52 {
53
54     // --------AntAction specific String constants for ------------
55
// --- parsing the XML specification ------------
56

57     private static final long serialVersionUID = 3258131345250005557L;
58
59     public static final String JavaDoc ANTACTIONS = "antactions";
60
61     public static final String JavaDoc ANTACTION = "antaction";
62
63     public static final String JavaDoc ANTCALL = "antcall";
64
65     private boolean quiet = false;
66
67     private boolean verbose = false;
68
69     private Properties JavaDoc properties = null;
70
71     private List JavaDoc targets = null;
72
73     private List JavaDoc uninstallTargets = null;
74
75     private String JavaDoc logFile = null;
76
77     private String JavaDoc buildFile = null;
78
79     private List JavaDoc propertyFiles = null;
80
81     /**
82      * Default constructor
83      */

84     public AntAction()
85     {
86         super();
87         properties = new Properties JavaDoc();
88         targets = new ArrayList JavaDoc();
89         uninstallTargets = new ArrayList JavaDoc();
90         propertyFiles = new ArrayList JavaDoc();
91     }
92
93     /**
94      * Performs all defined install actions.
95      *
96      * Calls {#performAction performAction(false)}.
97      *
98      * @throws Exception
99      */

100     public void performInstallAction() throws Exception JavaDoc
101     {
102         performAction(false);
103     }
104
105     /**
106      * Performs all defined uninstall actions.
107      *
108      * Calls {#performAction performAction(true)}.
109      *
110      * @throws Exception
111      */

112     public void performUninstallAction() throws Exception JavaDoc
113     {
114         performAction(true);
115     }
116
117     /**
118      * Performs all defined actions.
119      *
120      * @param uninstall An install/uninstall switch. If this is <tt>true</tt> only the uninstall
121      * actions, otherwise only the install actions are being performed.
122      *
123      * @see #performInstallAction() for calling all install actions.
124      * @see #performUninstallAction() for calling all uninstall actions.
125      *
126      * @throws Exception
127      */

128     public void performAction(boolean uninstall) throws Exception JavaDoc
129     {
130         if (verbose) System.out.println("Calling ANT with buildfile: " + buildFile);
131         SecurityManager JavaDoc oldsm = null;
132         if (!JavaEnvUtils.isJavaVersion("1.0") && !JavaEnvUtils.isJavaVersion("1.1"))
133             oldsm = System.getSecurityManager();
134         PrintStream JavaDoc err = System.err;
135         PrintStream JavaDoc out = System.out;
136         try
137         {
138             Project antProj = new Project();
139             antProj.setName("antcallproject");
140             antProj.addBuildListener(createLogger());
141             antProj.setInputHandler(new DefaultInputHandler());
142             antProj.setSystemProperties();
143             addProperties(antProj, getProperties());
144             addPropertiesFromPropertyFiles(antProj);
145             // TODO: propertyfiles, logFile
146
antProj.fireBuildStarted();
147             antProj.init();
148             List JavaDoc antcalls = new ArrayList JavaDoc();
149             List JavaDoc choosenTargets = (uninstall) ? uninstallTargets : targets;
150             if (choosenTargets.size() > 0)
151             {
152                 Ant antcall = null;
153                 for (int i = 0; i < choosenTargets.size(); i++)
154                 {
155                     antcall = (Ant) antProj.createTask("ant");
156                     antcall.setAntfile(getBuildFile());
157                     antcall.setTarget((String JavaDoc) choosenTargets.get(i));
158                     antcalls.add(antcall);
159                 }
160             }
161             Target target = new Target();
162             target.setName("calltarget");
163
164             for (int i = 0; i < antcalls.size(); i++)
165             {
166                 target.addTask((Ant) antcalls.get(i));
167             }
168             antProj.addTarget(target);
169             System.setOut(new PrintStream JavaDoc(new DemuxOutputStream(antProj, false)));
170             System.setErr(new PrintStream JavaDoc(new DemuxOutputStream(antProj, true)));
171             antProj.executeTarget("calltarget");
172         }
173         finally
174         {
175             if (oldsm != null) System.setSecurityManager(oldsm);
176             System.setOut(out);
177             System.setErr(err);
178         }
179     }
180
181     /**
182      * Returns the build file.
183      *
184      * @return the build file
185      */

186     public String JavaDoc getBuildFile()
187     {
188         return buildFile;
189     }
190
191     /**
192      * Sets the build file to be used to the given string.
193      *
194      * @param buildFile build file path to be used
195      */

196     public void setBuildFile(String JavaDoc buildFile)
197     {
198         this.buildFile = buildFile;
199     }
200
201     /**
202      * Returns the current logfile path as string.
203      *
204      * @return current logfile path
205      */

206     public String JavaDoc getLogFile()
207     {
208         return logFile;
209     }
210
211     /**
212      * Sets the logfile path to the given string.
213      *
214      * @param logFile to be set
215      */

216     public void setLogFile(String JavaDoc logFile)
217     {
218         this.logFile = logFile;
219     }
220
221     /**
222      * Returns the property file paths as list of strings.
223      *
224      * @return the property file paths
225      */

226     public List JavaDoc getPropertyFiles()
227     {
228         return propertyFiles;
229     }
230
231     /**
232      * Adds one property file path to the internal list of property file paths.
233      *
234      * @param propertyFile to be added
235      */

236     public void addPropertyFile(String JavaDoc propertyFile)
237     {
238         this.propertyFiles.add(propertyFile);
239     }
240
241     /**
242      * Sets the property file path list to the given list. Old settings will be lost.
243      *
244      * @param propertyFiles list of property file paths to be set
245      */

246     public void setPropertyFiles(List JavaDoc propertyFiles)
247     {
248         this.propertyFiles = propertyFiles;
249     }
250
251     /**
252      * Returns the properties.
253      *
254      * @return the properties
255      */

256     public Properties JavaDoc getProperties()
257     {
258         return properties;
259     }
260
261     /**
262      * Sets the internal properties to the given properties. Old settings will be lost.
263      *
264      * @param properties properties to be set
265      */

266     public void setProperties(Properties JavaDoc properties)
267     {
268         this.properties = properties;
269     }
270
271     /**
272      * Sets the given value to the property identified by the given name.
273      *
274      * @param name key of the property
275      * @param value value to be used for the property
276      */

277     public void setProperty(String JavaDoc name, String JavaDoc value)
278     {
279         this.properties.put(name, value);
280     }
281
282     /**
283      * Returns the value for the property identified by the given name.
284      *
285      * @param name name of the property
286      * @return value of the property
287      */

288     public String JavaDoc getProperty(String JavaDoc name)
289     {
290         return this.properties.getProperty(name);
291     }
292
293     /**
294      * Returns the quiet state.
295      *
296      * @return quiet state
297      */

298     public boolean isQuiet()
299     {
300         return quiet;
301     }
302
303     /**
304      * Sets whether the associated ant task should be performed quiet or not.
305      *
306      * @param quiet quiet state to set
307      */

308     public void setQuiet(boolean quiet)
309     {
310         this.quiet = quiet;
311     }
312
313     /**
314      * Returns the targets.
315      *
316      * @return the targets
317      */

318     public List JavaDoc getTargets()
319     {
320         return targets;
321     }
322
323     /**
324      * Sets the targets which should be performed at installation time. Old settings are lost.
325      *
326      * @param targets list of targets
327      */

328     public void setTargets(ArrayList JavaDoc targets)
329     {
330         this.targets = targets;
331     }
332
333     /**
334      * Adds the given target to the target list which should be performed at installation time.
335      *
336      * @param target target to be add
337      */

338     public void addTarget(String JavaDoc target)
339     {
340         this.targets.add(target);
341     }
342
343     /**
344      * Returns the uninstaller targets.
345      *
346      * @return the uninstaller targets
347      */

348     public List JavaDoc getUninstallTargets()
349     {
350         return uninstallTargets;
351     }
352
353     /**
354      * Sets the targets which should be performed at uninstallation time. Old settings are lost.
355      *
356      * @param targets list of targets
357      */

358     public void setUninstallTargets(ArrayList JavaDoc targets)
359     {
360         this.uninstallTargets = targets;
361     }
362
363     /**
364      * Adds the given target to the target list which should be performed at uninstallation time.
365      *
366      * @param target target to be add
367      */

368     public void addUninstallTarget(String JavaDoc target)
369     {
370         this.uninstallTargets.add(target);
371     }
372
373     /**
374      * Returns the verbose state.
375      *
376      * @return verbose state
377      */

378     public boolean isVerbose()
379     {
380         return verbose;
381     }
382
383     /**
384      * Sets the verbose state.
385      *
386      * @param verbose state to be set
387      */

388     public void setVerbose(boolean verbose)
389     {
390         this.verbose = verbose;
391     }
392
393     private BuildLogger createLogger()
394     {
395         int msgOutputLevel = 2;
396         if (verbose)
397             msgOutputLevel = 4;
398         else if (quiet) msgOutputLevel = 1;
399         BuildLogger logger = new DefaultLogger();
400         logger.setMessageOutputLevel(msgOutputLevel);
401         if (logFile != null)
402         {
403             PrintStream JavaDoc printStream;
404             try
405             {
406                 printStream = new PrintStream JavaDoc(new FileOutputStream JavaDoc(logFile));
407                 logger.setOutputPrintStream(printStream);
408                 logger.setErrorPrintStream(printStream);
409             }
410             catch (FileNotFoundException JavaDoc e)
411             {
412                 logger.setOutputPrintStream(System.out);
413                 logger.setErrorPrintStream(System.err);
414             }
415         }
416         else
417         {
418             logger.setOutputPrintStream(System.out);
419             logger.setErrorPrintStream(System.err);
420         }
421         return logger;
422     }
423
424     private void addProperties(Project proj, Properties JavaDoc props)
425     {
426         if (proj == null) return;
427         if (props.size() > 0)
428         {
429             Iterator JavaDoc iter = props.keySet().iterator();
430             String JavaDoc key = null;
431             while (iter.hasNext())
432             {
433                 key = (String JavaDoc) iter.next();
434                 proj.setProperty(key, props.getProperty(key));
435             }
436         }
437     }
438
439     private void addPropertiesFromPropertyFiles(Project proj) throws Exception JavaDoc
440     {
441         if (proj == null) return;
442         Properties JavaDoc props = new Properties JavaDoc();
443         File JavaDoc pf = null;
444         FileInputStream JavaDoc fis = null;
445         try
446         {
447             for (int i = 0; i < propertyFiles.size(); i++)
448             {
449                 pf = new File JavaDoc((String JavaDoc) propertyFiles.get(i));
450                 if (pf.exists())
451                 {
452                     fis = new FileInputStream JavaDoc(pf);
453                     props.load(fis);
454                     fis.close();
455                 }
456                 else
457                 {
458                     throw new Exception JavaDoc("Required propertyfile " + pf
459                             + " for antcall doesn't exist.");
460                 }
461             }
462         }
463         finally
464         {
465             if (fis != null) fis.close();
466         }
467         addProperties(proj, props);
468     }
469
470 }
471
Popular Tags