View Javadoc

1   /* $Id: NotificationDelegator.java 4010 2005-12-14 02:07:16Z stack-sf $
2    *
3    * Created on Dec 12, 2005
4    *
5    * Copyright (C) 2005 Internet Archive.
6    *  
7    * This file is part of the Heritrix Cluster Controller (crawler.archive.org).
8    *  
9    * HCC is free software; you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser Public License as published by
11   * the Free Software Foundation; either version 2.1 of the License, or
12   * any later version.
13   * 
14   * Heritrix is distributed in the hope that it will be useful, 
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU Lesser Public License for more details.
18   * 
19   * You should have received a copy of the GNU Lesser Public License
20   * along with Heritrix; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22   */
23  package org.archive.hcc.util;
24  
25  import java.util.logging.Level;
26  import java.util.logging.Logger;
27  
28  import javax.management.MBeanServerNotification;
29  import javax.management.Notification;
30  import javax.management.NotificationListener;
31  
32  
33  public class NotificationDelegator
34          extends Delegator implements NotificationListener {
35      
36      private static Logger log = 
37          Logger.getLogger(NotificationDelegator.class.getName());
38  
39      public NotificationDelegator() {
40          super(DelegatorPolicy.ACCEPT_ALL);
41      }
42  
43      public NotificationDelegator(DelegatorPolicy p) {
44          super(p);
45      }
46  
47      public void handleNotification(Notification notification, Object handback) {
48          if (log.isLoggable(Level.FINER)) {
49              log.finer("notification.type=" + notification.getType());
50              log.finer("notification.message=" + notification.getMessage());
51              log.finer("notification.userData=" + notification.getUserData());
52  
53              if (notification instanceof MBeanServerNotification) {
54                  log.finer("notification.mbeanName="
55                          + ((MBeanServerNotification) notification)
56                                  .getMBeanName());
57              }
58          }
59  
60          delegate(notification, handback);
61      }
62  
63      protected boolean delegate(Notification n, Object handback) {
64          boolean consumedAtLeastOne = false;
65          for (Delegatable h : handlers) {
66              boolean accepted = ((NotificationDelegatableBase) h).delegate(
67                      n,
68                      handback);
69              if (accepted) {
70                  consumedAtLeastOne = true;
71              }
72  
73              if (accepted && policy == DelegatorPolicy.ACCEPT_FIRST) {
74                  return true;
75              }
76          }
77  
78          return consumedAtLeastOne;
79      }
80  
81      public void addDelegatable(Delegatable d) {
82          super.addDelegatable((NotificationDelegatableBase) d);
83      }
84  }