KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > source > classpath > 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.java.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.java.platform.JavaPlatform;
33 import org.netbeans.api.java.platform.JavaPlatformManager;
34 import org.netbeans.modules.java.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  *
44  * @author Tomas Zezula
45  */

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