1 /*
2  * Copyright  2003-2004 The Apache Software Foundation
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 *
16 */
17package org.tp23.antinstaller.antmod;
18
19import java.io.File;
20import java.net.URL;
21import java.util.HashMap;
22import java.util.Iterator;
23import java.util.Map;
24
25import org.apache.tools.ant.Project;
26import org.apache.tools.ant.ProjectHelper;
27import org.tp23.antinstaller.InstallerContext;
28import org.tp23.antinstaller.antmod.taskdefs.LogTask;
29import org.tp23.antinstaller.antmod.taskdefs.MessageTask;
30import org.tp23.antinstaller.antmod.taskdefs.PropertyTask;
31import org.tp23.antinstaller.runtime.ExecInstall;
32import org.tp23.antinstaller.selfextract.NonExtractor;
33import org.tp23.antinstaller.selfextract.SelfExtractor;
34
35
36
37/**
38 * This is a launcher for Ant which swallows all messages and logs.
39 *
40 * This file has been modified by Paul Hinds for Antinstaller and is not the same
41 * as the one delivered with Ant 1.6
42 *
43 * @since Ant 1.6
44 * @version $Id$
45 */
46public class RuntimeLauncher {
47
48    public final static String CONTEXT_REFERENCE = "antinstaller.internal.context";
49
50    private final Map allProperties = new HashMap();
51    private final Project project = new Project();
52    private InstallerContext ctx;
53
54    public RuntimeLauncher(InstallerContext ctx) {
55        this.ctx = ctx;
56    }
57
58    public void updateProps(){
59        allProperties.clear();
60        allProperties.putAll(InstallerContext.getEnvironment());
61        allProperties.putAll(ctx.getInstaller().getResultContainer().getAllProperties());
62        // add properties
63        String arg;
64        String value;
65        Iterator iter = allProperties.keySet().iterator();
66        while (iter.hasNext()) {
67            arg = (String) iter.next();
68            value = (String) allProperties.get(arg);
69            project.setUserProperty(arg, value);
70        }
71    }
72
73    public void parseProject(){
74        project.setCoreLoader(this.getClass().getClassLoader());
75        //project.addBuildListener(this);
76        project.init();
77
78        ProjectHelper helper = new ProjectHelper3();
79        project.addReference("ant.projectHelper", helper);
80        
81        //SelfExtractor requirements
82        if(SelfExtractor.CONFIG_RESOURCE == ctx.getConfigResource()){
83            File buildXml = new File(ctx.getFileRoot(), ctx.getAntBuildFile());
84            if(!buildXml.exists()){
85                ctx.log("No build file found??: " + buildXml);
86            }
87            helper.parse(project, buildXml);
88            project.setUserProperty("ant.file", buildXml.getAbsolutePath());
89        }
90
91        //NonExtractor requirements
92        if(NonExtractor.CONFIG_RESOURCE == ctx.getConfigResource()){
93            URL buildIS = this.getClass().getResource("/" + ctx.getAntBuildFile());
94            helper.parse(project, buildIS);
95            project.setUserProperty("ant.file", buildIS.toExternalForm());
96            try {
97                File enclosingJar = SelfExtractor.getEnclosingJar(this);
98                project.setUserProperty(NonExtractor.ANTINSTALLER_JAR_PROPERTY, enclosingJar.getAbsolutePath());
99            } catch (RuntimeException e) {
00                ctx.log("No enclosing jar found");
01            }
02        }
03        
04        //Scripted install requirements
05        if(ExecInstall.CONFIG_RESOURCE == ctx.getConfigResource()){
06            File buildXml = new File(ctx.getFileRoot(), ctx.getAntBuildFile());
07            helper.parse(project, buildXml);
08            if(!buildXml.exists()){
09                ctx.log("No build file found??: " + buildXml);
10            }
11            project.setUserProperty("ant.file", buildXml.getAbsolutePath());
12        }
13        
14        project.setBaseDir(ctx.getFileRoot());
15        
16        // clever stuff for callbacks
17        project.addReference(CONTEXT_REFERENCE, ctx);
18        project.addTaskDefinition("antinstaller-property", PropertyTask.class);
19        project.addTaskDefinition("antinstaller-message", MessageTask.class);
20        project.addTaskDefinition("antinstaller-log", LogTask.class);
21
22    }
23    /**
24     * Run the launcher to launch Ant with a specific target, there is no classpath
25     * additions set or ant.home; everything should be loaded for this to run correctly.
26     *
27     * @param args the command line arguments
28     */
29    public int run(String target){
30        try {
31            ctx.getLogger().log("internal target execution started:" + target);
32            project.fireBuildStarted();
33            project.executeTarget(target);
34            project.fireBuildFinished(null);
35            ctx.getLogger().log("internal target execution successful:" + target);
36            return 0;
37        }
38        catch (Throwable t) {
39            ctx.getLogger().log("internal target execution error:" + target);
40            ctx.getLogger().log(ctx.getInstaller(), t);
41            return 1;
42        }
43    }
44}
45