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