1 package com.jofti.cache.adapter; 2 3 import java.io.Serializable ; 4 import java.util.Iterator ; 5 import java.util.List ; 6 import java.util.Map ; 7 import java.util.Properties ; 8 9 import net.sf.ehcache.Cache; 10 import net.sf.ehcache.CacheException; 11 import net.sf.ehcache.Element; 12 13 import org.apache.commons.logging.Log; 14 import org.apache.commons.logging.LogFactory; 15 16 import com.jofti.api.CacheAccessor; 17 import com.jofti.api.IndexQuery; 18 import com.jofti.cache.CacheListenerAdapter; 19 import com.jofti.cache.BaseAdaptor; 20 import com.jofti.cache.adapter.listener.EhEventListener; 21 import com.jofti.core.INameSpaceAware; 22 import com.jofti.core.IParsedQuery; 23 import com.jofti.core.InternalIndex; 24 import com.jofti.core.QueryId; 25 import com.jofti.core.QueryType; 26 import com.jofti.exception.JoftiException; 27 import com.jofti.util.CompositeComparator; 28 29 49 public class EhCacheListenerAdapter extends BaseAdaptor implements CacheAccessor, CacheListenerAdapter 50 { 51 52 53 private static Log log = LogFactory 54 .getLog(EhCacheListenerAdapter.class); 55 56 57 private String name; 58 59 60 private EhEventListener eventListener = null; 61 62 private net.sf.ehcache.Cache cache; 63 64 65 66 public EhCacheListenerAdapter() 67 { 68 } 69 70 public EhCacheListenerAdapter(Object cache) 71 { 72 this.cache = (net.sf.ehcache.Cache) cache; 73 74 } 75 78 public synchronized void init(Properties properties) throws JoftiException 79 { 80 if (properties != null) { 81 String key = null; 82 for (Iterator it = properties.keySet().iterator(); it.hasNext();) { 83 key = (String ) it.next(); 84 if (MUTABLE_VALUES.equalsIgnoreCase(key)) { 85 checkMutable = Boolean.valueOf( 86 properties.getProperty(key)).booleanValue(); 87 if (log.isInfoEnabled()) { 88 log.info("Mutability checking is set to " 89 + checkMutable); 90 } 91 } 92 if ("file".equalsIgnoreCase(key)) { 93 log.warn("Listener adapters cannot be used to start up a Cache - check docs for Adapter Type"); 94 throw new JoftiException("Listener adapters cannot be used to start up a Cache - check docs for Adapter Type"); 95 } 96 } 97 } 98 99 100 if (cache == null) { 101 throw new JoftiException("Cache cannot be null for listener adapter"); 102 } else { 103 log.info("Index started using cache "+ cache.getName()); 104 } 105 eventListener = new EhEventListener(this); 106 107 } 108 109 110 111 112 113 116 public synchronized void destroy() throws JoftiException 117 { 118 try { 119 if (index != null){ 120 index.removeAll(); 121 } 122 } catch (IllegalStateException e) { 123 throw new JoftiException(e); 124 } 125 } 126 127 130 public String getName() 131 { 132 return name; 133 } 134 135 138 public void setName(String name) 139 { 140 this.name = name; 141 } 142 143 public String toString() 144 { 145 return "EHCache(" + getName() + ')'; 146 } 147 148 149 150 151 156 public void setInternalIndex(InternalIndex index) 157 { 158 this.index = index; 159 160 } 161 162 163 164 167 public void setCacheImpl(Object cache){ 168 EhCacheListenerAdapter.this.cache = (Cache)cache; 169 if (cache != null){ 170 ((Cache)cache).getCacheEventNotificationService().registerListener(eventListener); 171 } 172 173 } 174 175 protected Object getCacheValue(Object key) { 176 try { 177 return cache.get((Serializable ) key).getValue(); 178 } catch (CacheException e) { 179 log.warn(e); 180 } 181 return null; 182 } 183 184 187 public void start() throws JoftiException { 188 if (cache != null){ 189 ((Cache)cache).getCacheEventNotificationService().registerListener(eventListener); 190 try { 191 loadInitialValues(cache); 192 } catch (CacheException ce) { 193 throw new JoftiException(ce); 194 } 195 } 196 197 198 } 199 200 private void loadInitialValues(net.sf.ehcache.Cache cache) 201 throws CacheException, JoftiException { 202 List keys = cache.getKeys(); 203 int size = keys.size(); 204 Iterator it = keys.iterator(); 205 for (int i=0;i<size;i++) { 206 207 Object key = it.next(); 208 Element element = cache.get((Serializable ) key); 209 eventListener.notifyElementPut(cache, element); 211 212 } 213 } 214 215 216 219 public InternalIndex getIndex() { 220 221 return index; 222 } 223 224 227 public Object getCacheImpl() { 228 229 return cache; 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 245 } | Popular Tags |