KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Set JavaDoc;
25 import java.util.Vector JavaDoc;
26
27 import javax.management.Notification JavaDoc;
28 import javax.management.NotificationFilter JavaDoc;
29
30 import org.jboss.deployment.DeploymentInfo;
31 import org.jboss.mx.util.JBossNotificationFilterSupport;
32 import org.jboss.system.NotificationFilterFactory;
33 import org.jboss.util.collection.CollectionsFactory;
34 import org.w3c.dom.Element JavaDoc;
35 import org.w3c.dom.Node JavaDoc;
36 import org.w3c.dom.NodeList JavaDoc;
37
38 /**
39  * Factory for {@link DeploymentInfoNotificationFilter} filters.
40  * (check the inner class).
41  *
42  * The produced filter is really meant for Notifications
43  * emitted by SubDeployers. The types of interest are:
44  *
45  * org.jboss.deployment.SubDeployer.init
46  * org.jboss.deployment.SubDeployer.create
47  * org.jboss.deployment.SubDeployer.start
48  * org.jboss.deployment.SubDeployer.stop
49  * org.jboss.deployment.SubDeployer.destroy
50  *
51  * The above subdeployer notifications carry a DeploymentInfo
52  * instance in their UserData. We can further filter based on
53  * DeploymentInfo.shortName, but is important to explicitly
54  * enable both the notification types and the desired shortNames.
55  *
56  * In practice, you'll be able to receive notifications when a
57  * particular deployment unit (e.g. my-app.ear) gets processed
58  * (e.g. started, stopped, etc.) by a subdeployer.
59  *
60  * The passed filterConfig xml element fragment should look like:
61  *
62  * <filter factory="DeploymentInfoNotificationFilterFactory">
63  * <enable type="org.jboss.deployment.SubDeployer.start"/>
64  * <enable type="org.jboss.deployment.SubDeployer.stop"/>
65  * ...
66  * <enable short-name="my-app.ear"/>
67  * <enable short-name="my-service.xml"/>
68  * ...
69  * </filter>
70  *
71  * Note: org.jboss.deployment.SubDeployer yields all five
72  * SubDeployer notifications.
73  *
74  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
75  * @version $Revision: 57108 $
76  * @since 4.0.3
77 **/

