KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > retouche > source > CacheClassPath


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.retouche.source/*.classpath*/;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.beans.PropertyChangeSupport JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.net.URI JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.util.LinkedList JavaDoc;
30 import java.util.List JavaDoc;
31 import org.netbeans.api.java.classpath.ClassPath;
32 import org.netbeans.api.retouche.source.ClassIndex;
33 import org.netbeans.modules.retouche.source.usages.ClassIndexManager;
34 import org.netbeans.modules.retouche.source.usages.Index;
35 import org.netbeans.spi.java.classpath.ClassPathFactory;
36 import org.netbeans.spi.java.classpath.ClassPathImplementation;
37 import org.netbeans.spi.java.classpath.PathResourceImplementation;
38 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
39 import org.openide.ErrorManager;
40 import org.openide.util.WeakListeners;
41
42 /**
43  * This file is originally from Retouche, the Java Support
44  * infrastructure in NetBeans. I have modified the file as little
45  * as possible to make merging Retouche fixes back as simple as
46  * possible.
47  *
48  *
49  * @author Tomas Zezula
50  */

51 public class CacheClassPath implements ClassPathImplementation, PropertyChangeListener JavaDoc {
52     
53     private final ClassPath cp;
54     private final boolean translate;
55     private final boolean isBoot;
56     private PropertyChangeSupport JavaDoc listeners;
57     private List JavaDoc<PathResourceImplementation> cache;
58     
59     /** Creates a new instance of CacheClassPath */
60     private CacheClassPath (ClassPath cp, boolean translate, boolean isBoot) {
61         this.listeners = new PropertyChangeSupport JavaDoc (this);
62         this.cp = cp;
63         this.translate = translate;
64         this.isBoot = isBoot;
65         this.cp.addPropertyChangeListener (WeakListeners.propertyChange(this,cp));
66     }
67
68     public void removePropertyChangeListener(final PropertyChangeListener JavaDoc listener) {
69         this.listeners.removePropertyChangeListener(listener);
70     }
71
72     public void addPropertyChangeListener(final PropertyChangeListener JavaDoc listener) {
73         this.listeners.addPropertyChangeListener(listener);
74     }
75     
76     public void propertyChange (final PropertyChangeEvent JavaDoc event) {
77         if (ClassPath.PROP_ENTRIES.equals(event.getPropertyName())) {
78             synchronized (this) {
79                 this.cache = null;
80             }
81             this.listeners.firePropertyChange(PROP_RESOURCES,null,null);
82         }
83     }
84
85     public synchronized List JavaDoc<? extends PathResourceImplementation> getResources() {
86         if (this.cache == null) {
87             List JavaDoc<ClassPath.Entry> entries = this.cp.entries();
88             this.cache = new LinkedList JavaDoc<PathResourceImplementation> ();
89             if (isBoot && entries.size() == 0) {
90                 //JavaPlatform defaultPlatform = JavaPlatformManager.getDefault().getDefaultPlatform();
91
//assert defaultPlatform != null;
92
//entries = defaultPlatform.getBootstrapLibraries().entries();
93
//assert entries.size() > 0;
94
//for (ClassPath.Entry entry : entries) {
95
// this.cache.add (ClassPathSupport.createResource(entry.getURL()));
96
//}
97
for (URL JavaDoc url : ClassIndexManager.getDefault().getBootRoots()) {
98                     this.cache.add (ClassPathSupport.createResource(url));
99                 }
100             }
101             else {
102                 final GlobalSourcePath gsp = GlobalSourcePath.getDefault();
103                 for (ClassPath.Entry entry : entries) {
104                     URL JavaDoc url = entry.getURL();
105                     URL JavaDoc[] sourceUrls;
106                     if (translate) {
107                         sourceUrls = gsp.getSourceRootForBinaryRoot(url, this.cp, true);
108                     }
109                     else {
110                         sourceUrls = new URL JavaDoc[] {url};
111                     }
112                     if (sourceUrls != null) {
113                         for (URL JavaDoc sourceUrl : sourceUrls) {
114                             try {
115                                 File JavaDoc cacheFolder = Index.getClassFolder(sourceUrl);
116                                 URL JavaDoc cacheUrl = cacheFolder.toURI().toURL();
117                                 if (!cacheFolder.exists()) {
118                                     cacheUrl = new URL JavaDoc (cacheUrl.toExternalForm()+"/"); //NOI18N
119
}
120                                 this.cache.add(ClassPathSupport.createResource(cacheUrl));
121                             } catch (IOException JavaDoc ioe) {
122                                 ErrorManager.getDefault().notify(ioe);
123                             }
124                         }
125                     } else {
126                         this.cache.add (ClassPathSupport.createResource(url));
127                     }
128                 }
129             }
130         }
131         return this.cache;
132     }
133     
134     
135     public static ClassPath forClassPath (final ClassPath cp) {
136         assert cp != null;
137         return ClassPathFactory.createClassPath(new CacheClassPath(cp,true,false));
138     }
139     
140     public static ClassPath forBootPath (final ClassPath cp) {
141         assert cp != null;
142         return ClassPathFactory.createClassPath(new CacheClassPath(cp,true,true));
143     }
144     
145     public static ClassPath forSourcePath (final ClassPath sourcePath) {
146         assert sourcePath != null;
147         return ClassPathFactory.createClassPath(new CacheClassPath(sourcePath,false,false));
148     }
149     
150 }
151
Popular Tags