1 package com.jofti.cache.adapter; 2 3 import java.util.Iterator ; 4 import java.util.Properties ; 5 6 import net.sf.ehcache.CacheException; 7 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 11 import com.jofti.api.IndexQuery; 12 import com.jofti.cache.BaseAdaptor; 13 import com.jofti.cache.CacheAdapter; 14 import com.jofti.cache.adapter.listener.OSEventListener; 15 import com.jofti.core.InternalIndex; 16 import com.jofti.exception.JoftiException; 17 import com.jofti.util.PropertyLoader; 18 import com.opensymphony.oscache.base.NeedsRefreshException; 19 import com.opensymphony.oscache.base.events.CacheEntryEventListener; 20 import com.opensymphony.oscache.general.GeneralCacheAdministrator; 21 22 45 46 47 public class OSCacheAdapter extends BaseAdaptor implements CacheAdapter { 48 49 private GeneralCacheAdministrator admin = null; 50 51 52 private static Log log = LogFactory.getLog(OSCacheAdapter.class); 53 54 private String name; 55 56 57 58 private int expiryTime =3600; 60 private OSEventListener eventListener =null; 61 64 public int getExpiryTime() { 65 return expiryTime; 66 } 67 70 public void setExpiryTime(int expiryTime) { 71 this.expiryTime = expiryTime; 72 } 73 public OSCacheAdapter() { 74 } 75 76 public OSCacheAdapter(Object cache) { 77 if (! (cache instanceof GeneralCacheAdministrator) ){ 78 throw new RuntimeException ("OSCacheAdapter requires GeneralCacheAdministrator "); 79 } 80 this.admin = (GeneralCacheAdministrator)cache; 81 } 82 83 public void setCacheImpl(Object cache){ 84 this.admin = (GeneralCacheAdministrator)cache; 85 86 } 87 88 91 public Object get(Object key) { 92 if (key != null && (!(key instanceof String ))) { 93 log.warn("key value for OSCache must by of type String " + key); 94 return null; 95 } 96 Object obj = null; 97 synchronized (getCacheLock(key)) { 98 try { 99 obj = admin.getFromCache((String ) key, expiryTime); 100 } catch (NeedsRefreshException nre) { 101 admin.cancelUpdate((String ) key); 104 admin.flushEntry((String ) key); 106 107 } 108 } 109 return obj; 112 } 113 114 115 116 119 public void put(Object key, Object value) throws JoftiException { 120 121 if (key != null && (!(key instanceof String ))) { 122 throw new JoftiException( 123 "key value for OSCache must by of type String " + key); 124 } 125 126 synchronized (getCacheLock(key)) { 129 130 admin.putInCache((String ) key, value); 132 133 } 134 135 } 136 137 146 public void remove(Object key) throws JoftiException{ 147 148 if (key != null && (!(key instanceof String ))){ 150 throw new JoftiException("key value for OSCache must by of type String "+ key); 151 } 152 admin.flushEntry((String )key); 153 154 } 155 156 161 public void removeAll() throws JoftiException{ 162 admin.flushAll(); 164 } 165 166 169 public synchronized void init(Properties properties) throws JoftiException { 170 try { 171 172 173 174 String cacheConfigFile = null; 175 if (properties != null){ 176 String key =null; 177 for (Iterator it = properties.keySet().iterator();it.hasNext();){ 178 key = (String )it.next(); 179 if (MUTABLE_VALUES.equalsIgnoreCase(key)){ 180 checkMutable = Boolean.valueOf(properties.getProperty(key)).booleanValue(); 181 if (log.isInfoEnabled()){ 182 log.info("Mutability checking is set to " + checkMutable); 183 } 184 } 185 if ("file".equalsIgnoreCase(key)){ 186 cacheConfigFile = properties.getProperty(key); 187 } 188 } 189 } 190 log.debug("looking up GeneralCacheAdministrator"); 191 if (admin == null){ 192 193 if (cacheConfigFile == null){ 194 log.debug("not found - creating default GeneralCacheAdministrator"); 195 admin = new GeneralCacheAdministrator(); 196 }else{ 197 log.debug("not found - creating default GeneralCacheAdministrator"); 199 Properties props =null; 200 try { 201 props= PropertyLoader.loadProperties(cacheConfigFile); 202 } catch (Throwable t){ 203 log.warn("Unable to load config file " + cacheConfigFile + " from classpath"); 204 } 205 if (props == null){ 206 throw new JoftiException("Unable to load config file " + cacheConfigFile + " from classpath"); 207 } 208 admin = new GeneralCacheAdministrator(props); 209 } 210 }else{ 211 log.debug("using supplied GeneralCacheAdministrator"); 212 } 213 if (properties != null){ 214 String expiryParam = properties.getProperty("expiryTime"); 215 if(expiryParam != null){ 216 try { 217 expiryTime = Integer.parseInt(expiryParam); 218 }catch (NumberFormatException nfe){ 219 log.error("Unable to set expiry time - using default of " + expiryTime,nfe); 220 } 221 } 222 } 223 224 225 eventListener = new OSEventListener(this); 226 227 admin.getCache().addCacheEventListener(eventListener,CacheEntryEventListener.class); 228 229 } catch (Exception e){ 230 throw new JoftiException(e); 231 } 232 233 234 } 235 236 239 public void destroy() throws JoftiException { 240 if (admin != null) { 241 admin.destroy(); 242 243 } 244 if (index != null){ 245 index.removeAll(); 246 } 247 248 } 249 250 251 252 public String getName() { 253 return name; 254 } 255 256 public void setName(String name) { 257 this.name = name; 258 } 259 260 public String toString() { 261 return "OSCache(" + getName() + ')'; 262 } 263 264 265 266 269 public Object getCacheImpl() { 270 return admin; 271 } 272 273 274 275 278 public void setInternalIndex(InternalIndex index) { 279 this.index = index; 280 281 } 282 283 286 public void start() throws JoftiException { 287 288 290 291 } 292 293 294 295 protected Object getCacheValue(Object key) { 296 return get(key); 297 } 298 301 public InternalIndex getIndex() { 302 return index; 303 } 304 305 public IndexQuery addQuery(String name, IndexQuery query)throws JoftiException { 306 307 return index.getParserManager().addQuery(name, query); 308 } 309 310 313 public IndexQuery getQuery(String name) { 314 315 return index.getParserManager().getQuery(name); 316 } 317 } | Popular Tags |