KickJava   Java API By Example, From Geeks To Geeks.

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


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
23 import java.io.File JavaDoc;
24 import java.io.FilenameFilter JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30
31 import java.util.jar.JarFile JavaDoc;
32 import java.util.jar.Manifest JavaDoc;
33 import org.openide.modules.ModuleInfo;
34
35 /** This class finds all patches in the system
36  *
37  * @author Petr Hrebejk
38  */

39 class PatchChecker extends Object JavaDoc {
40
41     private static final String JavaDoc JAR_EXTENSION = ".JAR"; // NOI18N
42
private static final String JavaDoc ZIP_EXTENSION = ".ZIP"; // NOI18N
43

44     private static ModuleInfo[] patchArray = null;
45
46     /** The class is singleton */
47     private PatchChecker() {
48     }
49
50     static ModuleInfo[] getPatches() {
51
52         if ( patchArray == null ) {
53
54             List JavaDoc/*<File>*/ patchDirectories = Autoupdater.Support.getPatchDirectories ();
55
56             Collection JavaDoc patches = new ArrayList JavaDoc();
57
58             if (!patchDirectories.isEmpty ()) {
59                 Iterator JavaDoc it = patchDirectories.iterator ();
60                 while (it.hasNext ()) {
61                     addPatches ((File JavaDoc)it.next (), patches);
62                 }
63             }
64             
65             patchArray = new ModuleInfo[ patches.size() ];
66             patches.toArray( patchArray );
67         }
68
69         return patchArray;
70     }
71
72     private static void addPatches( File JavaDoc directory, Collection JavaDoc result ) {
73
74         File JavaDoc dirList[] = directory.listFiles( new FilenameFilter JavaDoc() {
75                                                   public boolean accept( File JavaDoc dir, String JavaDoc name ) {
76                                                       return name.toUpperCase().endsWith( JAR_EXTENSION ) ||
77                                                              name.toUpperCase().endsWith( ZIP_EXTENSION );
78                                                   }
79                                               });
80     // Patch dir might not even exist:
81
if (dirList == null) return;
82
83         for ( int i = 0; i < dirList.length; i++ ) {
84
85             JarFile JavaDoc jarFile = null;
86             try {
87                 jarFile = new JarFile JavaDoc( dirList[i] );
88                 Manifest JavaDoc manifest = jarFile.getManifest();
89
90
91                 if ( manifest == null )
92                     continue; // This is not a standard NetBeans patch
93

94                 ModuleInfo md = new DummyModuleInfo(manifest.getMainAttributes());
95
96                 Iterator JavaDoc it = result.iterator();
97                 boolean found = false;
98                 while( it.hasNext() ) {
99                     ModuleInfo td = (ModuleInfo)it.next();
100
101                     if ( md.getCodeNameBase().equals( td.getCodeNameBase() ) ) {
102                         found = true;
103                         break;
104                     }
105
106                 }
107
108                 if ( !found )
109                     result.add( md );
110             }
111             catch ( IOException JavaDoc e ) {
112             }
113             catch ( IllegalArgumentException JavaDoc e ) {
114             }
115             finally {
116                 try {
117                     if ( jarFile != null )
118                         jarFile.close();
119                 } catch ( IOException JavaDoc ie ) {
120                 }
121             }
122
123         }
124     }
125 }
126
Popular Tags