001/*
002 * IzPack - Copyright 2001-2005 Julien Ponge, All Rights Reserved.
003 * 
004 * http://www.izforge.com/izpack/
005 * http://developer.berlios.de/projects/izpack/
006 * 
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 * 
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *     
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020package com.izforge.izpack.installer;
021
022import java.util.ArrayList;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026import java.util.Properties;
027import java.util.zip.ZipOutputStream;
028
029import net.n3.nanoxml.XMLElement;
030
031import com.izforge.izpack.Info;
032import com.izforge.izpack.LocaleDatabase;
033
034/**
035 * Encloses information about the install process. This implementation is not thread safe.
036 * 
037 * @author Julien Ponge <julien@izforge.com>
038 * @author Johannes Lehtinen <johannes.lehtinen@iki.fi>
039 */
040public class AutomatedInstallData
041{
042
043    // --- Static members -------------------------------------------------
044
045    /**
046     * Names of the custom actions types with which they are stored in the installer jar file. These
047     * names are also used to identify the type of custom action in the customData map. Slashes as
048     * first char are needed to use the names as "file" name in the installer jar.
049     */
050    // Attention !! Do not change the existent names and the order.
051    // Add a / as first char at new types. Add new type handling in
052    // Unpacker.
053    static final String[] CUSTOM_ACTION_TYPES = new String[] { "/installerListeners",
054            "/uninstallerListeners", "/uninstallerLibs", "/uninstallerJars"};
055
056    public static final int INSTALLER_LISTENER_INDEX = 0;
057
058    public static final int UNINSTALLER_LISTENER_INDEX = 1;
059
060    public static final int UNINSTALLER_LIBS_INDEX = 2;
061
062    public static final int UNINSTALLER_JARS_INDEX = 3;
063
064    // --- Instance members -----------------------------------------------
065
066    /** The language code. */
067    public String localeISO3;
068
069    /** The language pack. */
070    public LocaleDatabase langpack;
071
072    /** The uninstaller jar stream. */
073    public ZipOutputStream uninstallOutJar;
074
075    /** The inforamtions. */
076    public Info info;
077
078    /** The complete list of packs. */
079    public List allPacks;
080
081    /** The available packs. */
082    public List availablePacks;
083
084    /** The selected packs. */
085    public List selectedPacks;
086
087    /** The panels list. */
088    public List panels;
089
090    /** The panels order. */
091    public List panelsOrder;
092
093    /** The current panel. */
094    public int curPanelNumber;
095
096    /** Can we close the installer ? */
097    public boolean canClose = false;
098
099    /** Did the installation succeed ? */
100    public boolean installSuccess = true;
101
102    /** The xmlData for automated installers. */
103    public XMLElement xmlData;
104
105    /** Custom data. */
106    public Map customData;
107
108    /**
109     * Maps the variable names to their values
110     */
111    protected Properties variables;
112
113    /** The attributes used by the panels */
114    protected Map attributes;
115    
116    /** This class should be a singleton. Therefore
117     * the one possible object will be stored in this 
118     * static member.
119     */
120    private static AutomatedInstallData self = null;
121    
122    /**
123     * Returns the one possible object of this class.
124     * @return the one possible object of this class
125     */
126    public static AutomatedInstallData getInstance()
127    {
128        return( self);
129    }
130
131    /** Constructs a new instance of this class. 
132     * Only one should be possible, at a scound call a RuntimeException
133     * will be raised. */
134    public AutomatedInstallData()
135    {
136        availablePacks = new ArrayList();
137        selectedPacks = new ArrayList();
138        panels = new ArrayList();
139        panelsOrder = new ArrayList();
140        xmlData = new XMLElement("AutomatedInstallation");
141        variables = new Properties();
142        attributes = new HashMap();
143        customData = new HashMap();
144        if( self != null )
145            throw new RuntimeException("Panic!! second call of the InstallData Ctor!!");
146        self = this;
147    }
148
149    /**
150     * Returns the map of variable values. Modifying this will directly affect the current value of
151     * variables.
152     * 
153     * @return the map of variable values
154     */
155    public Properties getVariables()
156    {
157        return variables;
158    }
159
160    /**
161     * Sets a variable to the specified value. This is short hand for
162     * <code>getVariables().setProperty(var, val)</code>.
163     * 
164     * @param var the name of the variable
165     * @param val the new value of the variable
166     * @see #getVariable
167     */
168    public void setVariable(String var, String val)
169    {
170        variables.setProperty(var, val);
171    }
172
173    /**
174     * Returns the current value of the specified variable. This is short hand for
175     * <code>getVariables().getProperty(var)</code>.
176     * 
177     * @param var the name of the variable
178     * @return the value of the variable or null if not set
179     * @see #setVariable
180     */
181    public String getVariable(String var)
182    {
183        return variables.getProperty(var);
184    }
185
186    /**
187     * Sets the install path.
188     * 
189     * @param path the new install path
190     * @see #getInstallPath
191     */
192    public void setInstallPath(String path)
193    {
194        setVariable(ScriptParser.INSTALL_PATH, path);
195    }
196
197    /**
198     * Returns the install path.
199     * 
200     * @return the current install path or null if none set yet
201     * @see #setInstallPath
202     */
203    public String getInstallPath()
204    {
205        return getVariable(ScriptParser.INSTALL_PATH);
206    }
207
208    /**
209     * Returns the value of the named attribute.
210     * 
211     * @param attr the name of the attribute
212     * @return the value of the attribute or null if not set
213     * @see #setAttribute
214     */
215    public Object getAttribute(String attr)
216    {
217        return attributes.get(attr);
218    }
219
220    /**
221     * Sets a named attribute. The panels and other IzPack components can attach custom attributes
222     * to InstallData to communicate with each other. For example, a set of co-operating custom
223     * panels do not need to implement a common data storage but can use InstallData singleton. The
224     * name of the attribute should include the package and class name to prevent name space
225     * collisions.
226     * 
227     * @param attr the name of the attribute to set
228     * @param val the value of the attribute or null to unset the attribute
229     * @see #getAttribute
230     */
231    public void setAttribute(String attr, Object val)
232    {
233        if (val == null)
234            attributes.remove(attr);
235        else
236            attributes.put(attr, val);
237
238    }
239}