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  package org.apache.commons.modeler.modules;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import javax.management.DynamicMBean;
23  import javax.management.MBeanAttributeInfo;
24  import javax.management.MBeanInfo;
25  import javax.management.MBeanOperationInfo;
26  import javax.management.MBeanParameterInfo;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.commons.modeler.AttributeInfo;
31  import org.apache.commons.modeler.ManagedBean;
32  import org.apache.commons.modeler.OperationInfo;
33  import org.apache.commons.modeler.ParameterInfo;
34  import org.apache.commons.modeler.Registry;
35  
36  
37  /*** Extract metadata from a dynamic mbean.
38   * Used to wrap a dynamic mbean in order to implement persistence.
39   * 
40   * This is really an ugly asspect of the JMX spec - we need to convery 
41   * from normal metainfo to model metainfo. The info is the same, but
42   * they use a different class. Just like the DOM spec - where all implementations
43   * get an order of unneeded complexity from the various types. 
44   * 
45   */ 
46  public class MbeansDescriptorsDynamicMBeanSource extends ModelerSource
47  {
48      private static Log log = LogFactory.getLog(MbeansDescriptorsDynamicMBeanSource.class);
49  
50      Registry registry;
51      String location;
52      String type;
53      Object source;
54      List mbeans=new ArrayList();
55  
56      public void setRegistry(Registry reg) {
57          this.registry=reg;
58      }
59  
60      public void setLocation( String loc ) {
61          this.location=loc;
62      }
63  
64      /*** Used if a single component is loaded
65       *
66       * @param type
67       */
68      public void setType( String type ) {
69         this.type=type;
70      }
71  
72      public void setSource( Object source ) {
73          this.source=source;
74      }
75  
76      public List loadDescriptors( Registry registry, String location,
77                                   String type, Object source)
78              throws Exception
79      {
80          setRegistry(registry);
81          setLocation(location);
82          setType(type);
83          setSource(source);
84          execute();
85          return mbeans;
86      }
87  
88      public void execute() throws Exception {
89          if( registry==null ) registry=Registry.getRegistry();
90          try {
91              ManagedBean managed=createManagedBean(registry, null, source, type);
92              if( managed==null ) return;
93              managed.setName( type );
94              
95              mbeans.add(managed);
96  
97          } catch( Exception ex ) {
98              log.error( "Error reading descriptors ", ex);
99          }
100     }
101 
102 
103 
104     // ------------ Implementation for non-declared introspection classes
105 
106 
107     /***
108      * XXX Find if the 'className' is the name of the MBean or
109      *       the real class ( I suppose first )
110      * XXX Read (optional) descriptions from a .properties, generated
111      *       from source
112      * XXX Deal with constructors
113      *
114      */
115     public ManagedBean createManagedBean(Registry registry, String domain,
116                                          Object realObj, String type)
117     {
118         if( ! ( realObj instanceof DynamicMBean )) {
119             return null;
120         }
121         DynamicMBean dmb=(DynamicMBean)realObj;
122         
123         ManagedBean mbean= new ManagedBean();
124         
125         MBeanInfo mbi=dmb.getMBeanInfo();
126         
127         try {
128             MBeanAttributeInfo attInfo[]=mbi.getAttributes();
129             for( int i=0; i<attInfo.length; i++ ) {
130                 MBeanAttributeInfo mai=attInfo[i];
131                 String name=mai.getName();
132 
133                 AttributeInfo ai=new AttributeInfo();
134                 ai.setName( name );
135 
136                 ai.setType( mai.getType());
137                 ai.setReadable( mai.isReadable());
138                 ai.setWriteable( mai.isWritable());
139                                                 
140                 mbean.addAttribute(ai);
141             }
142 
143             MBeanOperationInfo opInfo[]=mbi.getOperations();
144             for( int i=0; i<opInfo.length; i++ ) {
145                 MBeanOperationInfo moi=opInfo[i];
146                 OperationInfo op=new OperationInfo();
147 
148                 op.setName(moi.getName());
149                 op.setReturnType(moi.getReturnType());
150                 
151                 MBeanParameterInfo parms[]=moi.getSignature();
152                 for(int j=0; j<parms.length; j++ ) {
153                     ParameterInfo pi=new ParameterInfo();
154                     pi.setType(parms[i].getType());
155                     pi.setName(parms[i].getName());
156                     op.addParameter(pi);
157                 }
158                 mbean.addOperation(op);
159             }
160 
161             if( log.isDebugEnabled())
162                 log.debug("Setting name: " + type );
163 
164             mbean.setName( type );
165 
166             return mbean;
167         } catch( Exception ex ) {
168             ex.printStackTrace();
169             return null;
170         }
171     }
172 
173 }