KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > event > AntActionInstallerListener


1 /*
2  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
3  *
4  * http://www.izforge.com/izpack/
5  * http://developer.berlios.de/projects/izpack/
6  *
7  * Copyright 2004 Klaus Bartz
8  * Copyright 2004 Thomas Guenter
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */

22
23 package com.izforge.izpack.event;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Vector JavaDoc;
29
30 import net.n3.nanoxml.XMLElement;
31
32 import com.izforge.izpack.Pack;
33 import com.izforge.izpack.installer.AutomatedInstallData;
34 import com.izforge.izpack.installer.InstallerException;
35 import com.izforge.izpack.installer.UninstallData;
36 import com.izforge.izpack.util.AbstractUIProgressHandler;
37 import com.izforge.izpack.util.Debug;
38 import com.izforge.izpack.util.ExtendedUIProgressHandler;
39 import com.izforge.izpack.util.SpecHelper;
40 import com.izforge.izpack.util.VariableSubstitutor;
41
42 /**
43  * Installer listener for performing ANT actions. The definition what should be done will be made in
44  * a specification file which is referenced by the resource id "AntActionsSpec.xml". There should be
45  * an entry in the install.xml file in the sub ELEMENT "res" of ELEMENT "resources" which references
46  * it. The specification of the xml file is done in the DTD antaction.dtd. The xml file specifies,
47  * for what pack what ant call should be performed at what time of installation.
48  *
49  * @author Thomas Guenter
50  * @author Klaus Bartz
51  */

