Statistics
| Revision:

root / import / ext3D / trunk / install-extension3d / IzPack / src / lib / com / izforge / izpack / event / AntActionInstallerListener.java @ 15280

History | View | Annotate | Download (11.9 KB)

1
/*
2
 *  $Id: AntActionInstallerListener.java,v 1.1 2006/06/14 07:29:07 cesar Exp $
3
 *  IzPack
4
 *  Copyright (C) 2004 Thomas Guenter, Klaus Bartz
5
 *
6
 *  File :               AntActionInstallerListener.java
7
 *  Description :        Installer listener for ant custom actions.
8
 *  Author's email :     bartzkau@users.berlios.de
9
 *  Website :            http://www.izforge.com
10
 * 
11
 *
12
 *  This program is free software; you can redistribute it and/or
13
 *  modify it under the terms of the GNU General Public License
14
 *  as published by the Free Software Foundation; either version 2
15
 *  of the License, or any later version.
16
 *
17
 *  This program is distributed in the hope that it will be useful,
18
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 *  GNU General Public License for more details.
21
 *
22
 *  You should have received a copy of the GNU General Public License
23
 *  along with this program; if not, write to the Free Software
24
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25
 */
26
package com.izforge.izpack.event;
27

    
28
import java.util.HashMap;
29
import java.util.Iterator;
30
import java.util.ArrayList;
31
import java.util.Vector;
32
import net.n3.nanoxml.XMLElement;
33

    
34
import com.izforge.izpack.Pack;
35
import com.izforge.izpack.installer.AutomatedInstallData;
36
import com.izforge.izpack.installer.InstallerException;
37
import com.izforge.izpack.installer.UninstallData;
38
import com.izforge.izpack.installer.VariableSubstitutor;
39
import com.izforge.izpack.util.AbstractUIProgressHandler;
40
import com.izforge.izpack.util.Debug;
41
import com.izforge.izpack.util.ExtendedUIProgressHandler;
42
import com.izforge.izpack.util.SpecHelper;
43

    
44

    
45
/**
46
 * Installer listener for performing ANT actions.
47
 * The definition what should be done will be made in a 
48
 * specification file which is referenced by the resource
49
 * id "AntActionsSpec.xml". There should be an entry in
50
 * the install.xml file in the sub ELEMENT "res" of 
51
 * ELEMENT "resources" which references it. The specification
52
 * of the xml file is done in the DTD antaction.dtd.
53
 * The xml file specifies, for what pack what ant call
54
 * should be performed at what time of installation.
55
 * 
56
 * @author     Thomas Guenter
57
 * @author     Klaus Bartz
58
 */
