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: package org.netbeans.spi.java.classpath.support;
042:
043: import org.netbeans.spi.java.classpath.PathResourceImplementation;
044: import org.netbeans.spi.java.classpath.ClassPathImplementation;
045: import org.netbeans.spi.java.classpath.ClassPathFactory;
046: import org.netbeans.modules.java.classpath.*;
047: import org.netbeans.api.java.classpath.ClassPath;
048: import org.openide.ErrorManager;
049: import org.openide.filesystems.FileObject;
050:
051: import java.net.URL;
052: import java.util.List;
053: import java.util.ArrayList;
054: import org.openide.filesystems.FileStateInvalidException;
055: import org.openide.filesystems.FileUtil;
056:
057: /**
058: * Convenience factory for creating classpaths of common sorts.
059: * @since org.netbeans.api.java/1 1.4
060: */
061: public class ClassPathSupport {
062:
063: private ClassPathSupport() {
064: }
065:
066: /** Creates leaf PathResourceImplementation.
067: * The created PathResourceImplementation has exactly one immutable root.
068: * @param url the root of the resource. The URL must refer to folder. In the case of archive file
069: * the jar protocol URL must be used.
070: * @return PathResourceImplementation
071: */
072: public static PathResourceImplementation createResource(URL url) {
073: if (url == null) {
074: throw new NullPointerException(
075: "Cannot pass null URL to ClassPathSupport.createResource"); // NOI18N
076: }
077: // FU.iAF is a bit slow, so don't call it except when assertions are on:
078: boolean assertions = false;
079: assert assertions = true;
080: if (assertions && FileUtil.isArchiveFile(url)) {
081: throw new IllegalArgumentException("File URL pointing to "
082: + // NOI18N
083: "JAR is not valid classpath entry. Use jar: URL. Was: "
084: + url); // NOI18N
085: }
086: if (!url.toExternalForm().endsWith("/")) { // NOI18N
087: throw new IllegalArgumentException(
088: "URL must be a folder URL (append '/' if necessary): "
089: + url); // NOI18N
090: }
091: return new SimplePathResourceImplementation(url);
092: }
093:
094: /**
095: * Create ClassPathImplementation for the given list of
096: * {@link PathResourceImplementation} entries.
097: * @param entries list of {@link PathResourceImplementation} instances;
098: * cannot be null; can be empty
099: * @return SPI classpath
100: */
101: public static ClassPathImplementation createClassPathImplementation(
102: List<? extends PathResourceImplementation> entries) {
103: if (entries == null) {
104: throw new NullPointerException("Cannot pass null entries"); // NOI18N
105: }
106: return new SimpleClassPathImplementation(entries);
107: }
108:
109: /**
110: * Create ClassPath for the given list of
111: * {@link PathResourceImplementation} entries.
112: * @param entries list of {@link PathResourceImplementation} instances;
113: * cannot be null; can be empty
114: * @return API classpath
115: */
116: public static ClassPath createClassPath(
117: List<? extends PathResourceImplementation> entries) {
118: if (entries == null) {
119: throw new NullPointerException("Cannot pass null entries"); // NOI18N
120: }
121: return ClassPathFactory
122: .createClassPath(createClassPathImplementation(entries));
123: }
124:
125: /**
126: * Create ClassPath for the given array of class path roots
127: * @param roots array of fileobjects which must correspond to directory.
128: * In the case of archive file, the FileObject representing the root of the
129: * archive must be used. Cannot be null; can be empty array; array can contain nulls.
130: * @return API classpath
131: */
132: public static ClassPath createClassPath(FileObject... roots) {
133: assert roots != null;
134: List<PathResourceImplementation> l = new ArrayList<PathResourceImplementation>();
135: for (FileObject root : roots) {
136: if (root == null) {
137: continue;
138: }
139: try {
140: URL u = root.getURL();
141: l.add(createResource(u));
142: } catch (FileStateInvalidException e) {
143: ErrorManager.getDefault().notify(e);
144: }
145: }
146: return createClassPath(l);
147: }
148:
149: /**
150: * Create ClassPath for the given array of class path roots
151: * @param roots array of URLs which must correspond to directory.
152: * In the case of archive file, the jar protocol URL must be used.
153: * Cannot be null; can be empty array; array can contain nulls.
154: * @return API classpath
155: */
156: public static ClassPath createClassPath(URL... roots) {
157: assert roots != null;
158: List<PathResourceImplementation> l = new ArrayList<PathResourceImplementation>();
159: for (URL root : roots) {
160: if (root == null)
161: continue;
162: l.add(createResource(root));
163: }
164: return createClassPath(l);
165: }
166:
167: /**
168: * Creates read only proxy ClassPathImplementation for given delegates.
169: * The order of resources is given by the order of the delegates
170: * @param delegates ClassPathImplementations to delegate to.
171: * @return SPI classpath
172: */
173: public static ClassPathImplementation createProxyClassPathImplementation(
174: ClassPathImplementation... delegates) {
175: return new ProxyClassPathImplementation(delegates);
176: }
177:
178: /**
179: * Creates read only proxy ClassPath for given delegates.
180: * The order of resources is given by the order of the delegates
181: * @param delegates ClassPaths to delegate to.
182: * @return API classpath
183: */
184: public static ClassPath createProxyClassPath(ClassPath... delegates) {
185: assert delegates != null;
186: ClassPathImplementation[] impls = new ClassPathImplementation[delegates.length];
187: for (int i = 0; i < delegates.length; i++) {
188: impls[i] = ClassPathAccessor.DEFAULT
189: .getClassPathImpl(delegates[i]);
190: }
191: return ClassPathFactory
192: .createClassPath(createProxyClassPathImplementation(impls));
193: }
194:
195: }
|