001: /*
002: * Copyright 2005 Paul Hinds
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: package org.tp23.antinstaller.runtime;
017:
018: import java.io.File;
019:
020: import org.tp23.antinstaller.InstallException;
021: import org.tp23.antinstaller.InstallerContext;
022: import org.tp23.antinstaller.renderer.AIResourceBundle;
023: import org.tp23.antinstaller.renderer.MessageRenderer;
024: import org.tp23.antinstaller.runtime.exe.ExecuteFilter;
025: import org.tp23.antinstaller.runtime.exe.ExecuteRunnerFilter;
026: import org.tp23.antinstaller.runtime.exe.FilterChain;
027: import org.tp23.antinstaller.runtime.exe.FilterFactory;
028: import org.tp23.antinstaller.runtime.exe.FinalizerFilter;
029: import org.tp23.antinstaller.selfextract.SelfExtractor;
030:
031: /**
032: * This is the Applications entry point, it has a main method to run the
033: * installer. The main method is only for scripted installs.
034: *
035: * It is here that the command line options are parsed and it
036: * is determined which type of install (swing or text) will be run.
037: * <p>Reads the config, determines the runner, runs it and outputs the
038: * properties file, The Ant targets are then called by the AntRunner.
039: * This class also builds the internal Objects from the XML config file.</p>
040: * <p>This class can also be called by external tools to launch the installer
041: * currently two options are provided to launch from Jars. </p>
042: * <p>Copyright: Copyright (c) 2004</p>
043: * <p>Company: tp23</p>
044: * @author Paul Hinds
045: * @version $Id: ExecInstall.java,v 1.9 2007/01/19 00:24:36 teknopaul Exp $
046: */
047: public class ExecInstall {
048:
049: private static final AIResourceBundle res = new AIResourceBundle();
050: public static final String CONFIG_RESOURCE = "/org/tp23/antinstaller/runtime/exe/script.fconfig";
051:
052: private final InstallerContext ctx = new InstallerContext();
053: private FilterChain chain;
054: private boolean exitOnError = true;
055:
056: /**
057: * @param chain chain of filters to be executed
058: */
059: public ExecInstall(FilterChain chain) {
060: this .chain = chain;
061: }
062:
063: public InstallerContext getInstallerContext() {
064: return ctx;
065: }
066:
067: /**
068: * Execute the installer, this reads the config fetches a runner and runs the install.
069: * Once the install pages have finished an AntRunner is used to run Ant
070: */
071: public void exec() {
072:
073: ExecuteFilter[] filters = null;
074: try {
075: chain.init(ctx);
076: filters = chain.getFilters();
077: } catch (Exception e) {
078: // This is a developer error or the package has not been built correctly
079: // It should never happen in a tested build
080: e.printStackTrace();
081: System.exit(1); // called manually since in Win it was not shutting down properly
082: }
083: loop: for (int i = 0; i < filters.length; i++) {
084: try {
085: ctx.log("Filter: " + filters[i].getClass().getName());
086: filters[i].exec(ctx);
087: } catch (ExecuteRunnerFilter.AbortException abort) {
088: MessageRenderer vLogger = ctx.getMessageRenderer();
089: vLogger.printMessage(res.getString("install.aborted"));
090: ctx.log("Aborted");
091: FinalizerFilter ff = (FinalizerFilter) filters[filters.length - 1];
092: ff.exec(ctx);
093: if (exitOnError) {
094: System.exit(1);
095: }
096: } catch (Exception ex) {
097:
098: // write errors to the log
099: ctx.log("Installation error: " + ex.getMessage() + ": "
100: + ex.getClass().toString());
101: boolean verbose = true; // be verbose if we cant load the config
102: if (ctx.getInstaller() != null) {
103: verbose = ctx.getInstaller().isVerbose();
104: }
105: ctx.log(verbose, ex);
106:
107: // write detailed errors to stdout for the GUI screens and text
108: if (ctx.getRunner() instanceof TextRunner) {
109: if (verbose) {
110: ex.printStackTrace();
111: }
112: } else {
113: if (verbose) {
114: ex.printStackTrace(System.err);
115: }
116: }
117:
118: // report the error to the user
119: MessageRenderer vLogger = ctx.getMessageRenderer();
120: if (vLogger != null) {
121: vLogger.printMessage(res
122: .getString("installation.failed")
123: + "\n" + ex.getMessage());
124: //Fixed BUG:1295944 vLogger.printMessage("Install failed\n" + ex.getMessage());
125: } else {
126: System.err.println(res
127: .getString("installation.failed")
128: + ex.getClass().getName());
129: System.err.println(ex.getMessage());
130: }
131:
132: if (ctx.getRunner() != null) {
133: ctx.getRunner().fatalError();
134: break loop;
135: } else { // the screens did not even start e.g. XML config error
136: if (exitOnError) {
137: System.exit(1);
138: }
139: }
140: }
141: }
142: }
143:
144: /**
145: * <p>Runs the installer from a script.</p>
146: *
147: * This install can be run from a set of files for example from a CD.
148: * @see org.tp23.antinstaller.selfextract.NonExtractor
149: * @see org.tp23.antinstaller.selfextract.SelfExtractor
150: *
151: * @param args String[] args are "default" or "swing" or "text" followed by the root directory of the install
152: */
153: public static void main(String[] args) {
154: try {
155: FilterChain chain = FilterFactory.factory(CONFIG_RESOURCE);
156: ExecInstall installExec = new ExecInstall(chain);
157: if (installExec.parseArgs(args, true)) {
158: installExec.exec();
159: }
160: } catch (InstallException e) {
161: // Installer developer error
162: System.out
163: .println("Cant load filter chain:/org/tp23/antinstaller/runtime/exe/script.fconfig");
164: e.printStackTrace();
165: }
166: }
167:
168: /**
169: * This method has been designed for backward compatibility with
170: * existing scripts. The root dir is passed on the command line for scripted
171: * installs but is determined automatically for installs from self-extracting Jars
172: * @param args
173: * @param requiresRootDir set to true if the args must include the root directory
174: */
175: public boolean parseArgs(String[] args, boolean requiresRootDir) {
176: String uiOverride = null;
177: String installType = null;
178: String installRoot = null;
179:
180: int i = 0;
181: if (args.length > i && !args[i].startsWith("-")) {
182: uiOverride = args[i];
183: i++;
184: ctx.setUIOverride(uiOverride);
185: }
186:
187: if (requiresRootDir) {
188: if (args.length > i && !args[i].startsWith("-")) {
189: installRoot = args[i];
190: i++;
191: ctx.setFileRoot(new File(installRoot));
192: } else {
193: printUsage();
194: return false;
195: }
196: }
197: // additional params should all have a -something prefix
198: for (; i < args.length; i++) {
199: // RFE 1569628
200: if ("-type".equals(args[i]) && args.length > i + 1) {
201: installType = args[i + 1];
202: i++;
203: String configFileName = "antinstall-config-"
204: + installType + ".xml";
205: String buildFileName = "build-" + installType + ".xml";
206: ctx.setInstallerConfigFile(configFileName);
207: ctx.setAntBuildFile(buildFileName);
208: }
209: }
210:
211: return true;
212: }
213:
214: private static void printUsage() {
215: System.out
216: .println("Usage java -cp $CLASSPATH org.tp23.antinstaller.ExecInstall [text|swing|default] [install root] (-type [buildtype])");
217: }
218:
219: /**
220: * Sets the UI override from the command line
221: * @param installRoot
222: */
223: // public void setUIOverride(String override) {
224: // ctx.setUIOverride(override);
225: // }
226: /**
227: * This is generated by the Main class which knows where it has
228: * extracted or where it has run from
229: * @param installRoot
230: */
231: public void setInstallRoot(File installRoot) {
232: ctx.setFileRoot(installRoot);
233: }
234:
235: /**
236: * This is AntInstalls temporary space which will generally be deleted
237: * except in debug mode when it is left to view the build process.
238: * installRoot and tempRoot can be the same if the directory
239: * is a new empty directory
240: * @param tempDir directory to be used for temporary storage
241: */
242: public void setTempRoot(File tempDir) {
243: addShutdownHook(tempDir);
244: }
245:
246: /**
247: * This shutdown hook is to facilitate debugging the app can be left open
248: * in the GUI view and the resources will not be deleted. Upon exit
249: * temporary files will be removed. This is required because the
250: * deleteOnExit() method fails if the directory is filled with files.
251: * @param tempDir
252: */
253: private void addShutdownHook(final File tempDir) {
254: Runnable hook = new Runnable() {
255: public void run() {
256: if (ctx.getInstaller() != null
257: && ctx.getInstaller().isDebug())
258: return;
259: if (tempDir != null && tempDir.exists()
260: && tempDir.isDirectory()) {
261: SelfExtractor.deleteRecursive(tempDir);
262: }
263: }
264: };
265: Thread cleanUp = new Thread(hook);
266: cleanUp.setDaemon(true);
267: Runtime.getRuntime().addShutdownHook(cleanUp);
268: }
269:
270: /**
271: * Indicates if the installer will call System.exit() in the case of errors or
272: * user aborting the installation
273: * @return
274: */
275: public boolean isExitOnError() {
276: return exitOnError;
277: }
278:
279: public void setExitOnError(boolean exitOnError) {
280: this.exitOnError = exitOnError;
281: }
282:
283: }
|