KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > system > filterfactory > MBeanServerNotificationFilterFactory


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.system.filterfactory;
23
24 import javax.management.NotificationFilter JavaDoc;
25 import javax.management.ObjectName JavaDoc;
26 import javax.management.relation.MBeanServerNotificationFilter JavaDoc;
27
28 import org.jboss.system.NotificationFilterFactory;
29 import org.w3c.dom.Element JavaDoc;
30 import org.w3c.dom.Node JavaDoc;
31 import org.w3c.dom.NodeList JavaDoc;
32
33 /**
34  * Factory for MBeanServerNotificationFilter filters.
35  *
36  * The produced filter is really meant for MBeanServerNotifications
37  * emitted by the "JMImplementation:type=MBeanServerDelegate" MBean.
38  *
39  * filters-in zero or more Notification types, and zero or more
40  * ObjectNames (contained in the MBeanServerNotification), so
41  * you need to explicitly enable both the types and the ObjectNames.
42  *
43  * In a nutshell, you'll be able to receive registration and/or unregistration
44  * notifications for selected ObjectNames (i.e. MBean instances)!
45  *
46  * The passed filterConfig xml element fragment should look like:
47  *
48  * <filter factory="MBeanServerNotificationFilterFactory">
49  * <enable type="JMX.mbean"/>
50  * ...
51  * <enable object-name="mydomain:type=mymbean"/>
52  * <enable object-name="mydomain:type=myothermbean"/>
53  * ...
54  * </filter>
55  *
56  * Note: JMX.mbean yields both notifications:
57  * JMX.mbean.registered
58  * JMX.mbean.unregistered
59  *
60  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
61  * @version $Revision: 57108 $
62 **/

63 public class MBeanServerNotificationFilterFactory
64    implements NotificationFilterFactory
65 {
66    // Constants -----------------------------------------------------
67

68    /** the xml element and attribute supported by this factory */
69    public static final String JavaDoc ENABLE_ELEMENT = "enable";
70    public static final String JavaDoc ENABLE_TYPE_ATTRIBUTE = "type";
71    public static final String JavaDoc ENABLE_OBJECTNAME_ATTRIBUTE = "object-name";
72    
73    /**
74     * Default public CTOR (necessary)
75     */

76    public MBeanServerNotificationFilterFactory()
77    {
78       // empty
79
}
80    
81    /**
82     * The actual filter factory implementation
83     */

84    public NotificationFilter JavaDoc createNotificationFilter(Element JavaDoc filterConfig)
85       throws Exception JavaDoc
86    {
87       // start off with a filter that does not allow any type/objectname
88
MBeanServerNotificationFilter JavaDoc filter = new MBeanServerNotificationFilter JavaDoc();
89       
90       // filterConfig should point to the <filter factory="..."> element,
91
// we are interested in its 'enable' children to configure the filter
92
NodeList JavaDoc filterChildren = filterConfig.getChildNodes();
93       
94       for (int i = 0; i < filterChildren.getLength(); i++)
95       {
96          Node JavaDoc filterChildNode = filterChildren.item(i);
97       
98          // check if this is an 'enable' element, ignore everything else
99
if (filterChildNode.getNodeName().equals(ENABLE_ELEMENT))
100          {
101             // look for 'type' attribute
102
if (((Element JavaDoc)filterChildNode).hasAttribute(ENABLE_TYPE_ATTRIBUTE))
103             {
104                String JavaDoc type = ((Element JavaDoc)filterChildNode).getAttribute(ENABLE_TYPE_ATTRIBUTE);
105                // enable this type in the filter
106
filter.enableType(type);
107             }
108             else if (((Element JavaDoc)filterChildNode).hasAttribute(ENABLE_OBJECTNAME_ATTRIBUTE))
109             {
110                String JavaDoc objectName = ((Element JavaDoc)filterChildNode).getAttribute(ENABLE_OBJECTNAME_ATTRIBUTE);
111                // enable this objectName in the filter
112
// may throw MalformedObjectNameException
113
filter.enableObjectName(new ObjectName JavaDoc(objectName));
114             }
115             else
116             {
117                throw new Exception JavaDoc("'" + ENABLE_ELEMENT + "' element must have a '"
118                      + ENABLE_TYPE_ATTRIBUTE + "' or a '" + ENABLE_OBJECTNAME_ATTRIBUTE + "' attribute");
119             }
120          }
121       }
122       // we are done
123
return filter;
124    }
125 }
126
Popular Tags