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.java.j2seplatform.libraries;
043:
044: import java.net.URL;
045: import java.util.ArrayList;
046: import java.util.List;
047: import org.netbeans.api.project.libraries.Library;
048: import org.netbeans.api.project.libraries.LibraryManager;
049: import org.netbeans.api.java.classpath.ClassPath;
050: import org.netbeans.spi.java.classpath.ClassPathProvider;
051: import org.netbeans.spi.java.classpath.support.ClassPathSupport;
052: import org.netbeans.api.java.platform.JavaPlatformManager;
053: import org.netbeans.spi.project.libraries.support.LibrariesSupport;
054: import org.openide.filesystems.FileObject;
055: import org.openide.filesystems.FileUtil;
056:
057: public class J2SELibraryClassPathProvider implements ClassPathProvider {
058:
059: public ClassPath findClassPath(FileObject file, String type) {
060: assert file != null;
061: Library ll = this .getLastUsedLibrary(file);
062: if (ll != null) {
063: ClassPath[] cp = findClassPathOrNull(file, type, ll);
064: return cp != null ? cp[0] : null;
065: } else {
066: for (LibraryManager mgr : LibraryManager.getOpenManagers()) {
067: for (Library lib : mgr.getLibraries()) {
068: ClassPath[] cp = findClassPathOrNull(file, type,
069: lib);
070: if (cp != null) {
071: return cp[0];
072: }
073: }
074: }
075: return null;
076: }
077: }
078:
079: private ClassPath[] findClassPathOrNull(FileObject file,
080: String type, Library lib) {
081: if (lib.getType().equals(J2SELibraryTypeProvider.LIBRARY_TYPE)) {
082: List<URL> resources = getResolvedVolume(lib,
083: J2SELibraryTypeProvider.VOLUME_TYPE_SRC);
084: ClassPath sourcePath = ClassPathSupport
085: .createClassPath(resources
086: .toArray(new URL[resources.size()]));
087: FileObject root = sourcePath.findOwnerRoot(file);
088: if (root != null) {
089: setLastUsedLibrary(root, lib);
090: if (ClassPath.SOURCE.equals(type)) {
091: return new ClassPath[] { sourcePath };
092: } else if (ClassPath.COMPILE.equals(type)) {
093: resources = getResolvedVolume(
094: lib,
095: J2SELibraryTypeProvider.VOLUME_TYPE_CLASSPATH);
096: return new ClassPath[] { ClassPathSupport
097: .createClassPath(resources
098: .toArray(new URL[resources.size()])) };
099: } else if (ClassPath.BOOT.equals(type)) {
100: return new ClassPath[] { JavaPlatformManager
101: .getDefault().getDefaultPlatform()
102: .getBootstrapLibraries() };
103: } else {
104: return new ClassPath[] { null };
105: }
106: }
107: }
108: return null;
109: }
110:
111: static List<URL> getResolvedVolume(Library lib, String volume) {
112: List<URL> resources = new ArrayList<URL>();
113: for (URL u : lib.getContent(volume)) {
114: resources.add(LibrariesSupport.resolveLibraryEntryURL(lib
115: .getManager().getLocation(), u));
116: }
117: return resources;
118: }
119:
120: private synchronized Library getLastUsedLibrary(FileObject fo) {
121: if (this .lastUsedRoot != null
122: && FileUtil.isParentOf(this .lastUsedRoot, fo)) {
123: return this .lastUsedLibrary;
124: } else {
125: return null;
126: }
127: }
128:
129: private synchronized void setLastUsedLibrary(FileObject root,
130: Library lib) {
131: this .lastUsedRoot = root;
132: this .lastUsedLibrary = lib;
133: }
134:
135: private FileObject lastUsedRoot;
136: private Library lastUsedLibrary;
137: }
|