KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > core > alert > source > GaugeAlertSource


1 /**
2  * Copyright 2004-2005 jManage.org
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 package org.jmanage.core.alert.source;
17
18 import org.jmanage.core.alert.AlertSource;
19 import org.jmanage.core.alert.AlertInfo;
20 import org.jmanage.core.config.AlertSourceConfig;
21 import org.jmanage.core.management.*;
22 import org.jmanage.core.util.Loggers;
23
24 import java.util.logging.Level JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.LinkedList JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.io.IOException JavaDoc;
30
31 /**
32  * Date: Aug 31, 2005 11:41:59 AM
33  * @author Bhavana
34  */

35 public class GaugeAlertSource extends AlertSource{
36
37     private static final Logger JavaDoc logger =
38             Loggers.getLogger(GaugeAlertSource.class);
39
40     private ObjectName monitorObjName = null;
41     private ObjectNotificationListener listener = null;
42     private ObjectNotificationFilterSupport filter = null;
43
44     public GaugeAlertSource(AlertSourceConfig sourceConfig){
45         super(sourceConfig);
46     }
47
48     protected void registerInternal(){
49
50         /* start looking for this notification */
51         monitorObjName = new ObjectName("jmanage.alerts:name=" + alertName +
52                 ",id=" + alertId + ",type=GaugeMonitor");
53
54         /* check if the MBean is already registered */
55         Set JavaDoc mbeans = connection.queryNames(monitorObjName);
56         if(mbeans != null && mbeans.size() > 0){
57             /* remove the MBean */
58             connection.unregisterMBean(monitorObjName);
59         }
60
61         /* create the MBean */
62         connection.createMBean("javax.management.monitor.GaugeMonitor",
63                 monitorObjName, new Object JavaDoc[0], new String JavaDoc[0]);
64         /* set attributes */
65         List JavaDoc attributes = new LinkedList JavaDoc();
66         attributes.add(new ObjectAttribute("GranularityPeriod", new Long JavaDoc(5000)));
67         attributes.add(new ObjectAttribute("NotifyHigh", Boolean.TRUE));
68         attributes.add(new ObjectAttribute("NotifyLow", Boolean.TRUE));
69         attributes.add(new ObjectAttribute("ObservedAttribute",
70                 sourceConfig.getAttributeName()));
71         // note the following is deprecated, but this is what weblogic exposes
72
attributes.add(new ObjectAttribute("ObservedObject",
73                 connection.buildObjectName(sourceConfig.getObjectName())));
74         connection.setAttributes(monitorObjName, attributes);
75         /* add observed object */
76         /*
77         connection.invoke(monitorObjName, "addObservedObject",
78                 new Object[]{new ObjectName(sourceConfig.getObjectName())},
79                 new String[]{"javax.management.ObjectName"});
80         */

81         /* set thresholds */
82         Object JavaDoc[] params = new Object JavaDoc[]{sourceConfig.getHighThreshold(),
83                                        sourceConfig.getLowThreshold()};
84         String JavaDoc[] signature = new String JavaDoc[]{Number JavaDoc.class.getName(),
85                 Number JavaDoc.class.getName()};
86         connection.invoke(monitorObjName, "setThresholds", params, signature);
87         /* start the monitor */
88         connection.invoke(monitorObjName, "start", new Object JavaDoc[0], new String JavaDoc[0]);
89
90         /* now look for notifications from this mbean */
91         listener = new ObjectNotificationListener(){
92             public void handleNotification(ObjectNotification notification,
93                                            Object JavaDoc handback) {
94                 try {
95                     GaugeAlertSource.this.handler.handle(
96                             new AlertInfo(notification));
97                 } catch (Exception JavaDoc e) {
98                     logger.log(Level.SEVERE, "Error while handling alert", e);
99                 }
100             }
101         };
102         filter = new ObjectNotificationFilterSupport();
103         filter.enableType("jmx.monitor.gauge.high");
104         filter.enableType("jmx.monitor.gauge.low");
105         filter.enableType("jmx.monitor.error.attribute");
106         filter.enableType("jmx.monitor.error.type");
107         filter.enableType("jmx.monitor.error.mbean");
108         filter.enableType("jmx.monitor.error.runtime");
109         filter.enableType("jmx.monitor.error.threshold");
110         connection.addNotificationListener(monitorObjName,
111                 listener, filter, null);
112     }
113
114     protected void unregisterInternal() {
115         assert connection != null;
116         assert monitorObjName != null;
117
118         try {
119             /* remove notification listener */
120             connection.removeNotificationListener(monitorObjName, listener,
121                    filter, null);
122         } catch (Exception JavaDoc e) {
123             logger.log(Level.WARNING,
124                     "Error while Removing Notification Listener. error: " +
125                     e.getMessage());
126         }
127
128         try {
129             /* unregister GaugeMonitor MBean */
130             connection.unregisterMBean(monitorObjName);
131         } catch (Exception JavaDoc e) {
132             logger.log(Level.WARNING,
133                     "Error while unregistering MBean: " + monitorObjName +
134                     ". error: " + e.getMessage());
135         }
136
137         listener = null;
138         filter = null;
139     }
140 }
141
Popular Tags