home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-tomcat-addon-1.4.9-installer.exe / jmx.jar / javax / management / NotificationBroadcasterSupport.class (.txt) < prev    next >
Encoding:
Java Class File  |  2002-10-28  |  6.6 KB  |  200 lines

  1. package javax.management;
  2.  
  3. import java.io.Serializable;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.Iterator;
  7. import mx4j.log.Log;
  8. import mx4j.log.Logger;
  9.  
  10. public class NotificationBroadcasterSupport implements NotificationBroadcaster {
  11.    private static final NotificationFilter NULL_FILTER = new 1();
  12.    private static final Object NULL_HANDBACK = new 2();
  13.    private HashMap m_listeners = new HashMap();
  14.  
  15.    private Logger getLogger() {
  16.       return Log.getLogger(this.getClass().getName());
  17.    }
  18.  
  19.    public void addNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) {
  20.       Logger logger = this.getLogger();
  21.       if (logger.isEnabledFor(20)) {
  22.          logger.info("Adding notification listener: " + listener + ", filter: " + filter + ", handback: " + handback);
  23.       }
  24.  
  25.       if (listener == null) {
  26.          throw new RuntimeOperationsException(new IllegalArgumentException("Notification listener cannot be null"));
  27.       } else {
  28.          if (filter == null) {
  29.             filter = NULL_FILTER;
  30.          }
  31.  
  32.          if (handback == null) {
  33.             handback = NULL_HANDBACK;
  34.          }
  35.  
  36.          FilterHandbackPair pair = new FilterHandbackPair(filter, handback);
  37.          synchronized(this) {
  38.             ArrayList pairs = (ArrayList)this.m_listeners.get(listener);
  39.             if (pairs == null) {
  40.                pairs = new ArrayList();
  41.                pairs.add(pair);
  42.                this.m_listeners.put(listener, pairs);
  43.             } else {
  44.                for(int i = 0; i < pairs.size(); ++i) {
  45.                   FilterHandbackPair other = (FilterHandbackPair)pairs.get(i);
  46.                   if (pair.getNotificationFilter().equals(other.getNotificationFilter()) && pair.getHandback().equals(other.getHandback())) {
  47.                      throw new RuntimeOperationsException(new IllegalArgumentException("Notification listener is already registered"));
  48.                   }
  49.                }
  50.  
  51.                pairs.add(pair);
  52.             }
  53.  
  54.             if (logger.isEnabledFor(10)) {
  55.                logger.debug("Filters - Handbacks for this listener: " + pairs);
  56.             }
  57.          }
  58.  
  59.          if (logger.isEnabledFor(20)) {
  60.             logger.info("Notification listener added successfully");
  61.          }
  62.  
  63.       }
  64.    }
  65.  
  66.    public void removeNotificationListener(NotificationListener listener) throws ListenerNotFoundException {
  67.       Logger logger = this.getLogger();
  68.       if (logger.isEnabledFor(20)) {
  69.          logger.info("Removing notification listener: " + listener);
  70.       }
  71.  
  72.       int removed = this.removeNotificationListenerImpl(listener, (NotificationFilter)null, (Object)null);
  73.       if (logger.isEnabledFor(20)) {
  74.          logger.info(removed + "notification listener(s) removed successfully");
  75.       }
  76.  
  77.    }
  78.  
  79.    private int removeNotificationListenerImpl(NotificationListener listener, NotificationFilter filter, Object handback) throws ListenerNotFoundException {
  80.       synchronized(this) {
  81.          ArrayList pairs = (ArrayList)this.m_listeners.get(listener);
  82.          if (pairs == null) {
  83.             throw new ListenerNotFoundException("Notification listener not found");
  84.          } else if (filter == null) {
  85.             if (handback == null) {
  86.                ArrayList removed = (ArrayList)this.m_listeners.remove(listener);
  87.                return removed.size();
  88.             } else {
  89.                int count = 0;
  90.  
  91.                for(int i = 0; i < pairs.size(); ++i) {
  92.                   Object hand = ((FilterHandbackPair)pairs.get(i)).getHandback();
  93.                   if (handback.equals(hand)) {
  94.                      pairs.remove(i);
  95.                      ++count;
  96.                   }
  97.                }
  98.  
  99.                if (pairs.isEmpty()) {
  100.                   this.m_listeners.remove(listener);
  101.                }
  102.  
  103.                return count;
  104.             }
  105.          } else if (handback == null) {
  106.             int count = 0;
  107.  
  108.             for(int i = 0; i < pairs.size(); ++i) {
  109.                Object filt = ((FilterHandbackPair)pairs.get(i)).getNotificationFilter();
  110.                if (filter.equals(filt)) {
  111.                   pairs.remove(i);
  112.                   ++count;
  113.                }
  114.             }
  115.  
  116.             if (pairs.isEmpty()) {
  117.                this.m_listeners.remove(listener);
  118.             }
  119.  
  120.             return count;
  121.          } else {
  122.             int count = 0;
  123.  
  124.             for(int i = 0; i < pairs.size(); ++i) {
  125.                FilterHandbackPair pair = (FilterHandbackPair)pairs.get(i);
  126.                if (filter.equals(pair.getNotificationFilter()) && handback.equals(pair.getHandback())) {
  127.                   pairs.remove(i);
  128.                   ++count;
  129.                }
  130.             }
  131.  
  132.             if (pairs.isEmpty()) {
  133.                this.m_listeners.remove(listener);
  134.             }
  135.  
  136.             return count;
  137.          }
  138.       }
  139.    }
  140.  
  141.    public MBeanNotificationInfo[] getNotificationInfo() {
  142.       return new MBeanNotificationInfo[0];
  143.    }
  144.  
  145.    private boolean hasListeners() {
  146.       synchronized(this) {
  147.          return this.m_listeners.size() > 0;
  148.       }
  149.    }
  150.  
  151.    public void sendNotification(Notification n) {
  152.       Logger logger = this.getLogger();
  153.       boolean debug = logger.isEnabledFor(10);
  154.       boolean info = logger.isEnabledFor(20);
  155.       HashMap listeners = null;
  156.       synchronized(this) {
  157.          listeners = (HashMap)this.m_listeners.clone();
  158.       }
  159.  
  160.       Iterator i = listeners.keySet().iterator();
  161.       if (i.hasNext() && info) {
  162.          logger.info("Sending notifications...");
  163.       }
  164.  
  165.       while(i.hasNext()) {
  166.          NotificationListener listener = (NotificationListener)i.next();
  167.          if (info) {
  168.             logger.info("\tListener is: " + listener);
  169.          }
  170.  
  171.          ArrayList pairs = null;
  172.          synchronized(this) {
  173.             pairs = (ArrayList)this.m_listeners.get(listener);
  174.             pairs = (ArrayList)pairs.clone();
  175.          }
  176.  
  177.          if (debug) {
  178.             logger.debug("\tFilters - Handback for this listener: " + pairs);
  179.          }
  180.  
  181.          for(int j = 0; j < pairs.size(); ++j) {
  182.             FilterHandbackPair pair = (FilterHandbackPair)pairs.get(j);
  183.             NotificationFilter filter = pair.getNotificationFilter();
  184.             Object hb = pair.getHandback();
  185.             if (hb == NULL_HANDBACK) {
  186.                hb = null;
  187.             }
  188.  
  189.             Runnable run = new 3(this, filter, n, info, logger, hb, listener);
  190.             if (listener instanceof Serializable) {
  191.                (new Thread(run, "NotificationBroadcasterSupport Thread")).start();
  192.             } else {
  193.                run.run();
  194.             }
  195.          }
  196.       }
  197.  
  198.    }
  199. }
  200.