KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tomcat > util > modeler > modules > MbeansDescriptorsDOMSource


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

17
18
19 package org.apache.tomcat.util.modeler.modules;
20
21 import java.io.InputStream JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.tomcat.util.DomUtil;
28 import org.apache.tomcat.util.modeler.AttributeInfo;
29 import org.apache.tomcat.util.modeler.ManagedBean;
30 import org.apache.tomcat.util.modeler.NotificationInfo;
31 import org.apache.tomcat.util.modeler.OperationInfo;
32 import org.apache.tomcat.util.modeler.ParameterInfo;
33 import org.apache.tomcat.util.modeler.Registry;
34 import org.w3c.dom.Document JavaDoc;
35 import org.w3c.dom.Node JavaDoc;
36
37
38 public class MbeansDescriptorsDOMSource extends ModelerSource
39 {
40     private static Log log = LogFactory.getLog(MbeansDescriptorsDOMSource.class);
41
42     Registry registry;
43     String JavaDoc location;
44     String JavaDoc type;
45     Object JavaDoc source;
46     List JavaDoc mbeans=new ArrayList JavaDoc();
47
48     public void setRegistry(Registry reg) {
49         this.registry=reg;
50     }
51
52     public void setLocation( String JavaDoc loc ) {
53         this.location=loc;
54     }
55
56     /** Used if a single component is loaded
57      *
58      * @param type
59      */

60     public void setType( String JavaDoc type ) {
61        this.type=type;
62     }
63
64     public void setSource( Object JavaDoc source ) {
65         this.source=source;
66     }
67
68     public List JavaDoc loadDescriptors( Registry registry, String JavaDoc location,
69                                  String JavaDoc type, Object JavaDoc source)
70             throws Exception JavaDoc
71     {
72         setRegistry(registry);
73         setLocation(location);
74         setType(type);
75         setSource(source);
76         execute();
77         return mbeans;
78     }
79
80     public void execute() throws Exception JavaDoc {
81         if( registry==null ) registry=Registry.getRegistry();
82
83         try {
84             InputStream JavaDoc stream=(InputStream JavaDoc)source;
85             long t1=System.currentTimeMillis();
86             Document JavaDoc doc=DomUtil.readXml(stream);
87             // Ignore for now the name of the root element
88
Node JavaDoc descriptorsN=doc.getDocumentElement();
89             //Node descriptorsN=DomUtil.getChild(doc, "mbeans-descriptors");
90
if( descriptorsN == null ) {
91                 log.error("No descriptors found");
92                 return;
93             }
94
95             Node JavaDoc firstMbeanN=null;
96             if( "mbean".equals( descriptorsN.getNodeName() ) ) {
97                 firstMbeanN=descriptorsN;
98             } else {
99                 firstMbeanN=DomUtil.getChild(descriptorsN, "mbean");
100             }
101
102             if( firstMbeanN==null ) {
103                 log.error(" No mbean tags ");
104                 return;
105             }
106
107             // Process each <mbean> element
108
for (Node JavaDoc mbeanN = firstMbeanN; mbeanN != null;
109                  mbeanN= DomUtil.getNext(mbeanN))
110             {
111
112                 // Create a new managed bean info
113
ManagedBean managed=new ManagedBean();
114                 DomUtil.setAttributes(managed, mbeanN);
115                 Node JavaDoc firstN;
116
117                 // Process descriptor subnode
118
/*Node mbeanDescriptorN =
119                     DomUtil.getChild(mbeanN, "descriptor");
120                 if (mbeanDescriptorN != null) {
121                     Node firstFieldN =
122                         DomUtil.getChild(mbeanDescriptorN, "field");
123                     for (Node fieldN = firstFieldN; fieldN != null;
124                          fieldN = DomUtil.getNext(fieldN)) {
125                         FieldInfo fi = new FieldInfo();
126                         DomUtil.setAttributes(fi, fieldN);
127                         managed.addField(fi);
128                     }
129                 }*/

130
131                 // process attribute nodes
132
firstN=DomUtil.getChild( mbeanN, "attribute");
133                 for (Node JavaDoc descN = firstN; descN != null;
134                      descN = DomUtil.getNext( descN ))
135                 {
136
137                     // Create new attribute info
138
AttributeInfo ai=new AttributeInfo();
139                     DomUtil.setAttributes(ai, descN);
140
141                     // Process descriptor subnode
142
/*Node descriptorN =
143                         DomUtil.getChild(descN, "descriptor");
144                     if (descriptorN != null) {
145                         Node firstFieldN =
146                             DomUtil.getChild(descriptorN, "field");
147                         for (Node fieldN = firstFieldN; fieldN != null;
148                              fieldN = DomUtil.getNext(fieldN)) {
149                             FieldInfo fi = new FieldInfo();
150                             DomUtil.setAttributes(fi, fieldN);
151                             ai.addField(fi);
152                         }
153                     }
154                     */

155
156                     // Add this info to our managed bean info
157
managed.addAttribute( ai );
158                     if (log.isTraceEnabled()) {
159                         log.trace("Create attribute " + ai);
160                     }
161
162                 }
163
164                 // process constructor nodes
165
/*
166                 firstN=DomUtil.getChild( mbeanN, "constructor");
167                 for (Node descN = firstN; descN != null;
168                      descN = DomUtil.getNext( descN )) {
169
170                     // Create new constructor info
171                     ConstructorInfo ci=new ConstructorInfo();
172                     DomUtil.setAttributes(ci, descN);
173
174                     // Process descriptor subnode
175                     Node firstDescriptorN =
176                         DomUtil.getChild(descN, "descriptor");
177                     if (firstDescriptorN != null) {
178                         Node firstFieldN =
179                             DomUtil.getChild(firstDescriptorN, "field");
180                         for (Node fieldN = firstFieldN; fieldN != null;
181                              fieldN = DomUtil.getNext(fieldN)) {
182                             FieldInfo fi = new FieldInfo();
183                             DomUtil.setAttributes(fi, fieldN);
184                             ci.addField(fi);
185                         }
186                     }
187
188                     // Process parameter subnodes
189                     Node firstParamN=DomUtil.getChild( descN, "parameter");
190                     for (Node paramN = firstParamN; paramN != null;
191                          paramN = DomUtil.getNext(paramN))
192                     {
193                         ParameterInfo pi=new ParameterInfo();
194                         DomUtil.setAttributes(pi, paramN);
195                         ci.addParameter( pi );
196                     }
197
198                     // Add this info to our managed bean info
199                     managed.addConstructor( ci );
200                     if (log.isTraceEnabled()) {
201                         log.trace("Create constructor " + ci);
202                     }
203
204                 }*/

205
206                 // process notification nodes
207
firstN=DomUtil.getChild( mbeanN, "notification");
208                 for (Node JavaDoc descN = firstN; descN != null;
209                      descN = DomUtil.getNext( descN ))
210                 {
211
212                     // Create new notification info
213
NotificationInfo ni=new NotificationInfo();
214                     DomUtil.setAttributes(ni, descN);
215
216                     // Process descriptor subnode
217
/*Node firstDescriptorN =
218                         DomUtil.getChild(descN, "descriptor");
219                     if (firstDescriptorN != null) {
220                         Node firstFieldN =
221                             DomUtil.getChild(firstDescriptorN, "field");
222                         for (Node fieldN = firstFieldN; fieldN != null;
223                              fieldN = DomUtil.getNext(fieldN)) {
224                             FieldInfo fi = new FieldInfo();
225                             DomUtil.setAttributes(fi, fieldN);
226                             ni.addField(fi);
227                         }
228                     }*/

229
230                     // Process notification-type subnodes
231
Node JavaDoc firstParamN=DomUtil.getChild( descN, "notification-type");
232                     for (Node JavaDoc paramN = firstParamN; paramN != null;
233                          paramN = DomUtil.getNext(paramN))
234                     {
235                         ni.addNotifType( DomUtil.getContent(paramN) );
236                     }
237
238                     // Add this info to our managed bean info
239
managed.addNotification( ni );
240                     if (log.isTraceEnabled()) {
241                         log.trace("Created notification " + ni);
242                     }
243
244                 }
245
246                 // process operation nodes
247
firstN=DomUtil.getChild( mbeanN, "operation");
248                 for (Node JavaDoc descN = firstN; descN != null;
249                      descN = DomUtil.getNext( descN ))
250
251                 {
252
253                     // Create new operation info
254
OperationInfo oi=new OperationInfo();
255                     DomUtil.setAttributes(oi, descN);
256
257                     // Process descriptor subnode
258
/*Node firstDescriptorN =
259                         DomUtil.getChild(descN, "descriptor");
260                     if (firstDescriptorN != null) {
261                         Node firstFieldN =
262                             DomUtil.getChild(firstDescriptorN, "field");
263                         for (Node fieldN = firstFieldN; fieldN != null;
264                              fieldN = DomUtil.getNext(fieldN)) {
265                             FieldInfo fi = new FieldInfo();
266                             DomUtil.setAttributes(fi, fieldN);
267                             oi.addField(fi);
268                         }
269                     }*/

270
271                     // Process parameter subnodes
272
Node JavaDoc firstParamN=DomUtil.getChild( descN, "parameter");
273                     for (Node JavaDoc paramN = firstParamN; paramN != null;
274                          paramN = DomUtil.getNext(paramN))
275                     {
276                         ParameterInfo pi=new ParameterInfo();
277                         DomUtil.setAttributes(pi, paramN);
278                         if( log.isTraceEnabled())
279                             log.trace("Add param " + pi.getName());
280                         oi.addParameter( pi );
281                     }
282
283                     // Add this info to our managed bean info
284
managed.addOperation( oi );
285                     if( log.isTraceEnabled()) {
286                         log.trace("Create operation " + oi);
287                     }
288
289                 }
290
291                 // Add the completed managed bean info to the registry
292
//registry.addManagedBean(managed);
293
mbeans.add( managed );
294
295             }
296
297             long t2=System.currentTimeMillis();
298             log.debug( "Reading descriptors ( dom ) " + (t2-t1));
299         } catch( Exception JavaDoc ex ) {
300             log.error( "Error reading descriptors ", ex);
301         }
302     }
303 }
304
Popular Tags