Source code: org/mobicents/slee/container/management/jmx/SbbUsageMBeanImpl.java
1 /*
2 * SbbUsageMBeanImpl.java
3 *
4 * Created on Jan 12, 2005
5 *
6 * Created by: M. Ranganathan
7 *
8 * The Mobicents Open SLEE project
9 *
10 * A SLEE for the people!
11 *
12 * The source code contained in this file is in in the public domain.
13 * It can be used in any project or product without prior permission,
14 * license or royalty payments. There is NO WARRANTY OF ANY KIND,
15 * EXPRESS, IMPLIED OR STATUTORY, INCLUDING, WITHOUT LIMITATION,
16 * THE IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
17 * AND DATA ACCURACY. We do not warrant or make any representations
18 * regarding the use of the software or the results thereof, including
19 * but not limited to the correctness, accuracy, reliability or
20 * usefulness of the software.
21 */
22
23 package org.mobicents.slee.container.management.jmx;
24
25 import java.lang.reflect.Method;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.Map;
29
30 import javax.management.ListenerNotFoundException;
31 import javax.management.MBeanNotificationInfo;
32 import javax.management.NotCompliantMBeanException;
33 import javax.management.NotificationBroadcaster;
34 import javax.management.NotificationFilter;
35 import javax.management.NotificationListener;
36 import javax.management.StandardMBean;
37 import javax.slee.InvalidArgumentException;
38 import javax.slee.InvalidStateException;
39 import javax.slee.SbbID;
40 import javax.slee.ServiceID;
41 import javax.slee.management.ManagementException;
42 import javax.slee.usage.SbbUsageMBean;
43 import javax.slee.usage.UsageNotification;
44 import javax.transaction.SystemException;
45
46 import org.mobicents.slee.container.SleeContainer;
47 import org.mobicents.slee.container.management.InstalledUsageParameterSet;
48 import org.mobicents.slee.container.management.SbbDescriptorImpl;
49 import org.mobicents.slee.container.service.Service;
50 import org.mobicents.slee.container.service.ServiceComponent;
51 import org.mobicents.slee.runtime.SbbEntity;
52 import org.mobicents.slee.runtime.transaction.SleeTransactionManager;
53
54 import EDU.oswego.cs.dl.util.concurrent.CopyOnWriteArrayList;
55
56 /**
57 * SbbUsageMBeanImpl -- implementation of the SbbUsageMBean
58 *
59 * @author M. Ranganathan
60 *
61 */
62 public class SbbUsageMBeanImpl extends StandardMBean implements SbbUsageMBean,
63 NotificationBroadcaster {
64
65 private SbbID sbbId;
66
67
68 private String name;
69
70
71
72 private List listeners;
73
74 private InstalledUsageParameterSet usagePramSet;
75
76 private ServiceID serviceId;
77
78
79
80 /*
81 * static { logger = Logger.getLogger(SbbUsageMBeanImpl.class); }
82 */
83 public SbbUsageMBeanImpl(String interfaceName)
84 throws NotCompliantMBeanException, ClassNotFoundException {
85 super(Thread.currentThread().getContextClassLoader().loadClass(
86 interfaceName));
87 listeners = new CopyOnWriteArrayList();
88
89 }
90
91 class ListenerFilterHandbackTriplet {
92 NotificationListener notificationListener;
93
94 NotificationFilter notificationFilter;
95
96 Object handbackObject;
97
98 public ListenerFilterHandbackTriplet(NotificationListener listener,
99 NotificationFilter notificationFilter, Object handbackObject) {
100 this.notificationListener = listener;
101 this.notificationFilter = notificationFilter;
102 this.handbackObject = handbackObject;
103 }
104 }
105
106 public SbbUsageMBeanImpl( ServiceID serviceID,
107 SbbID sbbId, String name, String interfaceName)
108 throws NotCompliantMBeanException, ClassNotFoundException {
109 this(interfaceName);
110 this.serviceId = serviceID;
111 this.sbbId = sbbId;
112 this.name = name;
113
114 }
115
116 /*
117 * (non-Javadoc)
118 *
119 * @see javax.slee.usage.SbbUsageMBean#getService()
120 */
121 public ServiceID getService() throws ManagementException {
122
123 return this.serviceId;
124
125 }
126
127 /*
128 * (non-Javadoc)
129 *
130 * @see javax.slee.usage.SbbUsageMBean#getSbb()
131 */
132 public SbbID getSbb() throws ManagementException {
133 return this.sbbId;
134
135 }
136
137 /*
138 * (non-Javadoc)
139 *
140 * @see javax.slee.usage.SbbUsageMBean#getUsageParameterSet()
141 */
142 public String getUsageParameterSet() throws ManagementException {
143 return name;
144
145 }
146
147 /*
148 * (non-Javadoc)
149 *
150 * @see javax.slee.usage.SbbUsageMBean#close()
151 */
152 public void close() throws InvalidStateException, ManagementException {
153 if (listeners.size() != 0) {
154 throw new InvalidStateException(
155 "Could not close Usage MBean listeners still attached!");
156 }
157 // TODO Auto-generated method stub
158
159 }
160
161 /*
162 * (non-Javadoc)
163 *
164 * @see javax.slee.usage.SbbUsageMBean#resetAllUsageParameters()
165 */
166 public void resetAllUsageParameters() throws ManagementException {
167 SleeContainer sleeContainer = SleeContainer.lookupFromJndi();
168 SleeTransactionManager txmgr = SleeContainer.getTransactionManager();
169 boolean rb = true;
170 try {
171 txmgr.begin();
172 ServiceComponent service = sleeContainer.getServiceComponent(this.serviceId);
173 Service svc = sleeContainer.getService(this.serviceId);
174 try {
175 String[] paramNames = service
176 .getNamedUsageParameterSets(this.sbbId);
177
178 for (int i = 0; paramNames != null && i < paramNames.length; i++) {
179 InstalledUsageParameterSet ups = svc
180 .getNamedUsageParameter(sbbId, name);
181 if ( ups != null)
182 ups.reset();
183 }
184 } catch ( InvalidArgumentException ex ) {
185 // This is ok because the service may not have
186 // any named usage parameters and getNamedUsageParameterSets will
187 // throw exception at this point.
188
189 }
190 InstalledUsageParameterSet ups = svc
191 .getDefaultUsageParameterSet(this.sbbId);
192 if ( ups != null ) ups.reset();
193 rb = false;
194
195 } catch (Exception ex) {
196 throw new ManagementException("error resetting usage parametes", ex);
197 } finally {
198 try {
199 if (rb)
200 txmgr.setRollbackOnly();
201 txmgr.commit();
202 } catch (SystemException e) {
203 throw new RuntimeException("Txmgr failed", e);
204 }
205
206 }
207
208 }
209
210 /*
211 * Add a notification listener Created from equivalent method in
212 * TraceMBeanImpl.
213 *
214 * @see javax.management.NotificationBroadcaster#addNotificationListener(javax.management.NotificationListener,
215 * javax.management.NotificationFilter, java.lang.Object)
216 */
217 public void addNotificationListener(NotificationListener listener,
218 NotificationFilter filter, Object handback)
219 throws IllegalArgumentException {
220 //logger.debug("addNotificationListener");
221 this.listeners.add(new ListenerFilterHandbackTriplet(listener, filter,
222 handback));
223 }
224
225 /*
226 * Remove the listener from the list. Created from equivalent method in
227 * TraceMBeanImpl.
228 *
229 * @see javax.management.NotificationBroadcaster#removeNotificationListener(javax.management.NotificationListener)
230 */
231 public void removeNotificationListener(NotificationListener listener)
232 throws ListenerNotFoundException {
233 //logger.debug("removeNotificationListener");
234 boolean found = false;
235 Iterator iter = listeners.iterator();
236 while (iter.hasNext()) {
237 ListenerFilterHandbackTriplet triplet = (ListenerFilterHandbackTriplet) iter
238 .next();
239 if (triplet.notificationListener == listener) {
240 found = true;
241 break;
242 }
243 }
244 if (found)
245 listeners.remove(listener);
246 else
247 throw new ListenerNotFoundException();
248 }
249
250 /*
251 * @see javax.management.NotificationBroadcaster#getNotificationInfo()
252 */
253 public MBeanNotificationInfo[] getNotificationInfo() {
254 //logger.debug("getNotificationInfo");
255 String[] notificationTypes = new String[] { USAGE_NOTIFICATION_TYPE };
256 MBeanNotificationInfo[] mbeanNotificationInfo = new MBeanNotificationInfo[] { new MBeanNotificationInfo(
257 notificationTypes, "UsageNotifications",
258 "Notification of SBB usage") };
259
260 return mbeanNotificationInfo;
261 }
262
263 /**
264 * Send the notification.
265 *
266 * @param value
267 * @param seqno
268 * @param usageParameterSetName
269 * @param usageParameterName
270 * @param isCounter
271 */
272 public void sendUsageNotification(long value, long seqno,
273 String usageParameterSetName, String usageParameterName,
274 boolean isCounter) {
275 UsageNotification notification = new UsageNotification(this,
276 this.serviceId, this.sbbId, usageParameterSetName,
277 usageParameterName, isCounter, value, seqno, System
278 .currentTimeMillis());
279 //logger.debug("sendNotification nListeners = " + listeners.size() );
280 Iterator iter = listeners.iterator();
281 while (iter.hasNext()) {
282 ListenerFilterHandbackTriplet triplet = (ListenerFilterHandbackTriplet) iter
283 .next();
284 if (triplet.notificationFilter == null
285 || triplet.notificationFilter
286 .isNotificationEnabled(notification)) {
287 triplet.notificationListener.handleNotification(notification,
288 triplet.handbackObject);
289 }
290 }
291
292 }
293
294 /**
295 * @param usageParam
296 */
297 public void setUsageParameter(InstalledUsageParameterSet usageParam) {
298 this.usagePramSet = usageParam;
299
300 }
301
302 }
303