KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > jmx > CommonsModelerExtension


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: CommonsModelerExtension.java 638 2006-06-13 08:04:28Z sauthieg $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.jmx;
27
28 import java.io.InputStream JavaDoc;
29
30 import org.apache.commons.modeler.AttributeInfo;
31 import org.apache.commons.modeler.ConstructorInfo;
32 import org.apache.commons.modeler.FieldInfo;
33 import org.apache.commons.modeler.ManagedBean;
34 import org.apache.commons.modeler.NotificationInfo;
35 import org.apache.commons.modeler.OperationInfo;
36 import org.apache.commons.modeler.ParameterInfo;
37 import org.apache.commons.modeler.Registry;
38 import org.apache.commons.modeler.util.DomUtil;
39 import org.w3c.dom.Document JavaDoc;
40 import org.w3c.dom.NamedNodeMap JavaDoc;
41 import org.w3c.dom.Node JavaDoc;
42
43 /**
44  * Utility class used to extends mbeans-descriptors.
45  * @author Guillaume Sauthier
46  */

47 public final class CommonsModelerExtension {
48
49     /**
50      * Private empty constructor for Utility class.
51      */

52     private CommonsModelerExtension() { }
53
54     /**
55      * Update the given Registry (and particulary the inner ManagedBean).
56      * @param registry Registry to be updated.
57      * @param stream mbeans-descriptors-ext.xml content
58      * @throws Exception when something fails.
59      */

60     public static void updateDescriptors(final Registry registry, final InputStream JavaDoc stream) throws Exception JavaDoc {
61
62         try {
63             Document JavaDoc doc = DomUtil.readXml(stream);
64             // Ignore for now the name of the root element
65
Node JavaDoc descriptorsN = doc.getDocumentElement();
66             // Node descriptorsN=DomUtil.getChild(doc, "mbeans-descriptors");
67
if (descriptorsN == null) {
68                 return;
69             }
70
71             Node JavaDoc firstMbeanN = null;
72             if ("mbean".equals(descriptorsN.getNodeName())) {
73                 firstMbeanN = descriptorsN;
74             } else {
75                 firstMbeanN = DomUtil.getChild(descriptorsN, "mbean");
76             }
77
78             if (firstMbeanN == null) {
79                 return;
80             }
81
82             // Process each <mbean> element
83
for (Node JavaDoc mbeanN = firstMbeanN; mbeanN != null; mbeanN = DomUtil.getNext(mbeanN)) {
84
85                 processMBeanNode(registry, mbeanN);
86              }
87
88         } catch (Exception JavaDoc ex) {
89             // TODO Add log statement
90
}
91     }
92
93     /**
94      * @param registry Model MBean registry
95      * @param mbeanNode mbean XML Node
96      */

97     private static void processMBeanNode(final Registry registry, final Node JavaDoc mbeanNode) {
98         // EasyBeans Change ---------------------------------------------
99
// Get the mbean name
100
NamedNodeMap JavaDoc attrs = mbeanNode.getAttributes();
101         Node JavaDoc n = attrs.getNamedItem("name");
102         String JavaDoc mbeanName = n.getNodeValue();
103
104         // Get the ManagedBean
105
ManagedBean managed = registry.findManagedBean(mbeanName);
106         // /EasyBeans Change ---------------------------------------------
107

108         DomUtil.setAttributes(managed, mbeanNode);
109
110         // Process descriptor subnode
111
processMBeanDescriptorNode(managed, mbeanNode);
112
113         // process attribute nodes
114
processMBeanAttributeNode(managed, mbeanNode);
115
116         // process constructor nodes
117
processMBeanConstructorNode(managed, mbeanNode);
118
119         // process notification nodes
120
processMBeanNotificationNode(managed, mbeanNode);
121
122         // process operation nodes
123
processMBeanOperationNode(managed, mbeanNode);
124     }
125
126     /**
127      * @param managed ManagedBean instance
128      * @param mbeanNode mbean XML Node
129      */

130     private static void processMBeanOperationNode(final ManagedBean managed, final Node JavaDoc mbeanNode) {
131         Node JavaDoc firstN;
132         firstN = DomUtil.getChild(mbeanNode, "operation");
133         for (Node JavaDoc descN = firstN; descN != null; descN = DomUtil.getNext(descN)) {
134
135             // Create new operation info
136
OperationInfo oi = new OperationInfo();
137             DomUtil.setAttributes(oi, descN);
138
139             // Process descriptor subnode
140
Node JavaDoc firstDescriptorN = DomUtil.getChild(descN, "descriptor");
141             if (firstDescriptorN != null) {
142                 Node JavaDoc firstFieldN = DomUtil.getChild(firstDescriptorN, "field");
143                 for (Node JavaDoc fieldN = firstFieldN; fieldN != null; fieldN = DomUtil.getNext(fieldN)) {
144                     FieldInfo fi = new FieldInfo();
145                     DomUtil.setAttributes(fi, fieldN);
146                     oi.addField(fi);
147                 }
148             }
149
150             // Process parameter subnodes
151
Node JavaDoc firstParamN = DomUtil.getChild(descN, "parameter");
152             for (Node JavaDoc paramN = firstParamN; paramN != null; paramN = DomUtil.getNext(paramN)) {
153                 ParameterInfo pi = new ParameterInfo();
154                 DomUtil.setAttributes(pi, paramN);
155                 oi.addParameter(pi);
156             }
157
158             // Add this info to our managed bean info
159
managed.addOperation(oi);
160
161         }
162     }
163
164     /**
165      * @param managed ManagedBean instance
166      * @param mbeanNode mbean XML Node
167      */

168     private static void processMBeanNotificationNode(final ManagedBean managed, final Node JavaDoc mbeanNode) {
169         Node JavaDoc firstN;
170         firstN = DomUtil.getChild(mbeanNode, "notification");
171         for (Node JavaDoc descN = firstN; descN != null; descN = DomUtil .getNext(descN)) {
172
173             // Create new notification info
174
NotificationInfo ni = new NotificationInfo();
175             DomUtil.setAttributes(ni, descN);
176
177             // Process descriptor subnode
178
Node JavaDoc firstDescriptorN = DomUtil.getChild(descN, "descriptor");
179             if (firstDescriptorN != null) {
180                 Node JavaDoc firstFieldN = DomUtil.getChild(firstDescriptorN, "field");
181                 for (Node JavaDoc fieldN = firstFieldN; fieldN != null; fieldN = DomUtil.getNext(fieldN)) {
182                     FieldInfo fi = new FieldInfo();
183                     DomUtil.setAttributes(fi, fieldN);
184                     ni.addField(fi);
185                 }
186             }
187
188             // Process notification-type subnodes
189
Node JavaDoc firstParamN = DomUtil.getChild(descN, "notification-type");
190             for (Node JavaDoc paramN = firstParamN; paramN != null; paramN = DomUtil.getNext(paramN)) {
191                 ni.addNotifType(DomUtil.getContent(paramN));
192             }
193
194             // Add this info to our managed bean info
195
managed.addNotification(ni);
196
197         }
198     }
199
200     /**
201      * @param managed ManagedBean instance
202      * @param mbeanNode mbean XML Node
203      */

204     private static void processMBeanConstructorNode(final ManagedBean managed, final Node JavaDoc mbeanNode) {
205         Node JavaDoc firstN;
206         firstN = DomUtil.getChild(mbeanNode, "constructor");
207         for (Node JavaDoc descN = firstN; descN != null; descN = DomUtil.getNext(descN)) {
208
209             // Create new constructor info
210
ConstructorInfo ci = new ConstructorInfo();
211             DomUtil.setAttributes(ci, descN);
212
213             // Process descriptor subnode
214
Node JavaDoc firstDescriptorN = DomUtil.getChild(descN, "descriptor");
215             if (firstDescriptorN != null) {
216                 Node JavaDoc firstFieldN = DomUtil.getChild(firstDescriptorN, "field");
217                 for (Node JavaDoc fieldN = firstFieldN; fieldN != null; fieldN = DomUtil.getNext(fieldN)) {
218                     FieldInfo fi = new FieldInfo();
219                     DomUtil.setAttributes(fi, fieldN);
220                     ci.addField(fi);
221                 }
222             }
223
224             // Process parameter subnodes
225
Node JavaDoc firstParamN = DomUtil.getChild(descN, "parameter");
226             for (Node JavaDoc paramN = firstParamN; paramN != null; paramN = DomUtil.getNext(paramN)) {
227                 ParameterInfo pi = new ParameterInfo();
228                 DomUtil.setAttributes(pi, paramN);
229                 ci.addParameter(pi);
230             }
231
232             // Add this info to our managed bean info
233
managed.addConstructor(ci);
234
235         }
236     }
237
238     /**
239      * @param managed ManagedBean instance
240      * @param mbeanNode mbean XML Node
241      */

242     private static void processMBeanAttributeNode(final ManagedBean managed, final Node JavaDoc mbeanNode) {
243         Node JavaDoc firstN;
244         firstN = DomUtil.getChild(mbeanNode, "attribute");
245         for (Node JavaDoc descN = firstN; descN != null; descN = DomUtil.getNext(descN)) {
246
247             // Create new attribute info
248
AttributeInfo ai = new AttributeInfo();
249             DomUtil.setAttributes(ai, descN);
250
251             // Process descriptor subnode
252
Node JavaDoc descriptorN = DomUtil.getChild(descN, "descriptor");
253             if (descriptorN != null) {
254                 Node JavaDoc firstFieldN = DomUtil.getChild(descriptorN, "field");
255                 for (Node JavaDoc fieldN = firstFieldN; fieldN != null; fieldN = DomUtil.getNext(fieldN)) {
256                     FieldInfo fi = new FieldInfo();
257                     DomUtil.setAttributes(fi, fieldN);
258                     ai.addField(fi);
259                 }
260             }
261
262             // Add this info to our managed bean info
263
managed.addAttribute(ai);
264
265         }
266     }
267
268     /**
269      * @param managed ManagedBean instance
270      * @param mbeanNode mbean XML Node
271      */

272     private static void processMBeanDescriptorNode(final ManagedBean managed, final Node JavaDoc mbeanNode) {
273         Node JavaDoc mbeanDescriptorN = DomUtil.getChild(mbeanNode, "descriptor");
274         if (mbeanDescriptorN != null) {
275             Node JavaDoc firstFieldN = DomUtil.getChild(mbeanDescriptorN, "field");
276             for (Node JavaDoc fieldN = firstFieldN; fieldN != null; fieldN = DomUtil.getNext(fieldN)) {
277                 FieldInfo fi = new FieldInfo();
278                 DomUtil.setAttributes(fi, fieldN);
279                 managed.addField(fi);
280             }
281         }
282     }
283 }
284
Popular Tags