001: /*
002: * Copyright 2003-2004 The Apache Software Foundation
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: */
017: package org.tp23.antinstaller.antmod;
018:
019: import java.io.File;
020: import java.net.URL;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.Map;
024:
025: import org.apache.tools.ant.Project;
026: import org.apache.tools.ant.ProjectHelper;
027: import org.tp23.antinstaller.InstallerContext;
028: import org.tp23.antinstaller.antmod.taskdefs.CallTargetTask;
029: import org.tp23.antinstaller.antmod.taskdefs.GetResourceTask;
030: import org.tp23.antinstaller.antmod.taskdefs.LogTask;
031: import org.tp23.antinstaller.antmod.taskdefs.MessageTask;
032: import org.tp23.antinstaller.antmod.taskdefs.PropertyTask;
033: import org.tp23.antinstaller.runtime.ExecInstall;
034: import org.tp23.antinstaller.selfextract.NonExtractor;
035: import org.tp23.antinstaller.selfextract.SelfExtractor;
036:
037: /**
038: * This is a launcher for Ant which swallows all messages and logs.
039: *
040: * This file has been modified by Paul Hinds for Antinstaller and is not the same
041: * as the one delivered with Ant 1.6
042: *
043: * @since Ant 1.6
044: * @version $Id$
045: */
046: public class RuntimeLauncher {
047:
048: public final static String CONTEXT_REFERENCE = "antinstaller.internal.context";
049:
050: private final Map allProperties = new HashMap();
051: private final Project project = new Project();
052: private InstallerContext ctx;
053:
054: public RuntimeLauncher(InstallerContext ctx) {
055: this .ctx = ctx;
056: }
057:
058: public void updateProps() {
059: allProperties.clear();
060: allProperties.putAll(InstallerContext.getEnvironment());
061: allProperties.putAll(ctx.getInstaller().getResultContainer()
062: .getAllProperties());
063: // add properties
064: String arg;
065: String value;
066: Iterator iter = allProperties.keySet().iterator();
067: while (iter.hasNext()) {
068: arg = (String) iter.next();
069: value = (String) allProperties.get(arg);
070: project.setUserProperty(arg, value);
071: }
072: }
073:
074: public void parseProject() {
075: project.setCoreLoader(this .getClass().getClassLoader());
076: //project.addBuildListener(this);
077: project.init();
078:
079: ProjectHelper helper = new ProjectHelper3();
080: project.addReference("ant.projectHelper", helper);
081:
082: //SelfExtractor requirements
083: if (SelfExtractor.CONFIG_RESOURCE == ctx.getConfigResource()) {
084: File buildXml = new File(ctx.getFileRoot(), ctx
085: .getAntBuildFile());
086: if (!buildXml.exists()) {
087: ctx.log("No build file found??: " + buildXml);
088: }
089: helper.parse(project, buildXml);
090: project.setUserProperty("ant.file", buildXml
091: .getAbsolutePath());
092: }
093:
094: //NonExtractor requirements
095: if (NonExtractor.CONFIG_RESOURCE == ctx.getConfigResource()) {
096: URL buildIS = this .getClass().getResource(
097: "/" + ctx.getAntBuildFile());
098: helper.parse(project, buildIS);
099: project.setUserProperty("ant.file", buildIS
100: .toExternalForm());
101: try {
102: File enclosingJar = SelfExtractor.getEnclosingJar(this );
103: project.setUserProperty(
104: NonExtractor.ANTINSTALLER_JAR_PROPERTY,
105: enclosingJar.getAbsolutePath());
106: } catch (Exception e) {
107: ctx.log("No enclosing jar found");
108: }
109: }
110:
111: //Scripted install requirements
112: if (ExecInstall.CONFIG_RESOURCE == ctx.getConfigResource()) {
113: File buildXml = new File(ctx.getFileRoot(), ctx
114: .getAntBuildFile());
115: helper.parse(project, buildXml);
116: if (!buildXml.exists()) {
117: ctx.log("No build file found??: " + buildXml);
118: }
119: project.setUserProperty("ant.file", buildXml
120: .getAbsolutePath());
121: }
122:
123: project.setBaseDir(ctx.getFileRoot());
124:
125: // clever stuff for callbacks
126: project.addReference(CONTEXT_REFERENCE, ctx);
127: project.addTaskDefinition("antinstaller-calltarget",
128: CallTargetTask.class);
129: project.addTaskDefinition("antinstaller-property",
130: PropertyTask.class);
131: project.addTaskDefinition("antinstaller-message",
132: MessageTask.class);
133: project.addTaskDefinition("antinstaller-log", LogTask.class);
134: project.addTaskDefinition("antinstaller-getresource",
135: GetResourceTask.class);
136:
137: }
138:
139: /**
140: * Run the launcher to launch Ant with a specific target, there is no classpath
141: * additions set or ant.home; everything should be loaded for this to run correctly.
142: *
143: * @param args the command line arguments
144: * @return 0 for success 1 for exception
145: */
146: public int run(String target) {
147: try {
148: ctx.getLogger().log(
149: "internal target execution started:" + target);
150: project.fireBuildStarted();
151: project.executeTarget(target);
152: project.fireBuildFinished(null);
153: ctx.getLogger().log(
154: "internal target execution successful:" + target);
155: return 0;
156: } catch (Throwable t) {
157: ctx.getLogger().log(
158: "internal target execution error:" + target);
159: ctx.getLogger().log(ctx.getInstaller(), t);
160: return 1;
161: }
162: }
163: }
|