KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > autoupdate > PreparedModules


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.modules.autoupdate;
21
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.util.*;
28 import org.netbeans.updater.UpdateTracking;
29
30 import org.w3c.dom.*;
31 import org.xml.sax.InputSource JavaDoc;
32
33 import org.openide.xml.XMLUtil;
34 import org.openide.modules.SpecificationVersion;
35 import org.openide.util.Exceptions;
36
37 /** This class represents one module update available on the web
38  *
39  * @author akemr
40  * @version
41  */

42 class PreparedModules extends Object JavaDoc {
43
44     private static final String JavaDoc ELEMENT_MODULES = "module_updates"; // NOI18N
45
private static final String JavaDoc ELEMENT_MODULE = "module"; // NOI18N
46
private static final String JavaDoc ATTR_CODENAMEBASE = "codenamebase"; // NOI18N
47
private static final String JavaDoc ATTR_NAME = "name"; // NOI18N
48
private static final String JavaDoc ATTR_SPEC_VERSION = "specification_version"; // NOI18N
49
private static final String JavaDoc ATTR_SIZE = "size"; // NOI18N
50
private static final String JavaDoc ATTR_NBM_NAME = "nbm_name"; // NOI18N
51

52     /** Holds value of property modules.
53      * Each element of this List is instance of Module class.
54      */

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     /** Getter for property modules.
74      * @return Value of property modules.
75      */

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         // install to install or to user idr?
90
if (mu.isToInstallDir ()) {
91             // install dir
92
mod.setCluster (mu.findInstallDirectory ());
93         } else {
94             // user dir
95
mod.setCluster (new File JavaDoc (System.getProperty ("netbeans.user"))); // NOI18N
96
}
97         
98         modules.add( mod );
99     }
100     
101     void removeModule(String JavaDoc 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 JavaDoc getPreparedVersion(String JavaDoc 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 JavaDoc 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/*<File>*/ clusters = UpdateTracking.clusters (true);
133         assert clusters != null;
134         Iterator it = clusters.iterator ();
135         while (it.hasNext ()) {
136             getInstall_Later ((File JavaDoc)it.next ()).delete ();
137         }
138     }
139     
140     private File JavaDoc getInstall_Later (File JavaDoc rootOfCluster) {
141         return Autoupdater.Support.getInstall_Later (rootOfCluster);
142     }
143     
144     void write () {
145         // loop for all clusters and write if needed
146
List/*<File>*/ clusters = UpdateTracking.clusters (true);
147         assert clusters != null : "Clusters cannot be empty."; // NOI18N
148
Iterator iter = clusters.iterator ();
149         while (iter.hasNext ()) {
150             writeToCluster ((File JavaDoc)iter.next ());
151         }
152 }
153     
154     private void writeToCluster (File JavaDoc 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             // pass this module to given cluster ?
165
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 JavaDoc installLaterFile = Autoupdater.Support.getInstall_Later (cluster);
184         installLaterFile.getParentFile ().mkdirs ();
185         
186         OutputStream JavaDoc os = null;
187         try {
188             os = new FileOutputStream JavaDoc (installLaterFile);
189             XMLUtil.write(document, os, "UTF-8"); // NOI18N
190
} catch (java.io.FileNotFoundException JavaDoc fnfe) {
191             Exceptions.printStackTrace(fnfe);
192         } catch (java.io.IOException JavaDoc ioe) {
193             Exceptions.printStackTrace(ioe);
194         } finally {
195             if (os != null) {
196                 try {
197                     os.close ();
198                 } catch (Exception JavaDoc x) {
199                     Exceptions.printStackTrace(x);
200                 }
201             }
202         }
203
204     }
205     
206     /** Scan through org.w3c.dom.Document document. */
207     private void read() {
208         modules.clear();
209         /** org.w3c.dom.Document document */
210         org.w3c.dom.Document JavaDoc document = null;
211
212         InputStream JavaDoc is;
213         try {
214
215             List/*<File>*/ clusters = UpdateTracking.clusters (true);
216             assert clusters != null : "Clusters cannot be empty."; // NOI18N
217
Iterator iter = clusters.iterator ();
218             while (iter.hasNext ()) {
219                 File JavaDoc cluster = (File JavaDoc)iter.next ();
220                 File JavaDoc installLaterFile = Autoupdater.Support.getInstall_Later (cluster);
221                 if (installLaterFile.exists ()) {
222                     is = new FileInputStream JavaDoc( installLaterFile );
223
224                     InputSource JavaDoc xmlInputSource = new InputSource JavaDoc( 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 JavaDoc 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 JavaDoc e ) {
239             System.out.println("Bad install_later" ); // NOI18N
240
e.printStackTrace ();
241             return;
242         }
243         catch ( java.io.IOException JavaDoc e ) {
244             System.out.println("Missing install_later" ); // NOI18N
245
e.printStackTrace ();
246             return;
247         }
248     }
249
250     /** Scan through org.w3c.dom.Element named module_updates. */
251     private void scanElement_module_updates (org.w3c.dom.Element JavaDoc element, File JavaDoc cluster) { // <module_updates>
252
org.w3c.dom.NodeList JavaDoc nodes = element.getChildNodes();
253         for (int i = 0; i < nodes.getLength(); i++) {
254             org.w3c.dom.Node JavaDoc node = nodes.item(i);
255             if (node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
256                 org.w3c.dom.Element JavaDoc nodeElement = (org.w3c.dom.Element JavaDoc)node;
257                 if (nodeElement.getTagName().equals(ELEMENT_MODULE)) {
258                     scanElement_module (nodeElement, cluster);
259                 }
260             }
261         }
262     }
263
264     /** Scan through org.w3c.dom.Element named module. */
265     private void scanElement_module (org.w3c.dom.Element JavaDoc element, File JavaDoc cluster) { // <module>
266
Module module = new Module();
267         org.w3c.dom.NamedNodeMap JavaDoc attrs = element.getAttributes();
268         for (int i = 0; i < attrs.getLength(); i++) {
269             org.w3c.dom.Attr JavaDoc attr = (org.w3c.dom.Attr JavaDoc)attrs.item(i);
270             if (attr.getName().equals(ATTR_CODENAMEBASE)) { // <module codenamebase="???">
271
module.setCodenamebase( attr.getValue() );
272             }
273             if (attr.getName().equals(ATTR_SPEC_VERSION)) { // <module specification_version="???">
274
module.setSpec_version( attr.getValue() );
275             }
276             if (attr.getName().equals(ATTR_NAME)) { // <module name="???">
277
module.setName( attr.getValue() );
278             }
279             if (attr.getName().equals(ATTR_SIZE)) { // <module size="???">
280
try {
281                     module.setSize( Long.parseLong( attr.getValue()));
282                 } catch (NumberFormatException JavaDoc nfe) {
283                     module.setSize( 0 );
284                 }
285             }
286             if (attr.getName().equals(ATTR_NBM_NAME)) { // <module nbm_name="???">
287
module.setNbm_name( attr.getValue() );
288             }
289             module.setCluster (cluster);
290         }
291         modules.add( module );
292     }
293     
294     class Module extends Object JavaDoc {
295         /** Holds value of property name. */
296         private String JavaDoc name;
297
298         /** Holds value of property codenamebase. */
299         private String JavaDoc codenamebase;
300
301         /** Holds value of property spec_version. */
302         private String JavaDoc spec_version;
303
304         /** Holds value of property size. */
305         private long size;
306
307         /** Holds value of property nbm_name. */
308         private String JavaDoc nbm_name;
309         
310         private File JavaDoc cluster;
311
312         /** Getter for property name.
313          * @return Value of property name.
314          */

315         public String JavaDoc getName() {
316             return name;
317         }
318
319         /** Setter for property name.
320          * @param name New value of property name.
321          */

322         public void setName(String JavaDoc name) {
323             this.name = name;
324         }
325
326         /** Getter for property codenamebase.
327          * @return Value of property codenamebase.
328          */

329         public String JavaDoc getCodenamebase() {
330             return codenamebase;
331         }
332
333         /** Setter for property codenamebase.
334          * @param codenamebase New value of property codenamebase.
335          */

336         public void setCodenamebase(String JavaDoc codenamebase) {
337             this.codenamebase = codenamebase;
338         }
339
340         /** Getter for property spec_version.
341          * @return Value of property spec_version.
342          */

343         public String JavaDoc getSpec_version() {
344             return spec_version;
345         }
346
347         /** Setter for property spec_version.
348          * @param spec_version New value of property spec_version.
349          */

350         public void setSpec_version(String JavaDoc spec_version) {
351             this.spec_version = spec_version;
352         }
353
354         /** Getter for property size.
355          * @return Value of property size.
356          */

357         public long getSize() {
358             return size;
359         }
360
361         /** Setter for property size.
362          * @param size New value of property size.
363          */

364         public void setSize(long size) {
365             this.size = size;
366         }
367
368         /** Getter for property nbm_name.
369          * @return Value of property nbm_name.
370          */

371         public String JavaDoc getNbm_name() {
372             return nbm_name;
373         }
374
375         /** Setter for property nbm_name.
376          * @param nbm_name New value of property nbm_name.
377          */

378         public void setNbm_name(String JavaDoc nbm_name) {
379             this.nbm_name = nbm_name;
380         }
381         
382         public void setCluster (File JavaDoc cluster) {
383             this.cluster = cluster;
384         }
385         
386         public File JavaDoc getCluster () {
387             return cluster;
388         }
389         
390     }
391     
392     class ErrorCatcher implements org.xml.sax.ErrorHandler JavaDoc {
393         private void message (String JavaDoc level, org.xml.sax.SAXParseException JavaDoc e) {
394             pError = true;
395         }
396
397         public void error (org.xml.sax.SAXParseException JavaDoc e) {
398             // normally a validity error
399
pError = true;
400         }
401
402         public void warning (org.xml.sax.SAXParseException JavaDoc e) {
403             //parseFailed = true;
404
}
405
406         public void fatalError (org.xml.sax.SAXParseException JavaDoc e) {
407             pError = true;
408         }
409     }
410 }
411
Popular Tags