KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > source > parsing > CachingArchiveProvider


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.java.source.parsing;
21
22
23
24 import java.io.File JavaDoc;
25 import java.net.URI JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.List JavaDoc;
30 import org.netbeans.api.java.classpath.ClassPath;
31 import org.openide.filesystems.FileObject;
32 import org.openide.filesystems.FileUtil;
33 import org.openide.filesystems.URLMapper;
34
35
36 /** Global cache for Archives (zip files and folders).
37  *
38  * XXX-Use j.net.URL rather than j.io.File
39  * XXX-Perf Add swapping for lower memory usage
40  *
41  * @author Petr Hrebejk
42  */

43 public class CachingArchiveProvider {
44
45     private static CachingArchiveProvider instance;
46
47     // Names to caching zip files
48
// XXX-PERF Consider swapping
49
HashMap JavaDoc<URL JavaDoc,Archive> archives;
50
51     public static synchronized CachingArchiveProvider getDefault () {
52         if (instance == null) {
53             instance = new CachingArchiveProvider ();
54         }
55         return instance;
56     }
57         
58     /** Creates a new instance CachingArchiveProvider
59      * Can be caleed only from UnitTests or {@link CachingArchiveProvider#getDefault} !!!!!
60      */

61     CachingArchiveProvider() {
62         archives = new HashMap JavaDoc<URL JavaDoc,Archive>();
63     }
64     
65     /** Gets archive for given file.
66      */

67     public synchronized Archive getArchive( URL JavaDoc root, boolean cacheFile) {
68                 
69         Archive archive = archives.get(root);
70
71         if (archive == null) {
72             archive = create(root, cacheFile);
73             if (archive != null) {
74                 archives.put(root, archive );
75             }
76         }
77         return archive;
78         
79     }
80     
81     /** Gets archives for files
82      */

83     public synchronized Iterable JavaDoc<Archive> getArchives( URL JavaDoc[] roots, boolean cacheFile) {
84         
85         List JavaDoc<Archive> archives = new ArrayList JavaDoc<Archive>(roots.length);
86         for( int i = 0; i < roots.length; i++ ) {
87             Archive a = getArchive( roots[i], cacheFile );
88             if (a != null) {
89                 archives.add(a);
90             }
91         }
92         return archives;
93     }
94     
95     
96     /** Gets archives for files
97      */

98     public synchronized Iterable JavaDoc<Archive> getArchives( ClassPath cp, boolean cacheFile) {
99         final List JavaDoc<ClassPath.Entry> entries = cp.entries();
100         final List JavaDoc<Archive> archives = new ArrayList JavaDoc<Archive> (entries.size());
101         for (ClassPath.Entry entry : entries) {
102             Archive a = getArchive(entry.getURL(), cacheFile);
103             if (a != null) {
104                 archives.add (a);
105             }
106         }
107         return archives;
108     }
109     
110     public synchronized void removeArchive (final URL JavaDoc root) {
111         final Archive archive = archives.remove(root);
112         if (archive != null) {
113             archive.clear();
114         }
115     }
116     
117     public synchronized void clearArchive (final URL JavaDoc root) {
118         Archive archive = archives.get(root);
119         if (archive != null) {
120             archive.clear();
121         }
122     }
123         
124     // Private methods ---------------------------------------------------------
125

126     /** Creates proper archive for given file.
127      */

128     private static Archive create( URL JavaDoc root, boolean cacheFile ) {
129         String JavaDoc protocol = root.getProtocol();
130         if ("file".equals(protocol)) {
131             File JavaDoc f = new File JavaDoc (URI.create(root.toExternalForm()));
132             if (f.isDirectory()) {
133                 return new FolderArchive (f);
134             }
135             else {
136                 return null;
137             }
138         }
139         if ("jar".equals(protocol)) {
140             URL JavaDoc inner = FileUtil.getArchiveFile(root);
141             protocol = inner.getProtocol();
142             if ("file".equals(protocol)) {
143                 File JavaDoc f = new File JavaDoc (URI.create(inner.toExternalForm()));
144                 if (f.isFile()) {
145                     return new CachingArchive( f, cacheFile );
146                 }
147                 else {
148                     return null;
149                 }
150             }
151         }
152         //Slow
153
FileObject fo = URLMapper.findFileObject(root);
154         if (fo != null) {
155             return new FileObjectArchive (fo);
156         }
157         else {
158             return null;
159         }
160     }
161             
162           
163 }
164
Popular Tags