KickJava   Java API By Example, From Geeks To Geeks.

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


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.Set JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.LinkedList JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import java.io.IOException JavaDoc;
30
31 /**
32  * Date: Aug 31, 2005 11:42:20 AM
33  * @author Bhavana
34  */

35 public class StringAlertSource extends AlertSource{
36
37     private static final Logger JavaDoc logger =
38             Loggers.getLogger(StringAlertSource.class);
39
40     private ObjectName monitorObjName = null;
41     private ObjectNotificationListener listener = null;
42     private ObjectNotificationFilterSupport filter = null;
43
44     public StringAlertSource(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=StringMonitor");
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.StringMonitor",
63                 monitorObjName, null, null);
64         /* set attributes */
65         List JavaDoc attributes = new LinkedList JavaDoc();
66         attributes.add(new ObjectAttribute("ObservedAttribute",
67                 sourceConfig.getAttributeName()));
68         // note the following is deprecated, but this is what weblogic exposes
69
attributes.add(new ObjectAttribute("ObservedObject",
70                 connection.buildObjectName(sourceConfig.getObjectName())));
71         attributes.add(new ObjectAttribute("StringToCompare",
72                 sourceConfig.getStringAttributeValue()));
73         attributes.add(new ObjectAttribute("NotifyMatch", Boolean.TRUE));
74         attributes.add(new ObjectAttribute("NotifyDiffer", Boolean.FALSE));
75         attributes.add(new ObjectAttribute("GranularityPeriod", new Long JavaDoc(5000)));
76
77         connection.setAttributes(monitorObjName, attributes);
78         /* start the monitor */
79         connection.invoke(monitorObjName, "start", new Object JavaDoc[0], new String JavaDoc[0]);
80         listener = new ObjectNotificationListener(){
81             public void handleNotification(ObjectNotification notification,
82                                            Object JavaDoc handback) {
83                 try {
84                     StringAlertSource.this.handler.handle(
85                             new AlertInfo(notification));
86                 } catch (Exception JavaDoc e) {
87                     logger.log(Level.SEVERE, "Error while handling alert", e);
88                 }
89             }
90         };
91         filter = new ObjectNotificationFilterSupport();
92         filter.enableType("jmx.monitor.string.matches");
93         filter.enableType("jmx.monitor.string.differs");
94         filter.enableType("jmx.monitor.error.attribute");
95         filter.enableType("jmx.monitor.error.type");
96         filter.enableType("jmx.monitor.error.mbean");
97         filter.enableType("jmx.monitor.error.runtime");
98         connection.addNotificationListener(monitorObjName,
99                 listener, filter, null);
100     }
101
102     protected void unregisterInternal() {
103         assert connection != null;
104         assert monitorObjName != null;
105
106         try {
107             /* remove notification listener */
108             connection.removeNotificationListener(monitorObjName, listener,
109                    filter, null);
110         } catch (Exception JavaDoc e) {
111             logger.log(Level.WARNING,
112                     "Error while Removing Notification Listener. error: " +
113                     e.getMessage());
114         }
115
116         try {
117             /* unregister GaugeMonitor MBean */
118             connection.unregisterMBean(monitorObjName);
119         } catch (Exception JavaDoc e) {
120             logger.log(Level.WARNING,
121                     "Error while unregistering MBean: " + monitorObjName +
122                     ". error=" + e.getMessage());
123         }
124
125         listener = null;
126         filter = null;
127     }
128 }
129
Popular Tags