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.source.usages;
043:
044: import java.io.IOException;
045: import java.net.URL;
046: import java.util.HashMap;
047: import java.util.HashSet;
048: import java.util.List;
049: import java.util.Map;
050: import java.util.Set;
051: import java.util.concurrent.CopyOnWriteArrayList;
052: import java.util.concurrent.locks.ReadWriteLock;
053: import java.util.concurrent.locks.ReentrantReadWriteLock;
054: import org.openide.util.Exceptions;
055:
056: /**
057: *
058: * @author Tomas Zezula
059: */
060: public final class ClassIndexManager {
061:
062: private static final byte OP_ADD = 1;
063: private static final byte OP_REMOVE = 2;
064:
065: private static ClassIndexManager instance;
066: private final Map<URL, ClassIndexImpl> instances = new HashMap<URL, ClassIndexImpl>();
067: private final ReadWriteLock lock;
068: private final List<ClassIndexManagerListener> listeners = new CopyOnWriteArrayList<ClassIndexManagerListener>();
069: private boolean invalid;
070: private Set<URL> added;
071: private Set<URL> removed;
072: private int depth = 0;
073: private Thread owner;
074:
075: private ClassIndexManager() {
076: this .lock = new ReentrantReadWriteLock(false);
077: }
078:
079: public void addClassIndexManagerListener(
080: final ClassIndexManagerListener listener) {
081: assert listener != null;
082: this .listeners.add(listener);
083: }
084:
085: public void removeClassIndexManagerListener(
086: final ClassIndexManagerListener listener) {
087: assert listener != null;
088: this .listeners.remove(listener);
089: }
090:
091: public <T> T writeLock(final ExceptionAction<T> r)
092: throws IOException, InterruptedException {
093: this .lock.writeLock().lock();
094: try {
095: if (depth == 0) {
096: this .owner = Thread.currentThread();
097: }
098: try {
099: depth++;
100: try {
101: if (depth == 1) {
102: this .added = new HashSet<URL>();
103: this .removed = new HashSet<URL>();
104: }
105: try {
106: return r.run();
107: } finally {
108: if (depth == 1) {
109: if (!removed.isEmpty()) {
110: fire(removed, OP_REMOVE);
111: removed.clear();
112: }
113: if (!added.isEmpty()) {
114: fire(added, OP_ADD);
115: added.clear();
116: }
117: }
118: }
119: } finally {
120: depth--;
121: }
122: } finally {
123: if (depth == 0) {
124: this .owner = null;
125: }
126: }
127: } finally {
128: this .lock.writeLock().unlock();
129: }
130: }
131:
132: public <T> T readLock(final ExceptionAction<T> r)
133: throws IOException, InterruptedException {
134: this .lock.readLock().lock();
135: try {
136: return r.run();
137: } finally {
138: this .lock.readLock().unlock();
139: }
140: }
141:
142: public boolean holdsWriteLock() {
143: return Thread.currentThread().equals(this .owner);
144: }
145:
146: public synchronized ClassIndexImpl getUsagesQuery(final URL root)
147: throws IOException {
148: assert root != null;
149: if (invalid) {
150: return null;
151: }
152: return this .instances.get(root);
153: }
154:
155: public synchronized ClassIndexImpl createUsagesQuery(
156: final URL root, final boolean source) throws IOException {
157: assert root != null;
158: if (invalid) {
159: return null;
160: }
161: ClassIndexImpl qi = this .instances.get(root);
162: if (qi == null) {
163: qi = PersistentClassIndex.create(root, Index
164: .getDataFolder(root), source);
165: this .instances.put(root, qi);
166: if (added != null) {
167: added.add(root);
168: }
169: } else if (source && !qi.isSource()) {
170: //Wrongly set up freeform project, which is common for it, prefer source
171: qi.close();
172: qi = PersistentClassIndex.create(root, Index
173: .getDataFolder(root), source);
174: this .instances.put(root, qi);
175: if (added != null) {
176: added.add(root);
177: }
178: }
179: return qi;
180: }
181:
182: synchronized void removeRoot(final URL root) throws IOException {
183: ClassIndexImpl ci = this .instances.remove(root);
184: if (ci != null) {
185: ci.close();
186: if (removed != null) {
187: removed.add(root);
188: }
189: }
190: }
191:
192: public synchronized void close() {
193: invalid = true;
194: for (ClassIndexImpl ci : instances.values()) {
195: try {
196: ci.close();
197: } catch (IOException ioe) {
198: Exceptions.printStackTrace(ioe);
199: }
200: }
201: }
202:
203: public static interface ExceptionAction<T> {
204: public T run() throws IOException, InterruptedException;
205: }
206:
207: private void fire(final Set<? extends URL> roots, final byte op) {
208: if (!this .listeners.isEmpty()) {
209: final ClassIndexManagerEvent event = new ClassIndexManagerEvent(
210: this , roots);
211: for (ClassIndexManagerListener listener : this .listeners) {
212: if (op == OP_ADD) {
213: listener.classIndexAdded(event);
214: } else if (op == OP_REMOVE) {
215: listener.classIndexRemoved(event);
216: } else {
217: assert false : "Unknown op: " + op; //NOI18N
218: }
219: }
220: }
221: }
222:
223: public static synchronized ClassIndexManager getDefault() {
224: if (instance == null) {
225: instance = new ClassIndexManager();
226: }
227: return instance;
228: }
229: }
|