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;
17
18import java.io.File;
19import java.io.IOException;
20import java.io.PrintStream;
21import java.util.Enumeration;
22import java.util.Iterator;
23import java.util.Properties;
24import java.util.Vector;
25
26import org.apache.tools.ant.BuildListener;
27import org.apache.tools.ant.taskdefs.Execute;
28import org.tp23.antinstaller.page.Page;
29import org.tp23.antinstaller.renderer.AILanguagePack;
30import org.tp23.antinstaller.renderer.AIResourceBundle;
31import org.tp23.antinstaller.renderer.AntOutputRenderer;
32import org.tp23.antinstaller.renderer.MessageRenderer;
33import org.tp23.antinstaller.runtime.Logger;
34import org.tp23.antinstaller.runtime.LoggingAntOutputRenderer;
35import org.tp23.antinstaller.runtime.Runner;
36import org.tp23.antinstaller.runtime.exe.AntLauncherFilter;
37import org.tp23.antinstaller.runtime.exe.LoadConfigFilter;
38/**
39 *
40 * <p>A single InstallerContext is created by the ExecInstall class and
41 * exist for the duration of the Install screens and the runing of
42 * the Ant Script. </p>
43 * @author Paul Hinds
44 * @version $Id: InstallerContext.java,v 1.10 2007/01/28 08:44:41 teknopaul Exp $
45 */
46public class InstallerContext {
47
48    private static final AILanguagePack langPack = new AILanguagePack();
49    /**
50     * This is the prefix for environment variables, unlike Ant this is fixed to
51     * the common prefix of "env".  If you dont like this complain to the bug reports
52     * on sourceforge
53     */
54    public static final String ENV_PREFIX = "env.";
55    /**
56     * This is the prefix for Java system property variables.
57     * This is fixed to "java."
58     */
59    public static final String JAVA_PREFIX = "java.";
60
61    private Logger logger = null;
62    private Installer installer = null;
63    private MessageRenderer messageRenderer = null;
64    private AntOutputRenderer antOutputRenderer = null;
65    private Runner runner = null;
66    private Page currentPage = null;
67    private java.io.File fileRoot = null; // ant basedir
68    private BuildListener buildListener = null;
69    private AntLauncherFilter antRunner = null;
70    private String uIOverride = null;
71    private String installerConfigFile = LoadConfigFilter.INSTALLER_CONFIG_FILE;
72    private String antBuildFile = "build.xml";
73    private String configResource;
74    private Object userObject;
75    /** indicates the GUI version is running */
76    private boolean gui;
77    
78    
79    // called after the Ant part has been run
80    private boolean installedSucceded = false;
81     
82    public InstallerContext() {
83        // seemingly useless defaults, but generally the logger will be set to a FileLogger
84        // this removes the need to have a progress page
85        logger = new Logger() {
86            public void log(String message) {
87            }
88            public void log(Throwable exception) {
89            }
90            public void log(Installer installer, Throwable exception) {
91            }
92            public void setFileName(String fileName) throws IOException {
93            }
94            public String getFileName() {
95                return null;
96            }
97            public void close() {
98            }
99            public void loge(int b) {
00            }
01            public void loge(String b) {
02            }
03        };
04        antOutputRenderer = new AntOutputRenderer() {
05            public PrintStream getOut() {
06                return System.out;
07            }
08            public PrintStream getErr() {
09                return System.err;
10            }
11        };
12    }
13
14    public void setInstallSucceded(boolean installedSucceded){
15        this.installedSucceded=installedSucceded;
16    }
17    public boolean isInstallSucceded(){
18        return installedSucceded;
19    }
20    
21    public void log(String message){
22        if(logger != null) {
23            logger.log(message);
24        }
25    }
26    public void log(Throwable message){
27        if(logger != null) {
28            logger.log(message);
29        }
30    }
31    public void loge(int b) {
32        if(logger != null) {
33            logger.loge(b);
34        }
35    }
36    public void loge(String b) {
37        if(logger != null) {
38            logger.loge(b);
39        }
40    }
41    public void log(boolean vebose, Throwable message){
42        if(vebose && logger != null) {
43            logger.log(message);
44        }
45    }
46
47    /**
48     * Check to see if the system is windoze to be able to return the correct prompted
49     * directories.  This method should be IsNotWindows since it assumes anything
50     * that is not windows is Unix
51     * @return boolean true if not windows in the os.name System Property
52     */
53    public static boolean isUnix(){
54        return System.getProperty("os.name").toLowerCase().indexOf("windows") == -1;
55    }
56
57    /**
58     * Use the standard Ant way to load the environment variables, this is not all inclusive
59     * (but will be come Java 1.5 I imagine)
60     * @return Properties
61     */
62    public static Properties getEnvironment(){
63        Properties props = new Properties();
64        try {
65            Vector osEnv = Execute.getProcEnvironment();
66            for (Enumeration e = osEnv.elements(); e.hasMoreElements(); ) {
67                String entry = (String) e.nextElement();
68                int pos = entry.indexOf('=');
69                if (pos != -1) {
70                    props.put(ENV_PREFIX + entry.substring(0, pos),
71                              entry.substring(pos + 1));
72                }
73            }
74        }
75        catch (Exception ex) {
76            // swallow exceptions so this can be loaded statically
77            // bit of a bugger if you need the environment on Mac OS 9 but not all apps
78            // do so we don't want to die inother situations
79            System.out.println("Can't load environment:"+ex.getClass()+","+ex.getMessage());
80        }
81        Properties javaSysProps = System.getProperties();
82        Iterator iter = javaSysProps.keySet().iterator();
83        while (iter.hasNext()) {
84            Object key = (Object)iter.next();
85            props.put(JAVA_PREFIX+key.toString(),javaSysProps.get(key));
86        }
87        return props;
88    }
89
90    // Bean methods
91    public Installer getInstaller() {
92        return installer;
93    }
94
95    public String getMinJavaVersion() {
96        return installer.getMinJavaVersion();
97    }
98
99    public MessageRenderer getMessageRenderer() {
00        return messageRenderer;
01    }
02
03    public void setMessageRenderer(MessageRenderer messageRenderer) {
04        this.messageRenderer = messageRenderer;
05        this.messageRenderer.setInstallerContext(this);
06    }
07    
08    public AntOutputRenderer getAntOutputRenderer() {
09        return antOutputRenderer;
10    }
11    
12    public void setAntOutputRenderer(AntOutputRenderer antOutputRenderer) {
13        if(installer.isDebug()){
14            this.antOutputRenderer = new LoggingAntOutputRenderer(antOutputRenderer.getOut(),
15                antOutputRenderer.getErr(), this);
16        }
17        else {
18            this.antOutputRenderer = antOutputRenderer;
19        }
20    }
21    
22    public Page getCurrentPage() {
23        return currentPage;
24    }
25    
26    public void setCurrentPage(Page currentPage) {
27        this.currentPage = currentPage;
28    }
29    /**
30     * in SelfExtractor - the directory the install has extracted to <br/>
31     * in Scripted installs - the base directory of the install      <br/>
32     * in NonExtractor - the temporary space created for the build   <br/> 
33     * @return 
34     */
35    public File getFileRoot() {
36        return fileRoot;
37    }
38
39    public void setFileRoot(File fileRoot) {
40        this.fileRoot = fileRoot;
41    }
42
43    public org.apache.tools.ant.BuildListener getBuildListener() {
44        return buildListener;
45    }
46
47    public void setBuildListener(org.apache.tools.ant.BuildListener buildListener) {
48        this.buildListener = buildListener;
49    }
50
51    public AntLauncherFilter getAntRunner() {
52        return antRunner;
53    }
54
55    public void setAntRunner(AntLauncherFilter antRunner) {
56        this.antRunner = antRunner;
57    }
58
59    public Logger getLogger() {
60        return logger;
61    }
62
63    public void setLogger(Logger logger) {
64        this.logger = logger;
65    }
66
67    public Runner getRunner() {
68        return runner;
69    }
70
71    public void setRunner(Runner runner) {
72        this.runner = runner;
73    }
74
75    public void setInstaller(Installer installer) {
76        this.installer = installer;
77    }
78
79    public String getUIOverride() {
80        return uIOverride;
81    }
82
83    public void setUIOverride(String override) {
84        uIOverride = override;
85    }
86    
87    public boolean isAutoBuild(){
88        return uIOverride != null && uIOverride.indexOf("-auto") > -1;
89    }
90
91    /**
92     * RFE 1569628, the antinstaller config file to use, defaults to antinstall-config.xml
93     * @return
94     */
95    public String getInstallerConfigFile() {
96        return installerConfigFile;
97    }
98
99    public void setInstallerConfigFile(String installerConfigFile) {
00        this.installerConfigFile = installerConfigFile;
01    }
02    /**
03     * RFE 1569628, the build file to use, defaults to build.xml
04     * There should never be any path info, that is derived elsewhere
05     * @return
06     */
07    public String getAntBuildFile() {
08        return antBuildFile;
09    }
10
11    public void setAntBuildFile(String antBuildFile) {
12        this.antBuildFile = antBuildFile;
13    }
14
15    public String getConfigResource() {
16        return configResource;
17    }
18
19    public void setConfigResource(String configResource) {
20        this.configResource = configResource;
21    }
22
23    public boolean isGui() {
24        return gui;
25    }
26
27    public void setGui(boolean gui) {
28        this.gui = gui;
29    }
30    
31    /**
32     * Reinitialise the LanguagePack to pick up DefaultLocale Changes
33     *
34     */
35    public static void reInitLocale() {
36        // this is a dubious design we need to reInitLocale() in three places
37        try {
38            AIResourceBundle.reInitLocale();
39            AILanguagePack.reInitLocale();
40        } catch (Exception e) {
41            // ignore, signifies no lang packs installed 
42        }
43    }
44
45    /**
46     * Can be used when embedding the installer screens to hook into the filter chain
47     */
48    public Object getUserObject() {
49        return userObject;
50    }
51
52    /**
53     * Can be used when embedding the installer screens to hook into the filter chain
54     */
55    public void setUserObject(Object userObject) {
56        this.userObject = userObject;
57    }
58
59
60}
61
62
63
64