View Javadoc

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;
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              // Ignore for now the name of the root element
89              Node descriptorsN=doc.getDocumentElement();
90              //Node descriptorsN=DomUtil.getChild(doc, "mbeans-descriptors");
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             // Process each <mbean> element
109             for (Node mbeanN = firstMbeanN; mbeanN != null;
110                  mbeanN= DomUtil.getNext(mbeanN))
111             {
112 
113                 // Create a new managed bean info
114                 ManagedBean managed=new ManagedBean();
115                 DomUtil.setAttributes(managed, mbeanN);
116                 Node firstN;
117 
118                 // Process descriptor subnode
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                 // process attribute nodes
133                 firstN=DomUtil.getChild( mbeanN, "attribute");
134                 for (Node descN = firstN; descN != null;
135                      descN = DomUtil.getNext( descN ))
136                 {
137 
138                     // Create new attribute info
139                     AttributeInfo ai=new AttributeInfo();
140                     DomUtil.setAttributes(ai, descN);
141 
142                     // Process descriptor subnode
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                     // 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                 firstN=DomUtil.getChild( mbeanN, "constructor");
166                 for (Node descN = firstN; descN != null;
167                      descN = DomUtil.getNext( descN )) {
168 
169                     // Create new constructor info
170                     ConstructorInfo ci=new ConstructorInfo();
171                     DomUtil.setAttributes(ci, descN);
172 
173                     // Process descriptor subnode
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                     // Process parameter subnodes
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                     // Add this info to our managed bean info
198                     managed.addConstructor( ci );
199                     if (log.isTraceEnabled()) {
200                         log.trace("Create constructor " + ci);
201                     }
202 
203                 }
204 
205                 // process notification nodes
206                 firstN=DomUtil.getChild( mbeanN, "notification");
207                 for (Node descN = firstN; descN != null;
208                      descN = DomUtil.getNext( descN ))
209                 {
210 
211                     // Create new notification info
212                     NotificationInfo ni=new NotificationInfo();
213                     DomUtil.setAttributes(ni, descN);
214 
215                     // Process descriptor subnode
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                     // Process notification-type subnodes
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                     // Add this info to our managed bean info
238                     managed.addNotification( ni );
239                     if (log.isTraceEnabled()) {
240                         log.trace("Created notification " + ni);
241                     }
242 
243                 }
244 
245                 // process operation nodes
246                 firstN=DomUtil.getChild( mbeanN, "operation");
247                 for (Node descN = firstN; descN != null;
248                      descN = DomUtil.getNext( descN ))
249 
250                 {
251 
252                     // Create new operation info
253                     OperationInfo oi=new OperationInfo();
254                     DomUtil.setAttributes(oi, descN);
255 
256                     // Process descriptor subnode
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                     // Process parameter subnodes
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                     // Add this info to our managed bean info
283                     managed.addOperation( oi );
284                     if( log.isTraceEnabled()) {
285                         log.trace("Create operation " + oi);
286                     }
287 
288                 }
289 
290                 // Add the completed managed bean info to the registry
291                 //registry.addManagedBean(managed);
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 }