source: release-kits/shared/ant-installer/src/org/tp23/antinstaller/runtime/ExecInstall.java@ 15210

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

Lots of changes to the installer. Now only look in LanguagePack resource bundle for strings.

File size: 8.3 KB
Line 
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;
19import java.util.ResourceBundle;
20
21import org.tp23.antinstaller.InstallException;
22import org.tp23.antinstaller.InstallerContext;
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 lauch 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 ResourceBundle res = ResourceBundle.getBundle("resources.LanguagePack");
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 /**
57 * @param chain chain of filters to be executed
58 */
59 public ExecInstall(FilterChain chain){
60 this.chain = chain;
61 }
62
63
64
65 /**
66 * Execute the installer, this reads the config fetches a runner and runs the install.
67 * Once the install pages have finished an AntRunner is used to run Ant
68 */
69 public void exec() {
70
71 ExecuteFilter[] filters = null;
72 try {
73 chain.init(ctx);
74 filters = chain.getFilters();
75 }
76 catch (Exception e) {
77 // This is a developer error or the package has not been built correctly
78 // It should never happen in a tested build
79 e.printStackTrace();
80 System.exit(1); // called manually since in Win it was not shutting down properly
81 }
82loop: for (int i = 0; i < filters.length; i++) {
83 try{
84 ctx.log("Filter: " + filters[i].getClass().getName());
85 filters[i].exec(ctx);
86 }
87 catch (ExecuteRunnerFilter.AbortException abort){
88 MessageRenderer vLogger = ctx.getMessageRenderer();
89 vLogger.printMessage(abort.getMessage());
90 ctx.log("Aborted");
91 FinalizerFilter ff = (FinalizerFilter)filters[filters.length - 1];
92 ff.exec(ctx);
93 System.exit(1);
94 }
95 catch (Exception ex) {
96
97 // write errors to the log
98 ctx.log("Installation error: " + ex.getMessage() + ": " + ex.getClass().toString());
99 boolean verbose = true; // be verbose if we cant load the config
100 if(ctx.getInstaller() != null) {
101 verbose = ctx.getInstaller().isVerbose();
102 }
103 ctx.log(verbose, ex);
104
105 // write detailed errors to stdout for the GUI screens and text
106 if (ctx.getRunner() instanceof TextRunner) {
107 if(verbose){
108 ex.printStackTrace();
109 }
110 }
111 else {
112 if(verbose){
113 ex.printStackTrace(System.err);
114 }
115 }
116
117 // report the error to the user
118 MessageRenderer vLogger = ctx.getMessageRenderer();
119 if(vLogger != null){
120 vLogger.printMessage(res.getString("installationFailed") + "\n" + ex.getMessage());
121 //Fixed BUG:1295944 vLogger.printMessage("Install failed\n" + ex.getMessage());
122 } else {
123 System.err.println(res.getString("installationFailed") + ex.getClass().getName());
124 System.err.println(ex.getMessage());
125 }
126
127 if(ctx.getRunner() != null){
128 ctx.getRunner().fatalError();
129 break loop;
130 }
131 else { // the screens did not even start e.g. XML config error
132 System.exit(1);
133 }
134 }
135 }
136
137 }
138
139
140
141
142
143 /**
144 * <p>Runs the installer from a script.</p>
145 *
146 * This install can be run from a set of files for example from a CD.
147 * @see org.tp23.antinstaller.selfextract.NonExtractor
148 * @see org.tp23.antinstaller.selfextract.SelfExtractor
149 *
150 * @param args String[] args are "default" or "swing" or "text" followed by the root directory of the install
151 */
152 public static void main(String[] args) {
153 try {
154 FilterChain chain = FilterFactory.factory(CONFIG_RESOURCE);
155 ExecInstall installExec = new ExecInstall(chain);
156 if(installExec.parseArgs(args, true)){
157 installExec.exec();
158 }
159 }
160 catch (InstallException e) {
161 // Installer developer error
162 System.out.println("Cant load filter chain:/org/tp23/antinstaller/runtime/exe/script.fconfig");
163 e.printStackTrace();
164 }
165 }
166
167 /**
168 * This method has been designed for backward compatibility with
169 * existing scripts. The root dir is passed on the command line for scripted
170 * installs but is determined automatically for installs from self-extracting Jars
171 * @param args
172 * @param requiresRootDir set to true if the args must include the root directory
173 */
174 public boolean parseArgs(String[] args, boolean requiresRootDir){
175 String uiOverride = null;
176 String installType = null;
177 String installRoot = null;
178
179 int i = 0;
180 if(args.length > i && !args[i].startsWith("-")) {
181 uiOverride = args[i];
182 i++;
183 ctx.setUIOverride(uiOverride);
184 }
185
186 if(requiresRootDir){
187 if(args.length > i && !args[i].startsWith("-")) {
188 installRoot = args[i];
189 i++;
190 ctx.setFileRoot(new File(installRoot));
191 }
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-" + installType + ".xml";
204 String buildFileName = "build-" + installType + ".xml";
205 ctx.setInstallerConfigFile(configFileName);
206 ctx.setAntBuildFile(buildFileName);
207 }
208 }
209
210 return true;
211 }
212
213 private static void printUsage(){
214 System.out.println("Usage java -cp $CLASSPATH org.tp23.antinstaller.ExecInstall [text|swing|default] [install root] (-type [buildtype])");
215 }
216
217
218 /**
219 * Sets the UI override from the command line
220 * @param installRoot
221 */
222// public void setUIOverride(String override) {
223// ctx.setUIOverride(override);
224// }
225 /**
226 * This is generated by the Main class which knows where it has
227 * extracted or where it has run from
228 * @param installRoot
229 */
230 public void setInstallRoot(File installRoot) {
231 ctx.setFileRoot(installRoot);
232 }
233
234 /**
235 * This is AntInstalls temporary space which will generally be deleted
236 * except in debug mode when it is left to view the build process.
237 * installRoot and tempRoot can be the same if the directory
238 * is a new empty directory
239 * @param tempDir directory to be used for temporary storage
240 */
241 public void setTempRoot(File tempDir) {
242 addShutdownHook(tempDir);
243 }
244 /**
245 * This shutdown hook is to facilitate debugging the app can be left open
246 * in the GUI view and the resources will not be deleted. Upon exit
247 * temporary files will be removed. This is required because the
248 * deleteOnExit() method fails if the directory is filled with files.
249 * @param tempDir
250 */
251 private void addShutdownHook(final File tempDir){
252 Runnable hook = new Runnable(){
253 public void run(){
254 if(ctx.getInstaller() != null &&
255 ctx.getInstaller().isDebug()) return;
256 if(tempDir != null && tempDir.exists() && tempDir.isDirectory()){
257 SelfExtractor.deleteRecursive(tempDir);
258 }
259 }
260 };
261 Thread cleanUp = new Thread(hook);
262 cleanUp.setDaemon(true);
263 Runtime.getRuntime().addShutdownHook(cleanUp);
264 }
265
266}
Note: See TracBrowser for help on using the repository browser.