1 package com.jofti.cache.adapter; 2 3 import java.util.Iterator ; 4 import java.util.Map ; 5 import java.util.Properties ; 6 7 import org.apache.commons.logging.Log; 8 import org.apache.commons.logging.LogFactory; 9 10 import com.jofti.api.CacheAccessor; 11 import com.jofti.api.IndexQuery; 12 import com.jofti.cache.CacheListenerAdapter; 13 import com.jofti.cache.BaseAdaptor; 14 import com.jofti.cache.adapter.listener.OSEventListener; 15 import com.jofti.core.INameSpaceAware; 16 import com.jofti.core.IParsedQuery; 17 import com.jofti.core.InternalIndex; 18 import com.jofti.core.QueryId; 19 import com.jofti.core.QueryType; 20 import com.jofti.exception.JoftiException; 21 import com.jofti.util.CompositeComparator; 22 import com.opensymphony.oscache.base.Cache; 23 import com.opensymphony.oscache.base.NeedsRefreshException; 24 import com.opensymphony.oscache.base.events.CacheEntryEventListener; 25 26 51 52 53 public class OSCacheListenerAdapter extends BaseAdaptor implements CacheAccessor, CacheListenerAdapter { 54 55 private Cache cache = null; 56 57 58 private static Log log = LogFactory.getLog(OSCacheListenerAdapter.class); 59 60 private String name; 61 62 63 64 private int expiryTime =3600; 66 private OSEventListener eventListener =null; 67 70 public int getExpiryTime() { 71 return expiryTime; 72 } 73 75 public OSCacheListenerAdapter() { 76 } 77 78 public OSCacheListenerAdapter(Object cache) { 79 if (!(cache instanceof Cache)){ 80 throw new RuntimeException ("Cache is not instance of "+ Cache.class.getName()); 81 } 82 this.cache = (Cache)cache; 83 } 84 85 86 87 88 91 public synchronized void init(Properties properties) throws JoftiException { 92 try { 93 94 if (properties != null){ 95 String key =null; 96 for (Iterator it = properties.keySet().iterator();it.hasNext();){ 97 key = (String )it.next(); 98 if (MUTABLE_VALUES.equalsIgnoreCase(key)){ 99 checkMutable = Boolean.valueOf(properties.getProperty(key)).booleanValue(); 100 if (log.isInfoEnabled()){ 101 log.info("Mutability checking is set to " + checkMutable); 102 } 103 } 104 105 } 106 if ("file".equalsIgnoreCase(key)) { 107 log.warn("Listener adapters cannot be used to start up a Cache - please use addCache method"); 108 throw new JoftiException("Listener adapters cannot be used to start up a Cache - please use addCache method"); 109 } 110 } 111 if (cache == null) { 112 throw new JoftiException("Cache cannot be null for listener adapter"); 113 } else { 114 log.info("Index started using cache "+ cache); 115 } 116 if (properties != null){ 118 String expiryParam = properties.getProperty("expiryTime"); 119 if(expiryParam != null){ 120 try { 121 expiryTime = Integer.parseInt(expiryParam); 122 }catch (NumberFormatException nfe){ 123 log.error("Unable to set expiry time - using default of " + expiryTime,nfe); 124 } 125 } 126 } 127 eventListener = new OSEventListener(this); 128 129 130 } catch (Exception e){ 131 throw new JoftiException(e); 132 } 133 134 135 } 136 137 140 public void destroy() throws JoftiException { 141 142 index.removeAll(); 143 144 } 145 146 147 148 public String getName() { 149 return name; 150 } 151 152 public void setName(String name) { 153 this.name = name; 154 } 155 156 public String toString() { 157 return "OSCache(" + getName() + ')'; 158 } 159 160 161 162 165 public Object getCacheImpl() { 166 return cache; 167 } 168 169 170 173 public void setInternalIndex(InternalIndex index) { 174 this.index = index; 175 176 } 177 178 181 public void start() throws JoftiException { 182 183 if (cache != null){ 184 cache.addCacheEventListener(eventListener,CacheEntryEventListener.class); 185 } 186 187 188 } 189 190 191 192 193 194 protected Object getCacheValue(Object key) { 195 Object obj=null; 196 try { 197 obj =cache.getFromCache((String )key, expiryTime); 198 }catch (NeedsRefreshException nre){ 199 cache.cancelUpdate((String )key); 201 cache.flushEntry((String )key); 203 204 } 205 return obj; 206 } 207 208 public void warnOutOfDate(Object key){ 209 if (log.isWarnEnabled()) { 210 log 211 .warn("Index and cache have become out of date for key - Probable cause element expiry - flushing entry for '" 212 + key +"'"); 213 } 214 } 215 216 219 public InternalIndex getIndex() { 220 return index; 221 } 222 223 226 public void setCacheImpl(Object cache) { 227 this.cache = (Cache)cache; 228 this.cache.addCacheEventListener(eventListener,CacheEntryEventListener.class); 229 230 } 231 232 public IndexQuery addQuery(String name, IndexQuery query)throws JoftiException { 233 234 return index.getParserManager().addQuery(name, query); 235 } 236 237 240 public IndexQuery getQuery(String name) { 241 242 return index.getParserManager().getQuery(name); 243 } 244 } | Popular Tags |