KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > nbbuild > UpdateTracking


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.nbbuild;
21
22 import java.io.*;
23 import java.util.*;
24 import java.io.FileOutputStream JavaDoc;
25
26 import org.w3c.dom.*;
27 import org.xml.sax.InputSource JavaDoc;
28
29 import org.apache.tools.ant.BuildException;
30 import org.xml.sax.ErrorHandler JavaDoc;
31 import org.xml.sax.SAXParseException JavaDoc;
32
33 /** This class represents module updates tracking
34  *
35  * @author akemr
36  */

37 class UpdateTracking {
38     private static final String JavaDoc ELEMENT_MODULE = "module"; // NOI18N
39
private static final String JavaDoc ATTR_CODENAME = "codename"; // NOI18N
40
private static final String JavaDoc ELEMENT_VERSION = "module_version"; // NOI18N
41
private static final String JavaDoc ATTR_VERSION = "specification_version"; // NOI18N
42
private static final String JavaDoc ATTR_ORIGIN = "origin"; // NOI18N
43
private static final String JavaDoc ATTR_LAST = "last"; // NOI18N
44
private static final String JavaDoc ATTR_INSTALL = "install_time"; // NOI18N
45
private static final String JavaDoc ELEMENT_FILE = "file"; // NOI18N
46
private static final String JavaDoc ATTR_FILE_NAME = "name"; // NOI18N
47
private static final String JavaDoc ATTR_CRC = "crc"; // NOI18N
48

49     private static final String JavaDoc NBM_ORIGIN = "nbm"; // NOI18N
50
private static final String JavaDoc INST_ORIGIN = "installer"; // NOI18N
51

52     /** Platform dependent file name separator */
53     private static final String JavaDoc FILE_SEPARATOR = System.getProperty ("file.separator"); // NOI18N
54

55     /** The name of the install_later file */
56     public static final String JavaDoc TRACKING_DIRECTORY = "update_tracking"; // NOI18N
57

58     private boolean pError = false;
59     
60     private File trackingFile = null;
61     
62     private String JavaDoc origin = NBM_ORIGIN;
63     private String JavaDoc nbPath = null;
64     private Module module = null;
65     protected InputStream is = null;
66     protected OutputStream JavaDoc os = null;
67    
68     // for generating xml in build process
69
public UpdateTracking( String JavaDoc nbPath ) {
70         this.nbPath = nbPath;
71         origin = INST_ORIGIN;
72     }
73     
74     /**
75      * Use this constructor, only when you want to use I/O Streams
76      */

77     public UpdateTracking () {
78         this.nbPath = null;
79         origin = INST_ORIGIN;
80     }
81     
82     public Version addNewModuleVersion( String JavaDoc codename, String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc getVersionForCodeName( String JavaDoc codeName ) throws BuildException {
111         module = new Module();
112         module.setCodename( codeName );
113 // if (this.is == null) {
114
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 // }
119
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 JavaDoc[] getListOfNBM( String JavaDoc 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 JavaDoc [] listFiles = new String JavaDoc[ 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 JavaDoc locale ) {
150         File updateDirectory = new File( nbPath, TRACKING_DIRECTORY );
151         File[] trackingFiles = updateDirectory.listFiles( new FileFilter() { // Get only *.xml files
152
public boolean accept( File file ) {
153                 return file.isFile() &&file.getName().endsWith(".xml"); //NOI18N
154
}
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"); //NOI18N
174
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         //document.getDocumentElement().normalize();
185
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 JavaDoc fos = null;
192             try {
193                 fos = new FileOutputStream JavaDoc(new File(directory,this.getTrackingFileName()));
194             } catch (Exception JavaDoc 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 JavaDoc 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 JavaDoc tFname) throws BuildException {
214         this.trackingFile = new File(dir,tFname);
215 // this.trackingFile.mkdirs();
216
try {
217             //setTrackingOutputStream(new FileOutputStream(this.trackingFile));
218
if (this.trackingFile.exists())
219                 setTrackingInputStream(new FileInputStream(this.trackingFile));
220         } catch (java.io.FileNotFoundException JavaDoc fnf) {
221             throw new BuildException("Unable to find tracking file "+this.trackingFile.getAbsolutePath(), fnf);
222         }
223     }
224     
225     public void setTrackingOutputStream(OutputStream JavaDoc tos) {
226         this.os = tos;
227     }
228     
229     public OutputStream JavaDoc getTrackingOutputStream() {
230         return this.os;
231     }
232     
233     public void setTrackingInputStream(InputStream tis) {
234         this.is = tis;
235     }
236     
237     public String JavaDoc getTrackingFileName() throws BuildException {
238         String JavaDoc 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"; //NOI18N
242
return trackingFileName;
243     }
244
245     /** Scan through Document document. */
246     private void read() throws BuildException {
247         /** Document document */
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 JavaDoc xmlInputSource = new InputSource JavaDoc( 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 JavaDoc 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 JavaDoc 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     /** Scan through Element named module. */
284     void scanElement_module(Element element) { // <module>
285
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 codename="???">
290
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     /** Scan through Element named module_version. */
306     void scanElement_module_version(Element element, Module module) { // <module_version>
307
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)) { // <module_version specification_version="???">
312
version.setVersion( attr.getValue() );
313             }
314             if (attr.getName().equals(ATTR_ORIGIN)) { // <module_version origin="???">
315
version.setOrigin( attr.getValue() );
316             }
317             if (attr.getName().equals(ATTR_LAST)) { // <module_version last="???">
318
version.setLast( Boolean.getBoolean(attr.getValue() ));
319             }
320             if (attr.getName().equals(ATTR_INSTALL)) { // <module_version install_time="???">
321
long li = 0;
322                 try {
323                     li = Long.parseLong( attr.getValue() );
324                 } catch ( NumberFormatException JavaDoc 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     /** Scan through Element named file. */
343     void scanElement_file(Element element, Version version) { // <file>
344
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 name="???">
349
file.setName( attr.getValue().replace(File.separatorChar,'/') );
350             }
351             if (attr.getName().equals(ATTR_CRC)) { // <file crc="???">
352
file.setCrc( attr.getValue() );
353             }
354         }
355         version.addFile (file );
356     }
357     
358     class Module extends Object JavaDoc {
359         
360         /** Holds value of property codename. */
361         private String JavaDoc codename;
362         
363         /** Holds value of property versions. */
364         private List<Version> versions = new ArrayList<Version>();
365         
366         /** Getter for property codenamebase.
367          * @return Value of property codenamebase.
368          */

369         String JavaDoc getCodenamebase() {
370         String JavaDoc codenamebase = new String JavaDoc(codename);
371             int idx = codenamebase.lastIndexOf ('/'); //NOI18N
372
if (idx != -1) codenamebase = codenamebase.substring (0, idx);
373
374             return codenamebase;
375         }
376
377          /** Getter for property codename.
378          * @return Value of property codename.
379          */

380         String JavaDoc getCodename() {
381             return codename;
382         }
383        
384         /** Setter for property codename.
385          * @param codename New value of property codename.
386          */

387         void setCodename(String JavaDoc codename) {
388             this.codename = codename;
389         }
390         
391         /** Getter for property versions.
392          * @return Value of property versions.
393          */

394         List<Version> getVersions() {
395             return versions;
396         }
397         
398         /** Setter for property versions.
399          * @param versions New value of property versions.
400          */

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 JavaDoc 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 JavaDoc {
425         
426         /** Holds value of property version. */
427         private String JavaDoc version;
428         
429         /** Holds value of property origin. */
430         private String JavaDoc origin;
431         
432         /** Holds value of property last. */
433         private boolean last;
434         
435         /** Holds value of property install_time. */
436         private long install_time = 0;
437         
438         /** Holds value of property files. */
439         private List<ModuleFile> files = new ArrayList<ModuleFile>();
440         
441         /** Getter for property version.
442          * @return Value of property version.
443          */

444         String JavaDoc getVersion() {
445             return version;
446         }
447         
448         /** Setter for property version.
449          * @param version New value of property version.
450          */

451         void setVersion(String JavaDoc version) {
452             this.version = version;
453         }
454         
455         /** Getter for property origin.
456          * @return Value of property origin.
457          */

458         String JavaDoc getOrigin() {
459             return origin;
460         }
461         
462         /** Setter for property origin.
463          * @param origin New value of property origin.
464          */

465         void setOrigin(String JavaDoc origin) {
466             this.origin = origin;
467         }
468         
469         /** Getter for property last.
470          * @return Value of property last.
471          */

472         boolean isLast() {
473             return last;
474         }
475         
476         /** Setter for property last.
477          * @param last New value of property last.
478          */

479         void setLast(boolean last) {
480             this.last = last;
481         }
482         
483         /** Getter for property install_time.
484          * @return Value of property install_time.
485          */

486         long getInstall_time() {
487             return install_time;
488         }
489         
490         /** Setter for property install_time.
491          * @param install_time New value of property install_time.
492          */

493         void setInstall_time(long install_time) {
494             this.install_time = install_time;
495         }
496         
497         /** Getter for property files.
498          * @return Value of property files.
499          */

500         List<ModuleFile> getFiles() {
501             return files;
502         }
503         
504         /** Setter for property files.
505          * @param files New value of property files.
506          */

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 JavaDoc filename, String JavaDoc 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 JavaDoc locale ) {
523             List<ModuleFile> newFiles = new ArrayList<ModuleFile>();
524             for (ModuleFile file : files) {
525                 if (file.getName().indexOf("_" + locale + ".") == -1 // NOI18N
526
&& file.getName().indexOf("_" + locale + "/") == -1 // NOI18N
527
&& !file.getName().endsWith("_" + locale) ) // NOI18N
528
newFiles.add ( file );
529             }
530             files = newFiles;
531             
532         }
533         
534     }
535     
536     class ModuleFile extends Object JavaDoc {
537         
538         /** Holds value of property name. */
539         private String JavaDoc name;
540         
541         /** Holds value of property crc. */
542         private String JavaDoc crc;
543         
544         /** Getter for property name.
545          * @return Value of property name.
546          */

547         String JavaDoc getName() {
548             return name;
549         }
550         
551         /** Setter for property name.
552          * @param name New value of property name.
553          */

554         void setName(String JavaDoc name) {
555             this.name = name.replace(File.separatorChar,'/');
556         }
557         
558         /** Getter for property crc.
559          * @return Value of property crc.
560          */

561         String JavaDoc getCrc() {
562             return crc;
563         }
564         
565         /** Setter for property crc.
566          * @param crc New value of property crc.
567          */

568         void setCrc(String JavaDoc crc) {
569             this.crc = crc;
570         }
571         
572     }
573
574     class ErrorCatcher implements ErrorHandler JavaDoc {
575         public void error(SAXParseException JavaDoc e) {
576             // normally a validity error
577
pError = true;
578         }
579
580         public void warning(SAXParseException JavaDoc e) {
581             //parseFailed = true;
582
}
583
584         public void fatalError(SAXParseException JavaDoc e) {
585             pError = true;
586         }
587     }
588     
589 }
590
Popular Tags