KickJava   Java API By Example, From Geeks To Geeks.

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


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.AttributeChangeNotificationFilter JavaDoc;
25 import javax.management.NotificationFilter JavaDoc;
26
27 import org.jboss.system.NotificationFilterFactory;
28 import org.w3c.dom.Element JavaDoc;
29 import org.w3c.dom.Node JavaDoc;
30 import org.w3c.dom.NodeList JavaDoc;
31
32 /**
33  * Factory for AttributeChangeNotificationFilter filters.
34  *
35  * The produced filter filters-in AttributeChangeNotifications
36  * for zero or more attributes, so you need to explicitly enable
37  * the attribute names you are interested in.
38  *
39  * The passed filterConfig xml element fragment should look like:
40  *
41  * <filter factory="AttributeChangeNotificationFilterFactory">
42  * <enable attribute-name="State"/>
43  * ...
44  * </filter>
45  *
46  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
47  * @version $Revision: 57108 $
48 **/

49 public class AttributeChangeNotificationFilterFactory
50    implements NotificationFilterFactory
51 {
52    // Constants -----------------------------------------------------
53

54    /** the xml element and attribute supported by this factory */
55    public static final String JavaDoc ENABLE_ELEMENT = "enable";
56    public static final String JavaDoc ENABLE_ATTRNAME_ATTRIBUTE = "attribute-name";
57    
58    /**
59     * Default public CTOR (necessary)
60     */

61    public AttributeChangeNotificationFilterFactory()
62    {
63       // empty
64
}
65    
66    /**
67     * The actual filter factory implementation
68     */

69    public NotificationFilter JavaDoc createNotificationFilter(Element JavaDoc filterConfig)
70       throws Exception JavaDoc
71    {
72       // start off with a filter that does not allow any named attribute
73
AttributeChangeNotificationFilter JavaDoc filter = new AttributeChangeNotificationFilter JavaDoc();
74       
75       // filterConfig should point to the <filter factory="..."> element,
76
// we are interested in its 'enable' children to configure the filter
77
NodeList JavaDoc filterChildren = filterConfig.getChildNodes();
78       
79       for (int i = 0; i < filterChildren.getLength(); i++)
80       {
81          Node JavaDoc filterChildNode = filterChildren.item(i);
82       
83          // check if this is an 'enable' element, ignore everything else
84
if (filterChildNode.getNodeName().equals(ENABLE_ELEMENT))
85          {
86             // look for 'attribute-name' attribute
87
if (((Element JavaDoc)filterChildNode).hasAttribute(ENABLE_ATTRNAME_ATTRIBUTE))
88             {
89                String JavaDoc attributeName = ((Element JavaDoc)filterChildNode).getAttribute(ENABLE_ATTRNAME_ATTRIBUTE);
90                // enable this type in the filter
91
filter.enableAttribute(attributeName);
92             }
93             else
94             {
95                throw new Exception JavaDoc("'" + ENABLE_ELEMENT + "' element must have a '"
96                      + ENABLE_ATTRNAME_ATTRIBUTE + "' attribute");
97             }
98          }
99       }
100       // we are done
101
return filter;
102    }
103 }
104
Popular Tags