1 /***
2 * Copyright 2003-2010 Terracotta, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package net.sf.ehcache.hibernate.management.impl;
18
19 import java.util.List;
20 import java.util.concurrent.CopyOnWriteArrayList;
21 import java.util.concurrent.atomic.AtomicLong;
22
23 import javax.management.ListenerNotFoundException;
24 import javax.management.MBeanNotificationInfo;
25 import javax.management.NotCompliantMBeanException;
26 import javax.management.Notification;
27 import javax.management.NotificationBroadcasterSupport;
28 import javax.management.NotificationEmitter;
29 import javax.management.NotificationFilter;
30 import javax.management.NotificationListener;
31 import javax.management.StandardMBean;
32
33 /***
34 * @author gkeim
35 *
36 */
37 public abstract class BaseEmitterBean extends StandardMBean implements NotificationEmitter {
38 /***
39 * emitter
40 */
41 protected final Emitter emitter = new Emitter();
42
43 /***
44 * sequenceNumber
45 */
46 protected final AtomicLong sequenceNumber = new AtomicLong();
47
48
49 private final List<NotificationListener> notificationListeners = new CopyOnWriteArrayList<NotificationListener>();
50
51 /***
52 * BaseEmitterBean
53 *
54 * @param <T>
55 * @param mbeanInterface
56 * @throws NotCompliantMBeanException
57 */
58 protected <T> BaseEmitterBean(Class<T> mbeanInterface) throws NotCompliantMBeanException {
59 super(mbeanInterface);
60 }
61
62 /***
63 * sendNotification
64 *
65 * @param eventType
66 */
67 public void sendNotification(String eventType) {
68 sendNotification(eventType, null, null);
69 }
70
71 /***
72 * sendNotification
73 *
74 * @param eventType
75 * @param data
76 */
77 public void sendNotification(String eventType, Object data) {
78 sendNotification(eventType, data, null);
79 }
80
81 /***
82 * sendNotification
83 *
84 * @param eventType
85 * @param data
86 * @param msg
87 */
88 public void sendNotification(String eventType, Object data, String msg) {
89 Notification notif = new Notification(eventType, this, sequenceNumber.incrementAndGet(), System.currentTimeMillis(), msg);
90 if (data != null) {
91 notif.setUserData(data);
92 }
93 emitter.sendNotification(notif);
94 }
95
96 /***
97 * Dispose of this SampledCacheManager and clean up held resources
98 */
99 public final void dispose() {
100 doDispose();
101 removeAllNotificationListeners();
102 }
103
104 /***
105 * Dispose callback of subclasses
106 */
107 protected abstract void doDispose();
108
109 /***
110 * @author gkeim
111 */
112 private class Emitter extends NotificationBroadcasterSupport {
113 /***
114 * @see javax.management.NotificationBroadcasterSupport#getNotificationInfo()
115 */
116 @Override
117 public MBeanNotificationInfo[] getNotificationInfo() {
118 return BaseEmitterBean.this.getNotificationInfo();
119 }
120 }
121
122 /***
123 * @see javax.management.NotificationBroadcaster#addNotificationListener(javax.management.NotificationListener,
124 * javax.management.NotificationFilter, java.lang.Object)
125 */
126 public void addNotificationListener(NotificationListener notif, NotificationFilter filter, Object callBack) {
127 emitter.addNotificationListener(notif, filter, callBack);
128 notificationListeners.add(notif);
129 }
130
131 /***
132 * remove all added notification listeners
133 */
134 private void removeAllNotificationListeners() {
135 for (NotificationListener listener : notificationListeners) {
136 try {
137 emitter.removeNotificationListener(listener);
138 } catch (ListenerNotFoundException e) {
139
140 }
141 }
142 notificationListeners.clear();
143 }
144
145 /***
146 * @see javax.management.NotificationBroadcaster#getNotificationInfo()
147 */
148 public abstract MBeanNotificationInfo[] getNotificationInfo();
149
150
151 /***
152 * @see javax.management.NotificationBroadcaster#removeNotificationListener(javax.management.NotificationListener)
153 */
154 public void removeNotificationListener(NotificationListener listener) throws ListenerNotFoundException {
155 emitter.removeNotificationListener(listener);
156 notificationListeners.remove(listener);
157 }
158
159 /***
160 * @see javax.management.NotificationEmitter#removeNotificationListener(javax.management.NotificationListener,
161 * javax.management.NotificationFilter, java.lang.Object)
162 */
163 public void removeNotificationListener(NotificationListener notif, NotificationFilter filter, Object callBack)
164 throws ListenerNotFoundException {
165 emitter.removeNotificationListener(notif, filter, callBack);
166 notificationListeners.remove(notif);
167 }
168 }