1 19 20 package org.netbeans.modules.autoupdate; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.InputStream ; 25 import java.io.FileOutputStream ; 26 import java.io.OutputStream ; 27 import java.util.*; 28 import org.netbeans.updater.UpdateTracking; 29 30 import org.w3c.dom.*; 31 import org.xml.sax.InputSource ; 32 33 import org.openide.xml.XMLUtil; 34 import org.openide.modules.SpecificationVersion; 35 import org.openide.util.Exceptions; 36 37 42 class PreparedModules extends Object { 43 44 private static final String ELEMENT_MODULES = "module_updates"; private static final String ELEMENT_MODULE = "module"; private static final String ATTR_CODENAMEBASE = "codenamebase"; private static final String ATTR_NAME = "name"; private static final String ATTR_SPEC_VERSION = "specification_version"; private static final String ATTR_SIZE = "size"; private static final String ATTR_NBM_NAME = "nbm_name"; 52 55 private List modules = new ArrayList(); 56 57 private boolean pError = false; 58 59 private static PreparedModules preparedModules; 60 61 static PreparedModules getPrepared() { 62 if ( preparedModules == null ) { 63 readPrepared(); 64 } 65 return preparedModules; 66 } 67 68 static void readPrepared() { 69 preparedModules = new PreparedModules(); 70 preparedModules.read(); 71 } 72 73 76 List getModules() { 77 return modules; 78 } 79 80 void addModule(ModuleUpdate mu) { 81 Module mod = new Module(); 82 mod.setCodenamebase( mu.getCodeNameBase() ); 83 mod.setName( mu.getName() ); 84 mod.setSize( mu.getDownloadSize() ); 85 SpecificationVersion sv = mu.getRemoteModule().getSpecificationVersion(); 86 mod.setSpec_version( sv == null ? null : sv.toString() ); 87 mod.setNbm_name( mu.getDistributionFilename() ); 88 89 if (mu.isToInstallDir ()) { 91 mod.setCluster (mu.findInstallDirectory ()); 93 } else { 94 mod.setCluster (new File (System.getProperty ("netbeans.user"))); } 97 98 modules.add( mod ); 99 } 100 101 void removeModule(String codenamebase) { 102 for (int i=0; i< modules.size(); i++) { 103 PreparedModules.Module mod = (PreparedModules.Module)modules.get(i); 104 if (mod.getCodenamebase().equals(codenamebase)) { 105 modules.remove(i); 106 return; 107 } 108 } 109 } 110 111 String getPreparedVersion(String codenamebase) { 112 for (int i=0; i< modules.size(); i++) { 113 PreparedModules.Module mod = (PreparedModules.Module)modules.get(i); 114 if (mod.getCodenamebase().equals(codenamebase)) { 115 return mod.getSpec_version(); 116 } 117 } 118 return null; 119 } 120 121 boolean hasNBM(String nbm_name) { 122 for (int i=0; i< modules.size(); i++) { 123 PreparedModules.Module mod = (PreparedModules.Module)modules.get(i); 124 if (mod.getNbm_name().equals(nbm_name)) { 125 return true; 126 } 127 } 128 return false; 129 } 130 131 void delete() { 132 List clusters = UpdateTracking.clusters (true); 133 assert clusters != null; 134 Iterator it = clusters.iterator (); 135 while (it.hasNext ()) { 136 getInstall_Later ((File )it.next ()).delete (); 137 } 138 } 139 140 private File getInstall_Later (File rootOfCluster) { 141 return Autoupdater.Support.getInstall_Later (rootOfCluster); 142 } 143 144 void write () { 145 List clusters = UpdateTracking.clusters (true); 147 assert clusters != null : "Clusters cannot be empty."; Iterator iter = clusters.iterator (); 149 while (iter.hasNext ()) { 150 writeToCluster ((File )iter.next ()); 151 } 152 } 153 154 private void writeToCluster (File cluster) { 155 Document document = XMLUtil.createDocument(ELEMENT_MODULES, null, null, null); 156 157 Element root = document.getDocumentElement(); 158 Element module = null; 159 Iterator it = modules.iterator(); 160 boolean empty = true; 161 while ( it.hasNext() ) { 162 Module mod = (Module)it.next(); 163 164 if (cluster.equals (mod.getCluster ())) { 166 module = document.createElement(ELEMENT_MODULE); 167 module.setAttribute(ATTR_CODENAMEBASE, mod.getCodenamebase()); 168 module.setAttribute(ATTR_NAME, mod.getName()); 169 module.setAttribute(ATTR_SPEC_VERSION, mod.getSpec_version()); 170 module.setAttribute(ATTR_SIZE, Long.toString(mod.getSize())); 171 module.setAttribute(ATTR_NBM_NAME, mod.getNbm_name()); 172 173 root.appendChild( module ); 174 empty = false; 175 } 176 } 177 178 if ( empty ) 179 return; 180 181 document.getDocumentElement().normalize(); 182 183 File installLaterFile = Autoupdater.Support.getInstall_Later (cluster); 184 installLaterFile.getParentFile ().mkdirs (); 185 186 OutputStream os = null; 187 try { 188 os = new FileOutputStream (installLaterFile); 189 XMLUtil.write(document, os, "UTF-8"); } catch (java.io.FileNotFoundException fnfe) { 191 Exceptions.printStackTrace(fnfe); 192 } catch (java.io.IOException ioe) { 193 Exceptions.printStackTrace(ioe); 194 } finally { 195 if (os != null) { 196 try { 197 os.close (); 198 } catch (Exception x) { 199 Exceptions.printStackTrace(x); 200 } 201 } 202 } 203 204 } 205 206 207 private void read() { 208 modules.clear(); 209 210 org.w3c.dom.Document document = null; 211 212 InputStream is; 213 try { 214 215 List clusters = UpdateTracking.clusters (true); 216 assert clusters != null : "Clusters cannot be empty."; Iterator iter = clusters.iterator (); 218 while (iter.hasNext ()) { 219 File cluster = (File )iter.next (); 220 File installLaterFile = Autoupdater.Support.getInstall_Later (cluster); 221 if (installLaterFile.exists ()) { 222 is = new FileInputStream ( installLaterFile ); 223 224 InputSource xmlInputSource = new InputSource ( is ); 225 document = XMLUtil.parse( xmlInputSource, false, false, new ErrorCatcher(), null ); 226 if (is != null) 227 is.close(); 228 229 assert document != null; 230 231 org.w3c.dom.Element element = document.getDocumentElement(); 232 if ((element != null) && element.getTagName().equals(ELEMENT_MODULES)) { 233 scanElement_module_updates (element, cluster); 234 } 235 } 236 } 237 } 238 catch ( org.xml.sax.SAXException e ) { 239 System.out.println("Bad install_later" ); e.printStackTrace (); 241 return; 242 } 243 catch ( java.io.IOException e ) { 244 System.out.println("Missing install_later" ); e.printStackTrace (); 246 return; 247 } 248 } 249 250 251 private void scanElement_module_updates (org.w3c.dom.Element element, File cluster) { org.w3c.dom.NodeList nodes = element.getChildNodes(); 253 for (int i = 0; i < nodes.getLength(); i++) { 254 org.w3c.dom.Node node = nodes.item(i); 255 if (node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) { 256 org.w3c.dom.Element nodeElement = (org.w3c.dom.Element )node; 257 if (nodeElement.getTagName().equals(ELEMENT_MODULE)) { 258 scanElement_module (nodeElement, cluster); 259 } 260 } 261 } 262 } 263 264 265 private void scanElement_module (org.w3c.dom.Element element, File cluster) { Module module = new Module(); 267 org.w3c.dom.NamedNodeMap attrs = element.getAttributes(); 268 for (int i = 0; i < attrs.getLength(); i++) { 269 org.w3c.dom.Attr attr = (org.w3c.dom.Attr )attrs.item(i); 270 if (attr.getName().equals(ATTR_CODENAMEBASE)) { module.setCodenamebase( attr.getValue() ); 272 } 273 if (attr.getName().equals(ATTR_SPEC_VERSION)) { module.setSpec_version( attr.getValue() ); 275 } 276 if (attr.getName().equals(ATTR_NAME)) { module.setName( attr.getValue() ); 278 } 279 if (attr.getName().equals(ATTR_SIZE)) { try { 281 module.setSize( Long.parseLong( attr.getValue())); 282 } catch (NumberFormatException nfe) { 283 module.setSize( 0 ); 284 } 285 } 286 if (attr.getName().equals(ATTR_NBM_NAME)) { module.setNbm_name( attr.getValue() ); 288 } 289 module.setCluster (cluster); 290 } 291 modules.add( module ); 292 } 293 294 class Module extends Object { 295 296 private String name; 297 298 299 private String codenamebase; 300 301 302 private String spec_version; 303 304 305 private long size; 306 307 308 private String nbm_name; 309 310 private File cluster; 311 312 315 public String getName() { 316 return name; 317 } 318 319 322 public void setName(String name) { 323 this.name = name; 324 } 325 326 329 public String getCodenamebase() { 330 return codenamebase; 331 } 332 333 336 public void setCodenamebase(String codenamebase) { 337 this.codenamebase = codenamebase; 338 } 339 340 343 public String getSpec_version() { 344 return spec_version; 345 } 346 347 350 public void setSpec_version(String spec_version) { 351 this.spec_version = spec_version; 352 } 353 354 357 public long getSize() { 358 return size; 359 } 360 361 364 public void setSize(long size) { 365 this.size = size; 366 } 367 368 371 public String getNbm_name() { 372 return nbm_name; 373 } 374 375 378 public void setNbm_name(String nbm_name) { 379 this.nbm_name = nbm_name; 380 } 381 382 public void setCluster (File cluster) { 383 this.cluster = cluster; 384 } 385 386 public File getCluster () { 387 return cluster; 388 } 389 390 } 391 392 class ErrorCatcher implements org.xml.sax.ErrorHandler { 393 private void message (String level, org.xml.sax.SAXParseException e) { 394 pError = true; 395 } 396 397 public void error (org.xml.sax.SAXParseException e) { 398 pError = true; 400 } 401 402 public void warning (org.xml.sax.SAXParseException e) { 403 } 405 406 public void fatalError (org.xml.sax.SAXParseException e) { 407 pError = true; 408 } 409 } 410 } 411 | Popular Tags |