1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.modeler.modules;
19
20 import java.io.InputStream;
21 import java.util.ArrayList;
22 import java.util.List;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.commons.modeler.AttributeInfo;
27 import org.apache.commons.modeler.ConstructorInfo;
28 import org.apache.commons.modeler.FieldInfo;
29 import org.apache.commons.modeler.ManagedBean;
30 import org.apache.commons.modeler.NotificationInfo;
31 import org.apache.commons.modeler.OperationInfo;
32 import org.apache.commons.modeler.ParameterInfo;
33 import org.apache.commons.modeler.Registry;
34 import org.apache.commons.modeler.util.DomUtil;
35 import org.w3c.dom.Document;
36 import org.w3c.dom.Node;
37
38
39 public class MbeansDescriptorsDOMSource extends ModelerSource
40 {
41 private static Log log = LogFactory.getLog(MbeansDescriptorsDOMSource.class);
42
43 Registry registry;
44 String location;
45 String type;
46 Object source;
47 List mbeans=new ArrayList();
48
49 public void setRegistry(Registry reg) {
50 this.registry=reg;
51 }
52
53 public void setLocation( String loc ) {
54 this.location=loc;
55 }
56
57 /*** Used if a single component is loaded
58 *
59 * @param type
60 */
61 public void setType( String type ) {
62 this.type=type;
63 }
64
65 public void setSource( Object source ) {
66 this.source=source;
67 }
68
69 public List loadDescriptors( Registry registry, String location,
70 String type, Object source)
71 throws Exception
72 {
73 setRegistry(registry);
74 setLocation(location);
75 setType(type);
76 setSource(source);
77 execute();
78 return mbeans;
79 }
80
81 public void execute() throws Exception {
82 if( registry==null ) registry=Registry.getRegistry();
83
84 try {
85 InputStream stream=(InputStream)source;
86 long t1=System.currentTimeMillis();
87 Document doc=DomUtil.readXml(stream);
88
89 Node descriptorsN=doc.getDocumentElement();
90
91 if( descriptorsN == null ) {
92 log.error("No descriptors found");
93 return;
94 }
95
96 Node firstMbeanN=null;
97 if( "mbean".equals( descriptorsN.getNodeName() ) ) {
98 firstMbeanN=descriptorsN;
99 } else {
100 firstMbeanN=DomUtil.getChild(descriptorsN, "mbean");
101 }
102
103 if( firstMbeanN==null ) {
104 log.error(" No mbean tags ");
105 return;
106 }
107
108
109 for (Node mbeanN = firstMbeanN; mbeanN != null;
110 mbeanN= DomUtil.getNext(mbeanN))
111 {
112
113
114 ManagedBean managed=new ManagedBean();
115 DomUtil.setAttributes(managed, mbeanN);
116 Node firstN;
117
118
119 Node mbeanDescriptorN =
120 DomUtil.getChild(mbeanN, "descriptor");
121 if (mbeanDescriptorN != null) {
122 Node firstFieldN =
123 DomUtil.getChild(mbeanDescriptorN, "field");
124 for (Node fieldN = firstFieldN; fieldN != null;
125 fieldN = DomUtil.getNext(fieldN)) {
126 FieldInfo fi = new FieldInfo();
127 DomUtil.setAttributes(fi, fieldN);
128 managed.addField(fi);
129 }
130 }
131
132
133 firstN=DomUtil.getChild( mbeanN, "attribute");
134 for (Node descN = firstN; descN != null;
135 descN = DomUtil.getNext( descN ))
136 {
137
138
139 AttributeInfo ai=new AttributeInfo();
140 DomUtil.setAttributes(ai, descN);
141
142
143 Node descriptorN =
144 DomUtil.getChild(descN, "descriptor");
145 if (descriptorN != null) {
146 Node firstFieldN =
147 DomUtil.getChild(descriptorN, "field");
148 for (Node fieldN = firstFieldN; fieldN != null;
149 fieldN = DomUtil.getNext(fieldN)) {
150 FieldInfo fi = new FieldInfo();
151 DomUtil.setAttributes(fi, fieldN);
152 ai.addField(fi);
153 }
154 }
155
156
157 managed.addAttribute( ai );
158 if (log.isTraceEnabled()) {
159 log.trace("Create attribute " + ai);
160 }
161
162 }
163
164
165 firstN=DomUtil.getChild( mbeanN, "constructor");
166 for (Node descN = firstN; descN != null;
167 descN = DomUtil.getNext( descN )) {
168
169
170 ConstructorInfo ci=new ConstructorInfo();
171 DomUtil.setAttributes(ci, descN);
172
173
174 Node firstDescriptorN =
175 DomUtil.getChild(descN, "descriptor");
176 if (firstDescriptorN != null) {
177 Node firstFieldN =
178 DomUtil.getChild(firstDescriptorN, "field");
179 for (Node fieldN = firstFieldN; fieldN != null;
180 fieldN = DomUtil.getNext(fieldN)) {
181 FieldInfo fi = new FieldInfo();
182 DomUtil.setAttributes(fi, fieldN);
183 ci.addField(fi);
184 }
185 }
186
187
188 Node firstParamN=DomUtil.getChild( descN, "parameter");
189 for (Node paramN = firstParamN; paramN != null;
190 paramN = DomUtil.getNext(paramN))
191 {
192 ParameterInfo pi=new ParameterInfo();
193 DomUtil.setAttributes(pi, paramN);
194 ci.addParameter( pi );
195 }
196
197
198 managed.addConstructor( ci );
199 if (log.isTraceEnabled()) {
200 log.trace("Create constructor " + ci);
201 }
202
203 }
204
205
206 firstN=DomUtil.getChild( mbeanN, "notification");
207 for (Node descN = firstN; descN != null;
208 descN = DomUtil.getNext( descN ))
209 {
210
211
212 NotificationInfo ni=new NotificationInfo();
213 DomUtil.setAttributes(ni, descN);
214
215
216 Node firstDescriptorN =
217 DomUtil.getChild(descN, "descriptor");
218 if (firstDescriptorN != null) {
219 Node firstFieldN =
220 DomUtil.getChild(firstDescriptorN, "field");
221 for (Node fieldN = firstFieldN; fieldN != null;
222 fieldN = DomUtil.getNext(fieldN)) {
223 FieldInfo fi = new FieldInfo();
224 DomUtil.setAttributes(fi, fieldN);
225 ni.addField(fi);
226 }
227 }
228
229
230 Node firstParamN=DomUtil.getChild( descN, "notification-type");
231 for (Node paramN = firstParamN; paramN != null;
232 paramN = DomUtil.getNext(paramN))
233 {
234 ni.addNotifType( DomUtil.getContent(paramN) );
235 }
236
237
238 managed.addNotification( ni );
239 if (log.isTraceEnabled()) {
240 log.trace("Created notification " + ni);
241 }
242
243 }
244
245
246 firstN=DomUtil.getChild( mbeanN, "operation");
247 for (Node descN = firstN; descN != null;
248 descN = DomUtil.getNext( descN ))
249
250 {
251
252
253 OperationInfo oi=new OperationInfo();
254 DomUtil.setAttributes(oi, descN);
255
256
257 Node firstDescriptorN =
258 DomUtil.getChild(descN, "descriptor");
259 if (firstDescriptorN != null) {
260 Node firstFieldN =
261 DomUtil.getChild(firstDescriptorN, "field");
262 for (Node fieldN = firstFieldN; fieldN != null;
263 fieldN = DomUtil.getNext(fieldN)) {
264 FieldInfo fi = new FieldInfo();
265 DomUtil.setAttributes(fi, fieldN);
266 oi.addField(fi);
267 }
268 }
269
270
271 Node firstParamN=DomUtil.getChild( descN, "parameter");
272 for (Node paramN = firstParamN; paramN != null;
273 paramN = DomUtil.getNext(paramN))
274 {
275 ParameterInfo pi=new ParameterInfo();
276 DomUtil.setAttributes(pi, paramN);
277 if( log.isTraceEnabled())
278 log.trace("Add param " + pi.getName());
279 oi.addParameter( pi );
280 }
281
282
283 managed.addOperation( oi );
284 if( log.isTraceEnabled()) {
285 log.trace("Create operation " + oi);
286 }
287
288 }
289
290
291
292 mbeans.add( managed );
293
294 }
295
296 long t2=System.currentTimeMillis();
297 log.debug( "Reading descriptors ( dom ) " + (t2-t1));
298 } catch( Exception ex ) {
299 log.error( "Error reading descriptors ", ex);
300 }
301 }
302 }