1 21 22 package com.izforge.izpack.installer; 23 24 import java.io.BufferedWriter ; 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.InputStream ; 28 import java.io.ObjectOutputStream ; 29 import java.io.OutputStreamWriter ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.TreeMap ; 33 import java.util.Vector ; 34 import java.util.zip.ZipEntry ; 35 import java.util.zip.ZipOutputStream ; 36 37 import net.n3.nanoxml.NonValidator; 38 import net.n3.nanoxml.StdXMLBuilder; 39 import net.n3.nanoxml.StdXMLParser; 40 import net.n3.nanoxml.StdXMLReader; 41 import net.n3.nanoxml.XMLElement; 42 43 import com.izforge.izpack.ExecutableFile; 44 import com.izforge.izpack.LocaleDatabase; 45 import com.izforge.izpack.Panel; 46 import com.izforge.izpack.util.Debug; 47 import com.izforge.izpack.util.Housekeeper; 48 import com.izforge.izpack.util.OsConstraint; 49 50 57 public class AutomatedInstaller extends InstallerBase 58 { 59 60 private TreeMap panelInstanceCount; 64 65 66 private AutomatedInstallData idata = new AutomatedInstallData(); 67 68 69 private boolean result = false; 70 71 77 public AutomatedInstaller(String inputFilename) throws Exception 78 { 79 super(); 80 81 File input = new File (inputFilename); 82 83 loadInstallData(this.idata); 85 86 this.idata.xmlData = getXMLData(input); 88 89 this.idata.localeISO3 = this.idata.xmlData.getAttribute("langpack", "eng"); 91 InputStream in = getClass().getResourceAsStream("/langpacks/" + this.idata.localeISO3 + ".xml"); 92 this.idata.langpack = new LocaleDatabase(in); 93 this.idata.setVariable(ScriptParser.ISO3_LANG, this.idata.localeISO3); 94 95 ResourceManager.create(this.idata); 97 98 addCustomLangpack(this.idata); 100 101 this.panelInstanceCount = new TreeMap (); 102 } 103 104 112 private boolean writeUninstallData() 113 { 114 try 115 { 116 UninstallData udata = UninstallData.getInstance(); 118 List files = udata.getFilesList(); 119 ZipOutputStream outJar = this.idata.uninstallOutJar; 120 121 if (outJar == null) 122 return true; 124 System.out.println("[ Writing the uninstaller data ... ]"); 125 126 outJar.putNextEntry(new ZipEntry ("install.log")); 128 BufferedWriter logWriter = new BufferedWriter (new OutputStreamWriter (outJar)); 129 logWriter.write(this.idata.getInstallPath()); 130 logWriter.newLine(); 131 Iterator iter = files.iterator(); 132 while (iter.hasNext()) 133 { 134 logWriter.write((String ) iter.next()); 135 if (iter.hasNext()) logWriter.newLine(); 136 } 137 logWriter.flush(); 138 outJar.closeEntry(); 139 140 outJar.putNextEntry(new ZipEntry ("jarlocation.log")); 142 logWriter = new BufferedWriter (new OutputStreamWriter (outJar)); 143 logWriter.write(udata.getUninstallerJarFilename()); 144 logWriter.newLine(); 145 logWriter.write(udata.getUninstallerPath()); 146 logWriter.flush(); 147 outJar.closeEntry(); 148 149 outJar.putNextEntry(new ZipEntry ("executables")); 151 ObjectOutputStream execStream = new ObjectOutputStream (outJar); 152 iter = udata.getExecutablesList().iterator(); 153 execStream.writeInt(udata.getExecutablesList().size()); 154 while (iter.hasNext()) 155 { 156 ExecutableFile file = (ExecutableFile) iter.next(); 157 execStream.writeObject(file); 158 } 159 execStream.flush(); 160 outJar.closeEntry(); 161 162 outJar.flush(); 164 outJar.close(); 165 return true; 166 } 167 catch (Exception err) 168 { 169 err.printStackTrace(); 170 return false; 171 } 172 } 173 174 179 protected void doInstall() throws Exception 180 { 181 System.out.println("[ Starting automated installation ]"); 183 Debug.log("[ Starting automated installation ]"); 184 185 try 186 { 187 this.result = true; 189 190 Iterator panelsIterator = this.idata.panelsOrder.iterator(); 192 while (panelsIterator.hasNext()) 193 { 194 Panel p = (Panel) panelsIterator.next(); 195 196 String praefix = "com.izforge.izpack.panels."; 197 if (p.className.compareTo(".") > -1) 198 praefix = ""; 200 if (!OsConstraint.oneMatchesCurrentSystem(p.osConstraints)) continue; 201 202 String panelClassName = p.className; 203 String automationHelperClassName = praefix + panelClassName + "AutomationHelper"; 204 Class automationHelperClass = null; 205 206 Debug.log( "AutomationHelper:" + automationHelperClassName ); 207 try 209 { 210 211 automationHelperClass = Class.forName(automationHelperClassName); 212 213 } 214 catch (ClassNotFoundException e) 215 { 216 Debug.log( "ClassNotFoundException-skip :" + automationHelperClassName ); 218 continue; 219 } 220 221 PanelAutomation automationHelperInstance = null; 223 if (automationHelperClass != null) 224 { 225 try 226 { 227 Debug.log( "Instantiate :" + automationHelperClassName ); 228 automationHelperInstance = (PanelAutomation) automationHelperClass 229 .newInstance(); 230 } 231 catch (Exception e) 232 { 233 Debug.log("ERROR: no default constructor for " 234 + automationHelperClassName + ", skipping..."); 235 continue; 236 } 237 } 238 239 Vector panelRoots = this.idata.xmlData.getChildrenNamed(panelClassName); 241 int panelRootNo = 0; 242 243 if (this.panelInstanceCount.containsKey(panelClassName)) 244 { 245 panelRootNo = ((Integer ) this.panelInstanceCount.get(panelClassName)).intValue(); 247 } 248 249 XMLElement panelRoot = (XMLElement) panelRoots.elementAt(panelRootNo); 250 251 this.panelInstanceCount.put(panelClassName, new Integer (panelRootNo + 1)); 252 253 if (automationHelperInstance != null) 256 { 257 try 258 { 259 Debug.log( "automationHelperInstance.runAutomated :" + automationHelperClassName + " entered." ); 260 if (! automationHelperInstance.runAutomated(this.idata, panelRoot)) 261 { 262 this.result = false; 264 return; 265 } 266 else 267 { 268 Debug.log( "automationHelperInstance.runAutomated :" + automationHelperClassName + " successfully done." ); 269 } 270 } 271 catch (Exception e) 272 { 273 Debug.log( "ERROR: automated installation failed for panel " 274 + panelClassName ); 275 e.printStackTrace(); 276 this.result = false; 277 continue; 278 } 279 280 } 281 282 } 283 284 writeUninstallData(); 286 287 if (this.result) 288 System.out.println("[ Automated installation done ]"); 289 else 290 System.out.println("[ Automated installation FAILED! ]"); 291 } 292 catch (Exception e) 293 { 294 this.result = false; 295 System.err.println(e.toString()); 296 e.printStackTrace(); 297 System.out.println("[ Automated installation FAILED! ]"); 298 } 299 finally 300 { 301 Housekeeper.getInstance().shutDown(this.result ? 0 : 1); 303 } 304 } 305 306 315 public XMLElement getXMLData(File input) throws Exception 316 { 317 FileInputStream in = new FileInputStream (input); 318 319 StdXMLParser parser = new StdXMLParser(); 321 parser.setBuilder(new StdXMLBuilder()); 322 parser.setReader(new StdXMLReader(in)); 323 parser.setValidator(new NonValidator()); 324 325 XMLElement rtn = (XMLElement) parser.parse(); 326 in.close(); 327 328 return rtn; 329 } 330 331 336 public boolean getResult() 337 { 338 return this.result; 339 } 340 } 341 | Popular Tags |