1 /* 
2  * Copyright 2005 Paul Hinds
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.tp23.antinstaller.runtime.exe;
17
18import java.util.List;
19import java.util.Map;
20
21import org.tp23.antinstaller.InstallException;
22import org.tp23.antinstaller.InstallerContext;
23import org.tp23.antinstaller.ResourceBundleHelper;
24import org.tp23.antinstaller.antmod.Launcher;
25
26/**
27 *
28 * <p>Runs the Ant script using the Apache Ant launcher.</p>
29 * <p>This runner uses a modified version of the Apache launcher to provide us
30 * with feed back as to the state of the install </p>
31 * <p>Copyright: Copyright (c) 2004</p>
32 * <p>Company: tp23</p>
33 * @todo get better feed back and progress for the targets that have run successfully
34 * @todo this should be an interface not a class
35 * @author Paul Hinds
36 * @version $Id: AntLauncherFilter.java,v 1.6 2007/01/12 14:47:44 anothermwilson Exp $
37 */
38public class AntLauncherFilter implements ExecuteFilter {
39
40    private static final ResourceBundleHelper resHelper = new ResourceBundleHelper("org.tp23.antinstaller.renderer.Res");
41
42
43    public AntLauncherFilter() {
44    }
45
46    public void exec(InstallerContext ctx) throws InstallException {
47        if(ctx.getInstaller().isVerbose())ctx.log("Starting Ant Launcher");
48
49        try {
50
51            //TODO this should be refactored to installer
52            List argsList = ctx.getInstaller().getTargets(ctx);
53            
54            String[] argsArr = new String[argsList.size() + 4];
55            argsList.toArray(argsArr);
56
57            if (ctx.getInstaller().isVerbose()) {
58                ctx.log("Running targets:" + printArray(argsArr));
59            }
60            System.out.println("Targets:"+printArray(argsArr));
61
62            argsArr[argsArr.length-2] = "-lib";
63            argsArr[argsArr.length-1] = "antlib";
64
65            argsArr[argsArr.length-4] = "-buildfile";
66            argsArr[argsArr.length - 3] = ctx.getFileRoot().getAbsolutePath() +
67                    System.getProperty("file.separator") +
68                    ctx.getAntBuildFile();
69
70            //Launcher uses stdout and stderr by default
71            System.setOut(ctx.getAntOutputRenderer().getOut());
72            System.setErr(ctx.getAntOutputRenderer().getErr());
73            
74            Map properties = ctx.getInstaller().getResultContainer().getAllProperties();
75            Launcher launcher = new Launcher(properties);
76            int ok = launcher.run(argsArr, ctx);
77            if(ok!=0) {
78                throw new InstallException( resHelper.getMessage( "ant.failure" ) );
79                //the default ctx.setInstallSucceded(false);
80            }
81            else {
82                ctx.setInstallSucceded(true);
83            }
84            ctx.log("Ant finished");
85            ctx.getRunner().antFinished();
86        }
87        catch (Throwable ex) {
88            throw new InstallException("Error running the install, " + ex.getMessage(), ex);
89        }
90    }
91    /**
92     * Used for debug to print the targets to system.out
93     * @param args Object[]
94     * @return String
95     */
96    private String printArray(Object[] args){
97        StringBuffer sb = new StringBuffer();
98        for (int i = 0; i < args.length-4; i++) {
99            if (i > 0) {
00                sb.append(',');
01            }
02            sb.append(args[i]);
03        }
04        return sb.toString();
05    }
06}
07