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

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

changes to the way ant-installer loads and reloads the language packs, and a new attribute to the select input which triggers it to change the language to the input value

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