001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: CommonsModelerExtension.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.jmx;
025:
026: import java.io.InputStream;
027:
028: import org.apache.commons.modeler.AttributeInfo;
029: import org.apache.commons.modeler.ConstructorInfo;
030: import org.apache.commons.modeler.FieldInfo;
031: import org.apache.commons.modeler.ManagedBean;
032: import org.apache.commons.modeler.NotificationInfo;
033: import org.apache.commons.modeler.OperationInfo;
034: import org.apache.commons.modeler.ParameterInfo;
035: import org.apache.commons.modeler.Registry;
036: import org.apache.commons.modeler.util.DomUtil;
037: import org.w3c.dom.Document;
038: import org.w3c.dom.NamedNodeMap;
039: import org.w3c.dom.Node;
040:
041: /**
042: * Utility class used to extends mbeans-descriptors.
043: * @author Guillaume Sauthier
044: */
045: public final class CommonsModelerExtension {
046:
047: /**
048: * Private empty constructor for Utility class.
049: */
050: private CommonsModelerExtension() {
051: }
052:
053: /**
054: * Update the given Registry (and particulary the inner ManagedBean).
055: * @param registry Registry to be updated.
056: * @param stream mbeans-descriptors-ext.xml content
057: * @throws Exception when something fails.
058: */
059: public static void updateDescriptors(final Registry registry,
060: final InputStream stream) throws Exception {
061:
062: try {
063: Document doc = DomUtil.readXml(stream);
064: // Ignore for now the name of the root element
065: Node descriptorsN = doc.getDocumentElement();
066: // Node descriptorsN=DomUtil.getChild(doc, "mbeans-descriptors");
067: if (descriptorsN == null) {
068: return;
069: }
070:
071: Node firstMbeanN = null;
072: if ("mbean".equals(descriptorsN.getNodeName())) {
073: firstMbeanN = descriptorsN;
074: } else {
075: firstMbeanN = DomUtil.getChild(descriptorsN, "mbean");
076: }
077:
078: if (firstMbeanN == null) {
079: return;
080: }
081:
082: // Process each <mbean> element
083: for (Node mbeanN = firstMbeanN; mbeanN != null; mbeanN = DomUtil
084: .getNext(mbeanN)) {
085:
086: processMBeanNode(registry, mbeanN);
087: }
088:
089: } catch (Exception ex) {
090: // TODO Add log statement
091: }
092: }
093:
094: /**
095: * @param registry Model MBean registry
096: * @param mbeanNode mbean XML Node
097: */
098: private static void processMBeanNode(final Registry registry,
099: final Node mbeanNode) {
100: // EasyBeans Change ---------------------------------------------
101: // Get the mbean name
102: NamedNodeMap attrs = mbeanNode.getAttributes();
103: Node n = attrs.getNamedItem("name");
104: String mbeanName = n.getNodeValue();
105:
106: // Get the ManagedBean
107: ManagedBean managed = registry.findManagedBean(mbeanName);
108: // /EasyBeans Change ---------------------------------------------
109:
110: DomUtil.setAttributes(managed, mbeanNode);
111:
112: // Process descriptor subnode
113: processMBeanDescriptorNode(managed, mbeanNode);
114:
115: // process attribute nodes
116: processMBeanAttributeNode(managed, mbeanNode);
117:
118: // process constructor nodes
119: processMBeanConstructorNode(managed, mbeanNode);
120:
121: // process notification nodes
122: processMBeanNotificationNode(managed, mbeanNode);
123:
124: // process operation nodes
125: processMBeanOperationNode(managed, mbeanNode);
126: }
127:
128: /**
129: * @param managed ManagedBean instance
130: * @param mbeanNode mbean XML Node
131: */
132: private static void processMBeanOperationNode(
133: final ManagedBean managed, final Node mbeanNode) {
134: Node firstN;
135: firstN = DomUtil.getChild(mbeanNode, "operation");
136: for (Node descN = firstN; descN != null; descN = DomUtil
137: .getNext(descN)) {
138:
139: // Create new operation info
140: OperationInfo oi = new OperationInfo();
141: DomUtil.setAttributes(oi, descN);
142:
143: // Process descriptor subnode
144: Node firstDescriptorN = DomUtil.getChild(descN,
145: "descriptor");
146: if (firstDescriptorN != null) {
147: Node firstFieldN = DomUtil.getChild(firstDescriptorN,
148: "field");
149: for (Node fieldN = firstFieldN; fieldN != null; fieldN = DomUtil
150: .getNext(fieldN)) {
151: FieldInfo fi = new FieldInfo();
152: DomUtil.setAttributes(fi, fieldN);
153: oi.addField(fi);
154: }
155: }
156:
157: // Process parameter subnodes
158: Node firstParamN = DomUtil.getChild(descN, "parameter");
159: for (Node paramN = firstParamN; paramN != null; paramN = DomUtil
160: .getNext(paramN)) {
161: ParameterInfo pi = new ParameterInfo();
162: DomUtil.setAttributes(pi, paramN);
163: oi.addParameter(pi);
164: }
165:
166: // Add this info to our managed bean info
167: managed.addOperation(oi);
168:
169: }
170: }
171:
172: /**
173: * @param managed ManagedBean instance
174: * @param mbeanNode mbean XML Node
175: */
176: private static void processMBeanNotificationNode(
177: final ManagedBean managed, final Node mbeanNode) {
178: Node firstN;
179: firstN = DomUtil.getChild(mbeanNode, "notification");
180: for (Node descN = firstN; descN != null; descN = DomUtil
181: .getNext(descN)) {
182:
183: // Create new notification info
184: NotificationInfo ni = new NotificationInfo();
185: DomUtil.setAttributes(ni, descN);
186:
187: // Process descriptor subnode
188: Node firstDescriptorN = DomUtil.getChild(descN,
189: "descriptor");
190: if (firstDescriptorN != null) {
191: Node firstFieldN = DomUtil.getChild(firstDescriptorN,
192: "field");
193: for (Node fieldN = firstFieldN; fieldN != null; fieldN = DomUtil
194: .getNext(fieldN)) {
195: FieldInfo fi = new FieldInfo();
196: DomUtil.setAttributes(fi, fieldN);
197: ni.addField(fi);
198: }
199: }
200:
201: // Process notification-type subnodes
202: Node firstParamN = DomUtil.getChild(descN,
203: "notification-type");
204: for (Node paramN = firstParamN; paramN != null; paramN = DomUtil
205: .getNext(paramN)) {
206: ni.addNotifType(DomUtil.getContent(paramN));
207: }
208:
209: // Add this info to our managed bean info
210: managed.addNotification(ni);
211:
212: }
213: }
214:
215: /**
216: * @param managed ManagedBean instance
217: * @param mbeanNode mbean XML Node
218: */
219: private static void processMBeanConstructorNode(
220: final ManagedBean managed, final Node mbeanNode) {
221: Node firstN;
222: firstN = DomUtil.getChild(mbeanNode, "constructor");
223: for (Node descN = firstN; descN != null; descN = DomUtil
224: .getNext(descN)) {
225:
226: // Create new constructor info
227: ConstructorInfo ci = new ConstructorInfo();
228: DomUtil.setAttributes(ci, descN);
229:
230: // Process descriptor subnode
231: Node firstDescriptorN = DomUtil.getChild(descN,
232: "descriptor");
233: if (firstDescriptorN != null) {
234: Node firstFieldN = DomUtil.getChild(firstDescriptorN,
235: "field");
236: for (Node fieldN = firstFieldN; fieldN != null; fieldN = DomUtil
237: .getNext(fieldN)) {
238: FieldInfo fi = new FieldInfo();
239: DomUtil.setAttributes(fi, fieldN);
240: ci.addField(fi);
241: }
242: }
243:
244: // Process parameter subnodes
245: Node firstParamN = DomUtil.getChild(descN, "parameter");
246: for (Node paramN = firstParamN; paramN != null; paramN = DomUtil
247: .getNext(paramN)) {
248: ParameterInfo pi = new ParameterInfo();
249: DomUtil.setAttributes(pi, paramN);
250: ci.addParameter(pi);
251: }
252:
253: // Add this info to our managed bean info
254: managed.addConstructor(ci);
255:
256: }
257: }
258:
259: /**
260: * @param managed ManagedBean instance
261: * @param mbeanNode mbean XML Node
262: */
263: private static void processMBeanAttributeNode(
264: final ManagedBean managed, final Node mbeanNode) {
265: Node firstN;
266: firstN = DomUtil.getChild(mbeanNode, "attribute");
267: for (Node descN = firstN; descN != null; descN = DomUtil
268: .getNext(descN)) {
269:
270: // Create new attribute info
271: AttributeInfo ai = new AttributeInfo();
272: DomUtil.setAttributes(ai, descN);
273:
274: // Process descriptor subnode
275: Node descriptorN = DomUtil.getChild(descN, "descriptor");
276: if (descriptorN != null) {
277: Node firstFieldN = DomUtil.getChild(descriptorN,
278: "field");
279: for (Node fieldN = firstFieldN; fieldN != null; fieldN = DomUtil
280: .getNext(fieldN)) {
281: FieldInfo fi = new FieldInfo();
282: DomUtil.setAttributes(fi, fieldN);
283: ai.addField(fi);
284: }
285: }
286:
287: // Add this info to our managed bean info
288: managed.addAttribute(ai);
289:
290: }
291: }
292:
293: /**
294: * @param managed ManagedBean instance
295: * @param mbeanNode mbean XML Node
296: */
297: private static void processMBeanDescriptorNode(
298: final ManagedBean managed, final Node mbeanNode) {
299: Node mbeanDescriptorN = DomUtil.getChild(mbeanNode,
300: "descriptor");
301: if (mbeanDescriptorN != null) {
302: Node firstFieldN = DomUtil.getChild(mbeanDescriptorN,
303: "field");
304: for (Node fieldN = firstFieldN; fieldN != null; fieldN = DomUtil
305: .getNext(fieldN)) {
306: FieldInfo fi = new FieldInfo();
307: DomUtil.setAttributes(fi, fieldN);
308: managed.addField(fi);
309: }
310: }
311: }
312: }
|