[jboss-cvs] JBossCache/src/org/jboss/cache/notifications ...

Manik Surtani msurtani at jboss.com
Wed Aug 16 06:52:50 EDT 2006


  User: msurtani
  Date: 06/08/16 06:52:50

  Added:       src/org/jboss/cache/notifications  Notifier.java
  Log:
  fixed simple notification events
  
  Revision  Changes    Path
  1.1      date: 2006/08/16 10:52:50;  author: msurtani;  state: Exp;JBossCache/src/org/jboss/cache/notifications/Notifier.java
  
  Index: Notifier.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.cache.notifications;
  
  import EDU.oswego.cs.dl.util.concurrent.CopyOnWriteArraySet;
  import org.jboss.cache.CacheListener;
  import org.jboss.cache.CacheSPI;
  import org.jboss.cache.Fqn;
  import org.jboss.cache.InvocationContext;
  import org.jgroups.View;
  
  import java.util.Collections;
  import java.util.Map;
  import java.util.Set;
  
  /**
   * Helper class that handles all notifications to registered listeners.
   *
   * @author <a href="mailto:manik at jboss.org">Manik Surtani (manik at jboss.org)</a>
   */
  public class Notifier
  {
  
      // calling iterator on a Concurrent Set is expensive due to synchronization - same problem
      // with calling isEmpty so hasListeners is an optimization to indicate whether or not listeners
      // is empty
      //
      private boolean hasListeners = false;
  
      // store this seperately from other listeners to avoid concurrency penalty of
      // iterating through Concurrent Set - eviction listener is always there (or almost always)
      // and there are less frequently other listeners so optimization is justified
      private CacheListener evictionPolicyListener;
  
      private Set<CacheListener> listeners = new CopyOnWriteArraySet();
  
  
      /**
       * Sets an eviction policy listener.
       * @param l
       */
      public void setEvictionPolicyListener(CacheListener l)
      {
          evictionPolicyListener = l;
      }
  
      /**
       * Adds a cache listener to the list of cache listeners registered.
       * @param l
       */
      public void addCacheListener(CacheListener l)
      {
          synchronized (listeners)
          {
              listeners.add(l);
              hasListeners = true;
          }
      }
  
      /**
       * Removes a cache listener from the list of cache listeners registered.
       * @param l
       */
      public void removeCacheListener(CacheListener l)
      {
          synchronized (listeners)
          {
              listeners.remove(l);
              hasListeners = !listeners.isEmpty();
          }
      }
  
      /**
       * Removes all listeners from the notifier, including the evictionPolicyListener.
       */
      public void removeAllCacheListeners()
      {
          listeners.clear();
          hasListeners = false;
      }
  
      /**
       * Retrieves an (unmodifiable) set of cache listeners registered.
       */
      public Set<CacheListener> getCacheListeners()
      {
          return Collections.unmodifiableSet(listeners);
      }
  
      /**
       * Notifies all registered listeners of a nodeCreated event.
       * @param fqn
       * @param pre
       */
      public void notifyNodeCreated(Fqn fqn, boolean pre)
      {
  
          boolean originLocal = InvocationContext.getCurrent().isOriginLocal();
          if (evictionPolicyListener != null)
          {
              evictionPolicyListener.nodeCreated(fqn, pre, originLocal);
          }
          if (hasListeners)
          {
              for (CacheListener listener : listeners)
              {
                  listener.nodeCreated(fqn, pre, originLocal);
              }
          }
      }
  
      /**
       * Notifies all registered listeners of a nodeModified event.
       * @param fqn
       * @param pre
       * @param data
       */
      public void notifyNodeModified(Fqn fqn, boolean pre, Map data)
      {
          boolean originLocal = InvocationContext.getCurrent().isOriginLocal();
          if (evictionPolicyListener != null)
          {
              evictionPolicyListener.nodeModified(fqn, pre, originLocal, data);
          }
          if (hasListeners)
          {
              for (CacheListener listener : listeners)
              {
                  listener.nodeModified(fqn, pre, originLocal, data);
              }
          }
      }
  
      /**
       * Notifies all registered listeners of a nodeRemoved event.
       * @param fqn
       * @param pre
       * @param data
       */
      public void notifyNodeRemoved(Fqn fqn, boolean pre, Map data)
      {
          boolean originLocal = InvocationContext.getCurrent().isOriginLocal();
          if (evictionPolicyListener != null)
          {
              evictionPolicyListener.nodeRemoved(fqn, pre, originLocal, data);
          }
          if (hasListeners)
          {
              for (CacheListener listener : listeners)
              {
                  listener.nodeRemoved(fqn, pre, originLocal, data);
              }
          }
      }
  
      /**
       * Notifies all registered listeners of a nodeVisited event.
       * @param fqn
       * @param pre
       */
      public void notifyNodeVisited(Fqn fqn, boolean pre)
      {
          if (evictionPolicyListener != null)
          {
              evictionPolicyListener.nodeVisited(fqn, pre);
          }
          if (hasListeners)
          {
              for (CacheListener listener : listeners)
              {
                  listener.nodeVisited(fqn, pre);
              }
          }
      }
  
      /**
       * Notifies all registered listeners of a nodeEvicted event.
       * @param fqn
       * @param pre
       */
      public void notifyNodeEvicted(Fqn fqn, boolean pre)
      {
          boolean originLocal = InvocationContext.getCurrent().isOriginLocal();
          if (evictionPolicyListener != null)
          {
              evictionPolicyListener.nodeEvicted(fqn, pre, originLocal);
          }
          if (hasListeners)
          {
              for (CacheListener listener : listeners)
              {
                  listener.nodeEvicted(fqn, pre, originLocal);
              }
          }
      }
  
      /**
       * Notifies all registered listeners of a nodeLoaded event.
       * @param fqn
       * @param pre
       * @param data
       */
      public void notifyNodeLoaded(Fqn fqn, boolean pre, Map data)
      {
          if (evictionPolicyListener != null)
          {
              evictionPolicyListener.nodeLoaded(fqn, pre, data);
          }
          if (hasListeners)
          {
              for (CacheListener listener : listeners)
              {
                  listener.nodeLoaded(fqn, pre, data);
              }
          }
  
      }
  
      /**
       * Notifies all registered listeners of a nodeActivated event.
       * @param fqn
       * @param pre
       */
      public void notifyNodeActivated(Fqn fqn, boolean pre)
      {
          if (evictionPolicyListener != null)
          {
              evictionPolicyListener.nodeActivated(fqn, pre);
          }
          if (hasListeners)
          {
              for (CacheListener listener : listeners)
              {
                  listener.nodeActivated(fqn, pre);
              }
          }
  
      }
  
      /**
       * Notifies all registered listeners of a nodePassivated event.
       * @param fqn
       * @param pre
       */
      public void notifyNodePassivated(Fqn fqn, boolean pre)
      {
          if (evictionPolicyListener != null)
          {
              evictionPolicyListener.nodePassivated(fqn, pre);
          }
          if (hasListeners)
          {
              for (CacheListener listener : listeners)
              {
                  listener.nodePassivated(fqn, pre);
              }
          }
      }
  
      /**
       * Notifies all registered listeners of a cacheStarted event.
       * @param cache
       */
      public void notifyCacheStarted(CacheSPI cache)
      {
          if (evictionPolicyListener != null)
          {
              evictionPolicyListener.cacheStarted(cache);
          }
          if (hasListeners)
          {
              for (CacheListener listener : listeners)
              {
                  listener.cacheStarted(cache);
              }
          }
      }
  
      /**
       * Notifies all registered listeners of a cacheStopped event.
       * @param cache
       */
      public void notifyCacheStopped(CacheSPI cache)
      {
          if (evictionPolicyListener != null)
          {
              evictionPolicyListener.cacheStopped(cache);
          }
          if (hasListeners)
          {
              for (CacheListener listener : listeners)
              {
                  listener.cacheStopped(cache);
              }
          }
      }
  
      /**
       * Notifies all registered listeners of a viewChange event.
       * @param new_view
       */
      public void notifyViewChange(View new_view)
      {
          if (evictionPolicyListener != null)
          {
              evictionPolicyListener.viewChange(new_view);
          }
          if (hasListeners)
          {
              for (CacheListener listener : listeners)
              {
                  listener.viewChange(new_view);
              }
          }
      }
  }
  
  
  



More information about the jboss-cvs-commits mailing list