001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.modeler.modules;
019:
020: import java.io.InputStream;
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026: import org.apache.commons.modeler.AttributeInfo;
027: import org.apache.commons.modeler.ConstructorInfo;
028: import org.apache.commons.modeler.FieldInfo;
029: import org.apache.commons.modeler.ManagedBean;
030: import org.apache.commons.modeler.NotificationInfo;
031: import org.apache.commons.modeler.OperationInfo;
032: import org.apache.commons.modeler.ParameterInfo;
033: import org.apache.commons.modeler.Registry;
034: import org.apache.commons.modeler.util.DomUtil;
035: import org.w3c.dom.Document;
036: import org.w3c.dom.Node;
037:
038: public class MbeansDescriptorsDOMSource extends ModelerSource {
039: private static Log log = LogFactory
040: .getLog(MbeansDescriptorsDOMSource.class);
041:
042: Registry registry;
043: String location;
044: String type;
045: Object source;
046: List mbeans = new ArrayList();
047:
048: public void setRegistry(Registry reg) {
049: this .registry = reg;
050: }
051:
052: public void setLocation(String loc) {
053: this .location = loc;
054: }
055:
056: /** Used if a single component is loaded
057: *
058: * @param type
059: */
060: public void setType(String type) {
061: this .type = type;
062: }
063:
064: public void setSource(Object source) {
065: this .source = source;
066: }
067:
068: public List loadDescriptors(Registry registry, String location,
069: String type, Object source) throws Exception {
070: setRegistry(registry);
071: setLocation(location);
072: setType(type);
073: setSource(source);
074: execute();
075: return mbeans;
076: }
077:
078: public void execute() throws Exception {
079: if (registry == null)
080: registry = Registry.getRegistry();
081:
082: try {
083: InputStream stream = (InputStream) source;
084: long t1 = System.currentTimeMillis();
085: Document doc = DomUtil.readXml(stream);
086: // Ignore for now the name of the root element
087: Node descriptorsN = doc.getDocumentElement();
088: //Node descriptorsN=DomUtil.getChild(doc, "mbeans-descriptors");
089: if (descriptorsN == null) {
090: log.error("No descriptors found");
091: return;
092: }
093:
094: Node firstMbeanN = null;
095: if ("mbean".equals(descriptorsN.getNodeName())) {
096: firstMbeanN = descriptorsN;
097: } else {
098: firstMbeanN = DomUtil.getChild(descriptorsN, "mbean");
099: }
100:
101: if (firstMbeanN == null) {
102: log.error(" No mbean tags ");
103: return;
104: }
105:
106: // Process each <mbean> element
107: for (Node mbeanN = firstMbeanN; mbeanN != null; mbeanN = DomUtil
108: .getNext(mbeanN)) {
109:
110: // Create a new managed bean info
111: ManagedBean managed = new ManagedBean();
112: DomUtil.setAttributes(managed, mbeanN);
113: Node firstN;
114:
115: // Process descriptor subnode
116: Node mbeanDescriptorN = DomUtil.getChild(mbeanN,
117: "descriptor");
118: if (mbeanDescriptorN != null) {
119: Node firstFieldN = DomUtil.getChild(
120: mbeanDescriptorN, "field");
121: for (Node fieldN = firstFieldN; fieldN != null; fieldN = DomUtil
122: .getNext(fieldN)) {
123: FieldInfo fi = new FieldInfo();
124: DomUtil.setAttributes(fi, fieldN);
125: managed.addField(fi);
126: }
127: }
128:
129: // process attribute nodes
130: firstN = DomUtil.getChild(mbeanN, "attribute");
131: for (Node descN = firstN; descN != null; descN = DomUtil
132: .getNext(descN)) {
133:
134: // Create new attribute info
135: AttributeInfo ai = new AttributeInfo();
136: DomUtil.setAttributes(ai, descN);
137:
138: // Process descriptor subnode
139: Node descriptorN = DomUtil.getChild(descN,
140: "descriptor");
141: if (descriptorN != null) {
142: Node firstFieldN = DomUtil.getChild(
143: descriptorN, "field");
144: for (Node fieldN = firstFieldN; fieldN != null; fieldN = DomUtil
145: .getNext(fieldN)) {
146: FieldInfo fi = new FieldInfo();
147: DomUtil.setAttributes(fi, fieldN);
148: ai.addField(fi);
149: }
150: }
151:
152: // Add this info to our managed bean info
153: managed.addAttribute(ai);
154: if (log.isTraceEnabled()) {
155: log.trace("Create attribute " + ai);
156: }
157:
158: }
159:
160: // process constructor nodes
161: firstN = DomUtil.getChild(mbeanN, "constructor");
162: for (Node descN = firstN; descN != null; descN = DomUtil
163: .getNext(descN)) {
164:
165: // Create new constructor info
166: ConstructorInfo ci = new ConstructorInfo();
167: DomUtil.setAttributes(ci, descN);
168:
169: // Process descriptor subnode
170: Node firstDescriptorN = DomUtil.getChild(descN,
171: "descriptor");
172: if (firstDescriptorN != null) {
173: Node firstFieldN = DomUtil.getChild(
174: firstDescriptorN, "field");
175: for (Node fieldN = firstFieldN; fieldN != null; fieldN = DomUtil
176: .getNext(fieldN)) {
177: FieldInfo fi = new FieldInfo();
178: DomUtil.setAttributes(fi, fieldN);
179: ci.addField(fi);
180: }
181: }
182:
183: // Process parameter subnodes
184: Node firstParamN = DomUtil.getChild(descN,
185: "parameter");
186: for (Node paramN = firstParamN; paramN != null; paramN = DomUtil
187: .getNext(paramN)) {
188: ParameterInfo pi = new ParameterInfo();
189: DomUtil.setAttributes(pi, paramN);
190: ci.addParameter(pi);
191: }
192:
193: // Add this info to our managed bean info
194: managed.addConstructor(ci);
195: if (log.isTraceEnabled()) {
196: log.trace("Create constructor " + ci);
197: }
198:
199: }
200:
201: // process notification nodes
202: firstN = DomUtil.getChild(mbeanN, "notification");
203: for (Node descN = firstN; descN != null; descN = DomUtil
204: .getNext(descN)) {
205:
206: // Create new notification info
207: NotificationInfo ni = new NotificationInfo();
208: DomUtil.setAttributes(ni, descN);
209:
210: // Process descriptor subnode
211: Node firstDescriptorN = DomUtil.getChild(descN,
212: "descriptor");
213: if (firstDescriptorN != null) {
214: Node firstFieldN = DomUtil.getChild(
215: firstDescriptorN, "field");
216: for (Node fieldN = firstFieldN; fieldN != null; fieldN = DomUtil
217: .getNext(fieldN)) {
218: FieldInfo fi = new FieldInfo();
219: DomUtil.setAttributes(fi, fieldN);
220: ni.addField(fi);
221: }
222: }
223:
224: // Process notification-type subnodes
225: Node firstParamN = DomUtil.getChild(descN,
226: "notification-type");
227: for (Node paramN = firstParamN; paramN != null; paramN = DomUtil
228: .getNext(paramN)) {
229: ni.addNotifType(DomUtil.getContent(paramN));
230: }
231:
232: // Add this info to our managed bean info
233: managed.addNotification(ni);
234: if (log.isTraceEnabled()) {
235: log.trace("Created notification " + ni);
236: }
237:
238: }
239:
240: // process operation nodes
241: firstN = DomUtil.getChild(mbeanN, "operation");
242: for (Node descN = firstN; descN != null; descN = DomUtil
243: .getNext(descN))
244:
245: {
246:
247: // Create new operation info
248: OperationInfo oi = new OperationInfo();
249: DomUtil.setAttributes(oi, descN);
250:
251: // Process descriptor subnode
252: Node firstDescriptorN = DomUtil.getChild(descN,
253: "descriptor");
254: if (firstDescriptorN != null) {
255: Node firstFieldN = DomUtil.getChild(
256: firstDescriptorN, "field");
257: for (Node fieldN = firstFieldN; fieldN != null; fieldN = DomUtil
258: .getNext(fieldN)) {
259: FieldInfo fi = new FieldInfo();
260: DomUtil.setAttributes(fi, fieldN);
261: oi.addField(fi);
262: }
263: }
264:
265: // Process parameter subnodes
266: Node firstParamN = DomUtil.getChild(descN,
267: "parameter");
268: for (Node paramN = firstParamN; paramN != null; paramN = DomUtil
269: .getNext(paramN)) {
270: ParameterInfo pi = new ParameterInfo();
271: DomUtil.setAttributes(pi, paramN);
272: if (log.isTraceEnabled())
273: log.trace("Add param " + pi.getName());
274: oi.addParameter(pi);
275: }
276:
277: // Add this info to our managed bean info
278: managed.addOperation(oi);
279: if (log.isTraceEnabled()) {
280: log.trace("Create operation " + oi);
281: }
282:
283: }
284:
285: // Add the completed managed bean info to the registry
286: //registry.addManagedBean(managed);
287: mbeans.add(managed);
288:
289: }
290:
291: long t2 = System.currentTimeMillis();
292: log.debug("Reading descriptors ( dom ) " + (t2 - t1));
293: } catch (Exception ex) {
294: log.error("Error reading descriptors ", ex);
295: }
296: }
297: }
|