78 public class DeploymentInfoNotificationFilterFactory
79    implements NotificationFilterFactory
80 {
81    // Constants -----------------------------------------------------
82

83    /** the xml element and attribute supported by this factory */
84    public static final String JavaDoc ENABLE_ELEMENT = "enable";
85    public static final String JavaDoc ENABLE_TYPE_ATTRIBUTE = "type";
86    public static final String JavaDoc ENABLE_SHORTNAME_ATTRIBUTE = "short-name";
87    
88    /**
89     * Default public CTOR (necessary)
90     */

91    public DeploymentInfoNotificationFilterFactory()
92    {
93       // empty
94
}
95    
96    /**
97     * The actual filter factory implementation
98     */

99    public NotificationFilter JavaDoc createNotificationFilter(Element JavaDoc filterConfig)
100       throws Exception JavaDoc
101    {
102       // start off with a filter that does not allow any type
103
DeploymentInfoNotificationFilter filter = new DeploymentInfoNotificationFilter();
104       
105       // filterConfig should point to the <filter factory="..."> element,
106
// we are interested in its 'enable' children to configure the filter
107
NodeList JavaDoc filterChildren = filterConfig.getChildNodes();
108       
109       for (int i = 0; i < filterChildren.getLength(); i++)
110       {
111          Node JavaDoc filterChildNode = filterChildren.item(i);
112       
113          // check if this is an 'enable' element, ignore everything else
114
if (filterChildNode.getNodeName().equals(ENABLE_ELEMENT))
115          {
116             // look for 'type' attribute
117
if (((Element JavaDoc)filterChildNode).hasAttribute(ENABLE_TYPE_ATTRIBUTE))
118             {
119                String JavaDoc type = ((Element JavaDoc)filterChildNode).getAttribute(ENABLE_TYPE_ATTRIBUTE);
120                // enable this type in the filter
121
filter.enableType(type);
122             }
123             else if (((Element JavaDoc)filterChildNode).hasAttribute(ENABLE_SHORTNAME_ATTRIBUTE))
124             {
125                String JavaDoc shortName = ((Element JavaDoc)filterChildNode).getAttribute(ENABLE_SHORTNAME_ATTRIBUTE);
126                // enable this shortName in the filter
127
filter.enableShortName(shortName);
128             }
129             else
130             {
131                throw new Exception JavaDoc("'" + ENABLE_ELEMENT + "' element must have a '"
132                      + ENABLE_TYPE_ATTRIBUTE + "' or a '" + ENABLE_SHORTNAME_ATTRIBUTE + "' attribute");
133             }
134          }
135       }
136       // we are done
137
return filter;
138    }
139    
140    /**
141     * A NotificationFilter that can filter Notifications that
142     * carry a DeploymentInfo payload in the UserData field.
143     *
144     * The Notification is filtered first on its type,
145     * then on its DeploymentInfo.shortName.
146     *
147     * Uses copy-on-write semantics for fast unsynchronized access.
148     */

149    public static class DeploymentInfoNotificationFilter extends JBossNotificationFilterSupport
150    {
151       private static final long serialVersionUID = -5067618040005609685L;
152       
153       /** The short names that will pass the filter */
154       private Set JavaDoc enabledShortNames;
155       
156       // Constructors -----------------------------------------------
157

158       /**
159        * Default CTOR.
160        *
161        * Create a filter that filters out all notification types/sortnames.
162        */

163       public DeploymentInfoNotificationFilter()
164       {
165          super();
166          enabledShortNames = CollectionsFactory.createCopyOnWriteSet();
167       }
168       
169       /**
170        * Disable all shortNames. Rejects all notifications.
171        */

172       public void disableAllShortNames()
173       {
174          enabledShortNames.clear();
175       }
176       
177       /**
178        * Disable a shortName.
179        *
180        * @param name the shortName to disable.
181        */

182       public void disableShortName(String JavaDoc name)
183       {
184          enabledShortNames.remove(name);
185       }
186       
187       /**
188        * Enable a shortName.
189        *
190        * @param name the shortName to enable.
191        * @exception IllegalArgumentException for a null name.
192        */

193       public void enableShortName(String JavaDoc name) throws IllegalArgumentException JavaDoc
194       {
195          if (name == null)
196          {
197             throw new IllegalArgumentException JavaDoc("null shortName");
198          }
199          enabledShortNames.add(name);
200       }
201       
202       /**
203        * Get all the enabled short names.<p>
204        *
205        * Returns a vector of enabled short names.<br>
206        * An empty vector means all short names disabled.
207        *
208        * @return the vector of enabled short names.
209        */

210       public Vector JavaDoc getEnabledShortNames()
211       {
212          return new Vector JavaDoc(enabledShortNames);
213       }
214       
215       /**
216        * @return human readable string.
217        */

218       public String JavaDoc toString()
219       {
220          StringBuffer JavaDoc sb = new StringBuffer JavaDoc(100);
221          
222          sb.append(getClass().getName()).append(':');
223          sb.append(" enabledTypes=").append(getEnabledTypes());
224          sb.append(" enabledShortNames=").append(getEnabledShortNames());
225          
226          return sb.toString();
227       }
228       
229       // NotificationFilter implementation ---------------------------
230

231       /**
232        * Test to see whether this notification is enabled
233        *
234        * @param notification the notification to filter
235        * @return true when the notification should be sent, false otherwise
236        * @exception IllegalArgumentException for null notification.
237        */

238       public boolean isNotificationEnabled(Notification JavaDoc notification)
239       {
240          // check if the notification type is not enabled in the super class
241
if (super.isNotificationEnabled(notification) == false)
242          {
243             return false;
244          }
245          
246          // Check the shortName in the payload. We assume that proper
247
// filtering on notification type, ensures that the
248
// notification actually carries a DeploymentInfo.
249
DeploymentInfo di = (DeploymentInfo)notification.getUserData();
250          String JavaDoc shortName = di.shortName;
251
252          // Return true if the shortName is enabled, false otherwise
253
return enabledShortNames.contains(shortName);
254       }
255    }
256 }
257
Popular Tags