1 5 package com.opensymphony.oscache.plugins.clustersupport; 6 7 import com.opensymphony.oscache.base.*; 8 import com.opensymphony.oscache.base.events.*; 9 10 import org.apache.commons.logging.Log; 11 import org.apache.commons.logging.LogFactory; 12 13 import java.util.Date ; 14 15 23 public abstract class AbstractBroadcastingListener implements CacheEntryEventListener, LifecycleAware { 24 private final static Log log = LogFactory.getLog(AbstractBroadcastingListener.class); 25 26 30 protected static final String CLUSTER_ORIGIN = "CLUSTER"; 31 protected Cache cache = null; 32 33 public AbstractBroadcastingListener() { 34 if (log.isInfoEnabled()) { 35 log.info("AbstractBroadcastingListener registered"); 36 } 37 } 38 39 43 public void cacheEntryFlushed(CacheEntryEvent event) { 44 if (!Cache.NESTED_EVENT.equals(event.getOrigin()) && !CLUSTER_ORIGIN.equals(event.getOrigin())) { 45 if (log.isDebugEnabled()) { 46 log.debug("cacheEntryFlushed called (" + event + ")"); 47 } 48 49 sendNotification(new ClusterNotification(ClusterNotification.FLUSH_KEY, event.getKey())); 50 } 51 } 52 53 58 public void cacheGroupFlushed(CacheGroupEvent event) { 59 if (!Cache.NESTED_EVENT.equals(event.getOrigin()) && !CLUSTER_ORIGIN.equals(event.getOrigin())) { 60 if (log.isDebugEnabled()) { 61 log.debug("cacheGroupFushed called (" + event + ")"); 62 } 63 64 sendNotification(new ClusterNotification(ClusterNotification.FLUSH_GROUP, event.getGroup())); 65 } 66 } 67 68 public void cachePatternFlushed(CachePatternEvent event) { 69 if (!Cache.NESTED_EVENT.equals(event.getOrigin()) && !CLUSTER_ORIGIN.equals(event.getOrigin())) { 70 if (log.isDebugEnabled()) { 71 log.debug("cachePatternFushed called (" + event + ")"); 72 } 73 74 sendNotification(new ClusterNotification(ClusterNotification.FLUSH_PATTERN, event.getPattern())); 75 } 76 } 77 78 public void cacheFlushed(CachewideEvent event) { 79 if (!Cache.NESTED_EVENT.equals(event.getOrigin()) && !CLUSTER_ORIGIN.equals(event.getOrigin())) { 80 if (log.isDebugEnabled()) { 81 log.debug("cacheFushed called (" + event + ")"); 82 } 83 84 sendNotification(new ClusterNotification(ClusterNotification.FLUSH_CACHE, event.getDate())); 85 } 86 } 87 88 public void cacheEntryAdded(CacheEntryEvent event) { 92 } 93 94 public void cacheEntryRemoved(CacheEntryEvent event) { 95 } 96 97 public void cacheEntryUpdated(CacheEntryEvent event) { 98 } 99 100 public void cacheGroupAdded(CacheGroupEvent event) { 101 } 102 103 public void cacheGroupEntryAdded(CacheGroupEvent event) { 104 } 105 106 public void cacheGroupEntryRemoved(CacheGroupEvent event) { 107 } 108 109 public void cacheGroupRemoved(CacheGroupEvent event) { 110 } 111 112 public void cacheGroupUpdated(CacheGroupEvent event) { 113 } 114 115 129 public void initialize(Cache cache, Config config) throws InitializationException { 130 this.cache = cache; 131 } 132 133 140 public void handleClusterNotification(ClusterNotification message) { 141 if (cache == null) { 142 log.warn("A cluster notification (" + message + ") was received, but no cache is registered on this machine. Notification ignored."); 143 144 return; 145 } 146 147 if (log.isInfoEnabled()) { 148 log.info("Cluster notification (" + message + ") was received."); 149 } 150 151 switch (message.getType()) { 152 case ClusterNotification.FLUSH_KEY: 153 cache.flushEntry((String ) message.getData(), CLUSTER_ORIGIN); 154 break; 155 case ClusterNotification.FLUSH_GROUP: 156 cache.flushGroup((String ) message.getData(), CLUSTER_ORIGIN); 157 break; 158 case ClusterNotification.FLUSH_PATTERN: 159 cache.flushPattern((String ) message.getData(), CLUSTER_ORIGIN); 160 break; 161 case ClusterNotification.FLUSH_CACHE: 162 cache.flushAll((Date ) message.getData(), CLUSTER_ORIGIN); 163 break; 164 default: 165 log.error("The cluster notification (" + message + ") is of an unknown type. Notification ignored."); 166 } 167 } 168 169 176 abstract protected void sendNotification(ClusterNotification message); 177 } 178 | Popular Tags |