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:
042: package org.netbeans.napi.gsfret.source;
043:
044: import java.net.URL;
045: import java.util.*;
046:
047: import org.netbeans.modules.gsfpath.api.classpath.ClassPath;
048: import org.netbeans.modules.gsfpath.api.classpath.GlobalPathRegistry;
049: import org.netbeans.modules.gsfret.source.usages.RepositoryUpdater;
050:
051: /**
052: *
053: * @author Dusan Balek
054: */
055: public class SourceUtils {
056:
057: private SourceUtils() {
058: }
059:
060: /**
061: * Tests whether the initial scan is in progress.
062: */
063: public static boolean isScanInProgress() {
064: return RepositoryUpdater.getDefault().isScanInProgress();
065: }
066:
067: /**
068: * Waits for the end of the initial scan, this helper method
069: * is designed for tests which require to wait for end of initial scan.
070: * @throws InterruptedException is thrown when the waiting thread is interrupted.
071: */
072: public static void waitScanFinished() throws InterruptedException {
073: RepositoryUpdater.getDefault().waitScanFinished();
074: }
075:
076: /**
077: * Returns the dependent source path roots for given source root.
078: * It returns all the open project source roots which have either
079: * direct or transitive dependency on the given source root.
080: * @param root to find the dependent roots for
081: * @return {@link Set} of {@link URL}s containinig at least the
082: * incomming root, never returns null.
083: * @since 0.10
084: */
085: public static Set<URL> getDependentRoots(final URL root) {
086: final Map<URL, List<URL>> deps = RepositoryUpdater.getDefault()
087: .getDependencies();
088: return getDependentRootsImpl(root, deps);
089: }
090:
091: static Set<URL> getDependentRootsImpl(final URL root,
092: final Map<URL, List<URL>> deps) {
093: //Create inverse dependencies
094: final Map<URL, List<URL>> inverseDeps = new HashMap<URL, List<URL>>();
095: for (Map.Entry<URL, List<URL>> entry : deps.entrySet()) {
096: final URL u1 = entry.getKey();
097: final List<URL> l1 = entry.getValue();
098: for (URL u2 : l1) {
099: List<URL> l2 = inverseDeps.get(u2);
100: if (l2 == null) {
101: l2 = new ArrayList<URL>();
102: inverseDeps.put(u2, l2);
103: }
104: l2.add(u1);
105: }
106: }
107: //Collect dependencies
108: final Set<URL> result = new HashSet<URL>();
109: final LinkedList<URL> todo = new LinkedList<URL>();
110: todo.add(root);
111: while (!todo.isEmpty()) {
112: final URL u = todo.removeFirst();
113: if (!result.contains(u)) {
114: result.add(u);
115: final List<URL> ideps = inverseDeps.get(u);
116: if (ideps != null) {
117: todo.addAll(ideps);
118: }
119: }
120: }
121: //Filter non opened projects
122: Set<ClassPath> cps = GlobalPathRegistry.getDefault().getPaths(
123: ClassPath.SOURCE);
124: Set<URL> toRetain = new HashSet<URL>();
125: for (ClassPath cp : cps) {
126: for (ClassPath.Entry e : cp.entries()) {
127: toRetain.add(e.getURL());
128: }
129: }
130: result.retainAll(toRetain);
131: return result;
132: }
133: }
|