KickJava   Java API By Example, From Geeks To Geeks.

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


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.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Hashtable JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33 import org.apache.tools.ant.BuildException;
34 import org.w3c.dom.Document JavaDoc;
35 import org.w3c.dom.Element JavaDoc;
36 import org.xml.sax.InputSource JavaDoc;
37
38 /** This class represents module updates tracking
39  *
40  * @author akemr
41  */

42 class ModuleTracking {
43     private static final String JavaDoc ELEMENT_MODULE_TRACKING = "tracking"; // NOI18N
44
private static final String JavaDoc ELEMENT_MODULE = "module"; // NOI18N
45
private static final String JavaDoc ATTR_MODULE_NAME = "name"; // NOI18N
46
private static final String JavaDoc ELEMENT_FILE = "file"; // NOI18N
47
private static final String JavaDoc ATTR_FILE_NAME = "name"; // NOI18N
48
private static final String JavaDoc ATTR_CODE_NAME = "codename"; // NOI18N
49
private static final String JavaDoc ATTR_MODULE_PATH = "path"; // NOI18N
50
private static final String JavaDoc ATTR_NBM_HOME_PAGE = "nbmhomepage"; // NOI18N
51
private static final String JavaDoc ATTR_NBM_FILE_NAME = "nbmfilename"; // NOI18N
52
private static final String JavaDoc ATTR_NBM_NEEDS_RESTART = "nbmneedsrestart"; // NOI18N
53
private static final String JavaDoc ATTR_NBM_IS_GLOBAL = "nbmisglobal"; // NOI18N
54
private static final String JavaDoc ATTR_NBM_TARGET_CLUSTER = "nbmtargetcluster"; // NOI18N
55
private static final String JavaDoc ATTR_NBM_RELEASE_DATE = "nbmreleasedate"; // NOI18N
56
private static final String JavaDoc ATTR_NBM_MODULE_AUTHOR = "nbmmoduleauthor"; // NOI18N
57

58     /** Platform dependent file name separator */
59     private static final String JavaDoc FILE_SEPARATOR = System.getProperty ("file.separator"); // NOI18N
60

61     /** The name of the file */
62     public static final String JavaDoc TRACKING_FILE = "module_tracking.xml"; // NOI18N
63

64     private boolean pError = false;
65     
66     private File JavaDoc trackingFile = null;
67     
68     private String JavaDoc nbPath = null;
69     private Tracking tracking = null;
70    
71     // for generating xml in build process
72
public ModuleTracking(String JavaDoc nbPath) {
73         this.nbPath = nbPath;
74         File JavaDoc directory = new File JavaDoc( nbPath );
75         if (!directory.exists()) {
76             directory.mkdirs();
77         }
78         trackingFile = new File JavaDoc(directory, TRACKING_FILE);
79         read();
80     }
81     
82 /* public Module addNewModule( String name ) {
83          = new Tracking();
84         module.setName( name );
85         return module;
86     }*/

87     
88     public void putModule(String JavaDoc name, String JavaDoc codename, String JavaDoc path, String JavaDoc nbmfilename,
89             String JavaDoc nbmhomepage, String JavaDoc nbmneedsrestart, String JavaDoc nbmreleasedate, String JavaDoc nbmmoduleauthor, String JavaDoc nbmisglobal, String JavaDoc nbmtargetcluster, String JavaDoc[] files) {
90         if (name == null) {
91             // #59426: external module, skip.
92
return;
93         }
94         Module modByName = tracking.getModule(name);
95     Module modByCodeName = tracking.getModuleByCodeName(codename);
96     Module module = null;
97     if (modByName != null) {
98         module = modByName;
99     } else if (modByCodeName != null) {
100         module = modByCodeName;
101     }
102         if (module == null) {
103             module = new Module();
104             module.setName(name);
105         module.setCodeName(codename);
106             module.setPath(path);
107             tracking.addModule(module);
108         }
109         if (module.getNbmFileName().equals("")) // NOI18N
110
module.setNbmFileName(nbmfilename);
111         if (module.getNbmHomePage().equals("")) // NOI18N
112
module.setNbmHomePage(nbmhomepage);
113         if (module.getNbmModuleAuthor().equals("")) // NOI18N
114
module.setNbmModuleAuthor(nbmmoduleauthor);
115         if (module.getNbmNeedsRestart().equals("")) // NOI18N
116
module.setNbmNeedsRestart(nbmneedsrestart);
117         if (module.getNbmReleaseDate().equals("")) // NOI18N
118
module.setNbmReleaseDate(nbmreleasedate);
119         if (module.getNbmIsGlobal().equals ("")) { // NOI18N
120
module.setNbmIsGlobal(nbmisglobal);
121         }
122         if (module.getNbmTargetCluster().equals ("")) { // NOI18N
123
module.setNbmTargetCluster(nbmtargetcluster);
124         }
125 /*
126     System.out.println(" module == "+name);
127     System.out.println(" path == "+path);
128     System.out.println(" codename == "+ codename);
129     System.out.println(" filename == "+module.getNbmFileName());
130     System.out.println(" homepage == "+module.getNbmHomePage());
131     System.out.println(" author == "+module.getNbmModuleAuthor());
132     System.out.println(" needsrestart == "+module.getNbmNeedsRestart());
133     System.out.println(" global == "+module.getNbmIsGlobal ());
134     System.out.println(" targetcluster == "+module.getNbmTargetCluster ());
135     System.out.println(" releasedate == "+module.getNbmReleaseDate());
136 */

137         module.putFiles( files );
138     }
139     
140     public Map JavaDoc<String JavaDoc,Module> getModules() {
141         return tracking.getModules();
142     }
143     
144     public Hashtable JavaDoc getModulesByCodeName() {
145             return tracking.getModulesByCodeName();
146     }
147
148     public Iterator JavaDoc getFilesForModule(String JavaDoc name) {
149         Module module = tracking.getModule(name);
150         if (module == null) return null;
151         String JavaDoc[] files = new String JavaDoc[module.getFiles().size()];
152         return module.getFiles().iterator();
153     }
154     
155     public String JavaDoc getModulePath(String JavaDoc name) {
156         Module module = tracking.getModule(name);
157         if (module == null) return null;
158         return module.getPath();
159     }
160     
161     void write( ) {
162         Document JavaDoc document = XMLUtil.createDocument(ELEMENT_MODULE_TRACKING);
163         Element JavaDoc e_module_tracking = document.getDocumentElement();
164         Iterator JavaDoc it2 = tracking.getModules().values().iterator();
165         while ( it2.hasNext() ) {
166             Module mod = (Module)it2.next();
167             Element JavaDoc e_module = document.createElement(ELEMENT_MODULE);
168             assert mod.getName() != null : mod.getCodeName();
169             e_module.setAttribute(ATTR_MODULE_NAME, mod.getName());
170             e_module.setAttribute(ATTR_CODE_NAME, mod.getCodeName());
171             e_module.setAttribute(ATTR_MODULE_PATH, mod.getPath());
172             e_module.setAttribute(ATTR_NBM_FILE_NAME, mod.getNbmFileName());
173             e_module.setAttribute(ATTR_NBM_HOME_PAGE, mod.getNbmHomePage());
174             e_module.setAttribute(ATTR_NBM_MODULE_AUTHOR, mod.getNbmModuleAuthor());
175             e_module.setAttribute(ATTR_NBM_NEEDS_RESTART, mod.getNbmNeedsRestart());
176             e_module.setAttribute(ATTR_NBM_IS_GLOBAL, mod.getNbmIsGlobal ());
177             e_module.setAttribute(ATTR_NBM_TARGET_CLUSTER, mod.getNbmTargetCluster ());
178             e_module.setAttribute(ATTR_NBM_RELEASE_DATE, mod.getNbmReleaseDate());
179             e_module_tracking.appendChild( e_module );
180             Iterator JavaDoc it3 = mod.getFiles().iterator();
181             while ( it3.hasNext() ) {
182                 String JavaDoc file = (String JavaDoc)it3.next();
183                 Element JavaDoc e_file = document.createElement(ELEMENT_FILE);
184                 e_file.setAttribute(ATTR_FILE_NAME, file.replace(File.separatorChar,'/'));
185                 e_module.appendChild( e_file );
186             }
187         }
188         
189         //document.getDocumentElement().normalize();
190
try {
191         OutputStream JavaDoc os = new FileOutputStream JavaDoc(trackingFile);
192             XMLUtil.write(document, os);
193             os.close();
194         } catch (Exception JavaDoc e) {
195             e.printStackTrace();
196             trackingFile.delete();
197             throw new BuildException("Could not write update tracking file " + trackingFile.getAbsolutePath(), e);
198         }
199     }
200
201     /** Scan through org.w3c.dom.Document document. */
202     private void read() {
203         /** org.w3c.dom.Document document */
204         org.w3c.dom.Document JavaDoc document;
205         if (trackingFile.exists()) {
206             InputStream JavaDoc is;
207             try {
208                 is = new FileInputStream JavaDoc( trackingFile );
209                 
210             InputSource JavaDoc xmlInputSource = new InputSource JavaDoc( is );
211             document = XMLUtil.parse( xmlInputSource, false, false, new ErrorCatcher(), null );
212             if (is != null)
213                 is.close();
214             }
215             catch ( org.xml.sax.SAXException JavaDoc e ) {
216                 System.out.println("Module tracking file " + trackingFile.getAbsolutePath() + " is not well formatted XML document" );
217                 e.printStackTrace();
218                 return;
219             }
220             catch ( java.io.IOException JavaDoc e ) {
221                 System.out.println("I/O error when accessing module tracking file " + trackingFile.getAbsolutePath() );
222                 e.printStackTrace();
223                 return;
224             }
225             
226             org.w3c.dom.Element JavaDoc element = document.getDocumentElement();
227             if ((element != null) && element.getTagName().equals(ELEMENT_MODULE_TRACKING)) {
228                 scanElement_module_tracking(element);
229             }
230         }
231         if (tracking == null)
232             tracking = new Tracking();
233     }
234     
235     /** Scan through org.w3c.dom.Element named module. */
236     void scanElement_module_tracking(org.w3c.dom.Element JavaDoc element) { // <tracking>
237
tracking = new Tracking();
238         org.w3c.dom.NodeList JavaDoc nodes = element.getChildNodes();
239         for (int i = 0; i < nodes.getLength(); i++) {
240             org.w3c.dom.Node JavaDoc node = nodes.item(i);
241             if ( node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE ) {
242                 org.w3c.dom.Element JavaDoc nodeElement = (org.w3c.dom.Element JavaDoc)node;
243                 if (nodeElement.getTagName().equals(ELEMENT_MODULE)) {
244                     scanElement_module(nodeElement, tracking);
245                 }
246             }
247         }
248     }
249     
250     /** Scan through org.w3c.dom.Element named module. */
251     void scanElement_module(org.w3c.dom.Element JavaDoc element, Tracking tracking) { // <module>
252
Module module = new Module();
253         org.w3c.dom.NamedNodeMap JavaDoc attrs = element.getAttributes();
254         for (int i = 0; i < attrs.getLength(); i++) {
255             org.w3c.dom.Attr JavaDoc attr = (org.w3c.dom.Attr JavaDoc)attrs.item(i);
256             if (attr.getName().equals(ATTR_MODULE_NAME)) { // <module name="???">
257
module.setName( attr.getValue() );
258             }
259             if (attr.getName().equals(ATTR_CODE_NAME)) { // <module codename="???">
260
module.setCodeName( attr.getValue() );
261             }
262             if (attr.getName().equals(ATTR_MODULE_PATH)) { // <module path="???">
263
module.setPath( attr.getValue() );
264             }
265             if (attr.getName().equals(ATTR_NBM_HOME_PAGE)) { // <module nbmhomepage="???">
266
module.setNbmHomePage( attr.getValue() );
267             }
268             if (attr.getName().equals(ATTR_NBM_FILE_NAME)) { // <module nbmfilename="???">
269
module.setNbmFileName( attr.getValue() );
270             }
271             if (attr.getName().equals(ATTR_NBM_NEEDS_RESTART)) { // <module nbmneedsrestart="???">
272
module.setNbmNeedsRestart( attr.getValue() );
273             }
274             if (attr.getName().equals(ATTR_NBM_IS_GLOBAL)) { // <module nbmisglobal="???">
275
module.setNbmIsGlobal( attr.getValue() );
276             }
277             if (attr.getName().equals(ATTR_NBM_TARGET_CLUSTER)) { // <module nbmtargetcluster="???">
278
module.setNbmTargetCluster ( attr.getValue() );
279             }
280             if (attr.getName().equals(ATTR_NBM_RELEASE_DATE)) { // <module nbmreleasedate="???">
281
module.setNbmReleaseDate( attr.getValue() );
282             }
283             if (attr.getName().equals(ATTR_NBM_MODULE_AUTHOR)) { // <module nbmmoduleauthor="???">
284
module.setNbmModuleAuthor( attr.getValue() );
285             }
286        }
287         org.w3c.dom.NodeList JavaDoc nodes = element.getChildNodes();
288         for (int i = 0; i < nodes.getLength(); i++) {
289             org.w3c.dom.Node JavaDoc node = nodes.item(i);
290             if ( node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE ) {
291                 org.w3c.dom.Element JavaDoc nodeElement = (org.w3c.dom.Element JavaDoc)node;
292                 if (nodeElement.getTagName().equals(ELEMENT_FILE)) {
293                     scanElement_file(nodeElement, module);
294                 }
295             }
296         }
297         tracking.addModule(module);
298     }
299     
300     /** Scan through org.w3c.dom.Element named file. */
301     void scanElement_file(org.w3c.dom.Element JavaDoc element, Module module) { // <file>
302
String JavaDoc file = null;
303         org.w3c.dom.NamedNodeMap JavaDoc attrs = element.getAttributes();
304         for (int i = 0; i < attrs.getLength(); i++) {
305             org.w3c.dom.Attr JavaDoc attr = (org.w3c.dom.Attr JavaDoc)attrs.item(i);
306             if (attr.getName().equals(ATTR_FILE_NAME)) { // <file name="???">
307
file = attr.getValue();
308             }
309         }
310         module.addFile (file.replace(File.separatorChar,'/'));
311     }
312
313     public class Tracking extends Object JavaDoc {
314         private Map JavaDoc<String JavaDoc,Module> modules = new HashMap JavaDoc<String JavaDoc,Module>();
315     private Hashtable JavaDoc<String JavaDoc,Module> modulesByCodeName = new Hashtable JavaDoc<String JavaDoc,Module>();
316
317         Map JavaDoc<String JavaDoc,Module> getModules() {
318             return modules;
319         }
320         
321          /** Getter for property files.
322          * @return Value of property files.
323          */

324         Hashtable JavaDoc<String JavaDoc,Module> getModulesByCodeName() {
325             return modulesByCodeName;
326         }
327         
328         /** Setter for property files.
329          * @param files New value of property files.
330          */

331         void setModulesByCodeName(Hashtable JavaDoc<String JavaDoc,Module> modules) {
332             this.modulesByCodeName = modules;
333         }
334         
335         public void addModule( Module module ) {
336             modules.put( module.getName(), module );
337         modulesByCodeName.put( module.getCodeName(), module );
338         }
339         
340         public Module getModule( String JavaDoc name ) {
341             return modules.get(name);
342         }
343         public Module getModuleByCodeName( String JavaDoc codename ) {
344             return modulesByCodeName.get(codename);
345         }
346     }
347
348     public class Module extends Object JavaDoc {
349         /** Holds value of property name. */
350         private String JavaDoc name;
351         
352         /** Holds value of property codename. */
353         private String JavaDoc codename;
354         
355         /** Holds value of property path. */
356         private String JavaDoc path;
357         
358         /** Holds value of property nbmhomepage. */
359         private String JavaDoc nbmhomepage = ""; // NOI18N
360

361         /** Holds value of property nbmfilename. */
362         private String JavaDoc nbmfilename = ""; // NOI18N
363

364         /** Holds value of property nbmneedsrestart. */
365         private String JavaDoc nbmneedsrestart = ""; // NOI18N
366

367         /** Holds value of property nbmisglobal. */
368         private String JavaDoc nbmisglobal = ""; // NOI18N
369

370         /** Holds value of property nbmtargetcluster. */
371         private String JavaDoc nbmtargetcluster = ""; // NOI18N
372

373         /** Holds value of property nbmreleasedate. */
374         private String JavaDoc nbmreleasedate = ""; // NOI18N
375

376         /** Holds value of property nbmmoduleauthor. */
377         private String JavaDoc nbmmoduleauthor = ""; // NOI18N
378

379         /** Holds value of property files. */
380         private List JavaDoc<String JavaDoc> files = new ArrayList JavaDoc<String JavaDoc>();
381
382         /** Getter for property name */
383         String JavaDoc getName() {
384             return name;
385         }
386         
387         /** Setter for property name */
388         void setName(String JavaDoc name) {
389             this.name = name;
390         }
391         
392         /** Getter for property codename */
393         String JavaDoc getCodeName() {
394             return codename;
395         }
396         
397         /** Setter for property codename */
398         void setCodeName(String JavaDoc codename) {
399             this.codename = codename;
400         }
401         
402         /** Getter for property path */
403         String JavaDoc getPath() {
404             return path;
405         }
406         
407         /** Setter for property path */
408         void setPath(String JavaDoc path) {
409             this.path = path;
410         }
411         
412         /** Getter for property nbmhomepage */
413         String JavaDoc getNbmHomePage () {
414             if (this.nbmhomepage == null) {
415                 return ""; // NOI18N
416
} else {
417                 return this.nbmhomepage;
418             }
419         }
420         
421         /** Setter for property nbmhomepage */
422         void setNbmHomePage (String JavaDoc nbmh) {
423             if (!(nbmh == null))
424                 this.nbmhomepage = nbmh;
425         }
426         
427         /** Getter for property nbmfilename */
428         String JavaDoc getNbmFileName () {
429             if (this.nbmfilename == null) {
430                 return ""; // NOI18N
431
} else {
432                 return this.nbmfilename;
433             }
434         }
435         
436         /** Setter for property nbmfilename */
437         void setNbmFileName (String JavaDoc nbmfn) {
438             if (!(nbmfn == null))
439                 this.nbmfilename = nbmfn;
440         }
441         
442         /** Getter for property nbmneedsrestart */
443         String JavaDoc getNbmNeedsRestart () {
444             if (this.nbmneedsrestart == null) {
445                 return ""; // NOI18N
446
} else {
447                 return this.nbmneedsrestart;
448             }
449         }
450         
451         /** Setter for property nbmneedsrestart */
452         void setNbmNeedsRestart (String JavaDoc nbmnr) {
453             if (!(nbmnr == null))
454                 this.nbmneedsrestart = nbmnr;
455         }
456         
457         /** Setter for property nbmisglobal */
458         void setNbmIsGlobal (String JavaDoc isGlobal) {
459             if (!(isGlobal == null))
460                 this.nbmisglobal = isGlobal;
461         }
462         
463         /** Getter for property nbmisglobal */
464         String JavaDoc getNbmIsGlobal () {
465             return this.nbmisglobal;
466         }
467         
468         void setNbmTargetCluster (String JavaDoc targetcluster) {
469             if (! (targetcluster == null)) {
470                 this.nbmtargetcluster = targetcluster;
471             }
472         }
473
474         /** Getter for property nbmtargetcluster */
475         String JavaDoc getNbmTargetCluster () {
476             return this.nbmtargetcluster;
477         }
478         
479         /** Getter for property nbmreleasedate */
480         String JavaDoc getNbmReleaseDate () {
481             if (this.nbmreleasedate == null) {
482                 return ""; // NOI18N
483
} else {
484                 return this.nbmreleasedate;
485             }
486         }
487         
488         /** Setter for property nbmreleasedate */
489         void setNbmReleaseDate (String JavaDoc nbmrd) {
490             if (!(nbmrd == null ))
491                 this.nbmreleasedate = nbmrd;
492         }
493         
494         /** Getter for property nbmmoduleauthor */
495         String JavaDoc getNbmModuleAuthor () {
496             if (this.nbmmoduleauthor == null) {
497                 return ""; // NOI18N
498
} else {
499                 return this.nbmmoduleauthor;
500             }
501         }
502         
503         /** Setter for property nbmmoduleauthor */
504         void setNbmModuleAuthor (String JavaDoc nbmma) {
505             if (!(nbmma == null))
506                 this.nbmmoduleauthor = nbmma;
507         }
508          /** Getter for property files.
509          * @return Value of property files.
510          */

511         List JavaDoc getFiles() {
512             return files;
513         }
514         
515         /** Setter for property files.
516          * @param files New value of property files.
517          */

518         void setFiles(List JavaDoc<String JavaDoc> files) {
519             this.files = files;
520         }
521         
522         public void addFile( String JavaDoc filename) {
523             files.add( filename.replace(File.separatorChar, '/') );
524         }
525         
526         public void putFiles( String JavaDoc[] list ) {
527             for (int i=0; i < list.length; i++) {
528                 if (!files.contains(list[i].replace(File.separatorChar,'/'))) {
529                     files.add(list[i].replace(File.separatorChar,'/'));
530                 }
531             }
532         }
533
534     }
535     
536     class ErrorCatcher implements org.xml.sax.ErrorHandler JavaDoc {
537         private void message (String JavaDoc level, org.xml.sax.SAXParseException JavaDoc e) {
538             pError = true;
539         }
540
541         public void error (org.xml.sax.SAXParseException JavaDoc e) {
542             // normally a validity error
543
pError = true;
544         }
545
546         public void warning (org.xml.sax.SAXParseException JavaDoc e) {
547             //parseFailed = true;
548
}
549
550         public void fatalError (org.xml.sax.SAXParseException JavaDoc e) {
551             pError = true;
552         }
553     }
554     
555 }
556
Popular Tags