View Javadoc

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.commons.modeler.modules;
20  
21  import java.io.InputStream;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.commons.modeler.AttributeInfo;
28  import org.apache.commons.modeler.ConstructorInfo;
29  import org.apache.commons.modeler.FieldInfo;
30  import org.apache.commons.modeler.ManagedBean;
31  import org.apache.commons.modeler.NotificationInfo;
32  import org.apache.commons.modeler.OperationInfo;
33  import org.apache.commons.modeler.ParameterInfo;
34  import org.apache.commons.modeler.Registry;
35  import org.apache.commons.modeler.util.DomUtil;
36  import org.w3c.dom.Document;
37  import org.w3c.dom.Node;
38  
39  
40  public class MbeansDescriptorsDOMSource extends ModelerSource
41  {
42      private static Log log = LogFactory.getLog(MbeansDescriptorsDOMSource.class);
43  
44      Registry registry;
45      String location;
46      String type;
47      Object source;
48      List mbeans=new ArrayList();
49  
50      public void setRegistry(Registry reg) {
51          this.registry=reg;
52      }
53  
54      public void setLocation( String loc ) {
55          this.location=loc;
56      }
57  
58      /** Used if a single component is loaded
59       *
60       * @param type
61       */
62      public void setType( String type ) {
63         this.type=type;
64      }
65  
66      public void setSource( Object source ) {
67          this.source=source;
68      }
69  
70      public List loadDescriptors( Registry registry, String location,
71                                   String type, Object source)
72              throws Exception
73      {
74          setRegistry(registry);
75          setLocation(location);
76          setType(type);
77          setSource(source);
78          execute();
79          return mbeans;
80      }
81  
82      public void execute() throws Exception {
83          if( registry==null ) registry=Registry.getRegistry();
84  
85          try {
86              InputStream stream=(InputStream)source;
87              long t1=System.currentTimeMillis();
88              Document doc=DomUtil.readXml(stream);
89              // Ignore for now the name of the root element
90              Node descriptorsN=doc.getDocumentElement();
91              //Node descriptorsN=DomUtil.getChild(doc, "mbeans-descriptors");
92              if( descriptorsN == null ) {
93                  log.error("No descriptors found");
94                  return;
95              }
96  
97              Node firstMbeanN=null;
98              if( "mbean".equals( descriptorsN.getNodeName() ) ) {
99                  firstMbeanN=descriptorsN;
100             } else {
101                 firstMbeanN=DomUtil.getChild(descriptorsN, "mbean");
102             }
103 
104             if( firstMbeanN==null ) {
105                 log.error(" No mbean tags ");
106                 return;
107             }
108 
109             // Process each <mbean> element
110             for (Node mbeanN = firstMbeanN; mbeanN != null;
111                  mbeanN= DomUtil.getNext(mbeanN))
112             {
113 
114                 // Create a new managed bean info
115                 ManagedBean managed=new ManagedBean();
116                 DomUtil.setAttributes(managed, mbeanN);
117                 Node firstN;
118 
119                 // Process descriptor subnode
120                 Node mbeanDescriptorN =
121                     DomUtil.getChild(mbeanN, "descriptor");
122                 if (mbeanDescriptorN != null) {
123                     Node firstFieldN =
124                         DomUtil.getChild(mbeanDescriptorN, "field");
125                     for (Node fieldN = firstFieldN; fieldN != null;
126                          fieldN = DomUtil.getNext(fieldN)) {
127                         FieldInfo fi = new FieldInfo();
128                         DomUtil.setAttributes(fi, fieldN);
129                         managed.addField(fi);
130                     }
131                 }
132 
133                 // process attribute nodes
134                 firstN=DomUtil.getChild( mbeanN, "attribute");
135                 for (Node descN = firstN; descN != null;
136                      descN = DomUtil.getNext( descN ))
137                 {
138 
139                     // Create new attribute info
140                     AttributeInfo ai=new AttributeInfo();
141                     DomUtil.setAttributes(ai, descN);
142 
143                     // Process descriptor subnode
144                     Node descriptorN =
145                         DomUtil.getChild(descN, "descriptor");
146                     if (descriptorN != null) {
147                         Node firstFieldN =
148                             DomUtil.getChild(descriptorN, "field");
149                         for (Node fieldN = firstFieldN; fieldN != null;
150                              fieldN = DomUtil.getNext(fieldN)) {
151                             FieldInfo fi = new FieldInfo();
152                             DomUtil.setAttributes(fi, fieldN);
153                             ai.addField(fi);
154                         }
155                     }
156 
157                     // Add this info to our managed bean info
158                     managed.addAttribute( ai );
159                     if (log.isTraceEnabled()) {
160                         log.trace("Create attribute " + ai);
161                     }
162 
163                 }
164 
165                 // process constructor nodes
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 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 firstParamN=DomUtil.getChild( descN, "notification-type");
232                     for (Node 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 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 firstParamN=DomUtil.getChild( descN, "parameter");
273                     for (Node 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 ex ) {
300             log.error( "Error reading descriptors ", ex);
301         }
302     }
303 }