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.net.URL;
022: import java.util.ArrayList;
023: import java.util.List;
024:
025: import org.apache.tomcat.util.digester.Digester;
026: import org.apache.tomcat.util.modeler.Registry;
027: import org.apache.juli.logging.Log;
028: import org.apache.juli.logging.LogFactory;
029:
030: public class MbeansDescriptorsDigesterSource extends ModelerSource {
031: private static Log log = LogFactory
032: .getLog(MbeansDescriptorsDigesterSource.class);
033:
034: Registry registry;
035: String location;
036: String type;
037: Object source;
038: List mbeans = new ArrayList();
039: protected static Digester digester = null;
040:
041: protected static Digester createDigester(Registry registry) {
042:
043: Digester digester = new Digester();
044: digester.setNamespaceAware(false);
045: digester.setValidating(false);
046: URL url = Registry
047: .getRegistry(null, null)
048: .getClass()
049: .getResource(
050: "/org/apache/tomcat/util/modeler/mbeans-descriptors.dtd");
051: digester
052: .register(
053: "-//Apache Software Foundation//DTD Model MBeans Configuration File",
054: url.toString());
055:
056: // Configure the parsing rules
057: digester.addObjectCreate("mbeans-descriptors/mbean",
058: "org.apache.tomcat.util.modeler.ManagedBean");
059: digester.addSetProperties("mbeans-descriptors/mbean");
060: digester.addSetNext("mbeans-descriptors/mbean", "add",
061: "java.lang.Object");
062:
063: digester.addObjectCreate("mbeans-descriptors/mbean/attribute",
064: "org.apache.tomcat.util.modeler.AttributeInfo");
065: digester.addSetProperties("mbeans-descriptors/mbean/attribute");
066: digester.addSetNext("mbeans-descriptors/mbean/attribute",
067: "addAttribute",
068: "org.apache.tomcat.util.modeler.AttributeInfo");
069:
070: /*digester.addObjectCreate
071: ("mbeans-descriptors/mbean/attribute/descriptor/field",
072: "org.apache.tomcat.util.modeler.FieldInfo");
073: digester.addSetProperties
074: ("mbeans-descriptors/mbean/attribute/descriptor/field");
075: digester.addSetNext
076: ("mbeans-descriptors/mbean/attribute/descriptor/field",
077: "addField",
078: "org.apache.tomcat.util.modeler.FieldInfo");
079:
080: digester.addObjectCreate
081: ("mbeans-descriptors/mbean/constructor",
082: "org.apache.tomcat.util.modeler.ConstructorInfo");
083: digester.addSetProperties
084: ("mbeans-descriptors/mbean/constructor");
085: digester.addSetNext
086: ("mbeans-descriptors/mbean/constructor",
087: "addConstructor",
088: "org.apache.tomcat.util.modeler.ConstructorInfo");
089:
090: digester.addObjectCreate
091: ("mbeans-descriptors/mbean/constructor/descriptor/field",
092: "org.apache.tomcat.util.modeler.FieldInfo");
093: digester.addSetProperties
094: ("mbeans-descriptors/mbean/constructor/descriptor/field");
095: digester.addSetNext
096: ("mbeans-descriptors/mbean/constructor/descriptor/field",
097: "addField",
098: "org.apache.tomcat.util.modeler.FieldInfo");
099:
100: digester.addObjectCreate
101: ("mbeans-descriptors/mbean/constructor/parameter",
102: "org.apache.tomcat.util.modeler.ParameterInfo");
103: digester.addSetProperties
104: ("mbeans-descriptors/mbean/constructor/parameter");
105: digester.addSetNext
106: ("mbeans-descriptors/mbean/constructor/parameter",
107: "addParameter",
108: "org.apache.tomcat.util.modeler.ParameterInfo");
109:
110: digester.addObjectCreate
111: ("mbeans-descriptors/mbean/descriptor/field",
112: "org.apache.tomcat.util.modeler.FieldInfo");
113: digester.addSetProperties
114: ("mbeans-descriptors/mbean/descriptor/field");
115: digester.addSetNext
116: ("mbeans-descriptors/mbean/descriptor/field",
117: "addField",
118: "org.apache.tomcat.util.modeler.FieldInfo");
119: */
120: digester.addObjectCreate(
121: "mbeans-descriptors/mbean/notification",
122: "org.apache.tomcat.util.modeler.NotificationInfo");
123: digester
124: .addSetProperties("mbeans-descriptors/mbean/notification");
125: digester.addSetNext("mbeans-descriptors/mbean/notification",
126: "addNotification",
127: "org.apache.tomcat.util.modeler.NotificationInfo");
128:
129: digester
130: .addObjectCreate(
131: "mbeans-descriptors/mbean/notification/descriptor/field",
132: "org.apache.tomcat.util.modeler.FieldInfo");
133: digester
134: .addSetProperties("mbeans-descriptors/mbean/notification/descriptor/field");
135: digester
136: .addSetNext(
137: "mbeans-descriptors/mbean/notification/descriptor/field",
138: "addField",
139: "org.apache.tomcat.util.modeler.FieldInfo");
140:
141: digester
142: .addCallMethod(
143: "mbeans-descriptors/mbean/notification/notification-type",
144: "addNotifType", 0);
145:
146: digester.addObjectCreate("mbeans-descriptors/mbean/operation",
147: "org.apache.tomcat.util.modeler.OperationInfo");
148: digester.addSetProperties("mbeans-descriptors/mbean/operation");
149: digester.addSetNext("mbeans-descriptors/mbean/operation",
150: "addOperation",
151: "org.apache.tomcat.util.modeler.OperationInfo");
152:
153: digester.addObjectCreate(
154: "mbeans-descriptors/mbean/operation/descriptor/field",
155: "org.apache.tomcat.util.modeler.FieldInfo");
156: digester
157: .addSetProperties("mbeans-descriptors/mbean/operation/descriptor/field");
158: digester.addSetNext(
159: "mbeans-descriptors/mbean/operation/descriptor/field",
160: "addField", "org.apache.tomcat.util.modeler.FieldInfo");
161:
162: digester.addObjectCreate(
163: "mbeans-descriptors/mbean/operation/parameter",
164: "org.apache.tomcat.util.modeler.ParameterInfo");
165: digester
166: .addSetProperties("mbeans-descriptors/mbean/operation/parameter");
167: digester.addSetNext(
168: "mbeans-descriptors/mbean/operation/parameter",
169: "addParameter",
170: "org.apache.tomcat.util.modeler.ParameterInfo");
171:
172: return digester;
173:
174: }
175:
176: public void setRegistry(Registry reg) {
177: this .registry = reg;
178: }
179:
180: public void setLocation(String loc) {
181: this .location = loc;
182: }
183:
184: /** Used if a single component is loaded
185: *
186: * @param type
187: */
188: public void setType(String type) {
189: this .type = type;
190: }
191:
192: public void setSource(Object source) {
193: this .source = source;
194: }
195:
196: public List loadDescriptors(Registry registry, String location,
197: String type, Object source) throws Exception {
198: setRegistry(registry);
199: setLocation(location);
200: setType(type);
201: setSource(source);
202: execute();
203: return mbeans;
204: }
205:
206: public void execute() throws Exception {
207: if (registry == null) {
208: registry = Registry.getRegistry(null, null);
209: }
210:
211: InputStream stream = (InputStream) source;
212:
213: if (digester == null) {
214: digester = createDigester(registry);
215: }
216:
217: synchronized (digester) {
218:
219: // Process the input file to configure our registry
220: try {
221: // Push our registry object onto the stack
222: digester.push(mbeans);
223: digester.parse(stream);
224: } catch (Exception e) {
225: log.error("Error digesting Registry data", e);
226: throw e;
227: } finally {
228: digester.reset();
229: }
230:
231: }
232:
233: }
234: }
|