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