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;
017:
018: import java.io.File;
019: import java.io.IOException;
020: import java.io.PrintStream;
021: import java.util.Enumeration;
022: import java.util.Iterator;
023: import java.util.Properties;
024: import java.util.Vector;
025:
026: import org.apache.tools.ant.BuildListener;
027: import org.apache.tools.ant.taskdefs.Execute;
028: import org.tp23.antinstaller.page.Page;
029: import org.tp23.antinstaller.renderer.AILanguagePack;
030: import org.tp23.antinstaller.renderer.AIResourceBundle;
031: import org.tp23.antinstaller.renderer.AntOutputRenderer;
032: import org.tp23.antinstaller.renderer.MessageRenderer;
033: import org.tp23.antinstaller.runtime.Logger;
034: import org.tp23.antinstaller.runtime.LoggingAntOutputRenderer;
035: import org.tp23.antinstaller.runtime.Runner;
036: import org.tp23.antinstaller.runtime.exe.AntLauncherFilter;
037: import org.tp23.antinstaller.runtime.exe.LoadConfigFilter;
038:
039: /**
040: *
041: * <p>A single InstallerContext is created by the ExecInstall class and
042: * exist for the duration of the Install screens and the runing of
043: * the Ant Script. </p>
044: * @author Paul Hinds
045: * @version $Id: InstallerContext.java,v 1.10 2007/01/28 08:44:41 teknopaul Exp $
046: */
047: public class InstallerContext {
048:
049: private static final AILanguagePack langPack = new AILanguagePack();
050: /**
051: * This is the prefix for environment variables, unlike Ant this is fixed to
052: * the common prefix of "env". If you dont like this complain to the bug reports
053: * on sourceforge
054: */
055: public static final String ENV_PREFIX = "env.";
056: /**
057: * This is the prefix for Java system property variables.
058: * This is fixed to "java."
059: */
060: public static final String JAVA_PREFIX = "java.";
061:
062: private Logger logger = null;
063: private Installer installer = null;
064: private MessageRenderer messageRenderer = null;
065: private AntOutputRenderer antOutputRenderer = null;
066: private Runner runner = null;
067: private Page currentPage = null;
068: private java.io.File fileRoot = null; // ant basedir
069: private BuildListener buildListener = null;
070: private AntLauncherFilter antRunner = null;
071: private String uIOverride = null;
072: private String installerConfigFile = LoadConfigFilter.INSTALLER_CONFIG_FILE;
073: private String antBuildFile = "build.xml";
074: private String configResource;
075: private Object userObject;
076: /** indicates the GUI version is running */
077: private boolean gui;
078:
079: // called after the Ant part has been run
080: private boolean installedSucceded = false;
081:
082: public InstallerContext() {
083: // seemingly useless defaults, but generally the logger will be set to a FileLogger
084: // this removes the need to have a progress page
085: logger = new Logger() {
086: public void log(String message) {
087: }
088:
089: public void log(Throwable exception) {
090: }
091:
092: public void log(Installer installer, Throwable exception) {
093: }
094:
095: public void setFileName(String fileName) throws IOException {
096: }
097:
098: public String getFileName() {
099: return null;
100: }
101:
102: public void close() {
103: }
104:
105: public void loge(int b) {
106: }
107:
108: public void loge(String b) {
109: }
110: };
111: antOutputRenderer = new AntOutputRenderer() {
112: public PrintStream getOut() {
113: return System.out;
114: }
115:
116: public PrintStream getErr() {
117: return System.err;
118: }
119: };
120: }
121:
122: public void setInstallSucceded(boolean installedSucceded) {
123: this .installedSucceded = installedSucceded;
124: }
125:
126: public boolean isInstallSucceded() {
127: return installedSucceded;
128: }
129:
130: public void log(String message) {
131: if (logger != null) {
132: logger.log(message);
133: }
134: }
135:
136: public void log(Throwable message) {
137: if (logger != null) {
138: logger.log(message);
139: }
140: }
141:
142: public void loge(int b) {
143: if (logger != null) {
144: logger.loge(b);
145: }
146: }
147:
148: public void loge(String b) {
149: if (logger != null) {
150: logger.loge(b);
151: }
152: }
153:
154: public void log(boolean vebose, Throwable message) {
155: if (vebose && logger != null) {
156: logger.log(message);
157: }
158: }
159:
160: /**
161: * Check to see if the system is windoze to be able to return the correct prompted
162: * directories. This method should be IsNotWindows since it assumes anything
163: * that is not windows is Unix
164: * @return boolean true if not windows in the os.name System Property
165: */
166: public static boolean isUnix() {
167: return System.getProperty("os.name").toLowerCase().indexOf(
168: "windows") == -1;
169: }
170:
171: /**
172: * Use the standard Ant way to load the environment variables, this is not all inclusive
173: * (but will be come Java 1.5 I imagine)
174: * @return Properties
175: */
176: public static Properties getEnvironment() {
177: Properties props = new Properties();
178: try {
179: Vector osEnv = Execute.getProcEnvironment();
180: for (Enumeration e = osEnv.elements(); e.hasMoreElements();) {
181: String entry = (String) e.nextElement();
182: int pos = entry.indexOf('=');
183: if (pos != -1) {
184: props.put(ENV_PREFIX + entry.substring(0, pos),
185: entry.substring(pos + 1));
186: }
187: }
188: } catch (Exception ex) {
189: // swallow exceptions so this can be loaded statically
190: // bit of a bugger if you need the environment on Mac OS 9 but not all apps
191: // do so we don't want to die inother situations
192: System.out.println("Can't load environment:"
193: + ex.getClass() + "," + ex.getMessage());
194: }
195: Properties javaSysProps = System.getProperties();
196: Iterator iter = javaSysProps.keySet().iterator();
197: while (iter.hasNext()) {
198: Object key = (Object) iter.next();
199: props.put(JAVA_PREFIX + key.toString(), javaSysProps
200: .get(key));
201: }
202: return props;
203: }
204:
205: // Bean methods
206: public Installer getInstaller() {
207: return installer;
208: }
209:
210: public String getMinJavaVersion() {
211: return installer.getMinJavaVersion();
212: }
213:
214: public MessageRenderer getMessageRenderer() {
215: return messageRenderer;
216: }
217:
218: public void setMessageRenderer(MessageRenderer messageRenderer) {
219: this .messageRenderer = messageRenderer;
220: this .messageRenderer.setInstallerContext(this );
221: }
222:
223: public AntOutputRenderer getAntOutputRenderer() {
224: return antOutputRenderer;
225: }
226:
227: public void setAntOutputRenderer(AntOutputRenderer antOutputRenderer) {
228: if (installer.isDebug()) {
229: this .antOutputRenderer = new LoggingAntOutputRenderer(
230: antOutputRenderer.getOut(), antOutputRenderer
231: .getErr(), this );
232: } else {
233: this .antOutputRenderer = antOutputRenderer;
234: }
235: }
236:
237: public Page getCurrentPage() {
238: return currentPage;
239: }
240:
241: public void setCurrentPage(Page currentPage) {
242: this .currentPage = currentPage;
243: }
244:
245: /**
246: * in SelfExtractor - the directory the install has extracted to <br/>
247: * in Scripted installs - the base directory of the install <br/>
248: * in NonExtractor - the temporary space created for the build <br/>
249: * @return
250: */
251: public File getFileRoot() {
252: return fileRoot;
253: }
254:
255: public void setFileRoot(File fileRoot) {
256: this .fileRoot = fileRoot;
257: }
258:
259: public org.apache.tools.ant.BuildListener getBuildListener() {
260: return buildListener;
261: }
262:
263: public void setBuildListener(
264: org.apache.tools.ant.BuildListener buildListener) {
265: this .buildListener = buildListener;
266: }
267:
268: public AntLauncherFilter getAntRunner() {
269: return antRunner;
270: }
271:
272: public void setAntRunner(AntLauncherFilter antRunner) {
273: this .antRunner = antRunner;
274: }
275:
276: public Logger getLogger() {
277: return logger;
278: }
279:
280: public void setLogger(Logger logger) {
281: this .logger = logger;
282: }
283:
284: public Runner getRunner() {
285: return runner;
286: }
287:
288: public void setRunner(Runner runner) {
289: this .runner = runner;
290: }
291:
292: public void setInstaller(Installer installer) {
293: this .installer = installer;
294: }
295:
296: public String getUIOverride() {
297: return uIOverride;
298: }
299:
300: public void setUIOverride(String override) {
301: uIOverride = override;
302: }
303:
304: public boolean isAutoBuild() {
305: return uIOverride != null && uIOverride.indexOf("-auto") > -1;
306: }
307:
308: /**
309: * RFE 1569628, the antinstaller config file to use, defaults to antinstall-config.xml
310: * @return
311: */
312: public String getInstallerConfigFile() {
313: return installerConfigFile;
314: }
315:
316: public void setInstallerConfigFile(String installerConfigFile) {
317: this .installerConfigFile = installerConfigFile;
318: }
319:
320: /**
321: * RFE 1569628, the build file to use, defaults to build.xml
322: * There should never be any path info, that is derived elsewhere
323: * @return
324: */
325: public String getAntBuildFile() {
326: return antBuildFile;
327: }
328:
329: public void setAntBuildFile(String antBuildFile) {
330: this .antBuildFile = antBuildFile;
331: }
332:
333: public String getConfigResource() {
334: return configResource;
335: }
336:
337: public void setConfigResource(String configResource) {
338: this .configResource = configResource;
339: }
340:
341: public boolean isGui() {
342: return gui;
343: }
344:
345: public void setGui(boolean gui) {
346: this .gui = gui;
347: }
348:
349: /**
350: * Reinitialise the LanguagePack to pick up DefaultLocale Changes
351: *
352: */
353: public static void reInitLocale() {
354: // this is a dubious design we need to reInitLocale() in three places
355: try {
356: AIResourceBundle.reInitLocale();
357: AILanguagePack.reInitLocale();
358: } catch (Exception e) {
359: // ignore, signifies no lang packs installed
360: }
361: }
362:
363: /**
364: * Can be used when embedding the installer screens to hook into the filter chain
365: */
366: public Object getUserObject() {
367: return userObject;
368: }
369:
370: /**
371: * Can be used when embedding the installer screens to hook into the filter chain
372: */
373: public void setUserObject(Object userObject) {
374: this.userObject = userObject;
375: }
376:
377: }
|