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.tomcat.util.modeler.modules;
019:
020: import java.io.InputStream;
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: import org.apache.juli.logging.Log;
025: import org.apache.juli.logging.LogFactory;
026: import org.apache.tomcat.util.DomUtil;
027: import org.apache.tomcat.util.modeler.AttributeInfo;
028: import org.apache.tomcat.util.modeler.ManagedBean;
029: import org.apache.tomcat.util.modeler.NotificationInfo;
030: import org.apache.tomcat.util.modeler.OperationInfo;
031: import org.apache.tomcat.util.modeler.ParameterInfo;
032: import org.apache.tomcat.util.modeler.Registry;
033: import org.w3c.dom.Document;
034: import org.w3c.dom.Node;
035:
036: public class MbeansDescriptorsDOMSource extends ModelerSource {
037: private static Log log = LogFactory
038: .getLog(MbeansDescriptorsDOMSource.class);
039:
040: Registry registry;
041: String location;
042: String type;
043: Object source;
044: List mbeans = new ArrayList();
045:
046: public void setRegistry(Registry reg) {
047: this .registry = reg;
048: }
049:
050: public void setLocation(String loc) {
051: this .location = loc;
052: }
053:
054: /** Used if a single component is loaded
055: *
056: * @param type
057: */
058: public void setType(String type) {
059: this .type = type;
060: }
061:
062: public void setSource(Object source) {
063: this .source = source;
064: }
065:
066: public List loadDescriptors(Registry registry, String location,
067: String type, Object source) throws Exception {
068: setRegistry(registry);
069: setLocation(location);
070: setType(type);
071: setSource(source);
072: execute();
073: return mbeans;
074: }
075:
076: public void execute() throws Exception {
077: if (registry == null)
078: registry = Registry.getRegistry();
079:
080: try {
081: InputStream stream = (InputStream) source;
082: long t1 = System.currentTimeMillis();
083: Document doc = DomUtil.readXml(stream);
084: // Ignore for now the name of the root element
085: Node descriptorsN = doc.getDocumentElement();
086: //Node descriptorsN=DomUtil.getChild(doc, "mbeans-descriptors");
087: if (descriptorsN == null) {
088: log.error("No descriptors found");
089: return;
090: }
091:
092: Node firstMbeanN = null;
093: if ("mbean".equals(descriptorsN.getNodeName())) {
094: firstMbeanN = descriptorsN;
095: } else {
096: firstMbeanN = DomUtil.getChild(descriptorsN, "mbean");
097: }
098:
099: if (firstMbeanN == null) {
100: log.error(" No mbean tags ");
101: return;
102: }
103:
104: // Process each <mbean> element
105: for (Node mbeanN = firstMbeanN; mbeanN != null; mbeanN = DomUtil
106: .getNext(mbeanN)) {
107:
108: // Create a new managed bean info
109: ManagedBean managed = new ManagedBean();
110: DomUtil.setAttributes(managed, mbeanN);
111: Node firstN;
112:
113: // Process descriptor subnode
114: /*Node mbeanDescriptorN =
115: DomUtil.getChild(mbeanN, "descriptor");
116: if (mbeanDescriptorN != null) {
117: Node firstFieldN =
118: DomUtil.getChild(mbeanDescriptorN, "field");
119: for (Node fieldN = firstFieldN; fieldN != null;
120: fieldN = DomUtil.getNext(fieldN)) {
121: FieldInfo fi = new FieldInfo();
122: DomUtil.setAttributes(fi, fieldN);
123: managed.addField(fi);
124: }
125: }*/
126:
127: // process attribute nodes
128: firstN = DomUtil.getChild(mbeanN, "attribute");
129: for (Node descN = firstN; descN != null; descN = DomUtil
130: .getNext(descN)) {
131:
132: // Create new attribute info
133: AttributeInfo ai = new AttributeInfo();
134: DomUtil.setAttributes(ai, descN);
135:
136: // Process descriptor subnode
137: /*Node descriptorN =
138: DomUtil.getChild(descN, "descriptor");
139: if (descriptorN != null) {
140: Node firstFieldN =
141: DomUtil.getChild(descriptorN, "field");
142: for (Node fieldN = firstFieldN; fieldN != null;
143: fieldN = DomUtil.getNext(fieldN)) {
144: FieldInfo fi = new FieldInfo();
145: DomUtil.setAttributes(fi, fieldN);
146: ai.addField(fi);
147: }
148: }
149: */
150:
151: // Add this info to our managed bean info
152: managed.addAttribute(ai);
153: if (log.isTraceEnabled()) {
154: log.trace("Create attribute " + ai);
155: }
156:
157: }
158:
159: // process constructor nodes
160: /*
161: firstN=DomUtil.getChild( mbeanN, "constructor");
162: for (Node descN = firstN; descN != null;
163: descN = DomUtil.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 =
171: DomUtil.getChild(descN, "descriptor");
172: if (firstDescriptorN != null) {
173: Node firstFieldN =
174: DomUtil.getChild(firstDescriptorN, "field");
175: for (Node fieldN = firstFieldN; fieldN != null;
176: fieldN = DomUtil.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, "parameter");
185: for (Node paramN = firstParamN; paramN != null;
186: paramN = DomUtil.getNext(paramN))
187: {
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 =
212: DomUtil.getChild(descN, "descriptor");
213: if (firstDescriptorN != null) {
214: Node firstFieldN =
215: DomUtil.getChild(firstDescriptorN, "field");
216: for (Node fieldN = firstFieldN; fieldN != null;
217: fieldN = DomUtil.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 =
253: DomUtil.getChild(descN, "descriptor");
254: if (firstDescriptorN != null) {
255: Node firstFieldN =
256: DomUtil.getChild(firstDescriptorN, "field");
257: for (Node fieldN = firstFieldN; fieldN != null;
258: fieldN = DomUtil.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: }
|