KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > modeler > modules > MbeansDescriptorsDynamicMBeanSource


1 package org.apache.commons.modeler.modules;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
5
6 import javax.management.DynamicMBean JavaDoc;
7 import javax.management.MBeanAttributeInfo JavaDoc;
8 import javax.management.MBeanInfo JavaDoc;
9 import javax.management.MBeanOperationInfo JavaDoc;
10 import javax.management.MBeanParameterInfo JavaDoc;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 import org.apache.commons.modeler.AttributeInfo;
15 import org.apache.commons.modeler.ManagedBean;
16 import org.apache.commons.modeler.OperationInfo;
17 import org.apache.commons.modeler.ParameterInfo;
18 import org.apache.commons.modeler.Registry;
19
20
21 /** Extract metadata from a dynamic mbean.
22  * Used to wrap a dynamic mbean in order to implement persistence.
23  *
24  * This is really an ugly asspect of the JMX spec - we need to convery
25  * from normal metainfo to model metainfo. The info is the same, but
26  * they use a different class. Just like the DOM spec - where all implementations
27  * get an order of unneeded complexity from the various types.
28  *
29  */

30 public class MbeansDescriptorsDynamicMBeanSource extends ModelerSource
31 {
32     private static Log log = LogFactory.getLog(MbeansDescriptorsDynamicMBeanSource.class);
33
34     Registry registry;
35     String JavaDoc location;
36     String JavaDoc type;
37     Object JavaDoc source;
38     List JavaDoc mbeans=new ArrayList JavaDoc();
39
40     public void setRegistry(Registry reg) {
41         this.registry=reg;
42     }
43
44     public void setLocation( String JavaDoc loc ) {
45         this.location=loc;
46     }
47
48     /** Used if a single component is loaded
49      *
50      * @param type
51      */

52     public void setType( String JavaDoc type ) {
53        this.type=type;
54     }
55
56     public void setSource( Object JavaDoc source ) {
57         this.source=source;
58     }
59
60     public List JavaDoc loadDescriptors( Registry registry, String JavaDoc location,
61                                  String JavaDoc type, Object JavaDoc source)
62             throws Exception JavaDoc
63     {
64         setRegistry(registry);
65         setLocation(location);
66         setType(type);
67         setSource(source);
68         execute();
69         return mbeans;
70     }
71
72     public void execute() throws Exception JavaDoc {
73         if( registry==null ) registry=Registry.getRegistry();
74         try {
75             ManagedBean managed=createManagedBean(registry, null, source, type);
76             if( managed==null ) return;
77             managed.setName( type );
78             
79             mbeans.add(managed);
80
81         } catch( Exception JavaDoc ex ) {
82             log.error( "Error reading descriptors ", ex);
83         }
84     }
85
86
87
88     // ------------ Implementation for non-declared introspection classes
89

90
91     /**
92      * XXX Find if the 'className' is the name of the MBean or
93      * the real class ( I suppose first )
94      * XXX Read (optional) descriptions from a .properties, generated
95      * from source
96      * XXX Deal with constructors
97      *
98      */

99     public ManagedBean createManagedBean(Registry registry, String JavaDoc domain,
100                                          Object JavaDoc realObj, String JavaDoc type)
101     {
102         if( ! ( realObj instanceof DynamicMBean JavaDoc )) {
103             return null;
104         }
105         DynamicMBean JavaDoc dmb=(DynamicMBean JavaDoc)realObj;
106         
107         ManagedBean mbean= new ManagedBean();
108         
109         MBeanInfo JavaDoc mbi=dmb.getMBeanInfo();
110         
111         try {
112             MBeanAttributeInfo JavaDoc attInfo[]=mbi.getAttributes();
113             for( int i=0; i<attInfo.length; i++ ) {
114                 MBeanAttributeInfo JavaDoc mai=attInfo[i];
115                 String JavaDoc name=mai.getName();
116
117                 AttributeInfo JavaDoc ai=new AttributeInfo JavaDoc();
118                 ai.setName( name );
119
120                 ai.setType( mai.getType());
121                 ai.setReadable( mai.isReadable());
122                 ai.setWriteable( mai.isWritable());
123                                                 
124                 mbean.addAttribute(ai);
125             }
126
127             MBeanOperationInfo JavaDoc opInfo[]=mbi.getOperations();
128             for( int i=0; i<opInfo.length; i++ ) {
129                 MBeanOperationInfo JavaDoc moi=opInfo[i];
130                 OperationInfo JavaDoc op=new OperationInfo JavaDoc();
131
132                 op.setName(moi.getName());
133                 op.setReturnType(moi.getReturnType());
134                 
135                 MBeanParameterInfo JavaDoc parms[]=moi.getSignature();
136                 for(int j=0; j<parms.length; j++ ) {
137                     ParameterInfo JavaDoc pi=new ParameterInfo JavaDoc();
138                     pi.setType(parms[i].getType());
139                     pi.setName(parms[i].getName());
140                     op.addParameter(pi);
141                 }
142                 mbean.addOperation(op);
143             }
144
145             if( log.isDebugEnabled())
146                 log.debug("Setting name: " + type );
147
148             mbean.setName( type );
149
150             return mbean;
151         } catch( Exception JavaDoc ex ) {
152             ex.printStackTrace();
153             return null;
154         }
155     }
156
157 }
158
Popular Tags