KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > modeler > modules > MbeansDescriptorsDigesterSource


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.apache.commons.modeler.modules;
19
20 import java.io.InputStream JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.apache.commons.digester.Digester;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.commons.modeler.Registry;
29
30 public class MbeansDescriptorsDigesterSource extends ModelerSource
31 {
32     private static Log log =
33             LogFactory.getLog(MbeansDescriptorsDigesterSource.class);
34
35     Registry registry;
36     String JavaDoc location;
37     String JavaDoc type;
38     Object JavaDoc source;
39     List JavaDoc mbeans=new ArrayList JavaDoc();
40
41     public void setRegistry(Registry reg) {
42         this.registry=reg;
43     }
44
45     public void setLocation( String JavaDoc loc ) {
46         this.location=loc;
47     }
48
49     /** Used if a single component is loaded
50      *
51      * @param type
52      */

53     public void setType( String JavaDoc type ) {
54        this.type=type;
55     }
56
57     public void setSource( Object JavaDoc source ) {
58         this.source=source;
59     }
60
61     public List JavaDoc loadDescriptors( Registry registry, String JavaDoc location,
62                                  String JavaDoc type, Object JavaDoc source)
63             throws Exception JavaDoc
64     {
65         setRegistry(registry);
66         setLocation(location);
67         setType(type);
68         setSource(source);
69         execute();
70         return mbeans;
71     }
72
73     public void execute() throws Exception JavaDoc {
74         if( registry==null ) registry=Registry.getRegistry();
75
76         InputStream JavaDoc stream=(InputStream JavaDoc)source;
77
78         long t1=System.currentTimeMillis();
79
80         Digester digester = new Digester();
81         digester.setNamespaceAware(false);
82         digester.setValidating(false);
83         URL JavaDoc url = registry.getClass().getResource
84                 ("/org/apache/commons/modeler/mbeans-descriptors.dtd");
85         digester.register
86                 ("-//Apache Software Foundation//DTD Model MBeans Configuration File",
87                         url.toString());
88
89         // Push our registry object onto the stack
90
digester.push(mbeans);
91
92         // Configure the parsing rules
93
digester.addObjectCreate
94                 ("mbeans-descriptors/mbean",
95                         "org.apache.commons.modeler.ManagedBean");
96         digester.addSetProperties
97                 ("mbeans-descriptors/mbean");
98         digester.addSetNext
99                 ("mbeans-descriptors/mbean",
100                         "add",
101                         "java.lang.Object");
102
103         digester.addObjectCreate
104                 ("mbeans-descriptors/mbean/attribute",
105                         "org.apache.commons.modeler.AttributeInfo");
106         digester.addSetProperties
107                 ("mbeans-descriptors/mbean/attribute");
108         digester.addSetNext
109                 ("mbeans-descriptors/mbean/attribute",
110                         "addAttribute",
111                         "org.apache.commons.modeler.AttributeInfo");
112
113         digester.addObjectCreate
114             ("mbeans-descriptors/mbean/attribute/descriptor/field",
115              "org.apache.commons.modeler.FieldInfo");
116         digester.addSetProperties
117             ("mbeans-descriptors/mbean/attribute/descriptor/field");
118         digester.addSetNext
119             ("mbeans-descriptors/mbean/attribute/descriptor/field",
120              "addField",
121              "org.apache.commons.modeler.FieldInfo");
122
123         digester.addObjectCreate
124                 ("mbeans-descriptors/mbean/constructor",
125                         "org.apache.commons.modeler.ConstructorInfo");
126         digester.addSetProperties
127                 ("mbeans-descriptors/mbean/constructor");
128         digester.addSetNext
129                 ("mbeans-descriptors/mbean/constructor",
130                         "addConstructor",
131                         "org.apache.commons.modeler.ConstructorInfo");
132
133         digester.addObjectCreate
134             ("mbeans-descriptors/mbean/constructor/descriptor/field",
135              "org.apache.commons.modeler.FieldInfo");
136         digester.addSetProperties
137             ("mbeans-descriptors/mbean/constructor/descriptor/field");
138         digester.addSetNext
139             ("mbeans-descriptors/mbean/constructor/descriptor/field",
140              "addField",
141              "org.apache.commons.modeler.FieldInfo");
142
143         digester.addObjectCreate
144                 ("mbeans-descriptors/mbean/constructor/parameter",
145                         "org.apache.commons.modeler.ParameterInfo");
146         digester.addSetProperties
147                 ("mbeans-descriptors/mbean/constructor/parameter");
148         digester.addSetNext
149                 ("mbeans-descriptors/mbean/constructor/parameter",
150                         "addParameter",
151                         "org.apache.commons.modeler.ParameterInfo");
152
153         digester.addObjectCreate
154             ("mbeans-descriptors/mbean/descriptor/field",
155              "org.apache.commons.modeler.FieldInfo");
156         digester.addSetProperties
157             ("mbeans-descriptors/mbean/descriptor/field");
158         digester.addSetNext
159             ("mbeans-descriptors/mbean/descriptor/field",
160              "addField",
161              "org.apache.commons.modeler.FieldInfo");
162
163         digester.addObjectCreate
164                 ("mbeans-descriptors/mbean/notification",
165                         "org.apache.commons.modeler.NotificationInfo");
166         digester.addSetProperties
167                 ("mbeans-descriptors/mbean/notification");
168         digester.addSetNext
169                 ("mbeans-descriptors/mbean/notification",
170                         "addNotification",
171                         "org.apache.commons.modeler.NotificationInfo");
172
173         digester.addObjectCreate
174             ("mbeans-descriptors/mbean/notification/descriptor/field",
175              "org.apache.commons.modeler.FieldInfo");
176         digester.addSetProperties
177             ("mbeans-descriptors/mbean/notification/descriptor/field");
178         digester.addSetNext
179             ("mbeans-descriptors/mbean/notification/descriptor/field",
180              "addField",
181              "org.apache.commons.modeler.FieldInfo");
182
183         digester.addCallMethod
184                 ("mbeans-descriptors/mbean/notification/notification-type",
185                         "addNotifType", 0);
186
187         digester.addObjectCreate
188                 ("mbeans-descriptors/mbean/operation",
189                         "org.apache.commons.modeler.OperationInfo");
190         digester.addSetProperties
191                 ("mbeans-descriptors/mbean/operation");
192         digester.addSetNext
193                 ("mbeans-descriptors/mbean/operation",
194                         "addOperation",
195                         "org.apache.commons.modeler.OperationInfo");
196
197         digester.addObjectCreate
198             ("mbeans-descriptors/mbean/operation/descriptor/field",
199              "org.apache.commons.modeler.FieldInfo");
200         digester.addSetProperties
201             ("mbeans-descriptors/mbean/operation/descriptor/field");
202         digester.addSetNext
203             ("mbeans-descriptors/mbean/operation/descriptor/field",
204              "addField",
205              "org.apache.commons.modeler.FieldInfo");
206
207         digester.addObjectCreate
208                 ("mbeans-descriptors/mbean/operation/parameter",
209                         "org.apache.commons.modeler.ParameterInfo");
210         digester.addSetProperties
211                 ("mbeans-descriptors/mbean/operation/parameter");
212         digester.addSetNext
213                 ("mbeans-descriptors/mbean/operation/parameter",
214                         "addParameter",
215                         "org.apache.commons.modeler.ParameterInfo");
216
217         // Process the input file to configure our registry
218
try {
219             digester.parse(stream);
220         } catch (Exception JavaDoc e) {
221             log.error("Error digesting Registry data", e);
222             throw e;
223         }
224         long t2=System.currentTimeMillis();
225 // if( t2-t1 > 500 )
226
log.info("Loaded registry information (digester) " + ( t2 - t1 ) + " ms");
227     }
228 }
229
Popular Tags