KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > management > AbstractTerracottaMBean


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tc.management;
6
7 import EDU.oswego.cs.dl.util.concurrent.CopyOnWriteArrayList;
8
9 import com.tc.exception.TCRuntimeException;
10 import com.tc.logging.TCLogger;
11 import com.tc.logging.TCLogging;
12 import com.tc.properties.TCPropertiesImpl;
13
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Locale JavaDoc;
17 import java.util.MissingResourceException JavaDoc;
18 import java.util.ResourceBundle JavaDoc;
19
20 import javax.management.ListenerNotFoundException JavaDoc;
21 import javax.management.MBeanFeatureInfo JavaDoc;
22 import javax.management.MBeanNotificationInfo JavaDoc;
23 import javax.management.NotCompliantMBeanException JavaDoc;
24 import javax.management.Notification JavaDoc;
25 import javax.management.NotificationEmitter JavaDoc;
26 import javax.management.NotificationFilter JavaDoc;
27 import javax.management.NotificationListener JavaDoc;
28 import javax.management.StandardMBean JavaDoc;
29
30 public abstract class AbstractTerracottaMBean extends StandardMBean JavaDoc implements NotificationEmitter JavaDoc, TerracottaMBean {
31
32   private static final ResourceBundle JavaDoc DEFAULT_BUNDLE = getBundleForMBean(TerracottaMBean.class, TCLogging
33                                                                 .getLogger(TerracottaMBean.class));
34
35   private static final boolean ENABLED = TCPropertiesImpl.getProperties().getBoolean(
36                                                                 "tc.management.mbeans.enabled");
37
38   private final TCLogger logger;
39   private final ResourceBundle JavaDoc beanBundle;
40   private final boolean isNotificationBroadcaster;
41
42   // NOTE: The use of NotificationBroadcasterSupport has been removed and re-implemented internally
43
// to avoid issues with JDK logging (DEV-421)
44
private final List notificationListeners = new CopyOnWriteArrayList();
45   private boolean isActive;
46
47   protected AbstractTerracottaMBean(final Class JavaDoc mBeanInterface, final boolean isNotificationBroadcaster)
48       throws NotCompliantMBeanException JavaDoc {
49     this(mBeanInterface, isNotificationBroadcaster, ENABLED);
50   }
51
52   protected AbstractTerracottaMBean(final Class JavaDoc mBeanInterface, final boolean isNotificationBroadcaster,
53                                     final boolean isActive) throws NotCompliantMBeanException JavaDoc {
54     super(mBeanInterface);
55     this.logger = TCLogging.getLogger(mBeanInterface);
56     this.beanBundle = getBundleForMBean(mBeanInterface, logger);
57     this.isNotificationBroadcaster = isNotificationBroadcaster;
58     this.isActive = isActive;
59   }
60
61   public final String JavaDoc getInterfaceClassName() {
62     return getMBeanInterface().getName();
63   }
64
65   public final void addNotificationListener(final NotificationListener JavaDoc listener, final NotificationFilter JavaDoc filter,
66                                             final Object JavaDoc obj) {
67     notificationListeners.add(new Listener JavaDoc(listener, filter, obj));
68   }
69
70   public MBeanNotificationInfo JavaDoc[] getNotificationInfo() {
71     if (isNotificationBroadcaster()) {
72       final RuntimeException JavaDoc re = new TCRuntimeException("MBean error: this MBean[" + getClass().getName()
73           + "] must override getNotificationInfo() since" + " it broadcasts notifications");
74       throw re;
75     }
76     return new MBeanNotificationInfo JavaDoc[0];
77   }
78
79   public final void removeNotificationListener(final NotificationListener JavaDoc listener, final NotificationFilter JavaDoc filter,
80                                                final Object JavaDoc obj) throws ListenerNotFoundException JavaDoc {
81     boolean removed = false;
82
83     for (Iterator JavaDoc i = notificationListeners.iterator(); i.hasNext();) {
84       Listener JavaDoc lsnr = (Listener JavaDoc) i.next();
85       if (lsnr.listener == listener && lsnr.filter == filter && lsnr.handback == obj) {
86         removed = true;
87         i.remove();
88       }
89     }
90
91     if (!removed) { throw new ListenerNotFoundException JavaDoc(); }
92   }
93
94   public final void removeNotificationListener(final NotificationListener JavaDoc listener) throws ListenerNotFoundException JavaDoc {
95     boolean removed = false;
96
97     for (Iterator JavaDoc i = notificationListeners.iterator(); i.hasNext();) {
98       Listener JavaDoc lsnr = (Listener JavaDoc) i.next();
99       if (lsnr.listener == listener) {
100         removed = true;
101         i.remove();
102       }
103     }
104
105     if (!removed) { throw new ListenerNotFoundException JavaDoc(); }
106   }
107
108   public final void sendNotification(final Notification JavaDoc notification) {
109     if (isEnabled()) {
110       for (Iterator JavaDoc i = notificationListeners.iterator(); i.hasNext();) {
111         Listener JavaDoc lsnr = (Listener JavaDoc) i.next();
112
113         if (lsnr.filter == null || lsnr.filter.isNotificationEnabled(notification)) {
114           lsnr.listener.handleNotification(notification, lsnr.handback);
115         }
116       }
117     }
118   }
119
120   public final boolean isNotificationBroadcaster() {
121     return isNotificationBroadcaster;
122   }
123
124   public final void enable() {
125     setState(true);
126   }
127
128   public final void disable() {
129     setState(false);
130   }
131
132   private synchronized void setState(final boolean isActive) {
133     if (this.isActive && !isActive) {
134       reset();
135     }
136     this.isActive = isActive;
137   }
138
139   public final synchronized boolean isEnabled() {
140     return isActive;
141   }
142
143   /**
144    * As far as I can tell (at least with the Sun implementation), most if not all of the {@link StandardMBean}
145    * customization hooks for descriptions come through this one method. Since we are using a {@link ResourceBundle} we
146    * don't really need to worry about the exact type of the feature (only the name), so we should be able to get away
147    * with overriding only this particular method to supply descriptions.
148    */

149   protected String JavaDoc getDescription(final MBeanFeatureInfo JavaDoc featureInfo) {
150     final String JavaDoc name = featureInfo.getName();
151     String JavaDoc bundleDescription = null;
152     if (beanBundle != null) {
153       try {
154         bundleDescription = beanBundle.getString(name);
155       } catch (MissingResourceException JavaDoc mre) {
156         if (DEFAULT_BUNDLE != null) {
157           try {
158             bundleDescription = DEFAULT_BUNDLE.getString(name);
159           } catch (MissingResourceException JavaDoc defaultMre) {
160             // We tried :)
161
}
162         }
163       } catch (Throwable JavaDoc t) {
164         // Not important enough to do anything about, but the log might reveal an operational problem
165
logger.warn("Unexpected error while trying to retrieve feature description[" + name + "]", t);
166       } finally {
167         if (bundleDescription == null) {
168           bundleDescription = super.getDescription(featureInfo);
169         }
170       }
171     }
172     return bundleDescription;
173   }
174
175   private static ResourceBundle JavaDoc getBundleForMBean(final Class JavaDoc mBeanInterface, final TCLogger logger) {
176     ResourceBundle JavaDoc bundle = null;
177     try {
178       bundle = ResourceBundle.getBundle(mBeanInterface.getName(), Locale.getDefault(), AbstractTerracottaMBean.class
179           .getClassLoader());
180     } catch (MissingResourceException JavaDoc mre) {
181       logger.info("No resource bundle exists for MBean " + mBeanInterface.getName());
182     } catch (Throwable JavaDoc t) {
183       logger.warn("Unexpected error loading resource bundle for MBean " + mBeanInterface.getName(), t);
184     }
185     return bundle;
186   }
187
188   private static class Listener {
189     private final NotificationListener JavaDoc listener;
190     private final NotificationFilter JavaDoc filter;
191     private final Object JavaDoc handback;
192
193     Listener(NotificationListener JavaDoc listener, NotificationFilter JavaDoc filter, Object JavaDoc obj) {
194       this.listener = listener;
195       this.filter = filter;
196       this.handback = obj;
197     }
198
199   }
200
201 }
202
Popular Tags