001: /*
002: * Copyright 2005 Paul Hinds
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.tp23.antinstaller.runtime.exe;
017:
018: import java.util.List;
019: import java.util.Map;
020:
021: import org.tp23.antinstaller.InstallException;
022: import org.tp23.antinstaller.InstallerContext;
023: import org.tp23.antinstaller.AIResourceBundleHelper;
024: import org.tp23.antinstaller.antmod.Launcher;
025:
026: /**
027: *
028: * <p>Runs the Ant script using the Apache Ant launcher.</p>
029: * <p>This runner uses a modified version of the Apache launcher to provide us
030: * with feed back as to the state of the install </p>
031: * <p>Copyright: Copyright (c) 2004</p>
032: * <p>Company: tp23</p>
033: * @todo get better feed back and progress for the targets that have run successfully
034: * @todo this should be an interface not a class
035: * @author Paul Hinds
036: * @version $Id: AntLauncherFilter.java,v 1.6 2007/01/12 14:47:44 anothermwilson Exp $
037: */
038: public class AntLauncherFilter implements ExecuteFilter {
039:
040: private static final AIResourceBundleHelper resHelper = new AIResourceBundleHelper();
041:
042: public AntLauncherFilter() {
043: }
044:
045: public void exec(InstallerContext ctx) throws InstallException {
046: if (ctx.getInstaller().isVerbose())
047: ctx.log("Starting Ant Launcher");
048:
049: try {
050:
051: //TODO this should be refactored to installer
052: List argsList = ctx.getInstaller().getTargets(ctx);
053:
054: String[] argsArr = new String[argsList.size() + 4];
055: argsList.toArray(argsArr);
056:
057: if (ctx.getInstaller().isVerbose()) {
058: ctx.log("Running targets:" + printArray(argsArr));
059: }
060: System.out.println("Targets:" + printArray(argsArr));
061:
062: argsArr[argsArr.length - 2] = "-lib";
063: argsArr[argsArr.length - 1] = "antlib";
064:
065: argsArr[argsArr.length - 4] = "-buildfile";
066: argsArr[argsArr.length - 3] = ctx.getFileRoot()
067: .getAbsolutePath()
068: + System.getProperty("file.separator")
069: + ctx.getAntBuildFile();
070:
071: //Launcher uses stdout and stderr by default
072: System.setOut(ctx.getAntOutputRenderer().getOut());
073: System.setErr(ctx.getAntOutputRenderer().getErr());
074:
075: Map properties = ctx.getInstaller().getResultContainer()
076: .getAllProperties();
077: Launcher launcher = new Launcher(properties);
078: int ok = launcher.run(argsArr, ctx);
079: if (ok != 0) {
080: throw new InstallException(resHelper
081: .getMessage("ant.failure"));
082: //the default ctx.setInstallSucceded(false);
083: } else {
084: ctx.setInstallSucceded(true);
085: }
086: ctx.log("Ant finished");
087: ctx.getRunner().antFinished();
088: } catch (Throwable ex) {
089: throw new InstallException("Error running the install, "
090: + ex.getMessage(), ex);
091: }
092: }
093:
094: /**
095: * Used for debug to print the targets to system.out
096: * @param args Object[]
097: * @return String
098: */
099: private String printArray(Object[] args) {
100: StringBuffer sb = new StringBuffer();
101: for (int i = 0; i < args.length - 4; i++) {
102: if (i > 0) {
103: sb.append(',');
104: }
105: sb.append(args[i]);
106: }
107: return sb.toString();
108: }
109: }
|