001: /*
002: * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
003: *
004: * http://izpack.org/
005: * http://izpack.codehaus.org/
006: *
007: * Copyright 2004 Klaus Bartz
008: * Copyright 2004 Thomas Guenter
009: *
010: * Licensed under the Apache License, Version 2.0 (the "License");
011: * you may not use this file except in compliance with the License.
012: * You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: */
022:
023: package com.izforge.izpack.event;
024:
025: import java.io.File;
026: import java.io.FileInputStream;
027: import java.io.FileNotFoundException;
028: import java.io.FileOutputStream;
029: import java.io.PrintStream;
030: import java.util.ArrayList;
031: import java.util.Iterator;
032: import java.util.List;
033: import java.util.Properties;
034:
035: import org.apache.tools.ant.BuildLogger;
036: import org.apache.tools.ant.DefaultLogger;
037: import org.apache.tools.ant.DemuxOutputStream;
038: import org.apache.tools.ant.Project;
039: import org.apache.tools.ant.Target;
040: import org.apache.tools.ant.input.DefaultInputHandler;
041: import org.apache.tools.ant.taskdefs.Ant;
042: import org.apache.tools.ant.util.JavaEnvUtils;
043:
044: /**
045: * This class contains data and 'perform' logic for ant action listeners.
046: *
047: * @author Thomas Guenter
048: * @author Klaus Bartz
049: *
050: */
051: public class AntAction extends ActionBase {
052:
053: // --------AntAction specific String constants for ------------
054: // --- parsing the XML specification ------------
055:
056: private static final long serialVersionUID = 3258131345250005557L;
057:
058: public static final String ANTACTIONS = "antactions";
059:
060: public static final String ANTACTION = "antaction";
061:
062: public static final String ANTCALL = "antcall";
063:
064: private boolean quiet = false;
065:
066: private boolean verbose = false;
067:
068: private Properties properties = null;
069:
070: private List<String> targets = null;
071:
072: private List<String> uninstallTargets = null;
073:
074: private String logFile = null;
075:
076: private String buildFile = null;
077:
078: private List<String> propertyFiles = null;
079:
080: /**
081: * Default constructor
082: */
083: public AntAction() {
084: super ();
085: properties = new Properties();
086: targets = new ArrayList<String>();
087: uninstallTargets = new ArrayList<String>();
088: propertyFiles = new ArrayList<String>();
089: }
090:
091: /**
092: * Performs all defined install actions.
093: *
094: * Calls {#performAction performAction(false)}.
095: *
096: * @throws Exception
097: */
098: public void performInstallAction() throws Exception {
099: performAction(false);
100: }
101:
102: /**
103: * Performs all defined uninstall actions.
104: *
105: * Calls {#performAction performAction(true)}.
106: *
107: * @throws Exception
108: */
109: public void performUninstallAction() throws Exception {
110: performAction(true);
111: }
112:
113: /**
114: * Performs all defined actions.
115: *
116: * @param uninstall An install/uninstall switch. If this is <tt>true</tt> only the uninstall
117: * actions, otherwise only the install actions are being performed.
118: *
119: * @see #performInstallAction() for calling all install actions.
120: * @see #performUninstallAction() for calling all uninstall actions.
121: *
122: * @throws Exception
123: */
124: public void performAction(boolean uninstall) throws Exception {
125: if (verbose)
126: System.out.println("Calling ANT with buildfile: "
127: + buildFile);
128: SecurityManager oldsm = null;
129: if (!JavaEnvUtils.isJavaVersion("1.0")
130: && !JavaEnvUtils.isJavaVersion("1.1"))
131: oldsm = System.getSecurityManager();
132: PrintStream err = System.err;
133: PrintStream out = System.out;
134: try {
135: Project antProj = new Project();
136: antProj.setName("antcallproject");
137: antProj.addBuildListener(createLogger());
138: antProj.setInputHandler(new DefaultInputHandler());
139: antProj.setSystemProperties();
140: addProperties(antProj, getProperties());
141: addPropertiesFromPropertyFiles(antProj);
142: // TODO: propertyfiles, logFile
143: antProj.fireBuildStarted();
144: antProj.init();
145: List<Ant> antcalls = new ArrayList<Ant>();
146: List<String> choosenTargets = (uninstall) ? uninstallTargets
147: : targets;
148: if (choosenTargets.size() > 0) {
149: Ant antcall = null;
150: for (String choosenTarget : choosenTargets) {
151: antcall = (Ant) antProj.createTask("ant");
152: antcall.setAntfile(getBuildFile());
153: antcall.setTarget(choosenTarget);
154: antcalls.add(antcall);
155: }
156: }
157: Target target = new Target();
158: target.setName("calltarget");
159:
160: for (Ant antcall : antcalls) {
161: target.addTask(antcall);
162: }
163: antProj.addTarget(target);
164: System.setOut(new PrintStream(new DemuxOutputStream(
165: antProj, false)));
166: System.setErr(new PrintStream(new DemuxOutputStream(
167: antProj, true)));
168: antProj.executeTarget("calltarget");
169: } finally {
170: if (oldsm != null)
171: System.setSecurityManager(oldsm);
172: System.setOut(out);
173: System.setErr(err);
174: }
175: }
176:
177: /**
178: * Returns the build file.
179: *
180: * @return the build file
181: */
182: public String getBuildFile() {
183: return buildFile;
184: }
185:
186: /**
187: * Sets the build file to be used to the given string.
188: *
189: * @param buildFile build file path to be used
190: */
191: public void setBuildFile(String buildFile) {
192: this .buildFile = buildFile;
193: }
194:
195: /**
196: * Returns the current logfile path as string.
197: *
198: * @return current logfile path
199: */
200: public String getLogFile() {
201: return logFile;
202: }
203:
204: /**
205: * Sets the logfile path to the given string.
206: *
207: * @param logFile to be set
208: */
209: public void setLogFile(String logFile) {
210: this .logFile = logFile;
211: }
212:
213: /**
214: * Returns the property file paths as list of strings.
215: *
216: * @return the property file paths
217: */
218: public List<String> getPropertyFiles() {
219: return propertyFiles;
220: }
221:
222: /**
223: * Adds one property file path to the internal list of property file paths.
224: *
225: * @param propertyFile to be added
226: */
227: public void addPropertyFile(String propertyFile) {
228: this .propertyFiles.add(propertyFile);
229: }
230:
231: /**
232: * Sets the property file path list to the given list. Old settings will be lost.
233: *
234: * @param propertyFiles list of property file paths to be set
235: */
236: public void setPropertyFiles(List<String> propertyFiles) {
237: this .propertyFiles = propertyFiles;
238: }
239:
240: /**
241: * Returns the properties.
242: *
243: * @return the properties
244: */
245: public Properties getProperties() {
246: return properties;
247: }
248:
249: /**
250: * Sets the internal properties to the given properties. Old settings will be lost.
251: *
252: * @param properties properties to be set
253: */
254: public void setProperties(Properties properties) {
255: this .properties = properties;
256: }
257:
258: /**
259: * Sets the given value to the property identified by the given name.
260: *
261: * @param name key of the property
262: * @param value value to be used for the property
263: */
264: public void setProperty(String name, String value) {
265: this .properties.put(name, value);
266: }
267:
268: /**
269: * Returns the value for the property identified by the given name.
270: *
271: * @param name name of the property
272: * @return value of the property
273: */
274: public String getProperty(String name) {
275: return this .properties.getProperty(name);
276: }
277:
278: /**
279: * Returns the quiet state.
280: *
281: * @return quiet state
282: */
283: public boolean isQuiet() {
284: return quiet;
285: }
286:
287: /**
288: * Sets whether the associated ant task should be performed quiet or not.
289: *
290: * @param quiet quiet state to set
291: */
292: public void setQuiet(boolean quiet) {
293: this .quiet = quiet;
294: }
295:
296: /**
297: * Returns the targets.
298: *
299: * @return the targets
300: */
301: public List<String> getTargets() {
302: return targets;
303: }
304:
305: /**
306: * Sets the targets which should be performed at installation time. Old settings are lost.
307: *
308: * @param targets list of targets
309: */
310: public void setTargets(ArrayList<String> targets) {
311: this .targets = targets;
312: }
313:
314: /**
315: * Adds the given target to the target list which should be performed at installation time.
316: *
317: * @param target target to be add
318: */
319: public void addTarget(String target) {
320: this .targets.add(target);
321: }
322:
323: /**
324: * Returns the uninstaller targets.
325: *
326: * @return the uninstaller targets
327: */
328: public List<String> getUninstallTargets() {
329: return uninstallTargets;
330: }
331:
332: /**
333: * Sets the targets which should be performed at uninstallation time. Old settings are lost.
334: *
335: * @param targets list of targets
336: */
337: public void setUninstallTargets(ArrayList<String> targets) {
338: this .uninstallTargets = targets;
339: }
340:
341: /**
342: * Adds the given target to the target list which should be performed at uninstallation time.
343: *
344: * @param target target to be add
345: */
346: public void addUninstallTarget(String target) {
347: this .uninstallTargets.add(target);
348: }
349:
350: /**
351: * Returns the verbose state.
352: *
353: * @return verbose state
354: */
355: public boolean isVerbose() {
356: return verbose;
357: }
358:
359: /**
360: * Sets the verbose state.
361: *
362: * @param verbose state to be set
363: */
364: public void setVerbose(boolean verbose) {
365: this .verbose = verbose;
366: }
367:
368: private BuildLogger createLogger() {
369: int msgOutputLevel = 2;
370: if (verbose)
371: msgOutputLevel = 4;
372: else if (quiet)
373: msgOutputLevel = 1;
374: BuildLogger logger = new DefaultLogger();
375: logger.setMessageOutputLevel(msgOutputLevel);
376: if (logFile != null) {
377: PrintStream printStream;
378: try {
379: printStream = new PrintStream(new FileOutputStream(
380: logFile));
381: logger.setOutputPrintStream(printStream);
382: logger.setErrorPrintStream(printStream);
383: } catch (FileNotFoundException e) {
384: logger.setOutputPrintStream(System.out);
385: logger.setErrorPrintStream(System.err);
386: }
387: } else {
388: logger.setOutputPrintStream(System.out);
389: logger.setErrorPrintStream(System.err);
390: }
391: return logger;
392: }
393:
394: private void addProperties(Project proj, Properties props) {
395: if (proj == null)
396: return;
397: if (props.size() > 0) {
398: Iterator iter = props.keySet().iterator();
399: String key = null;
400: while (iter.hasNext()) {
401: key = (String) iter.next();
402: proj.setProperty(key, props.getProperty(key));
403: }
404: }
405: }
406:
407: private void addPropertiesFromPropertyFiles(Project proj)
408: throws Exception {
409: if (proj == null)
410: return;
411: Properties props = new Properties();
412: File pf = null;
413: FileInputStream fis = null;
414: try {
415: for (String propertyFile : propertyFiles) {
416: pf = new File(propertyFile);
417: if (pf.exists()) {
418: fis = new FileInputStream(pf);
419: props.load(fis);
420: fis.close();
421: } else {
422: throw new Exception("Required propertyfile " + pf
423: + " for antcall doesn't exist.");
424: }
425: }
426: } finally {
427: if (fis != null)
428: fis.close();
429: }
430: addProperties(proj, props);
431: }
432:
433: }
|