source: release-kits/lirk3/bin/ant-installer/src/org/tp23/antinstaller/antmod/RuntimeLauncher.java@ 14982

Last change on this file since 14982 was 14982, checked in by oranfry, 16 years ago

initial import of LiRK3

File size: 4.7 KB
Line 
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) {
100 ctx.log("No enclosing jar found");
101 }
102 }
103
104 //Scripted install requirements
105 if(ExecInstall.CONFIG_RESOURCE == ctx.getConfigResource()){
106 File buildXml = new File(ctx.getFileRoot(), ctx.getAntBuildFile());
107 helper.parse(project, buildXml);
108 if(!buildXml.exists()){
109 ctx.log("No build file found??: " + buildXml);
110 }
111 project.setUserProperty("ant.file", buildXml.getAbsolutePath());
112 }
113
114 project.setBaseDir(ctx.getFileRoot());
115
116 // clever stuff for callbacks
117 project.addReference(CONTEXT_REFERENCE, ctx);
118 project.addTaskDefinition("antinstaller-property", PropertyTask.class);
119 project.addTaskDefinition("antinstaller-message", MessageTask.class);
120 project.addTaskDefinition("antinstaller-log", LogTask.class);
121
122 }
123 /**
124 * Run the launcher to launch Ant with a specific target, there is no classpath
125 * additions set or ant.home; everything should be loaded for this to run correctly.
126 *
127 * @param args the command line arguments
128 */
129 public int run(String target){
130 try {
131 ctx.getLogger().log("internal target execution started:" + target);
132 project.fireBuildStarted();
133 project.executeTarget(target);
134 project.fireBuildFinished(null);
135 ctx.getLogger().log("internal target execution successful:" + target);
136 return 0;
137 }
138 catch (Throwable t) {
139 ctx.getLogger().log("internal target execution error:" + target);
140 ctx.getLogger().log(ctx.getInstaller(), t);
141 return 1;
142 }
143 }
144}
Note: See TracBrowser for help on using the repository browser.