KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > retouche > source > usages > ClassIndexManager


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.retouche.source.usages;
21
22 import java.io.IOException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.concurrent.locks.ReadWriteLock JavaDoc;
31 import java.util.concurrent.locks.ReentrantReadWriteLock JavaDoc;
32 import org.openide.util.Exceptions;
33
34 /**
35  * This file is originally from Retouche, the Java Support
36  * infrastructure in NetBeans. I have modified the file as little
37  * as possible to make merging Retouche fixes back as simple as
38  * possible.
39  *
40  *
41  * @author Tomas Zezula
42  */

43 public class ClassIndexManager {
44
45     private static ClassIndexManager instance;
46     private final Map JavaDoc<URL JavaDoc, ClassIndexImpl> instances = new HashMap JavaDoc<URL JavaDoc, ClassIndexImpl> ();
47     private ReadWriteLock JavaDoc lock;
48     private boolean invalid;
49     
50     
51     private ClassIndexManager() {
52         this.lock = new ReentrantReadWriteLock JavaDoc (false);
53     }
54     
55     public <T> T writeLock (final ExceptionAction<T> r) throws IOException JavaDoc {
56         this.lock.writeLock().lock();
57         try {
58             return r.run();
59         } finally {
60             this.lock.writeLock().unlock();
61         }
62     }
63     
64     public <T> T readLock (final ExceptionAction<T> r) throws IOException JavaDoc {
65         this.lock.readLock().lock();
66         try {
67             return r.run();
68         } finally {
69             this.lock.readLock().unlock();
70         }
71     }
72     
73     public synchronized ClassIndexImpl getUsagesQuery (final URL JavaDoc root) throws IOException JavaDoc {
74         assert root != null;
75         if (invalid) {
76             return null;
77         }
78         // BEGIN TOR MODIFICATIONS
79
// XXX Figure out why I often get this on boot class paths
80
//return this.instances.get (root);
81
ClassIndexImpl ci = this.instances.get (root);
82         if (ci == null) {
83             ci = createUsagesQuery(root, false);
84         }
85         
86         return ci;
87         // END TOR MODIFICATIONS
88
}
89     
90     public synchronized ClassIndexImpl createUsagesQuery (final URL JavaDoc root, final boolean source) throws IOException JavaDoc {
91         assert root != null;
92         if (invalid) {
93             return null;
94         }
95         ClassIndexImpl qi = this.instances.get (root);
96         if (qi == null) {
97             qi = PersistentClassIndex.create (root, Index.getDataFolder(root), source);
98             this.instances.put(root,qi);
99         }
100         return qi;
101     }
102     
103     synchronized void removeRoot (final URL JavaDoc root) throws IOException JavaDoc {
104         ClassIndexImpl ci = this.instances.remove(root);
105         if (ci != null) {
106             ci.close();
107         }
108     }
109     
110     public synchronized void close () {
111         invalid = true;
112         for (ClassIndexImpl ci : instances.values()) {
113             try {
114                 ci.close();
115             } catch (IOException JavaDoc ioe) {
116                 Exceptions.printStackTrace(ioe);
117             }
118         }
119     }
120     
121     public static interface ExceptionAction<T> {
122         public T run () throws IOException JavaDoc;
123     }
124     
125     
126     public static synchronized ClassIndexManager getDefault () {
127         if (instance == null) {
128             instance = new ClassIndexManager ();
129         }
130         return instance;
131     }
132
133     // BEGIN TOR MODIFICATIONS
134
// This is for development use only (the index browser)
135
public synchronized Map JavaDoc<URL JavaDoc, ClassIndexImpl> getAllIndices() {
136         if (invalid) {
137             return Collections.emptyMap();
138         }
139         return Collections.unmodifiableMap(this.instances);
140     }
141     
142     // To avoid excessive compilation at startup etc.
143
private final Set JavaDoc<URL JavaDoc> isBoot = new HashSet JavaDoc<URL JavaDoc>();
144     
145     public boolean isBootRoot(URL JavaDoc root) {
146         return isBoot.contains(root);
147     }
148     
149     public void setBootRoots(List JavaDoc<URL JavaDoc> urls) {
150         if (isBoot.size() > 0) {
151             isBoot.clear();
152         }
153         for (URL JavaDoc url : urls) {
154             isBoot.add(url);
155         }
156     }
157     
158     public Set JavaDoc<URL JavaDoc> getBootRoots() {
159         return isBoot;
160     }
161     // END TOR MODIFICATIONS
162
}
163
Popular Tags