001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.gsfret.source;
043:
044: import java.beans.PropertyChangeEvent;
045: import java.beans.PropertyChangeListener;
046: import java.beans.PropertyChangeSupport;
047: import java.io.File;
048: import java.io.IOException;
049: import java.net.URL;
050: import java.util.LinkedList;
051: import java.util.List;
052: import org.netbeans.modules.gsfpath.api.classpath.ClassPath;
053: import org.netbeans.modules.gsf.Language;
054: import org.netbeans.modules.gsfret.source.parsing.FileObjects;
055: import org.netbeans.modules.gsfret.source.usages.ClassIndexManager;
056: import org.netbeans.modules.gsfret.source.usages.Index;
057: import org.netbeans.modules.gsfpath.spi.classpath.ClassPathFactory;
058: import org.netbeans.modules.gsfpath.spi.classpath.ClassPathImplementation;
059: import org.netbeans.modules.gsfpath.spi.classpath.PathResourceImplementation;
060: import org.netbeans.modules.gsfpath.spi.classpath.support.ClassPathSupport;
061: import org.openide.ErrorManager;
062: import org.openide.filesystems.FileObject;
063: import org.openide.filesystems.FileUtil;
064: import org.openide.filesystems.URLMapper;
065: import org.openide.util.Exceptions;
066: import org.openide.util.WeakListeners;
067:
068: /**
069: * This file is originally from Retouche, the Java Support
070: * infrastructure in NetBeans. I have modified the file as little
071: * as possible to make merging Retouche fixes back as simple as
072: * possible.
073: *
074: *
075: * @author Tomas Zezula
076: */
077: public class CacheClassPath implements ClassPathImplementation,
078: PropertyChangeListener {
079:
080: private final ClassPath cp;
081: private final boolean translate;
082: private final boolean isBoot;
083: private PropertyChangeSupport listeners;
084: private List<PathResourceImplementation> cache;
085:
086: /** Creates a new instance of CacheClassPath */
087: private CacheClassPath(ClassPath cp, boolean translate,
088: boolean isBoot) {
089: this .listeners = new PropertyChangeSupport(this );
090: this .cp = cp;
091: this .translate = translate;
092: this .isBoot = isBoot;
093: this .cp.addPropertyChangeListener(WeakListeners.propertyChange(
094: this , cp));
095: }
096:
097: public void removePropertyChangeListener(
098: final PropertyChangeListener listener) {
099: this .listeners.removePropertyChangeListener(listener);
100: }
101:
102: public void addPropertyChangeListener(
103: final PropertyChangeListener listener) {
104: this .listeners.addPropertyChangeListener(listener);
105: }
106:
107: public void propertyChange(final PropertyChangeEvent event) {
108: if (ClassPath.PROP_ENTRIES.equals(event.getPropertyName())) {
109: synchronized (this ) {
110: this .cache = null;
111: }
112: this .listeners.firePropertyChange(PROP_RESOURCES, null,
113: null);
114: }
115: }
116:
117: public synchronized List<? extends PathResourceImplementation> getResources() {
118: if (this .cache == null) {
119: List<ClassPath.Entry> entries = this .cp.entries();
120: this .cache = new LinkedList<PathResourceImplementation>();
121: if (isBoot && entries.size() == 0) {
122: //JavaPlatform defaultPlatform = JavaPlatformManager.getDefault().getDefaultPlatform();
123: //assert defaultPlatform != null;
124: //entries = defaultPlatform.getBootstrapLibraries().entries();
125: //assert entries.size() > 0;
126: //for (ClassPath.Entry entry : entries) {
127: // this.cache.add (ClassPathSupport.createResource(entry.getURL()));
128: //}
129: for (URL url : ClassIndexManager.getAllBootRoots()) {
130: this .cache
131: .add(ClassPathSupport.createResource(url));
132: }
133: } else {
134: final GlobalSourcePath gsp = GlobalSourcePath
135: .getDefault();
136: for (ClassPath.Entry entry : entries) {
137: URL url = entry.getURL();
138: URL[] sourceUrls;
139: if (translate) {
140: sourceUrls = gsp.getSourceRootForBinaryRoot(
141: url, this .cp, true);
142: } else {
143: sourceUrls = new URL[] { url };
144: }
145: if (sourceUrls != null) {
146: // for (URL sourceUrl : sourceUrls) {
147: // try {
148: // File cacheFolder = Index.getClassFolder(language, sourceUrl);
149: // URL cacheUrl = cacheFolder.toURI().toURL();
150: // if (!cacheFolder.exists()) {
151: // cacheUrl = new URL (cacheUrl.toExternalForm()+"/"); //NOI18N
152: // }
153: // this.cache.add(ClassPathSupport.createResource(cacheUrl));
154: // } catch (IOException ioe) {
155: // ErrorManager.getDefault().notify(ioe);
156: // }
157: // }
158: } else {
159: if (FileObjects.JAR.equals(url.getProtocol())) {
160: URL foo = FileUtil.getArchiveFile(url);
161: if (!FileObjects.FILE.equals(foo
162: .getProtocol())) {
163: FileObject fo = URLMapper
164: .findFileObject(foo);
165: if (fo != null) {
166: foo = URLMapper.findURL(fo,
167: URLMapper.EXTERNAL);
168: if (FileObjects.FILE.equals(foo
169: .getProtocol())) {
170: url = FileUtil
171: .getArchiveRoot(foo);
172: }
173: }
174: }
175: } else if (!FileObjects.FILE.equals(url
176: .getProtocol())) {
177: FileObject fo = URLMapper
178: .findFileObject(url);
179: if (fo != null) {
180: URL foo = URLMapper.findURL(fo,
181: URLMapper.EXTERNAL);
182: if (FileObjects.FILE.equals(foo
183: .getProtocol())) {
184: url = foo;
185: }
186: }
187: }
188: // try {
189: // File sigs = Index.getClassFolder(language, url);
190: // this.cache.add (ClassPathSupport.createResource(sigs.toURI().toURL()));
191: // } catch (IOException ioe) {
192: // Exceptions.printStackTrace(ioe);
193: // }
194: this .cache.add(ClassPathSupport
195: .createResource(url));
196: }
197: }
198: }
199: }
200: return this .cache;
201: }
202:
203: public static ClassPath forClassPath(final ClassPath cp) {
204: assert cp != null;
205: return ClassPathFactory.createClassPath(new CacheClassPath(cp,
206: true, false));
207: }
208:
209: public static ClassPath forBootPath(final ClassPath cp) {
210: assert cp != null;
211: return ClassPathFactory.createClassPath(new CacheClassPath(cp,
212: true, true));
213: }
214:
215: public static ClassPath forSourcePath(final ClassPath sourcePath) {
216: assert sourcePath != null;
217: return ClassPathFactory.createClassPath(new CacheClassPath(
218: sourcePath, false, false));
219: }
220:
221: }
|