Commits

la...@81dbac14-341a-0410-aa85-cbcd92e6f43e  committed 41be739

CACHE-284 Cache.dispatchCacheEntryEvent and Cache.addEventListener

  • Participants
  • Parent commits 667b579

Comments (0)

Files changed (7)

File src/java/com/opensymphony/oscache/base/AbstractCacheAdministrator.java

                 }
 
                 if (listeners[i] instanceof CacheEntryEventListener) {
-                    cache.addCacheEventListener(listeners[i], CacheEntryEventListener.class);
-                }
-
-                if (listeners[i] instanceof CacheMapAccessEventListener) {
-                    cache.addCacheEventListener(listeners[i], CacheMapAccessEventListener.class);
+                    cache.addCacheEventListener(listeners[i]);
+                } else if (listeners[i] instanceof CacheMapAccessEventListener) {
+                    cache.addCacheEventListener(listeners[i]);
                 }
             }
         }

File src/java/com/opensymphony/oscache/base/Cache.java

      * one of the child interfaces of the {@link CacheEventListener} interface.
      *
      * @param listener  The object that listens to events.
+     * @since 2.4
+     */
+    public void addCacheEventListener(CacheEventListener listener) {
+        // listenerList.add(CacheEventListener.class, listener);
+        listenerList.add(listener.getClass(), listener);
+    }
+    
+    /**
+     * Register a listener for Cache events. The listener must implement
+     * one of the child interfaces of the {@link CacheEventListener} interface.
+     *
+     * @param listener  The object that listens to events.
+     * @param clazz the type of the listener to be added
+     * @deprecated use {@link #addCacheEventListener(CacheEventListener)}
      */
     public void addCacheEventListener(CacheEventListener listener, Class clazz) {
         if (CacheEventListener.class.isAssignableFrom(clazz)) {
      * Unregister a listener for Cache events.
      *
      * @param listener  The object that currently listens to events.
+     * @param clazz  The registrated class of listening object.
+     * @deprecated use instead {@link #removeCacheEventListener(CacheEventListener)}
      */
     public void removeCacheEventListener(CacheEventListener listener, Class clazz) {
         listenerList.remove(clazz, listener);
     }
 
     /**
+     * Unregister a listener for Cache events.
+     *
+     * @param listener  The object that currently listens to events.
+     * @since 2.4
+     */
+    public void removeCacheEventListener(CacheEventListener listener) {
+        // listenerList.remove(CacheEventListener.class, listener);
+        listenerList.remove(listener.getClass(), listener);
+    }
+
+    /**
      * Get an entry from this cache or create one if it doesn't exist.
      *
      * @param key    The key of the cache entry
         // Process the listeners last to first, notifying
         // those that are interested in this event
         for (int i = listeners.length - 2; i >= 0; i -= 2) {
-            if (listeners[i] == CacheEntryEventListener.class) {
+            if (listeners[i+1] instanceof CacheEntryEventListener) {
+                CacheEntryEventListener listener = (CacheEntryEventListener) listeners[i+1];
                 if (eventType.equals(CacheEntryEventType.ENTRY_ADDED)) {
-                    ((CacheEntryEventListener) listeners[i + 1]).cacheEntryAdded(event);
+                    listener.cacheEntryAdded(event);
                 } else if (eventType.equals(CacheEntryEventType.ENTRY_UPDATED)) {
-                    ((CacheEntryEventListener) listeners[i + 1]).cacheEntryUpdated(event);
+                    listener.cacheEntryUpdated(event);
                 } else if (eventType.equals(CacheEntryEventType.ENTRY_FLUSHED)) {
-                    ((CacheEntryEventListener) listeners[i + 1]).cacheEntryFlushed(event);
+                    listener.cacheEntryFlushed(event);
                 } else if (eventType.equals(CacheEntryEventType.ENTRY_REMOVED)) {
-                    ((CacheEntryEventListener) listeners[i + 1]).cacheEntryRemoved(event);
+                    listener.cacheEntryRemoved(event);
                 }
             }
         }
         // Process the listeners last to first, notifying
         // those that are interested in this event
         for (int i = listeners.length - 2; i >= 0; i -= 2) {
-            if (listeners[i] == CacheEntryEventListener.class) {
+            if (listeners[i+1] instanceof CacheEntryEventListener) {
+                CacheEntryEventListener listener = (CacheEntryEventListener) listeners[i + 1];
                 if (eventType.equals(CacheEntryEventType.GROUP_FLUSHED)) {
-                    ((CacheEntryEventListener) listeners[i + 1]).cacheGroupFlushed(event);
+                    listener.cacheGroupFlushed(event);
                 }
             }
         }
         // Process the listeners last to first, notifying
         // those that are interested in this event
         for (int i = listeners.length - 2; i >= 0; i -= 2) {
-            if (listeners[i] == CacheMapAccessEventListener.class) {
-                ((CacheMapAccessEventListener) listeners[i + 1]).accessed(event);
+            if (listeners[i+1] instanceof CacheMapAccessEventListener) {
+                CacheMapAccessEventListener listener = (CacheMapAccessEventListener) listeners[i + 1];
+                listener.accessed(event);
             }
         }
     }
         // Process the listeners last to first, notifying
         // those that are interested in this event
         for (int i = listeners.length - 2; i >= 0; i -= 2) {
-            if (listeners[i] == CacheEntryEventListener.class) {
+            if (listeners[i+1] instanceof CacheEntryEventListener) {
                 if (eventType.equals(CacheEntryEventType.PATTERN_FLUSHED)) {
-                    ((CacheEntryEventListener) listeners[i + 1]).cachePatternFlushed(event);
+                    CacheEntryEventListener listener = (CacheEntryEventListener) listeners[i+1];
+                    listener.cachePatternFlushed(event);
                 }
             }
         }
         // Process the listeners last to first, notifying
         // those that are interested in this event
         for (int i = listeners.length - 2; i >= 0; i -= 2) {
-            if (listeners[i] == CacheEntryEventListener.class) {
+            if (listeners[i] instanceof CacheEntryEventListener) {
                 if (eventType.equals(CachewideEventType.CACHE_FLUSHED)) {
-                    ((CacheEntryEventListener) listeners[i + 1]).cacheFlushed(event);
+                    CacheEntryEventListener listener = (CacheEntryEventListener) listeners[i+1];
+                    listener.cacheFlushed(event);
                 }
             }
         }

File src/java/com/opensymphony/oscache/web/ServletCacheAdministrator.java

 
             for (int i = 0; i < listeners.length; i++) {
                 if (listeners[i] instanceof ScopeEventListener) {
-                    newCache.addCacheEventListener(listeners[i], ScopeEventListener.class);
+                    newCache.addCacheEventListener(listeners[i]);
                 }
             }
         }
         // Process the listeners last to first, notifying
         // those that are interested in this event
         for (int i = listeners.length - 2; i >= 0; i -= 2) {
-            if (listeners[i] == ScopeEventListener.class) {
+            if (listeners[i+1] instanceof ScopeEventListener) {
                 ((ScopeEventListener) listeners[i + 1]).scopeFlushed(event);
             }
         }

File src/test/java/com/opensymphony/oscache/base/algorithm/TestAbstractCache.java

         
         CacheEntry entry2 = new CacheEntry(KEY+"2", null);
         entry.setContent(CONTENT+"2");
-        getCache().put(entry.getKey(), entry);
+        getCache().put(entry2.getKey(), entry2);
 
         Enumeration enumeration = getCache().elements();
         assertTrue(enumeration.hasMoreElements());

File src/test/java/com/opensymphony/oscache/general/TestGeneralCacheAdministrator.java

 import java.util.Date;
 
 import com.opensymphony.oscache.base.*;
-import com.opensymphony.oscache.base.events.CacheEntryEventListener;
-import com.opensymphony.oscache.base.events.CacheMapAccessEventListener;
 import com.opensymphony.oscache.extra.CacheEntryEventListenerImpl;
 import com.opensymphony.oscache.extra.CacheMapAccessEventListenerImpl;
 
         cacheMapAccessEventListener = new CacheMapAccessEventListenerImpl();
 
         // Register the listeners on the cache map
-        admin.getCache().addCacheEventListener(cacheEntryEventListener, CacheEntryEventListener.class);
-        admin.getCache().addCacheEventListener(cacheMapAccessEventListener, CacheMapAccessEventListener.class);
+        admin.getCache().addCacheEventListener(cacheEntryEventListener);
+        admin.getCache().addCacheEventListener(cacheMapAccessEventListener);
     }
 
     /**
 
     protected void tearDown() throws Exception {
         if (admin != null) {
-            admin.getCache().removeCacheEventListener(cacheEntryEventListener, CacheEntryEventListener.class);
-            admin.getCache().removeCacheEventListener(cacheMapAccessEventListener, CacheMapAccessEventListener.class);
+            admin.getCache().removeCacheEventListener(cacheEntryEventListener);
+            admin.getCache().removeCacheEventListener(cacheMapAccessEventListener);
         }
     }
 

File src/test/java/com/opensymphony/oscache/plugins/clustersupport/BaseTestBroadcastingListener.java

 package com.opensymphony.oscache.plugins.clustersupport;
 
 import com.opensymphony.oscache.base.*;
-import com.opensymphony.oscache.base.events.CacheEntryEventListener;
 
 import junit.framework.TestCase;
 
                 fail(e.getMessage());
             }
 
-            cache.addCacheEventListener(listener, CacheEntryEventListener.class);
+            cache.addCacheEventListener(listener);
         }
     }
 

File src/test/java/com/opensymphony/oscache/plugins/clustersupport/ListenForClusterTests.java

 import com.opensymphony.oscache.base.Config;
 import com.opensymphony.oscache.base.FinalizationException;
 import com.opensymphony.oscache.base.InitializationException;
-import com.opensymphony.oscache.base.events.CacheEntryEventListener;
 
 import java.util.ArrayList;
 import java.util.Iterator;
             testcase = new TestJavaGroupsBroadcastingListener("JavaGroups");
             listener = testcase.getListener();
             listener.initialize(cache, testcase.getConfig());
-            cache.addCacheEventListener(listener, CacheEntryEventListener.class);
+            cache.addCacheEventListener(listener);
             listeners.add(listener);
         } catch (InitializationException e) {
             System.out.println("The JavaGroups listener could not be initialized: " + e);
             config.set("cache.cluster.jms.node.name", "cacheNode2");
 
             listener.initialize(cache, config);
-            cache.addCacheEventListener(listener, CacheEntryEventListener.class);
+            cache.addCacheEventListener(listener);
             listeners.add(listener);
         } catch (InitializationException e) {
             System.out.println("The JMS listener could not be initialized: " + e);