1 19 20 package org.netbeans.modules.retouche.source.usages; 21 22 import java.io.IOException ; 23 import java.net.URL ; 24 import java.util.Collections ; 25 import java.util.HashMap ; 26 import java.util.HashSet ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.Set ; 30 import java.util.concurrent.locks.ReadWriteLock ; 31 import java.util.concurrent.locks.ReentrantReadWriteLock ; 32 import org.openide.util.Exceptions; 33 34 43 public class ClassIndexManager { 44 45 private static ClassIndexManager instance; 46 private final Map <URL , ClassIndexImpl> instances = new HashMap <URL , ClassIndexImpl> (); 47 private ReadWriteLock lock; 48 private boolean invalid; 49 50 51 private ClassIndexManager() { 52 this.lock = new ReentrantReadWriteLock (false); 53 } 54 55 public <T> T writeLock (final ExceptionAction<T> r) throws IOException { 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 { 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 root) throws IOException { 74 assert root != null; 75 if (invalid) { 76 return null; 77 } 78 ClassIndexImpl ci = this.instances.get (root); 82 if (ci == null) { 83 ci = createUsagesQuery(root, false); 84 } 85 86 return ci; 87 } 89 90 public synchronized ClassIndexImpl createUsagesQuery (final URL root, final boolean source) throws IOException { 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 root) throws IOException { 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 ioe) { 116 Exceptions.printStackTrace(ioe); 117 } 118 } 119 } 120 121 public static interface ExceptionAction<T> { 122 public T run () throws IOException ; 123 } 124 125 126 public static synchronized ClassIndexManager getDefault () { 127 if (instance == null) { 128 instance = new ClassIndexManager (); 129 } 130 return instance; 131 } 132 133 public synchronized Map <URL , ClassIndexImpl> getAllIndices() { 136 if (invalid) { 137 return Collections.emptyMap(); 138 } 139 return Collections.unmodifiableMap(this.instances); 140 } 141 142 private final Set <URL > isBoot = new HashSet <URL >(); 144 145 public boolean isBootRoot(URL root) { 146 return isBoot.contains(root); 147 } 148 149 public void setBootRoots(List <URL > urls) { 150 if (isBoot.size() > 0) { 151 isBoot.clear(); 152 } 153 for (URL url : urls) { 154 isBoot.add(url); 155 } 156 } 157 158 public Set <URL > getBootRoots() { 159 return isBoot; 160 } 161 } 163 | Popular Tags |