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.gsfpath.api.queries;
043:
044: import java.net.URL;
045: import java.util.Arrays;
046: import java.util.logging.Level;
047: import java.util.logging.Logger;
048: import javax.swing.event.ChangeListener;
049: import org.netbeans.modules.gsfpath.spi.queries.SourceForBinaryQueryImplementation;
050: import org.openide.filesystems.FileObject;
051: import org.openide.filesystems.FileUtil;
052: import org.openide.util.Lookup;
053:
054: /**
055: * The query is used for finding sources for binaries.
056: * The examples of usage of this query are:
057: * <ul>
058: * <li><p>finding source for library</p></li>
059: * <li><p>finding src.zip for platform</p></li>
060: * <li><p>finding source folder for compiled jar or build folder</p></li>
061: * </ul>
062: * @see SourceForBinaryQueryImplementation
063: * @since org.netbeans.modules.gsfpath.api/1 1.4
064: */
065: public class SourceForBinaryQuery {
066:
067: private static final Logger LOG = Logger
068: .getLogger(SourceForBinaryQuery.class.getName());
069:
070: private static final Lookup.Result<? extends SourceForBinaryQueryImplementation> implementations = Lookup
071: .getDefault().lookupResult(
072: SourceForBinaryQueryImplementation.class);
073:
074: private SourceForBinaryQuery() {
075: }
076:
077: /**
078: * Returns the source root for given binary root (for example, src folder for jar file or build folder).
079: * @param binaryRoot the ClassPath root of compiled files.
080: * @return a result object encapsulating the answer (never null)
081: */
082: public static Result findSourceRoots(URL binaryRoot) {
083: if (FileUtil.isArchiveFile(binaryRoot)) {
084: throw new IllegalArgumentException("File URL pointing to "
085: + // NOI18N
086: "JAR is not valid classpath entry. Use jar: URL. Was: "
087: + binaryRoot); // NOI18N
088: }
089: if (!binaryRoot.toExternalForm().endsWith("/")) {
090: throw new IllegalArgumentException(
091: "Folder URL must end with '/'. Was: " + binaryRoot);
092: }
093: for (SourceForBinaryQueryImplementation impl : implementations
094: .allInstances()) {
095: Result result = impl.findSourceRoots(binaryRoot);
096: if (result != null) {
097: if (LOG.isLoggable(Level.FINE)) {
098: LOG.log(Level.FINE,
099: "findSourceRoots({0}) -> {1} from {2}",
100: new Object[] { binaryRoot,
101: Arrays.asList(result.getRoots()),
102: impl });
103: }
104: return result;
105: }
106: }
107: LOG.log(Level.FINE, "findSourceRoots({0}) -> nil", binaryRoot);
108: return EMPTY_RESULT;
109: }
110:
111: /**
112: * Result of finding sources, encapsulating the answer as well as the
113: * ability to listen to it.
114: */
115: public interface Result {
116:
117: /**
118: * Get the source roots.
119: * @return array of roots of sources (may be empty but not null)
120: */
121: FileObject[] getRoots();
122:
123: /**
124: * Add a listener to changes in the roots.
125: * @param l a listener to add
126: */
127: void addChangeListener(ChangeListener l);
128:
129: /**
130: * Remove a listener to changes in the roots.
131: * @param l a listener to remove
132: */
133: void removeChangeListener(ChangeListener l);
134:
135: }
136:
137: private static final Result EMPTY_RESULT = new EmptyResult();
138:
139: private static final class EmptyResult implements Result {
140: private static final FileObject[] NO_ROOTS = new FileObject[0];
141:
142: EmptyResult() {
143: }
144:
145: public FileObject[] getRoots() {
146: return NO_ROOTS;
147: }
148:
149: public void addChangeListener(ChangeListener l) {
150: }
151:
152: public void removeChangeListener(ChangeListener l) {
153: }
154: }
155:
156: }
|