| 1 package com.jofti.cache.adapter; 2 3 import java.io.Serializable ; 4 import java.util.Comparator ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 import java.util.Map ; 8 import java.util.Properties ; 9 import java.util.Set ; 10 11 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.CoherenceEventListener; 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 import com.tangosol.net.NamedCache; 29 import com.tangosol.util.MapEvent; 30 31 51 public class CoherenceListenerAdapter extends BaseAdaptor implements CacheAccessor, CacheListenerAdapter 52 { 53 54 55 private static Log log = LogFactory 56 .getLog(CoherenceListenerAdapter.class); 57 58 59 private String name; 60 61 public InternalIndex index; 62 63 private CoherenceEventListener eventListener = null; 64 65 66 private NamedCache cache =null; 67 68 69 public CoherenceListenerAdapter() 70 { 71 } 72 73 public CoherenceListenerAdapter(Object cache) 74 { 75 this.cache = (NamedCache) cache; 76 77 } 78 81 public synchronized void init(Properties properties) throws JoftiException 82 { 83 if (properties != null) { 84 String key = null; 85 for (Iterator it = properties.keySet().iterator(); it.hasNext();) { 86 key = (String ) it.next(); 87 if (MUTABLE_VALUES.equalsIgnoreCase(key)) { 88 checkMutable = Boolean.valueOf( 89 properties.getProperty(key)).booleanValue(); 90 if (log.isInfoEnabled()) { 91 log.info("Mutability checking is set to " 92 + checkMutable); 93 } 94 } 95 if ("file".equalsIgnoreCase(key)) { 96 log.warn("Listener adapters cannot be used to start up a Cache - check docs for Adapter Type"); 97 throw new JoftiException("Listener adapters cannot be used to start up a Cache - check docs for Adapter Type"); 98 } 99 } 100 } 101 102 103 if (cache == null) { 104 throw new JoftiException("Cache cannot be null for listener adapter"); 105 } else { 106 log.info("Index started using cache "+ cache.getCacheName()); 107 } 108 eventListener = new CoherenceEventListener(this); 109 110 } 111 112 113 114 115 116 119 public synchronized void destroy() throws JoftiException 120 { 121 try { 122 cache.removeMapListener(eventListener); 123 if (index != null){ 124 index.removeAll(); 125 } 126 } catch (IllegalStateException e) { 127 throw new JoftiException(e); 128 } 129 } 130 131 134 public String getName() 135 { 136 return name; 137 } 138 139 142 public void setName(String name) 143 { 144 this.name = name; 145 } 146 147 public String toString() 148 { 149 return "CoherenceCache(" + getName() + ')'; 150 } 151 152 153 154 155 160 public void setInternalIndex(InternalIndex index) 161 { 162 this.index = index; 163 164 } 165 166 169 public Map query(IndexQuery query) throws JoftiException 170 { 171 172 Map temp = null; 173 174 query = processQuery(query,index.getParserManager()); 175 176 acquireQueryLock(); 177 try { 178 179 temp=index.query(query); 180 } finally { 181 releaseQueryLock(); 182 } 183 return getCacheValues(temp, (IParsedQuery)query,index.getIntrospector()); 184 185 } 186 187 188 189 192 protected Object checkMutable(Object key, Object result) 193 { 194 try { 195 Map cacheObjectValues = index.getIntrospector().getAttributeValues( 197 result); 198 199 Map indexObjectValues = index.getAttributesByKey(decorateKey( key)); 200 201 if (cacheObjectValues.equals(indexObjectValues)) { 202 return result; 203 } else { 204 if (log.isDebugEnabled()) { 205 log.debug("Object under Key " + key 206 + " - attributes changed without re-insert"); 207 } 208 } 209 211 } catch (JoftiException e) { 212 log.warn("Error checking mutability", e); 213 } 214 215 return null; 216 } 217 218 221 public void setCacheImpl(Object cache){ 222 CoherenceListenerAdapter.this.cache = (NamedCache)cache; 223 if (this.cache != null){ 224 this.cache.addMapListener(eventListener); 225 } 226 227 } 228 229 protected Object getCacheValue(Object key) { 230 231 return cache.get(key); 232 233 } 234 235 238 public void start() throws JoftiException { 239 if (cache != null){ 240 cache.addMapListener(eventListener); 241 242 loadInitialValues(cache); 243 244 245 246 } 247 248 249 } 250 251 private void loadInitialValues(NamedCache cache) 252 throws JoftiException { 253 Set entries = cache.entrySet(); 254 int size = entries.size(); 255 Iterator it = entries.iterator(); 256 for (int i=0;i<size;i++) { 257 Map.Entry entry = (Map.Entry )it.next(); 258 259 eventListener.entryInserted(new MapEvent(cache,1,entry.getKey(),entry.getValue(),entry.getValue())); 260 261 } 262 } 263 264 265 268 public InternalIndex getIndex() { 269 270 return index; 271 } 272 273 276 public Object getCacheImpl() { 277 278 return cache; 279 } 280 281 public IndexQuery addQuery(String name, IndexQuery query)throws JoftiException { 282 283 return index.getParserManager().addQuery(name, query); 284 } 285 286 289 public IndexQuery getQuery(String name) { 290 291 return index.getParserManager().getQuery(name); 292 } 293 294 } | Popular Tags |