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 | */
|
---|
17 | package org.tp23.antinstaller.antmod;
|
---|
18 |
|
---|
19 | import java.io.File;
|
---|
20 | import java.net.URL;
|
---|
21 | import java.util.HashMap;
|
---|
22 | import java.util.Iterator;
|
---|
23 | import java.util.Map;
|
---|
24 |
|
---|
25 | import org.apache.tools.ant.Project;
|
---|
26 | import org.apache.tools.ant.ProjectHelper;
|
---|
27 | import org.tp23.antinstaller.InstallerContext;
|
---|
28 | import org.tp23.antinstaller.antmod.taskdefs.LogTask;
|
---|
29 | import org.tp23.antinstaller.antmod.taskdefs.MessageTask;
|
---|
30 | import org.tp23.antinstaller.antmod.taskdefs.PropertyTask;
|
---|
31 | import org.tp23.antinstaller.runtime.ExecInstall;
|
---|
32 | import org.tp23.antinstaller.selfextract.NonExtractor;
|
---|
33 | import 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 | */
|
---|
46 | public 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 | }
|
---|