1 19 package org.openharmonise.rm.factory; 20 21 22 import java.util.*; 23 import java.util.logging.*; 24 25 import org.openharmonise.commons.cache.*; 26 import org.openharmonise.commons.dsi.AbstractDataStoreInterface; 27 import org.openharmonise.rm.resources.AbstractObject; 28 29 import EDU.oswego.cs.dl.util.concurrent.*; 30 31 39 public class CacheHandler { 40 41 44 private static CacheHandler m_instance = null; 45 46 49 private static final int DEFAULT_CACHESIZE = 50; 50 51 54 private AbstractDataStoreInterface m_dsi = null; 55 56 59 private Map m_caches; 60 61 64 private ReadWriteLock m_lock = new WriterPreferenceReadWriteLock(); 65 66 67 private static Logger m_logger = Logger.getLogger(CacheHandler.class.getName()); 68 69 74 private CacheHandler(AbstractDataStoreInterface dbinterf) { 75 m_dsi = dbinterf; 76 m_caches = new HashMap(DEFAULT_CACHESIZE); 77 } 78 79 85 public static synchronized CacheHandler getInstance(AbstractDataStoreInterface dbinterf) { 86 if (m_instance == null) { 87 m_instance = new CacheHandler(dbinterf); 88 } 89 90 return m_instance; 91 } 92 93 101 public Object getObject(String sClassname, int nId) 102 throws CacheException { 103 Object cachedObject = null; 104 105 if (nId > 0) { 106 AbstractCache cache = getCache(sClassname); 107 108 cachedObject = cache.getObject(String.valueOf(nId)); 109 } else { 110 cachedObject = GeneralCache.createObject(m_dsi, sClassname, 111 nId); 112 } 113 114 return cachedObject; 115 } 116 117 125 public Object getObjectFromPath(String sClassname, String sPath) 126 throws CacheException { 127 Object cachedObject = null; 128 129 if ((sPath != null) && (sPath.length() > 0)) { 130 GeneralCache cache = (GeneralCache) getCache(sClassname); 131 132 cachedObject = cache.getObjectFromPath(sPath); 133 } else { 134 cachedObject = GeneralCache.createObject(m_dsi, sClassname, 135 -1); 136 } 137 138 return cachedObject; 139 } 140 141 149 public Object getObject(String sClassname, String sKey) 150 throws CacheException { 151 Object cachedObject = null; 152 153 if ((sKey != null) && (sKey.length() > 0)) { 154 155 AbstractCache cache = getCache(sClassname); 156 157 cachedObject = cache.getObject(sKey); 158 } else { 159 cachedObject = GeneralCache.createObject(m_dsi, sClassname, 160 sKey); 161 } 162 163 return cachedObject; 164 } 165 166 173 public void addToCache(AbstractObject xobj) throws CacheException { 174 Object cachedObject = null; 175 int nId = xobj.getId(); 176 177 if (nId > 0) { 178 AbstractCache cache = getCache(xobj.getClass()); 179 180 cache.addToCache(String.valueOf(nId), xobj); 181 } 182 } 183 184 197 public void changeObject(String key, String sType, Object newObj) 198 throws CacheException { 199 AbstractCache cache = getCache(newObj.getClass()); 200 201 cache.changeObject(key, sType, newObj); 202 } 203 204 217 public void changeObject(Object oldObj, String sType, Object newObj) 218 throws CacheException { 219 AbstractCache cache = getCache(oldObj.getClass()); 220 221 cache.changeObject(String.valueOf(((AbstractObject) oldObj).getId()), sType, 222 newObj); 223 } 224 225 232 public GeneralCache getCache(String sClassname) 233 throws CacheException { 234 GeneralCache cache = null; 235 236 try { 237 m_lock.readLock().acquire(); 238 cache = (GeneralCache) m_caches.get(sClassname); 239 m_lock.readLock().release(); 240 } catch (InterruptedException e) { 241 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 242 throw new CacheException(e); 243 } 244 245 if (cache == null) { 246 try { 247 cache = getCache(Class.forName(sClassname)); 248 } catch (ClassNotFoundException e) { 249 throw new CacheException(e); 250 } 251 } 252 253 return cache; 254 } 255 256 263 public GeneralCache getCache(AbstractObject obj) 264 throws CacheException { 265 return getCache(obj.getClass()); 266 } 267 268 275 public GeneralCache getCache(Class clss) throws CacheException { 276 GeneralCache cache = null; 277 String sClassname = clss.getName(); 278 279 try { 280 m_lock.readLock().acquire(); 281 cache = (GeneralCache) m_caches.get(sClassname); 282 m_lock.readLock().release(); 283 284 if (cache == null) { 285 m_lock.writeLock().acquire(); 286 cache = new GeneralCache(this.m_dsi, sClassname); 288 289 try { 290 Object obj = clss.newInstance(); 291 292 if(CacheDependant.class.isAssignableFrom(clss)) { 293 cache.setDependancyAware(true); 294 } 295 296 } catch (InstantiationException e) { 299 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 300 } catch (IllegalAccessException e) { 301 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 302 } 303 304 m_caches.put(sClassname, cache); 306 m_lock.writeLock().release(); 307 } 308 } catch (InterruptedException e) { 309 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 310 throw new CacheException(e); 311 } 312 313 return cache; 314 } 315 316 324 public void removeObjectFromCache(AbstractObject obj) throws CacheException { 325 GeneralCache cache = getCache(obj); 326 327 cache.removeObjectFromCache(String.valueOf(obj.getId())); 328 } 329 330 339 public CachePointer getCachePointer(AbstractObject obj) throws CacheException { 340 CachePointer pointer = null; 341 342 if(obj != null) { 343 GeneralCache cache = getCache(obj.getClass()); 344 345 pointer = cache.getObjectPointer(String.valueOf(obj.getId())); 346 } 347 348 return pointer; 349 } 350 351 357 public List getCacheList() { 358 try { 359 m_lock.readLock().acquire(); 360 } catch (InterruptedException e) { 361 m_logger.log(Level.WARNING, "Couldn't aquire lock",e); 362 } 363 Set keys = m_caches.keySet(); 364 ArrayList vec = new ArrayList(); 365 Iterator iter = keys.iterator(); 366 367 while (iter.hasNext()) { 368 vec.add(iter.next()); 369 } 370 m_lock.readLock().release(); 371 372 return vec; 373 } 374 375 381 public List getCaches() { 382 try { 383 m_lock.readLock().acquire(); 384 } catch (InterruptedException e) { 385 m_logger.log(Level.WARNING, "Couldn't aquire lock",e); 386 } 387 Set keys = m_caches.keySet(); 388 ArrayList vec = new ArrayList(); 389 Iterator iter = keys.iterator(); 390 391 while (iter.hasNext()) { 392 vec.add(this.m_caches.get(iter.next())); 393 } 394 395 m_lock.readLock().release(); 396 397 return vec; 398 } 399 400 403 public String toString() { 404 StringBuffer buffer = new StringBuffer (); 405 List list = getCacheList(); 406 buffer.append("["); 407 408 Iterator iter = list.iterator(); 409 410 while (iter.hasNext()) { 411 412 buffer.append((String ) iter.next()); 413 if (iter.hasNext()) { 414 buffer.append(","); 415 } 416 } 417 418 buffer.append("]"); 419 420 return (buffer.toString()); 421 } 422 } | Popular Tags |