KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > 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.java.source.usages;
21
22 import java.io.IOException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.concurrent.locks.ReadWriteLock JavaDoc;
27 import java.util.concurrent.locks.ReentrantReadWriteLock JavaDoc;
28 import org.openide.util.Exceptions;
29
30 /**
31  *
32  * @author Tomas Zezula
33  */

34 public class ClassIndexManager {
35
36     private static ClassIndexManager instance;
37     private final Map JavaDoc<URL JavaDoc, ClassIndexImpl> instances = new HashMap JavaDoc<URL JavaDoc, ClassIndexImpl> ();
38     private ReadWriteLock JavaDoc lock;
39     private boolean invalid;
40     
41     
42     private ClassIndexManager() {
43         this.lock = new ReentrantReadWriteLock JavaDoc (false);
44     }
45     
46     public <T> T writeLock (final ExceptionAction<T> r) throws IOException JavaDoc {
47         this.lock.writeLock().lock();
48         try {
49             return r.run();
50         } finally {
51             this.lock.writeLock().unlock();
52         }
53     }
54     
55     public <T> T readLock (final ExceptionAction<T> r) throws IOException JavaDoc {
56         this.lock.readLock().lock();
57         try {
58             return r.run();
59         } finally {
60             this.lock.readLock().unlock();
61         }
62     }
63     
64     public synchronized ClassIndexImpl getUsagesQuery (final URL JavaDoc root) throws IOException JavaDoc {
65         assert root != null;
66         if (invalid) {
67             return null;
68         }
69         return this.instances.get (root);
70     }
71     
72     public synchronized ClassIndexImpl createUsagesQuery (final URL JavaDoc root, final boolean source) throws IOException JavaDoc {
73         assert root != null;
74         if (invalid) {
75             return null;
76         }
77         ClassIndexImpl qi = this.instances.get (root);
78         if (qi == null) {
79             qi = PersistentClassIndex.create (root, Index.getDataFolder(root), source);
80             this.instances.put(root,qi);
81         }
82         return qi;
83     }
84     
85     synchronized void removeRoot (final URL JavaDoc root) throws IOException JavaDoc {
86         ClassIndexImpl ci = this.instances.remove(root);
87         if (ci != null) {
88             ci.close();
89         }
90     }
91     
92     public synchronized void close () {
93         invalid = true;
94         for (ClassIndexImpl ci : instances.values()) {
95             try {
96                 ci.close();
97             } catch (IOException JavaDoc ioe) {
98                 Exceptions.printStackTrace(ioe);
99             }
100         }
101     }
102     
103     public static interface ExceptionAction<T> {
104         public T run () throws IOException JavaDoc;
105     }
106     
107     
108     public static synchronized ClassIndexManager getDefault () {
109         if (instance == null) {
110             instance = new ClassIndexManager ();
111         }
112         return instance;
113     }
114     
115 }
116
Popular Tags