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 | */
|
---|
16 | package org.tp23.antinstaller.runtime.exe;
|
---|
17 |
|
---|
18 | import java.io.File;
|
---|
19 | import java.io.FileOutputStream;
|
---|
20 | import java.io.IOException;
|
---|
21 | import java.io.InputStream;
|
---|
22 | import java.io.PrintStream;
|
---|
23 | import java.net.MalformedURLException;
|
---|
24 | import java.net.URL;
|
---|
25 | import java.net.URLClassLoader;
|
---|
26 | import java.util.Iterator;
|
---|
27 | import java.util.Map;
|
---|
28 | import java.util.Properties;
|
---|
29 |
|
---|
30 | import org.apache.tools.ant.BuildException;
|
---|
31 | import org.apache.tools.ant.BuildListener;
|
---|
32 | import org.apache.tools.ant.DefaultLogger;
|
---|
33 | import org.apache.tools.ant.Diagnostics;
|
---|
34 | import org.apache.tools.ant.Project;
|
---|
35 | import org.apache.tools.ant.ProjectHelper;
|
---|
36 | import org.apache.tools.ant.input.DefaultInputHandler;
|
---|
37 | import org.apache.tools.ant.launch.Locator;
|
---|
38 | import org.tp23.antinstaller.InstallException;
|
---|
39 | import org.tp23.antinstaller.InstallerContext;
|
---|
40 | import org.tp23.antinstaller.antmod.ProjectHelper3;
|
---|
41 | import org.tp23.antinstaller.selfextract.NonExtractor;
|
---|
42 | import org.tp23.antinstaller.selfextract.SelfExtractor;
|
---|
43 | /**
|
---|
44 | *
|
---|
45 | * <p>This AntRunner runs Ant builds directly from a Jar without having to extract
|
---|
46 | * the build.xml to temporary space.</p>
|
---|
47 | * <p> </p>
|
---|
48 | * <p>Copyright: Copyright (c) 2004</p>
|
---|
49 | * <p>Company: tp23</p>
|
---|
50 | * @author Paul Hinds
|
---|
51 | * @version $Id: AntProjectFilter.java,v 1.10 2007/01/28 08:44:39 teknopaul Exp $
|
---|
52 | */
|
---|
53 | public class AntProjectFilter implements ExecuteFilter{
|
---|
54 |
|
---|
55 | //TODO certain jars are added from Ant default directories that are probably not needed
|
---|
56 |
|
---|
57 | private static String antVersion = null;
|
---|
58 |
|
---|
59 | /** The Ant Home property - from default Launcher */
|
---|
60 | public static final String ANTHOME_PROPERTY = "ant.home";
|
---|
61 |
|
---|
62 | /** The location of a per-user library directory - from default Launcher */
|
---|
63 | public static final String USER_LIBDIR = ".ant/lib";
|
---|
64 |
|
---|
65 | public AntProjectFilter() {
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * run Ant
|
---|
70 | *
|
---|
71 | * @param ctx InstallerContext
|
---|
72 | * @throws InstallException
|
---|
73 | * @todo Implement this org.tp23.antinstaller.runtime.AntRunner method
|
---|
74 | */
|
---|
75 | public void exec(InstallerContext ctx) throws InstallException {
|
---|
76 | if(ctx.getInstaller().isVerbose()) {
|
---|
77 | ctx.log("Starting Ant Project");
|
---|
78 | }
|
---|
79 |
|
---|
80 | try {
|
---|
81 |
|
---|
82 | Project project = new Project();
|
---|
83 | appendClassPath();
|
---|
84 | setAntHome(ctx);
|
---|
85 | project.setCoreLoader(this.getClass().getClassLoader());
|
---|
86 |
|
---|
87 | DefaultLogger antLogger = new DefaultLogger();
|
---|
88 | antLogger.setOutputPrintStream(ctx.getAntOutputRenderer().getOut());
|
---|
89 | antLogger.setErrorPrintStream(ctx.getAntOutputRenderer().getErr());
|
---|
90 | antLogger.setMessageOutputLevel(Project.MSG_INFO);
|
---|
91 | BuildListener bl = ctx.getBuildListener();
|
---|
92 | if(bl != null){
|
---|
93 | project.addBuildListener(bl);
|
---|
94 | }
|
---|
95 | project.addBuildListener(antLogger);
|
---|
96 |
|
---|
97 | /*
|
---|
98 | * Log useful ant task output to the log file to help debugging
|
---|
99 | * and for customer support
|
---|
100 | */
|
---|
101 | final String logFileName = ctx.getLogger().getFileName();
|
---|
102 | if( logFileName != null && logFileName.length() > 0 )
|
---|
103 | {
|
---|
104 | PrintStream stream = new PrintStream( new FileOutputStream(logFileName, true), true);
|
---|
105 | DefaultLogger antFileLogger = new DefaultLogger();
|
---|
106 | antFileLogger.setOutputPrintStream( stream );
|
---|
107 | antFileLogger.setErrorPrintStream( stream );
|
---|
108 | int logLevel = (ctx.getInstaller().isVerbose())
|
---|
109 | ? Project.MSG_VERBOSE
|
---|
110 | : Project.MSG_INFO;
|
---|
111 | antFileLogger.setMessageOutputLevel( logLevel );
|
---|
112 |
|
---|
113 | project.addBuildListener( antFileLogger );
|
---|
114 | }
|
---|
115 |
|
---|
116 | // irrelevant really but might help someone on a command line
|
---|
117 | project.setInputHandler(new DefaultInputHandler());
|
---|
118 | project.fireBuildStarted();
|
---|
119 |
|
---|
120 | project.init();
|
---|
121 | project.setUserProperty("ant.version", getAntVersion());
|
---|
122 |
|
---|
123 |
|
---|
124 | // add properties
|
---|
125 | // N.B. properties are not loaded from the file it exists for debugging installers
|
---|
126 | String arg;
|
---|
127 | String value;
|
---|
128 | Map properties = ctx.getInstaller().getResultContainer().getAllProperties();
|
---|
129 | Iterator iter = properties.keySet().iterator();
|
---|
130 | while (iter.hasNext()) {
|
---|
131 | arg = (String) iter.next();
|
---|
132 | value = (String) properties.get(arg);
|
---|
133 | project.setUserProperty(arg, value);
|
---|
134 | }
|
---|
135 |
|
---|
136 | // From here we immitate Main
|
---|
137 | try {
|
---|
138 | Diagnostics.validateVersion();
|
---|
139 | } catch (Throwable exc) {
|
---|
140 | // minimal messages for the benefit of the command line install
|
---|
141 | System.err.println("Version error:" + exc.getClass() + "," + exc.getMessage());
|
---|
142 | return;
|
---|
143 | }
|
---|
144 |
|
---|
145 | ProjectHelper helper = new ProjectHelper3();
|
---|
146 | project.addReference("ant.projectHelper", helper);
|
---|
147 |
|
---|
148 | File buildXml = new File(ctx.getFileRoot(), ctx.getAntBuildFile());
|
---|
149 | if(buildXml.exists()){
|
---|
150 | helper.parse(project, buildXml);
|
---|
151 | project.setUserProperty("ant.file",buildXml.getAbsolutePath());
|
---|
152 | } else {
|
---|
153 | URL buildIS = this.getClass().getResource("/" + ctx.getAntBuildFile());
|
---|
154 | helper.parse(project, buildIS);
|
---|
155 | project.setUserProperty("ant.file", buildIS.toExternalForm());
|
---|
156 | }
|
---|
157 |
|
---|
158 | File enclosingJar = SelfExtractor.getEnclosingJar(this);
|
---|
159 | project.setUserProperty(NonExtractor.ANTINSTALLER_JAR_PROPERTY , enclosingJar.getAbsolutePath());
|
---|
160 | System.out.println(NonExtractor.ANTINSTALLER_JAR_PROPERTY + enclosingJar.getAbsolutePath());
|
---|
161 |
|
---|
162 | //what is this !?! project.setKeepGoingMode(keepGoingMode);
|
---|
163 |
|
---|
164 | project.setBaseDir(ctx.getFileRoot());
|
---|
165 |
|
---|
166 | project.executeTargets(ctx.getInstaller().getTargets(ctx));
|
---|
167 | project.fireBuildFinished(null);
|
---|
168 | ctx.setInstallSucceded(true);
|
---|
169 | ctx.log("Ant finished");
|
---|
170 | }
|
---|
171 | catch (Throwable e) {
|
---|
172 | throw new InstallException("Error running the install, " + e.getMessage(), e);
|
---|
173 | }
|
---|
174 | finally {
|
---|
175 | ctx.getRunner().antFinished();
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | public static synchronized String getAntVersion() throws BuildException {
|
---|
181 | if (antVersion == null) {
|
---|
182 | try {
|
---|
183 | Properties props = new Properties();
|
---|
184 | InputStream in =
|
---|
185 | AntProjectFilter.class.getResourceAsStream("/org/apache/tools/ant/version.txt");
|
---|
186 | props.load(in);
|
---|
187 | in.close();
|
---|
188 |
|
---|
189 | StringBuffer msg = new StringBuffer();
|
---|
190 | msg.append("Apache Ant version ");
|
---|
191 | msg.append(props.getProperty("VERSION"));
|
---|
192 | msg.append(" compiled on ");
|
---|
193 | msg.append(props.getProperty("DATE"));
|
---|
194 | antVersion = msg.toString();
|
---|
195 | } catch (IOException ioe) {
|
---|
196 | throw new BuildException("Could not load the version information:"
|
---|
197 | + ioe.getMessage());
|
---|
198 | } catch (NullPointerException npe) {
|
---|
199 | throw new BuildException("Could not load the version information.");
|
---|
200 | }
|
---|
201 | }
|
---|
202 | return antVersion;
|
---|
203 | }
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * Append extra Ant jars to the classpath the original classpath
|
---|
207 | * is not removed incase the installer is launched from a script
|
---|
208 | *
|
---|
209 | */
|
---|
210 | private static void appendClassPath(){
|
---|
211 | try {
|
---|
212 | // now update the class.path property
|
---|
213 | StringBuffer baseClassPath
|
---|
214 | = new StringBuffer(System.getProperty("java.class.path"));
|
---|
215 | if (baseClassPath.charAt(baseClassPath.length() - 1)
|
---|
216 | == File.pathSeparatorChar) {
|
---|
217 | baseClassPath.setLength(baseClassPath.length() - 1);
|
---|
218 | }
|
---|
219 | URL[] jars = getLibPaths();
|
---|
220 | for (int i = 0; i < jars.length; ++i) {
|
---|
221 | baseClassPath.append(File.pathSeparatorChar);
|
---|
222 | baseClassPath.append(Locator.fromURI(jars[i].toString()));
|
---|
223 | }
|
---|
224 |
|
---|
225 | System.setProperty("java.class.path", baseClassPath.toString());
|
---|
226 |
|
---|
227 | URLClassLoader loader = new URLClassLoader(jars);
|
---|
228 | Thread.currentThread().setContextClassLoader(loader);
|
---|
229 | }
|
---|
230 | catch (MalformedURLException e) {
|
---|
231 | // swallow exception, normally all resources are already loaded
|
---|
232 | System.err.println("Invalid Jar path");
|
---|
233 | }
|
---|
234 | }
|
---|
235 |
|
---|
236 |
|
---|
237 | /**
|
---|
238 | * Ant home is not a requirement but can exist prior to loading
|
---|
239 | * the default Ant mechanism of using the current Jars parent
|
---|
240 | * is consipicuously absent, do not rely on ANT_HOME out side of a
|
---|
241 | * controlled environment (e.g. a normal install)
|
---|
242 | */
|
---|
243 | private static void setAntHome(InstallerContext ctx){
|
---|
244 | String antHomeProperty = System.getProperty(ANTHOME_PROPERTY);
|
---|
245 | if(antHomeProperty==null){
|
---|
246 | System.setProperty(ANTHOME_PROPERTY, ctx.getFileRoot().getAbsolutePath());
|
---|
247 | }
|
---|
248 | }
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * To maintain compatability with previous verisons currently the only
|
---|
252 | * Ant command line argument supported is the -lib parameter with the value
|
---|
253 | * "antlib"
|
---|
254 | * @TODO this should probably be removed
|
---|
255 | * @throws MalformedURLException
|
---|
256 | */
|
---|
257 | private static URL[] getLibPaths() throws MalformedURLException{
|
---|
258 |
|
---|
259 | // add all Jars from the ./antlib directory at the time of the build
|
---|
260 | // this is NOT based on ANT_HOME
|
---|
261 | URL[] libJars = Locator.getLocationURLs(new File("antlib"));
|
---|
262 |
|
---|
263 | // add all the Jars from ~/.ant/lib
|
---|
264 | // this is probably irrelevant for a normal install
|
---|
265 | URL[] userJars = Locator.getLocationURLs(new File(USER_LIBDIR));
|
---|
266 |
|
---|
267 | // Now try and find JAVA_HOME
|
---|
268 | File toolsJar = Locator.getToolsJar();
|
---|
269 |
|
---|
270 | int jarsLength = libJars.length + userJars.length + (toolsJar!=null?1:0);
|
---|
271 | URL[] allJars = new URL[jarsLength];
|
---|
272 | int i = 0;
|
---|
273 | if(toolsJar != null){
|
---|
274 | allJars[i++] = toolsJar.toURL();
|
---|
275 | }
|
---|
276 | if(libJars.length != 0){
|
---|
277 | System.arraycopy(libJars, 0, allJars, i, libJars.length);
|
---|
278 | i += libJars.length;
|
---|
279 | }
|
---|
280 | if(userJars.length != 0){
|
---|
281 | System.arraycopy(userJars, 0, allJars, i, userJars.length);
|
---|
282 | //i+=userJars.length;
|
---|
283 | //assert(allJars.length=i-1);
|
---|
284 | }
|
---|
285 | return libJars;
|
---|
286 | }
|
---|
287 |
|
---|
288 |
|
---|
289 | }
|
---|