1 20 package org.enhydra.dods.cache; 21 22 import java.lang.reflect.Method ; 23 import java.util.Enumeration ; 24 import java.util.Vector ; 25 import java.util.HashMap ; 26 import org.enhydra.dods.DODS; 27 import com.lutris.logging.Logger; 28 29 30 38 public final class Wrapper { 39 40 43 private static Wrapper instance = null; 44 private long locked; 45 private long timeout; 46 47 50 public static Wrapper getInstance() { 51 if (null == instance) { 52 instance = new Wrapper(); 53 } 54 return instance; 55 } 56 57 60 protected Vector vec; 61 protected HashMap nonVisibleSimpleComplexQCList; 62 protected HashMap nonVisibleMultiJoinQCList; 63 64 67 private Wrapper() { 68 vec = new Vector (); 69 nonVisibleSimpleComplexQCList = new HashMap (); 70 nonVisibleMultiJoinQCList = new HashMap (); 71 locked = 0; 72 } 73 74 77 public void registerCache(DataStructCache dc) { 78 if (dc instanceof QueryCache) { 79 vec.add(dc); 80 } 81 } 82 83 86 public void setTimeout(long to) { 87 timeout = to; 88 } 89 90 93 public synchronized long lock() { 94 if (locked > 0 && locked < System.currentTimeMillis()) { 95 return 0; 96 } 97 for (Enumeration e = vec.elements(); e.hasMoreElements();) { 98 ((DataStructCache) e.nextElement()).lock(); 99 } 100 locked = System.currentTimeMillis() + timeout; 101 return locked; 102 } 103 104 107 public synchronized void unlock(long handle) { 108 if (handle != locked) { 109 DODS.getLogChannel().write(Logger.DEBUG, 110 "Wrapper: wrong handle to unlock " + handle); 111 return; 112 } 113 for (Enumeration e = vec.elements(); e.hasMoreElements();) { 114 ((DataStructCache) e.nextElement()).unlock(); 115 } 116 locked = 0; 117 } 118 119 122 public void removeAllComplexQueries() { 123 for (Enumeration e = vec.elements(); e.hasMoreElements();) { 124 Object obj = e.nextElement(); 125 126 if (obj instanceof QueryCache) { 127 ((QueryCache) obj).getCacheAdministration(CacheConstants.COMPLEX_QUERY_CACHE).refresh(); 128 } 129 } 130 } 131 132 135 public void removeAllMultiJoinQueries() { 136 for (Enumeration e = vec.elements(); e.hasMoreElements();) { 137 Object obj = e.nextElement(); 138 139 if (obj instanceof QueryCache) { 140 ((QueryCache) obj).getCacheAdministration(CacheConstants.MULTI_JOIN_QUERY_CACHE).refresh(); 141 } 142 } 143 } 144 145 148 public void makeSimpleComplexQCachesInvisible(Vector vecQC) { 149 for (Enumeration e = vecQC.elements(); e.hasMoreElements();) { 150 Class doClass = (Class ) e.nextElement(); 151 Method mtd = null; 152 QueryCache qc = null; 153 154 try { 155 mtd = doClass.getMethod("getConfigurationAdministration", 156 new Class [] {}); 157 qc = (QueryCache) mtd.invoke(null, new Object [] {}); 158 } catch (Throwable t) { 159 DODS.getLogChannel().write(Logger.DEBUG, 160 "Wrapper: couldn't get QueryCache for " + doClass); 161 continue; 162 } 163 Integer intObj = (Integer ) nonVisibleSimpleComplexQCList.get(qc); 164 int num; 165 166 if (intObj != null) { 167 num = intObj.intValue(); 168 num++; 169 nonVisibleSimpleComplexQCList.put(qc, new Integer (num)); 170 } else { 171 nonVisibleSimpleComplexQCList.put(qc, new Integer (1)); 172 qc.lockSimpleComplexQCache(); 173 } 174 } 175 } 176 177 180 public void makeSimpleComplexQCachesVisible(Vector vecQC) { 181 for (Enumeration e = vecQC.elements(); e.hasMoreElements();) { 182 Class doClass = (Class ) e.nextElement(); 183 Method mtd = null; 184 QueryCache qc = null; 185 186 try { 187 mtd = doClass.getMethod("getConfigurationAdministration", 188 new Class [] {}); 189 qc = (QueryCache) mtd.invoke(null, new Object [] {}); 190 } catch (Throwable t) { 191 DODS.getLogChannel().write(Logger.DEBUG, 192 "Wrapper: couldn't get QueryCache for " + doClass); 193 continue; 194 } 195 Integer intObj = (Integer ) nonVisibleSimpleComplexQCList.get(qc); 196 int num; 197 198 if (intObj != null) { 199 num = intObj.intValue(); 200 num--; 201 if (num == 0) { 202 nonVisibleSimpleComplexQCList.remove(qc); 203 qc.unlockSimpleComplexQCache(); 204 } else { 205 nonVisibleSimpleComplexQCList.put(qc, new Integer (num)); 206 } 207 } 208 } 209 } 210 public void makeMultiJoinQCachesInvisible() { 211 for (Enumeration e = vec.elements(); e.hasMoreElements();) { 212 Object obj = e.nextElement(); 213 214 if (obj instanceof QueryCache) { 215 QueryCache qc = (QueryCache)obj; 216 Integer intObj = (Integer ) nonVisibleMultiJoinQCList.get(qc); 217 int num; 218 219 if (intObj != null) { 220 num = intObj.intValue(); 221 num++; 222 nonVisibleMultiJoinQCList.put(qc, new Integer (num)); 223 } else { 224 nonVisibleMultiJoinQCList.put(qc, new Integer (1)); 225 qc.lockMultiJoinQCache(); 226 } 227 } 228 } 229 } 230 public void makeMultiJoinQCachesVisible() { 231 for (Enumeration e = vec.elements(); e.hasMoreElements();) { 232 Object obj = e.nextElement(); 233 234 if (obj instanceof QueryCache) { 235 QueryCache qc = (QueryCache)obj; 236 237 Integer intObj = (Integer ) nonVisibleSimpleComplexQCList.get(qc); 238 int num; 239 240 if (intObj != null) { 241 num = intObj.intValue(); 242 num--; 243 if (num == 0) { 244 nonVisibleMultiJoinQCList.remove(qc); 245 qc.unlockMultiJoinQCache(); 246 } 247 else { 248 nonVisibleMultiJoinQCList.put(qc, new Integer (num)); 249 } 250 } 251 } 252 } 253 } 254 } 255 | Popular Tags |