1 19 20 package org.netbeans.nbbuild; 21 22 import java.io.*; 23 import java.util.*; 24 import java.io.FileOutputStream ; 25 26 import org.w3c.dom.*; 27 import org.xml.sax.InputSource ; 28 29 import org.apache.tools.ant.BuildException; 30 import org.xml.sax.ErrorHandler ; 31 import org.xml.sax.SAXParseException ; 32 33 37 class UpdateTracking { 38 private static final String ELEMENT_MODULE = "module"; private static final String ATTR_CODENAME = "codename"; private static final String ELEMENT_VERSION = "module_version"; private static final String ATTR_VERSION = "specification_version"; private static final String ATTR_ORIGIN = "origin"; private static final String ATTR_LAST = "last"; private static final String ATTR_INSTALL = "install_time"; private static final String ELEMENT_FILE = "file"; private static final String ATTR_FILE_NAME = "name"; private static final String ATTR_CRC = "crc"; 49 private static final String NBM_ORIGIN = "nbm"; private static final String INST_ORIGIN = "installer"; 52 53 private static final String FILE_SEPARATOR = System.getProperty ("file.separator"); 55 56 public static final String TRACKING_DIRECTORY = "update_tracking"; 58 private boolean pError = false; 59 60 private File trackingFile = null; 61 62 private String origin = NBM_ORIGIN; 63 private String nbPath = null; 64 private Module module = null; 65 protected InputStream is = null; 66 protected OutputStream os = null; 67 68 public UpdateTracking( String nbPath ) { 70 this.nbPath = nbPath; 71 origin = INST_ORIGIN; 72 } 73 74 77 public UpdateTracking () { 78 this.nbPath = null; 79 origin = INST_ORIGIN; 80 } 81 82 public Version addNewModuleVersion( String codename, String spec_version ) { 83 module = new Module(); 84 module.setCodename( codename ); 85 Version version = new Version(); 86 version.setVersion( spec_version ); 87 version.setOrigin( origin ); 88 version.setLast( true ); 89 version.setInstall_time( System.currentTimeMillis() ); 90 module.setVersion( version ); 91 return version; 92 } 93 94 public String getVersionFromFile (File utf) throws BuildException { 95 this.setTrackingFile(utf.getParentFile(), utf.getName()); 96 read(); 97 if ( module.getVersions().size() != 1 ) 98 throw new BuildException ("Module described in update tracking file " + utf.getAbsolutePath() + " has got " + module.getVersions().size() + " specification versions. Correct number is 1."); 99 return module.getVersions().get(0).getVersion(); 100 } 101 102 public String getCodenameFromFile (File utf) throws BuildException { 103 this.setTrackingFile(utf.getParentFile(), utf.getName()); 104 read(); 105 if ( module.getVersions().size() != 1 ) 106 throw new BuildException ("Module described in update tracking file " + utf.getAbsolutePath() + " has got " + module.getVersions().size() + " specification versions. Correct number is 1."); 107 return module.getCodename(); 108 } 109 110 public String getVersionForCodeName( String codeName ) throws BuildException { 111 module = new Module(); 112 module.setCodename( codeName ); 113 File directory = new File( nbPath + FILE_SEPARATOR + TRACKING_DIRECTORY ); 115 setTrackingFile(directory, getTrackingFileName()); 116 if (!trackingFile.exists() || !trackingFile.isFile()) 117 throw new BuildException ("Tracking file " + trackingFile.getAbsolutePath() + " cannot be found for module codenamebase " + codeName ); 118 read(); 120 if ( module.getVersions().size() != 1 ) 121 throw new BuildException ("Module with codenamebase " + codeName + " has got " + module.getVersions().size() + " specification versions. Correct number is 1."); 122 return module.getVersions().get(0).getVersion(); 123 } 124 125 public String [] getListOfNBM( String codeName ) throws BuildException { 126 module = new Module(); 127 module.setCodename( codeName ); 128 if (this.is == null) { 129 File directory = new File( nbPath + FILE_SEPARATOR + TRACKING_DIRECTORY ); 130 setTrackingFile(directory, getTrackingFileName()); 131 if (!trackingFile.exists() || !trackingFile.isFile()) 132 throw new BuildException ("Tracking file " + trackingFile.getAbsolutePath() + " cannot be found for module codenamebase " + codeName ); 133 } 134 135 read(); 136 137 if ( module.getVersions().size() != 1 ) 138 throw new BuildException ("Module with codenamebase " + codeName + " has got " + module.getVersions().size() + " specification versions. Correct number is 1."); 139 140 List<ModuleFile> files = module.getVersions().get(0).getFiles(); 141 String [] listFiles = new String [ files.size() ]; 142 for (int i=0; i < files.size(); i++) { 143 listFiles[i] = files.get(i).getName().replace(File.separatorChar,'/'); 144 } 145 146 return listFiles; 147 } 148 149 public void removeLocalized( String locale ) { 150 File updateDirectory = new File( nbPath, TRACKING_DIRECTORY ); 151 File[] trackingFiles = updateDirectory.listFiles( new FileFilter() { public boolean accept( File file ) { 153 return file.isFile() &&file.getName().endsWith(".xml"); } 155 } ); 156 if (trackingFiles != null) 157 for (int i = trackingFiles.length-1; i >= 0; i--) { 158 trackingFile = trackingFiles[i]; 159 read(); 160 module.removeLocalized( locale ); 161 write(); 162 } 163 } 164 165 void write( ) throws BuildException{ 166 Document document = XMLUtil.createDocument(ELEMENT_MODULE); 167 Element e_module = document.getDocumentElement(); 168 e_module.setAttribute(ATTR_CODENAME, module.getCodename()); 169 for (Version ver : module.getVersions()) { 170 Element e_version = document.createElement(ELEMENT_VERSION); 171 e_version.setAttribute(ATTR_VERSION, ver.getVersion()); 172 e_version.setAttribute(ATTR_ORIGIN, ver.getOrigin()); 173 e_version.setAttribute(ATTR_LAST, "true"); e_version.setAttribute(ATTR_INSTALL, Long.toString(ver.getInstall_time())); 175 e_module.appendChild( e_version ); 176 for (ModuleFile file : ver.getFiles()) { 177 Element e_file = document.createElement(ELEMENT_FILE); 178 e_file.setAttribute(ATTR_FILE_NAME, file.getName().replace(File.separatorChar,'/')); 179 e_file.setAttribute(ATTR_CRC, file.getCrc()); 180 e_version.appendChild( e_file ); 181 } 182 } 183 184 if (this.os == null) { 186 File directory = new File( nbPath + FILE_SEPARATOR + TRACKING_DIRECTORY ); 187 if (!directory.exists()) { 188 directory.mkdirs(); 189 } 190 setTrackingFile(directory, this.getTrackingFileName()); 191 FileOutputStream fos = null; 192 try { 193 fos = new FileOutputStream (new File(directory,this.getTrackingFileName())); 194 } catch (Exception e) { 195 throw new BuildException("Could not get outputstream to write update tracking", e); 196 } 197 this.setTrackingOutputStream(fos); 198 } 199 try { 200 try { 201 XMLUtil.write(document, this.os); 202 } finally { 203 this.os.close(); 204 } 205 } catch (Exception e) { 206 e.printStackTrace(); 207 if ((trackingFile != null) && (trackingFile.exists())) 208 trackingFile.delete(); 209 throw new BuildException("Could not write update tracking", e); 210 } 211 } 212 213 protected void setTrackingFile (File dir, String tFname) throws BuildException { 214 this.trackingFile = new File(dir,tFname); 215 try { 217 if (this.trackingFile.exists()) 219 setTrackingInputStream(new FileInputStream(this.trackingFile)); 220 } catch (java.io.FileNotFoundException fnf) { 221 throw new BuildException("Unable to find tracking file "+this.trackingFile.getAbsolutePath(), fnf); 222 } 223 } 224 225 public void setTrackingOutputStream(OutputStream tos) { 226 this.os = tos; 227 } 228 229 public OutputStream getTrackingOutputStream() { 230 return this.os; 231 } 232 233 public void setTrackingInputStream(InputStream tis) { 234 this.is = tis; 235 } 236 237 public String getTrackingFileName() throws BuildException { 238 String trackingFileName = module.getCodenamebase(); 239 if ( ( trackingFileName == null ) || ( trackingFileName.length() == 0 ) ) 240 throw new BuildException ("Empty codenamebase, unable to locate tracking file"); 241 trackingFileName = trackingFileName.replace('.', '-') + ".xml"; return trackingFileName; 243 } 244 245 246 private void read() throws BuildException { 247 248 Document document; 249 if (this.is == null) { 250 File directory = new File( nbPath + FILE_SEPARATOR + TRACKING_DIRECTORY ); 251 if (!directory.exists()) { 252 directory.mkdirs(); 253 } 254 setTrackingFile(directory,getTrackingFileName()); 255 } 256 try { 257 InputSource xmlInputSource = new InputSource ( this.is ); 258 document = XMLUtil.parse( xmlInputSource, false, false, new ErrorCatcher(), null ); 259 if (is != null) 260 is.close(); 261 } catch ( org.xml.sax.SAXException e ) { 262 e.printStackTrace(); 263 if (trackingFile == null) { 264 throw new BuildException ("Update tracking data in external InputStream is not well formatted XML document.", e); 265 } else { 266 throw new BuildException ("Update tracking file " + trackingFile.getAbsolutePath() + " is not well formatted XML document.", e); 267 } 268 } catch ( java.io.IOException e ) { 269 e.printStackTrace(); 270 if (trackingFile == null) { 271 throw new BuildException ("I/O error while accessing tracking data in InputStream", e); 272 } else { 273 throw new BuildException ("I/O error while accessing tracking file " + trackingFile.getAbsolutePath(), e); 274 } 275 } 276 277 Element element = document.getDocumentElement(); 278 if ((element != null) && element.getTagName().equals(ELEMENT_MODULE)) { 279 scanElement_module(element); 280 } 281 } 282 283 284 void scanElement_module(Element element) { module = new Module(); 286 NamedNodeMap attrs = element.getAttributes(); 287 for (int i = 0; i < attrs.getLength(); i++) { 288 Attr attr = (Attr) attrs.item(i); 289 if (attr.getName().equals(ATTR_CODENAME)) { module.setCodename( attr.getValue() ); 291 } 292 } 293 NodeList nodes = element.getChildNodes(); 294 for (int i = 0; i < nodes.getLength(); i++) { 295 Node node = nodes.item(i); 296 if ( node.getNodeType() == Node.ELEMENT_NODE ) { 297 Element nodeElement = (Element) node; 298 if (nodeElement.getTagName().equals(ELEMENT_VERSION)) { 299 scanElement_module_version(nodeElement, module); 300 } 301 } 302 } 303 } 304 305 306 void scanElement_module_version(Element element, Module module) { Version version = new Version(); 308 NamedNodeMap attrs = element.getAttributes(); 309 for (int i = 0; i < attrs.getLength(); i++) { 310 Attr attr = (Attr) attrs.item(i); 311 if (attr.getName().equals(ATTR_VERSION)) { version.setVersion( attr.getValue() ); 313 } 314 if (attr.getName().equals(ATTR_ORIGIN)) { version.setOrigin( attr.getValue() ); 316 } 317 if (attr.getName().equals(ATTR_LAST)) { version.setLast( Boolean.getBoolean(attr.getValue() )); 319 } 320 if (attr.getName().equals(ATTR_INSTALL)) { long li = 0; 322 try { 323 li = Long.parseLong( attr.getValue() ); 324 } catch ( NumberFormatException nfe ) { 325 } 326 version.setInstall_time( li ); 327 } 328 } 329 NodeList nodes = element.getChildNodes(); 330 for (int i = 0; i < nodes.getLength(); i++) { 331 Node node = nodes.item(i); 332 if (node.getNodeType() == Node.ELEMENT_NODE) { 333 Element nodeElement = (Element) node; 334 if (nodeElement.getTagName().equals(ELEMENT_FILE)) { 335 scanElement_file(nodeElement, version); 336 } 337 } 338 } 339 module.addVersion( version ); 340 } 341 342 343 void scanElement_file(Element element, Version version) { ModuleFile file = new ModuleFile(); 345 NamedNodeMap attrs = element.getAttributes(); 346 for (int i = 0; i < attrs.getLength(); i++) { 347 Attr attr = (Attr)attrs.item(i); 348 if (attr.getName().equals(ATTR_FILE_NAME)) { file.setName( attr.getValue().replace(File.separatorChar,'/') ); 350 } 351 if (attr.getName().equals(ATTR_CRC)) { file.setCrc( attr.getValue() ); 353 } 354 } 355 version.addFile (file ); 356 } 357 358 class Module extends Object { 359 360 361 private String codename; 362 363 364 private List<Version> versions = new ArrayList<Version>(); 365 366 369 String getCodenamebase() { 370 String codenamebase = new String (codename); 371 int idx = codenamebase.lastIndexOf ('/'); if (idx != -1) codenamebase = codenamebase.substring (0, idx); 373 374 return codenamebase; 375 } 376 377 380 String getCodename() { 381 return codename; 382 } 383 384 387 void setCodename(String codename) { 388 this.codename = codename; 389 } 390 391 394 List<Version> getVersions() { 395 return versions; 396 } 397 398 401 void setVersions(List<Version> versions) { 402 this.versions = versions; 403 } 404 405 void addVersion( Version version ) { 406 versions = new ArrayList<Version>(); 407 versions.add( version ); 408 } 409 410 void setVersion( Version version ) { 411 versions = new ArrayList<Version>(); 412 versions.add( version ); 413 } 414 415 void removeLocalized( String locale ) { 416 Iterator it = versions.iterator(); 417 while (it.hasNext()) { 418 Version ver = (Version) it.next(); 419 ver.removeLocalized( locale ); 420 } 421 } 422 } 423 424 public class Version extends Object { 425 426 427 private String version; 428 429 430 private String origin; 431 432 433 private boolean last; 434 435 436 private long install_time = 0; 437 438 439 private List<ModuleFile> files = new ArrayList<ModuleFile>(); 440 441 444 String getVersion() { 445 return version; 446 } 447 448 451 void setVersion(String version) { 452 this.version = version; 453 } 454 455 458 String getOrigin() { 459 return origin; 460 } 461 462 465 void setOrigin(String origin) { 466 this.origin = origin; 467 } 468 469 472 boolean isLast() { 473 return last; 474 } 475 476 479 void setLast(boolean last) { 480 this.last = last; 481 } 482 483 486 long getInstall_time() { 487 return install_time; 488 } 489 490 493 void setInstall_time(long install_time) { 494 this.install_time = install_time; 495 } 496 497 500 List<ModuleFile> getFiles() { 501 return files; 502 } 503 504 507 void setFiles(List<ModuleFile> files) { 508 this.files = files; 509 } 510 511 void addFile( ModuleFile file ) { 512 files.add( file ); 513 } 514 515 public void addFileWithCrc( String filename, String crc ) { 516 ModuleFile file = new ModuleFile(); 517 file.setName( filename ); 518 file.setCrc( crc); 519 files.add( file ); 520 } 521 522 public void removeLocalized( String locale ) { 523 List<ModuleFile> newFiles = new ArrayList<ModuleFile>(); 524 for (ModuleFile file : files) { 525 if (file.getName().indexOf("_" + locale + ".") == -1 && file.getName().indexOf("_" + locale + "/") == -1 && !file.getName().endsWith("_" + locale) ) newFiles.add ( file ); 529 } 530 files = newFiles; 531 532 } 533 534 } 535 536 class ModuleFile extends Object { 537 538 539 private String name; 540 541 542 private String crc; 543 544 547 String getName() { 548 return name; 549 } 550 551 554 void setName(String name) { 555 this.name = name.replace(File.separatorChar,'/'); 556 } 557 558 561 String getCrc() { 562 return crc; 563 } 564 565 568 void setCrc(String crc) { 569 this.crc = crc; 570 } 571 572 } 573 574 class ErrorCatcher implements ErrorHandler { 575 public void error(SAXParseException e) { 576 pError = true; 578 } 579 580 public void warning(SAXParseException e) { 581 } 583 584 public void fatalError(SAXParseException e) { 585 pError = true; 586 } 587 } 588 589 } 590 | Popular Tags |