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-2007 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.modules.gsfpath.api.queries;
042:
043: import java.net.URL;
044: import java.util.HashSet;
045: import java.util.Set;
046: import javax.swing.event.ChangeListener;
047: import org.netbeans.modules.gsfpath.api.classpath.ClassPath;
048: import org.netbeans.modules.gsfpath.spi.queries.BinaryForSourceQueryImplementation;
049: import org.openide.filesystems.FileObject;
050: import org.openide.filesystems.FileStateInvalidException;
051: import org.openide.filesystems.URLMapper;
052: import org.openide.util.Exceptions;
053: import org.openide.util.Lookup;
054:
055: /**
056: *
057: * The query is used for finding binaries for sources,
058: * this is intended to be the inverse of the SourceForBinaryQuery.
059: * @see BinaryForSourceQueryImplementation
060: * @see SourceForBinaryQuery
061: * @since org.netbeans.modules.gsfpath.api/1 1.12
062: * @author Tomas Zezula
063: *
064: */
065: public final class BinaryForSourceQuery {
066:
067: /** Creates a new instance of BInaryForSOurceQuery */
068: private BinaryForSourceQuery() {
069: }
070:
071: /**
072: * Returns the binary root for given source root.
073: * @param sourceRoot the source path root.
074: * @return a result object encapsulating the answer (never null)
075: */
076: public static Result findBinaryRoots(final URL sourceRoot) {
077: assert sourceRoot != null;
078: for (BinaryForSourceQueryImplementation impl : Lookup
079: .getDefault().lookupAll(
080: BinaryForSourceQueryImplementation.class)) {
081: BinaryForSourceQuery.Result result = impl
082: .findBinaryRoots(sourceRoot);
083: if (result != null) {
084: return result;
085: }
086: }
087: return new DefaultResult(sourceRoot);
088: }
089:
090: /**
091: * Result of finding binaries, encapsulating the answer as well as the
092: * ability to listen to it.
093: */
094: public static interface Result {
095:
096: /**
097: * Get the binary roots.
098: * @return array of roots of compiled classes (may be empty but not null)
099: */
100: URL[] getRoots();
101:
102: /**
103: * Add a listener to changes in the roots.
104: * @param l a listener to add
105: */
106: void addChangeListener(ChangeListener l);
107:
108: /**
109: * Remove a listener to changes in the roots.
110: * @param l a listener to remove
111: */
112: void removeChangeListener(ChangeListener l);
113: }
114:
115: private static class DefaultResult implements Result {
116:
117: private final URL sourceRoot;
118:
119: DefaultResult(final URL sourceRoot) {
120: this .sourceRoot = sourceRoot;
121: }
122:
123: public URL[] getRoots() {
124: FileObject fo = URLMapper.findFileObject(sourceRoot);
125: if (fo == null) {
126: return new URL[0];
127: }
128: ClassPath exec = ClassPath.getClassPath(fo,
129: ClassPath.EXECUTE);
130: if (exec == null) {
131: return new URL[0];
132: }
133: Set<URL> result = new HashSet<URL>();
134: for (ClassPath.Entry e : exec.entries()) {
135: FileObject[] roots = SourceForBinaryQuery
136: .findSourceRoots(e.getURL()).getRoots();
137: for (FileObject root : roots) {
138: try {
139: if (sourceRoot.equals(root.getURL())) {
140: result.add(e.getURL());
141: }
142: } catch (FileStateInvalidException fsie) {
143: Exceptions.printStackTrace(fsie);
144: }
145: }
146: }
147: return result.toArray(new URL[result.size()]);
148: }
149:
150: public void addChangeListener(ChangeListener l) {
151: }
152:
153: public void removeChangeListener(ChangeListener l) {
154: }
155: }
156: }
|