1 19 20 package org.netbeans.modules.java.source.usages; 21 22 import java.io.IOException ; 23 import java.net.URL ; 24 import java.util.HashMap ; 25 import java.util.Map ; 26 import java.util.concurrent.locks.ReadWriteLock ; 27 import java.util.concurrent.locks.ReentrantReadWriteLock ; 28 import org.openide.util.Exceptions; 29 30 34 public class ClassIndexManager { 35 36 private static ClassIndexManager instance; 37 private final Map <URL , ClassIndexImpl> instances = new HashMap <URL , ClassIndexImpl> (); 38 private ReadWriteLock lock; 39 private boolean invalid; 40 41 42 private ClassIndexManager() { 43 this.lock = new ReentrantReadWriteLock (false); 44 } 45 46 public <T> T writeLock (final ExceptionAction<T> r) throws IOException { 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 { 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 root) throws IOException { 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 root, final boolean source) throws IOException { 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 root) throws IOException { 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 ioe) { 98 Exceptions.printStackTrace(ioe); 99 } 100 } 101 } 102 103 public static interface ExceptionAction<T> { 104 public T run () throws IOException ; 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 |