52 public class AntActionInstallerListener extends SimpleInstallerListener
53 {
54
55     // ------------------------------------------------------------------------
56
// Constant Definitions
57
// ------------------------------------------------------------------------
58
// --------String constants for parsing the XML specification ------------
59
// -------- see class AntAction -----------------------------------------
60
/** Name of the specification file */
61     public static final String JavaDoc SPEC_FILE_NAME = "AntActionsSpec.xml";
62
63     private HashMap JavaDoc actions = null;
64
65     private ArrayList JavaDoc uninstActions = null;
66
67     /**
68      * Default constructor
69      */

70     public AntActionInstallerListener()
71     {
72         super(true);
73         actions = new HashMap JavaDoc();
74         uninstActions = new ArrayList JavaDoc();
75     }
76
77     /**
78      * Returns the actions map.
79      *
80      * @return the actions map
81      */

82     public HashMap JavaDoc getActions()
83     {
84         return (actions);
85     }
86
87     /*
88      * (non-Javadoc)
89      *
90      * @see com.izforge.izpack.installer.InstallerListener#beforePacks(com.izforge.izpack.installer.AutomatedInstallData,
91      * java.lang.Integer, com.izforge.izpack.util.AbstractUIProgressHandler)
92      */

93     public void beforePacks(AutomatedInstallData idata, Integer JavaDoc npacks,
94             AbstractUIProgressHandler handler) throws Exception JavaDoc
95     {
96         super.beforePacks(idata, npacks, handler);
97
98         getSpecHelper().readSpec(SPEC_FILE_NAME, new VariableSubstitutor(idata.getVariables()));
99
100         if (getSpecHelper().getSpec() == null) return;
101
102         // Selected packs.
103
Iterator JavaDoc iter = idata.selectedPacks.iterator();
104         Pack p = null;
105         while (iter != null && iter.hasNext())
106         {
107             p = (Pack) iter.next();
108
109             // Resolve data for current pack.
110
XMLElement pack = getSpecHelper().getPackForName(p.name);
111             if (pack == null) continue;
112
113             // Prepare the action cache
114
HashMap JavaDoc packActions = new HashMap JavaDoc();
115             packActions.put(ActionBase.BEFOREPACK, new ArrayList JavaDoc());
116             packActions.put(ActionBase.AFTERPACK, new ArrayList JavaDoc());
117             packActions.put(ActionBase.BEFOREPACKS, new ArrayList JavaDoc());
118             packActions.put(ActionBase.AFTERPACKS, new ArrayList JavaDoc());
119
120             // Get all entries for antcalls.
121
Vector JavaDoc antCallEntries = pack.getChildrenNamed(AntAction.ANTCALL);
122             if (antCallEntries != null && antCallEntries.size() >= 1)
123             {
124                 Iterator JavaDoc entriesIter = antCallEntries.iterator();
125                 while (entriesIter != null && entriesIter.hasNext())
126                 {
127                     AntAction act = readAntCall((XMLElement) entriesIter.next());
128                     if (act != null)
129                     {
130                         ((ArrayList JavaDoc) packActions.get(act.getOrder())).add(act);
131                     }
132                 }
133                 // Set for progress bar interaction.
134
if (((ArrayList JavaDoc) packActions.get(ActionBase.AFTERPACKS)).size() > 0)
135                     this.setProgressBarCaller();
136             }
137
138             actions.put(p.name, packActions);
139         }
140         iter = idata.availablePacks.iterator();
141         while (iter.hasNext())
142         {
143             String JavaDoc currentPack = ((Pack) iter.next()).name;
144             performAllActions(currentPack, ActionBase.BEFOREPACKS, null);
145         }
146     }
147
148     /*
149      * (non-Javadoc)
150      *
151      * @see com.izforge.izpack.installer.InstallerListener#beforePack(com.izforge.izpack.Pack,
152      * java.lang.Integer, com.izforge.izpack.util.AbstractUIProgressHandler)
153      */

154     public void beforePack(Pack pack, Integer JavaDoc i, AbstractUIProgressHandler handler)
155             throws Exception JavaDoc
156     {
157         performAllActions(pack.name, ActionBase.BEFOREPACK, handler);
158     }
159
160     /*
161      * (non-Javadoc)
162      *
163      * @see com.izforge.izpack.installer.InstallerListener#afterPack(com.izforge.izpack.Pack,
164      * java.lang.Integer, com.izforge.izpack.util.AbstractUIProgressHandler)
165      */

166     public void afterPack(Pack pack, Integer JavaDoc i, AbstractUIProgressHandler handler) throws Exception JavaDoc
167     {
168         performAllActions(pack.name, ActionBase.AFTERPACK, handler);
169     }
170
171     /*
172      * (non-Javadoc)
173      *
174      * @see com.izforge.izpack.compiler.InstallerListener#afterPacks(com.izforge.izpack.installer.AutomatedInstallData,
175      * com.izforge.izpack.util.AbstractUIProgressHandler)
176      */

177     public void afterPacks(AutomatedInstallData idata, AbstractUIProgressHandler handler)
178             throws Exception JavaDoc
179     {
180         if (informProgressBar())
181         {
182             handler.nextStep(getMsg("AntAction.pack"), getProgressBarCallerId(), getActionCount(
183                     idata, ActionBase.AFTERPACKS));
184         }
185         Iterator JavaDoc iter = idata.selectedPacks.iterator();
186         while (iter.hasNext())
187         {
188             String JavaDoc currentPack = ((Pack) iter.next()).name;
189             performAllActions(currentPack, ActionBase.AFTERPACKS, handler);
190         }
191         if (uninstActions.size() > 0)
192         {
193             UninstallData.getInstance().addAdditionalData("antActions", uninstActions);
194         }
195     }
196
197     private int getActionCount(AutomatedInstallData idata, String JavaDoc order)
198     {
199         int retval = 0;
200         Iterator JavaDoc iter = idata.selectedPacks.iterator();
201         while (iter.hasNext())
202         {
203             String JavaDoc currentPack = ((Pack) iter.next()).name;
204             ArrayList JavaDoc actList = getActions(currentPack, order);
205             if (actList != null) retval += actList.size();
206         }
207         return (retval);
208     }
209
210     /**
211      * Returns the defined actions for the given pack in the requested order.
212      *
213      * @param packName name of the pack for which the actions should be returned
214      * @param order order to be used; valid are <i>beforepack</i> and <i>afterpack</i>
215      * @return a list which contains all defined actions for the given pack and order
216      */

217     // -------------------------------------------------------
218
protected ArrayList JavaDoc getActions(String JavaDoc packName, String JavaDoc order)
219     {
220         if (actions == null) return null;
221
222         HashMap JavaDoc packActions = (HashMap JavaDoc) actions.get(packName);
223         if (packActions == null || packActions.size() == 0) return null;
224
225         return (ArrayList JavaDoc) packActions.get(order);
226     }
227
228     /**
229      * Performs all actions which are defined for the given pack and order.
230      *
231      * @param packName name of the pack for which the actions should be performed
232      * @param order order to be used; valid are <i>beforepack</i> and <i>afterpack</i>
233      * @throws InstallerException
234      */

235     private void performAllActions(String JavaDoc packName, String JavaDoc order, AbstractUIProgressHandler handler)
236             throws InstallerException
237     {
238         ArrayList JavaDoc actList = getActions(packName, order);
239         if (actList == null || actList.size() == 0) return;
240
241         Debug.trace("******* Executing all " + order + " actions of " + packName + " ...");
242         for (int i = 0; i < actList.size(); i++)
243         {
244             AntAction act = (AntAction) actList.get(i);
245             // Inform progress bar if needed. Works only
246
// on AFTER_PACKS
247
if (informProgressBar() && handler != null
248                     && handler instanceof ExtendedUIProgressHandler
249                     && order.equals(ActionBase.AFTERPACKS))
250             {
251                 ((ExtendedUIProgressHandler) handler)
252                         .progress((act.getMessageID() != null) ? getMsg(act.getMessageID()) : "");
253             }
254             try
255             {
256                 act.performInstallAction();
257             }
258             catch (Exception JavaDoc e)
259             {
260                 throw new InstallerException(e);
261             }
262             if (act.getUninstallTargets().size() > 0) uninstActions.add(act);
263         }
264     }
265
266     /**
267      * Returns an ant call which is defined in the given XML element.
268      *
269      * @param el XML element which contains the description of an ant call
270      * @return an ant call which is defined in the given XML element
271      * @throws InstallerException
272      */

273     private AntAction readAntCall(XMLElement el) throws InstallerException
274     {
275         if (el == null) return null;
276         SpecHelper spec = getSpecHelper();
277         AntAction act = new AntAction();
278         try
279         {
280             act.setOrder(spec.getRequiredAttribute(el, ActionBase.ORDER));
281             act.setUninstallOrder(el.getAttribute(ActionBase.UNINSTALL_ORDER,
282                     ActionBase.BEFOREDELETION));
283         }
284         catch (Exception JavaDoc e)
285         {
286             throw new InstallerException(e);
287         }
288
289         act.setQuiet(spec.isAttributeYes(el, ActionBase.QUIET, false));
290         act.setVerbose(spec.isAttributeYes(el, ActionBase.VERBOSE, false));
291         act.setBuildFile(spec.getRequiredAttribute(el, ActionBase.BUILDFILE));
292         String JavaDoc str = el.getAttribute(ActionBase.LOGFILE);
293         if (str != null)
294         {
295             act.setLogFile(str);
296         }
297         String JavaDoc msgId = el.getAttribute(ActionBase.MESSAGEID);
298         if (msgId != null && msgId.length() > 0) act.setMessageID(msgId);
299
300         // read propertyfiles
301
Iterator JavaDoc iter = el.getChildrenNamed(ActionBase.PROPERTYFILE).iterator();
302         while (iter.hasNext())
303         {
304             XMLElement propEl = (XMLElement) iter.next();
305             act.addPropertyFile(spec.getRequiredAttribute(propEl, ActionBase.PATH));
306         }
307
308         // read properties
309
iter = el.getChildrenNamed(ActionBase.PROPERTY).iterator();
310         while (iter.hasNext())
311         {
312             XMLElement propEl = (XMLElement) iter.next();
313             act.setProperty(spec.getRequiredAttribute(propEl, ActionBase.NAME), spec
314                     .getRequiredAttribute(propEl, ActionBase.VALUE));
315         }
316
317         // read targets
318
iter = el.getChildrenNamed(ActionBase.TARGET).iterator();
319         while (iter.hasNext())
320         {
321             XMLElement targEl = (XMLElement) iter.next();
322             act.addTarget(spec.getRequiredAttribute(targEl, ActionBase.NAME));
323         }
324
325         // read uninstall rules
326
iter = el.getChildrenNamed(ActionBase.UNINSTALL_TARGET).iterator();
327         while (iter.hasNext())
328         {
329             XMLElement utargEl = (XMLElement) iter.next();
330             act.addUninstallTarget(spec.getRequiredAttribute(utargEl, ActionBase.NAME));
331         }
332
333         return act;
334     }
335
336 }
337
Popular Tags