1 22 23 package com.izforge.izpack.event; 24 25 import java.util.ArrayList ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.Vector ; 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 52 public class AntActionInstallerListener extends SimpleInstallerListener 53 { 54 55 61 public static final String SPEC_FILE_NAME = "AntActionsSpec.xml"; 62 63 private HashMap actions = null; 64 65 private ArrayList uninstActions = null; 66 67 70 public AntActionInstallerListener() 71 { 72 super(true); 73 actions = new HashMap (); 74 uninstActions = new ArrayList (); 75 } 76 77 82 public HashMap getActions() 83 { 84 return (actions); 85 } 86 87 93 public void beforePacks(AutomatedInstallData idata, Integer npacks, 94 AbstractUIProgressHandler handler) throws Exception 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 Iterator iter = idata.selectedPacks.iterator(); 104 Pack p = null; 105 while (iter != null && iter.hasNext()) 106 { 107 p = (Pack) iter.next(); 108 109 XMLElement pack = getSpecHelper().getPackForName(p.name); 111 if (pack == null) continue; 112 113 HashMap packActions = new HashMap (); 115 packActions.put(ActionBase.BEFOREPACK, new ArrayList ()); 116 packActions.put(ActionBase.AFTERPACK, new ArrayList ()); 117 packActions.put(ActionBase.BEFOREPACKS, new ArrayList ()); 118 packActions.put(ActionBase.AFTERPACKS, new ArrayList ()); 119 120 Vector antCallEntries = pack.getChildrenNamed(AntAction.ANTCALL); 122 if (antCallEntries != null && antCallEntries.size() >= 1) 123 { 124 Iterator entriesIter = antCallEntries.iterator(); 125 while (entriesIter != null && entriesIter.hasNext()) 126 { 127 AntAction act = readAntCall((XMLElement) entriesIter.next()); 128 if (act != null) 129 { 130 ((ArrayList ) packActions.get(act.getOrder())).add(act); 131 } 132 } 133 if (((ArrayList ) 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 currentPack = ((Pack) iter.next()).name; 144 performAllActions(currentPack, ActionBase.BEFOREPACKS, null); 145 } 146 } 147 148 154 public void beforePack(Pack pack, Integer i, AbstractUIProgressHandler handler) 155 throws Exception 156 { 157 performAllActions(pack.name, ActionBase.BEFOREPACK, handler); 158 } 159 160 166 public void afterPack(Pack pack, Integer i, AbstractUIProgressHandler handler) throws Exception 167 { 168 performAllActions(pack.name, ActionBase.AFTERPACK, handler); 169 } 170 171 177 public void afterPacks(AutomatedInstallData idata, AbstractUIProgressHandler handler) 178 throws Exception 179 { 180 if (informProgressBar()) 181 { 182 handler.nextStep(getMsg("AntAction.pack"), getProgressBarCallerId(), getActionCount( 183 idata, ActionBase.AFTERPACKS)); 184 } 185 Iterator iter = idata.selectedPacks.iterator(); 186 while (iter.hasNext()) 187 { 188 String 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 order) 198 { 199 int retval = 0; 200 Iterator iter = idata.selectedPacks.iterator(); 201 while (iter.hasNext()) 202 { 203 String currentPack = ((Pack) iter.next()).name; 204 ArrayList actList = getActions(currentPack, order); 205 if (actList != null) retval += actList.size(); 206 } 207 return (retval); 208 } 209 210 217 protected ArrayList getActions(String packName, String order) 219 { 220 if (actions == null) return null; 221 222 HashMap packActions = (HashMap ) actions.get(packName); 223 if (packActions == null || packActions.size() == 0) return null; 224 225 return (ArrayList ) packActions.get(order); 226 } 227 228 235 private void performAllActions(String packName, String order, AbstractUIProgressHandler handler) 236 throws InstallerException 237 { 238 ArrayList 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 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 e) 259 { 260 throw new InstallerException(e); 261 } 262 if (act.getUninstallTargets().size() > 0) uninstActions.add(act); 263 } 264 } 265 266 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 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 str = el.getAttribute(ActionBase.LOGFILE); 293 if (str != null) 294 { 295 act.setLogFile(str); 296 } 297 String msgId = el.getAttribute(ActionBase.MESSAGEID); 298 if (msgId != null && msgId.length() > 0) act.setMessageID(msgId); 299 300 Iterator 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 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 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 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 |