1 package com.jofti.cache.adapter; 2 3 import java.io.IOException ; 4 import java.io.Serializable ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 import java.util.Map ; 8 import java.util.Properties ; 9 10 import net.sf.ehcache.Cache; 11 import net.sf.ehcache.CacheException; 12 import net.sf.ehcache.CacheManager; 13 import net.sf.ehcache.Element; 14 15 import org.apache.commons.logging.Log; 16 import org.apache.commons.logging.LogFactory; 17 18 import com.jofti.api.IndexQuery; 19 import com.jofti.cache.CacheAdapter; 20 import com.jofti.cache.BaseAdaptor; 21 import com.jofti.cache.adapter.listener.EhEventListener; 22 import com.jofti.core.INameSpaceAware; 23 import com.jofti.core.IParsedQuery; 24 import com.jofti.core.InternalIndex; 25 import com.jofti.core.QueryId; 26 import com.jofti.core.QueryType; 27 import com.jofti.exception.JoftiException; 28 import com.jofti.util.CompositeComparator; 29 30 48 public class EhCacheAdapter extends BaseAdaptor implements CacheAdapter 49 { 50 51 private net.sf.ehcache.CacheManager manager; 52 53 private static Log log = LogFactory 54 .getLog(EhCacheAdapter.class); 55 56 private net.sf.ehcache.Cache cache; 57 58 private String name; 59 60 61 private EhEventListener eventListener =null; 62 63 public EhCacheAdapter() 64 { 65 } 66 67 public EhCacheAdapter(Object cache) 68 { 69 this.cache = (net.sf.ehcache.Cache) cache; 70 71 } 72 73 public void setCacheImpl(Object cache) 74 { 75 this.cache = (net.sf.ehcache.Cache) cache; 76 77 } 78 79 84 public Object get(Object key) 85 { 86 87 try { 88 if (key != null) { 89 Comparable tempKey = (Comparable ) key; 93 synchronized (getCacheLock(key)) { 96 97 Element element = cache.get((Serializable ) tempKey); 99 if (element == null) { 101 return null; 102 } else { 103 107 return element.getValue(); 108 } 109 } 110 111 } else { 112 return null; 113 } 114 } catch (net.sf.ehcache.CacheException e) { 115 log.warn("Unable to retrieve value from cache", e); 116 117 } 118 return null; 119 } 120 121 126 public void put(Object key, Object value) throws JoftiException 127 { 128 129 try{ 130 synchronized (getCacheLock(key)) { 131 132 134 Element element = new Element((Serializable ) key, 135 (Serializable ) value); 136 cache.put(element); 137 138 } 139 } catch (IllegalArgumentException e) { 140 throw new JoftiException(e); 141 } catch (IllegalStateException e) { 142 throw new JoftiException(e); 143 } 144 145 } 146 147 156 public void remove(Object key) throws JoftiException 157 { 158 159 160 try { 161 162 synchronized (getCacheLock(key)) { 163 165 166 cache.remove((Serializable ) key); 167 } 168 169 } catch (ClassCastException e) { 170 throw new JoftiException(e); 171 } catch (IllegalStateException e) { 172 throw new JoftiException(e); 173 } 174 } 175 176 181 public synchronized void removeAll() throws JoftiException 182 { 183 try { 184 185 cache.removeAll(); 186 index.removeAll(); 187 188 } catch (IllegalStateException e) { 189 throw new JoftiException(e); 190 } catch (Exception e) { 191 throw new JoftiException(e); 192 } 193 } 194 195 198 public synchronized void init(Properties properties) throws JoftiException 199 { 200 try { 201 String cacheConfigFile = null; 202 if (properties != null) { 203 String key = null; 204 for (Iterator it = properties.keySet().iterator(); it.hasNext();) { 205 key = (String ) it.next(); 206 if (MUTABLE_VALUES.equalsIgnoreCase(key)) { 207 checkMutable = Boolean.valueOf( 208 properties.getProperty(key)).booleanValue(); 209 if (log.isInfoEnabled()) { 210 log.info("Mutability checking is set to " 211 + checkMutable); 212 } 213 } 214 if ("file".equalsIgnoreCase(key)) { 215 cacheConfigFile = properties.getProperty(key); 216 } 217 } 218 } 219 log.debug("looking up CacheManager for EHCache"); 220 if (manager == null) { 221 log.debug("no CacheManager found obtaining one via static CacheManager methods"); 222 if (cacheConfigFile != null) { 223 log.debug("obtaining CacheManager using config file "+ cacheConfigFile); 224 manager = net.sf.ehcache.CacheManager 225 .create(cacheConfigFile); 226 } else { 227 log.debug("obtaining CacheManager using no config file "); 228 manager = CacheManager.create(); 229 } 230 } 231 log.debug("finished looking up CacheManager for EHCache"); 232 233 if (cache == null) { 234 log.debug("EHCache impl is null - initialising cache"); 235 initCache(); 236 } else { 237 log.debug("EHCache impl is not null - checking if exists in CacheManager"); 239 if (!(manager.cacheExists(cache.getName()))) { 240 log 241 .info("Cache " 242 + cache.getName() 243 + "does not exist in CacheManager - adding new cache"); 244 manager.addCache(cache); 245 } else { 246 log 247 .info("Cache " 248 + cache.getName() 249 + "already exists in CacheManager - not adding new cache"); 250 } 251 } 252 eventListener = new EhEventListener(this); 253 } catch (net.sf.ehcache.CacheException e) { 254 throw new JoftiException(e); 255 } 256 257 } 258 259 private void initCache() throws JoftiException 260 { 261 if (cache == null) { 262 if (manager == null){ 263 throw new JoftiException("No Cache Manager specified unable to initialise cache "+name); 264 } 265 cache = manager.getCache(name); 266 if (cache == null) { 267 log.warn("Unable to find cache named '" + name 268 + "' in EHCacheManager"); 269 String [] names = manager.getCacheNames(); 270 log.warn("Available cache names are:"); 271 for (int i = 0; i < names.length; i++) { 272 log.warn("IndexCache:" + names[i]); 273 } 274 throw new JoftiException("Unable to find cache named '" + name 275 + "' in EHCacheManager"); 276 } 277 } 278 279 } 280 281 284 public synchronized void start() throws JoftiException 285 { 286 if (cache != null){ 287 ((Cache)cache).getCacheEventNotificationService().registerListener(eventListener); 288 289 } 290 try { 291 loadInitialValues(cache); 292 } catch (CacheException ce) { 293 throw new JoftiException(ce); 294 } 295 } 296 297 private void loadInitialValues(net.sf.ehcache.Cache cache) 298 throws CacheException, JoftiException 299 { 300 if (cache == null){ 301 log.info("No initial values to load as Cache is null"); 302 return; 303 } 304 List keys = cache.getKeys(); 305 int size = keys.size(); 306 Iterator it = keys.iterator(); 307 for (int i=0;i<size;i++) { 308 Object key = it.next(); 309 Element element = cache.get((Serializable ) key); 310 if (key instanceof Comparable ) { 311 Comparable tempKey = (Comparable ) key; 312 if (element != null && element.getValue() != null) { 313 index.insert(tempKey, element.getValue()); 315 } 316 } else { 317 log.info("Ignoring value at key:" + key 318 + " as key is not Comparable"); 319 } 320 } 321 } 322 323 326 public synchronized void destroy() throws JoftiException 327 { 328 try { 329 if (manager != null) { 330 manager.removeCache(name); 331 manager.shutdown(); 332 index.removeAll(); 333 } 334 } catch (IllegalStateException e) { 335 throw new JoftiException(e); 336 } 337 } 338 339 342 public String getName() 343 { 344 return name; 345 } 346 347 350 public void setName(String name) 351 { 352 this.name = name; 353 } 354 355 public String toString() 356 { 357 return "EHCache(" + getName() + ')'; 358 } 359 360 361 366 public Object getCacheImpl() 367 { 368 return cache; 369 } 370 371 376 public void setInternalIndex(InternalIndex index) 377 { 378 this.index = index; 379 380 } 381 382 383 384 385 388 protected Object getCacheValue(Object key) { 389 try{ 390 Element res= cache.get((Serializable )key); 391 if (res != null){ 392 return res.getValue(); 393 }else{ 394 log.warn("No Cache value found for indexed key "+key); 395 } 396 } catch (Throwable e){ 397 log.warn(e); 398 } 399 return null; 400 } 401 402 405 public InternalIndex getIndex() { 406 return index; 407 } 408 409 412 public IndexQuery addQuery(String name, IndexQuery query)throws JoftiException { 413 414 return index.getParserManager().addQuery(name, query); 415 } 416 417 420 public IndexQuery getQuery(String name) { 421 422 return index.getParserManager().getQuery(name); 423 } 424 } | Popular Tags |