| ExecInstall.java |
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 */
16package org.tp23.antinstaller.runtime;
17
18import java.io.File;
19
20import org.tp23.antinstaller.InstallException;
21import org.tp23.antinstaller.InstallerContext;
22import org.tp23.antinstaller.renderer.AIResourceBundle;
23import org.tp23.antinstaller.renderer.MessageRenderer;
24import org.tp23.antinstaller.runtime.exe.ExecuteFilter;
25import org.tp23.antinstaller.runtime.exe.ExecuteRunnerFilter;
26import org.tp23.antinstaller.runtime.exe.FilterChain;
27import org.tp23.antinstaller.runtime.exe.FilterFactory;
28import org.tp23.antinstaller.runtime.exe.FinalizerFilter;
29import org.tp23.antinstaller.selfextract.SelfExtractor;
30
31
32
33/**
34 * This is the Applications entry point, it has a main method to run the
35 * installer. The main method is only for scripted installs.
36 *
37 * It is here that the command line options are parsed and it
38 * is determined which type of install (swing or text) will be run.
39 * <p>Reads the config, determines the runner, runs it and outputs the
40 * properties file, The Ant targets are then called by the AntRunner.
41 * This class also builds the internal Objects from the XML config file.</p>
42 * <p>This class can also be called by external tools to launch the installer
43 * currently two options are provided to launch from Jars. </p>
44 * <p>Copyright: Copyright (c) 2004</p>
45 * <p>Company: tp23</p>
46 * @author Paul Hinds
47 * @version $Id: ExecInstall.java,v 1.9 2007/01/19 00:24:36 teknopaul Exp $
48 */
49public class ExecInstall {
50
51 private static final AIResourceBundle res = new AIResourceBundle();
52 public static final String CONFIG_RESOURCE = "/org/tp23/antinstaller/runtime/exe/script.fconfig";
53
54 private final InstallerContext ctx = new InstallerContext();
55 private FilterChain chain;
56 private boolean exitOnError = true;
57 /**
58 * @param chain chain of filters to be executed
59 */
60 public ExecInstall(FilterChain chain){
61 this.chain = chain;
62 }
63
64 public InstallerContext getInstallerContext() {
65 return ctx;
66 }
67
68 /**
69 * Execute the installer, this reads the config fetches a runner and runs the install.
70 * Once the install pages have finished an AntRunner is used to run Ant
71 */
72 public void exec() {
73
74 ExecuteFilter[] filters = null;
75 try {
76 chain.init(ctx);
77 filters = chain.getFilters();
78 }
79 catch (Exception e) {
80 // This is a developer error or the package has not been built correctly
81 // It should never happen in a tested build
82 e.printStackTrace();
83 System.exit(1); // called manually since in Win it was not shutting down properly
84 }
85loop: for (int i = 0; i < filters.length; i++) {
86 try{
87 ctx.log("Filter: " + filters[i].getClass().getName());
88 filters[i].exec(ctx);
89 }
90 catch (ExecuteRunnerFilter.AbortException abort){
91 MessageRenderer vLogger = ctx.getMessageRenderer();
92 vLogger.printMessage(res.getString("install.aborted"));
93 ctx.log("Aborted");
94 FinalizerFilter ff = (FinalizerFilter)filters[filters.length - 1];
95 ff.exec(ctx);
96 if (exitOnError) {
97 System.exit(1);
98 }
99 }
00 catch (Exception ex) {
01
02 // write errors to the log
03 ctx.log("Installation error: " + ex.getMessage() + ": " + ex.getClass().toString());
04 boolean verbose = true; // be verbose if we cant load the config
05 if(ctx.getInstaller() != null) {
06 verbose = ctx.getInstaller().isVerbose();
07 }
08 ctx.log(verbose, ex);
09
10 // write detailed errors to stdout for the GUI screens and text
11 if (ctx.getRunner() instanceof TextRunner) {
12 if(verbose){
13 ex.printStackTrace();
14 }
15 }
16 else {
17 if(verbose){
18 ex.printStackTrace(System.err);
19 }
20 }
21
22 // report the error to the user
23 MessageRenderer vLogger = ctx.getMessageRenderer();
24 if(vLogger != null){
25 vLogger.printMessage(res.getString("installation.failed") + "\n" + ex.getMessage());
26 //Fixed BUG:1295944 vLogger.printMessage("Install failed\n" + ex.getMessage());
27 } else {
28 System.err.println(res.getString("installation.failed") + ex.getClass().getName());
29 System.err.println(ex.getMessage());
30 }
31
32 if (ctx.getRunner() != null) {
33 ctx.getRunner().fatalError();
34 break loop;
35 }
36 else { // the screens did not even start e.g. XML config error
37 if (exitOnError) {
38 System.exit(1);
39 }
40 }
41 }
42 }
43 }
44
45
46
47
48
49 /**
50 * <p>Runs the installer from a script.</p>
51 *
52 * This install can be run from a set of files for example from a CD.
53 * @see org.tp23.antinstaller.selfextract.NonExtractor
54 * @see org.tp23.antinstaller.selfextract.SelfExtractor
55 *
56 * @param args String[] args are "default" or "swing" or "text" followed by the root directory of the install
57 */
58 public static void main(String[] args) {
59 try {
60 FilterChain chain = FilterFactory.factory(CONFIG_RESOURCE);
61 ExecInstall installExec = new ExecInstall(chain);
62 if(installExec.parseArgs(args, true)){
63 installExec.exec();
64 }
65 }
66 catch (InstallException e) {
67 // Installer developer error
68 System.out.println("Cant load filter chain:/org/tp23/antinstaller/runtime/exe/script.fconfig");
69 e.printStackTrace();
70 }
71 }
72
73 /**
74 * This method has been designed for backward compatibility with
75 * existing scripts. The root dir is passed on the command line for scripted
76 * installs but is determined automatically for installs from self-extracting Jars
77 * @param args
78 * @param requiresRootDir set to true if the args must include the root directory
79 */
80 public boolean parseArgs(String[] args, boolean requiresRootDir){
81 String uiOverride = null;
82 String installType = null;
83 String installRoot = null;
84
85 int i = 0;
86 if(args.length > i && !args[i].startsWith("-")) {
87 uiOverride = args[i];
88 i++;
89 ctx.setUIOverride(uiOverride);
90 }
91
92 if(requiresRootDir){
93 if(args.length > i && !args[i].startsWith("-")) {
94 installRoot = args[i];
95 i++;
96 ctx.setFileRoot(new File(installRoot));
97 }
98 else{
99 printUsage();
00 return false;
01 }
02 }
03 // additional params should all have a -something prefix
04 for (; i < args.length; i++) {
05 // RFE 1569628
06 if("-type".equals(args[i]) && args.length > i + 1){
07 installType = args[i + 1];
08 i++;
09 String configFileName = "antinstall-config-" + installType + ".xml";
10 String buildFileName = "build-" + installType + ".xml";
11 ctx.setInstallerConfigFile(configFileName);
12 ctx.setAntBuildFile(buildFileName);
13 }
14 }
15
16 return true;
17 }
18
19 private static void printUsage(){
20 System.out.println("Usage java -cp $CLASSPATH org.tp23.antinstaller.ExecInstall [text|swing|default] [install root] (-type [buildtype])");
21 }
22
23
24 /**
25 * Sets the UI override from the command line
26 * @param installRoot
27 */
28// public void setUIOverride(String override) {
29// ctx.setUIOverride(override);
30// }
31 /**
32 * This is generated by the Main class which knows where it has
33 * extracted or where it has run from
34 * @param installRoot
35 */
36 public void setInstallRoot(File installRoot) {
37 ctx.setFileRoot(installRoot);
38 }
39
40 /**
41 * This is AntInstalls temporary space which will generally be deleted
42 * except in debug mode when it is left to view the build process.
43 * installRoot and tempRoot can be the same if the directory
44 * is a new empty directory
45 * @param tempDir directory to be used for temporary storage
46 */
47 public void setTempRoot(File tempDir) {
48 addShutdownHook(tempDir);
49 }
50 /**
51 * This shutdown hook is to facilitate debugging the app can be left open
52 * in the GUI view and the resources will not be deleted. Upon exit
53 * temporary files will be removed. This is required because the
54 * deleteOnExit() method fails if the directory is filled with files.
55 * @param tempDir
56 */
57 private void addShutdownHook(final File tempDir){
58 Runnable hook = new Runnable(){
59 public void run(){
60 if(ctx.getInstaller() != null &&
61 ctx.getInstaller().isDebug()) return;
62 if(tempDir != null && tempDir.exists() && tempDir.isDirectory()){
63 SelfExtractor.deleteRecursive(tempDir);
64 }
65 }
66 };
67 Thread cleanUp = new Thread(hook);
68 cleanUp.setDaemon(true);
69 Runtime.getRuntime().addShutdownHook(cleanUp);
70 }
71
72 /**
73 * Indicates if the installer will call System.exit() in the case of errors or
74 * user aborting the installation
75 * @return
76 */
77 public boolean isExitOnError() {
78 return exitOnError;
79 }
80
81 public void setExitOnError(boolean exitOnError) {
82 this.exitOnError = exitOnError;
83 }
84
85
86}
87