59
public class AntActionInstallerListener extends SimpleInstallerListener
60
{
61
  // ------------------------------------------------------------------------
62
  // Constant Definitions
63
  // ------------------------------------------------------------------------
64
  // --------String constants for parsing the XML specification  ------------
65
  // --------  see class  AntAction -----------------------------------------
66
  /** Name of the specification file */
67
  public static final String SPEC_FILE_NAME = "AntActionsSpec.xml";
68

    
69
  
70
  private HashMap actions = null;
71
  private ArrayList uninstActions = null;
72
  
73
  /**
74
   * Default constructor
75
   */
76
  public AntActionInstallerListener()
77
  {
78
    super(true);
79
    actions = new HashMap();
80
    uninstActions = new ArrayList();
81
  }
82
  
83
  /**
84
   * Returns the actions map.
85
   * @return the actions map
86
   */
87
  public HashMap getActions()
88
  {
89
    return(actions);
90
  }
91

    
92
  /* (non-Javadoc)
93
   * @see com.izforge.izpack.installer.InstallerListener#beforePacks(com.izforge.izpack.installer.AutomatedInstallData, java.lang.Integer, com.izforge.izpack.util.AbstractUIProgressHandler)
94
   */
95
  public void beforePacks(
96
    AutomatedInstallData idata, 
97
    Integer npacks,
98
    AbstractUIProgressHandler handler
99
    ) throws Exception
100
  {
101
    super.beforePacks(idata, npacks, handler);
102

    
103
    getSpecHelper().readSpec( SPEC_FILE_NAME, new VariableSubstitutor( idata.getVariables() ) );
104

    
105
    if( getSpecHelper().getSpec() == null )
106
      return;
107

    
108
    // Selected packs.
109
    Iterator iter = idata.selectedPacks.iterator();
110
    Pack p = null;
111
    while( iter != null && iter.hasNext())
112
    {
113
      p = (Pack)iter.next();
114
      
115
      // Resolve data for current pack.
116
      XMLElement pack = getSpecHelper().getPackForName( p.name );
117
      if( pack == null )
118
        continue;
119
     
120
      // Prepare the action cache
121
      HashMap packActions = new HashMap();
122
      packActions.put(ActionBase.BEFOREPACK, new ArrayList());
123
      packActions.put(ActionBase.AFTERPACK, new ArrayList());
124
      packActions.put(ActionBase.BEFOREPACKS, new ArrayList());
125
      packActions.put(ActionBase.AFTERPACKS, new ArrayList());
126

    
127
      // Get all entries for antcalls.
128
      Vector antCallEntries = pack.getChildrenNamed(AntAction.ANTCALL);
129
      if (antCallEntries != null && antCallEntries.size() >= 1 )
130
      {
131
        Iterator entriesIter = antCallEntries.iterator();
132
        while( entriesIter != null && entriesIter.hasNext() )
133
        {
134
            AntAction act = readAntCall((XMLElement)entriesIter.next());
135
            if (act != null)
136
            {
137
              ((ArrayList)packActions.get(act.getOrder())).add(act);
138
            }
139
        }
140
        // Set for progress bar interaction.
141
        if( ((ArrayList)packActions.get(ActionBase.AFTERPACKS)).size() > 0 )
142
          this.setProgressBarCaller();
143
      }
144
      
145
      actions.put(p.name, packActions);
146
    }
147
    iter = idata.availablePacks.iterator();
148
    while( iter.hasNext() )
149
    {
150
      String currentPack = ((Pack)iter.next()).name;
151
      performAllActions( currentPack, ActionBase.BEFOREPACKS, null );
152
    }
153
  }
154

    
155
  /* (non-Javadoc)
156
   * @see com.izforge.izpack.installer.InstallerListener#beforePack(com.izforge.izpack.Pack, java.lang.Integer, com.izforge.izpack.util.AbstractUIProgressHandler)
157
   */
158
  public void beforePack(
159
    Pack pack, 
160
    Integer i, 
161
    AbstractUIProgressHandler handler)
162
    throws Exception
163
  {
164
    performAllActions( pack.name, ActionBase.BEFOREPACK, handler );
165
  }
166
  
167
  /* (non-Javadoc)
168
   * @see com.izforge.izpack.installer.InstallerListener#afterPack(com.izforge.izpack.Pack, java.lang.Integer, com.izforge.izpack.util.AbstractUIProgressHandler)
169
   */
170
  public void afterPack(
171
    Pack pack, 
172
    Integer i, 
173
    AbstractUIProgressHandler handler)
174
    throws Exception
175
  {
176
    performAllActions( pack.name, ActionBase.AFTERPACK, handler );
177
  }
178
        
179
  /* (non-Javadoc)
180
   * @see com.izforge.izpack.compiler.InstallerListener#afterPacks(com.izforge.izpack.installer.AutomatedInstallData, com.izforge.izpack.util.AbstractUIProgressHandler)
181
   */
182
  public void afterPacks(
183
    AutomatedInstallData idata,
184
    AbstractUIProgressHandler handler)
185
    throws Exception
186
  {
187
    if( informProgressBar())
188
    {
189
      handler.nextStep( getMsg("AntAction.pack"), 
190
        getProgressBarCallerId(), getActionCount(idata, ActionBase.AFTERPACKS));
191
    }
192
    Iterator iter = idata.selectedPacks.iterator();
193
    while( iter.hasNext() )
194
    {
195
      String currentPack = ((Pack)iter.next()).name;
196
      performAllActions( currentPack, ActionBase.AFTERPACKS, handler );
197
    }
198
    if( uninstActions.size() > 0 )
199
    {
200
      UninstallData.getInstance().addAdditionalData("antActions", uninstActions);
201
    }
202
  }
203

    
204
  private int getActionCount(AutomatedInstallData idata, String order)
205
  {
206
     int retval = 0;
207
    Iterator iter = idata.selectedPacks.iterator();
208
    while( iter.hasNext() )
209
    {
210
      String currentPack = ((Pack)iter.next()).name;
211
      ArrayList actList = getActions(currentPack, order);
212
      if( actList != null)
213
       retval += actList.size();
214
    }
215
    return(retval);
216
  }
217
  /**
218
   * Returns the defined actions for the given pack in the
219
   * requested order.
220
   * @param packName name of the pack for which the 
221
   * actions should be returned 
222
   * @param order order to be used; valid are <i>beforepack</i> and <i>afterpack</i>
223
   * @return a list which contains all defined actions for
224
   * the given pack and order
225
   */
226
  //-------------------------------------------------------
227
  protected ArrayList getActions( String packName, String order )
228
  {
229
    if (actions == null)
230
      return null;
231
    
232
    HashMap packActions = (HashMap)actions.get(packName);
233
    if (packActions == null || packActions.size() == 0)
234
      return null;
235
    
236
    return (ArrayList)packActions.get(order);
237
  }
238

    
239
  /**
240
   * Performs all actions which are defined for the given pack
241
   * and order.
242
   * @param packName name of the pack for which the 
243
   * actions should be performed
244
   * @param order order to be used; valid are <i>beforepack</i> and <i>afterpack</i>
245
   * @throws InstallerException
246
   */
247
  private void performAllActions( String packName, String order, 
248
    AbstractUIProgressHandler handler )
249
    throws InstallerException
250
  {
251
    ArrayList actList = getActions(packName, order);
252
    if (actList == null || actList.size() == 0)
253
      return;
254

    
255
    Debug.trace ("******* Executing all " + order + " actions of " + packName + " ...");
256
    for (int i = 0; i < actList.size(); i++)
257
    {
258
      AntAction act = (AntAction)actList.get(i);
259
      // Inform progress bar if needed. Works only
260
      // on AFTER_PACKS
261
      if( informProgressBar() && handler != null &&
262
        handler instanceof ExtendedUIProgressHandler 
263
        && order.equals(ActionBase.AFTERPACKS))
264
      {
265
        ((ExtendedUIProgressHandler)handler).progress( 
266
          (act.getMessageID() != null) ? getMsg(act.getMessageID()) : "");
267
      }
268
      try
269
      {
270
        act.performInstallAction();
271
      }
272
      catch (Exception e)
273
      {
274
        throw new InstallerException(e);
275
      }
276
      if( act.getUninstallTargets().size() > 0 )
277
        uninstActions.add(act);
278
    }
279
  }
280
  
281
  /**
282
   * Returns an ant call which is defined in the given
283
   * XML element.
284
   * @param el XML element which contains the description of
285
   * an ant call
286
   * @return an ant call which is defined in the given
287
   * XML element
288
   * @throws InstallerException
289
   */
290
  private AntAction readAntCall( XMLElement el )
291
  throws InstallerException
292
  {
293
    if (el == null)
294
      return null;
295
    SpecHelper spec = getSpecHelper();
296
    AntAction act = new AntAction();
297
    try
298
    {
299
      act.setOrder( spec.getRequiredAttribute(el, ActionBase.ORDER) );
300
      act.setUninstallOrder( el.getAttribute(ActionBase.UNINSTALL_ORDER, ActionBase.BEFOREDELETION) );
301
    }
302
    catch (Exception e)
303
    {
304
      throw new InstallerException(e);
305
    }
306
    
307
    act.setQuiet( spec.isAttributeYes(el, ActionBase.QUIET, false) );
308
    act.setVerbose( spec.isAttributeYes(el, ActionBase.VERBOSE, false) );
309
    act.setBuildFile( spec.getRequiredAttribute(el, ActionBase.BUILDFILE) );
310
    String str = el.getAttribute(ActionBase.LOGFILE);
311
    if (str != null)
312
    {
313
      act.setLogFile(str);
314
    }
315
    String msgId = el.getAttribute(ActionBase.MESSAGEID);
316
    if( msgId != null && msgId.length() > 0)
317
      act.setMessageID(msgId);
318

    
319
    // read propertyfiles
320
    Iterator iter = el.getChildrenNamed(ActionBase.PROPERTYFILE).iterator();
321
    while (iter.hasNext())
322
    {
323
      XMLElement propEl = (XMLElement) iter.next();
324
      act.addPropertyFile( spec.getRequiredAttribute(propEl, ActionBase.PATH) );
325
    }
326
    
327
    // read properties
328
    iter = el.getChildrenNamed(ActionBase.PROPERTY).iterator();
329
    while (iter.hasNext())
330
    {
331
      XMLElement propEl = (XMLElement) iter.next();
332
      act.setProperty( spec.getRequiredAttribute(propEl, ActionBase.NAME), 
333
        spec.getRequiredAttribute(propEl, ActionBase.VALUE) );
334
    }
335
    
336
    // read targets
337
    iter = el.getChildrenNamed(ActionBase.TARGET).iterator();
338
    while (iter.hasNext())
339
    {
340
      XMLElement targEl = (XMLElement) iter.next();
341
      act.addTarget( spec.getRequiredAttribute(targEl, ActionBase.NAME) );
342
    }
343
    
344
    // read uninstall rules
345
    iter = el.getChildrenNamed(ActionBase.UNINSTALL_TARGET).iterator();
346
    while (iter.hasNext())
347
    {
348
      XMLElement utargEl = (XMLElement) iter.next();
349
      act.addUninstallTarget( spec.getRequiredAttribute(utargEl, ActionBase.NAME) );
350
    }
351
    
352
     return act;
353
  }
354
  
355
}
356