1 package org.apache.commons.modeler.modules;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import javax.management.DynamicMBean;
7 import javax.management.MBeanAttributeInfo;
8 import javax.management.MBeanInfo;
9 import javax.management.MBeanOperationInfo;
10 import javax.management.MBeanParameterInfo;
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 location;
36 String type;
37 Object source;
38 List mbeans=new ArrayList();
39
40 public void setRegistry(Registry reg) {
41 this.registry=reg;
42 }
43
44 public void setLocation( String 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 type ) {
53 this.type=type;
54 }
55
56 public void setSource( Object source ) {
57 this.source=source;
58 }
59
60 public List loadDescriptors( Registry registry, String location,
61 String type, Object source)
62 throws Exception
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 {
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 ex ) {
82 log.error( "Error reading descriptors ", ex);
83 }
84 }
85
86
87
88
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 domain,
100 Object realObj, String type)
101 {
102 if( ! ( realObj instanceof DynamicMBean )) {
103 return null;
104 }
105 DynamicMBean dmb=(DynamicMBean)realObj;
106
107 ManagedBean mbean= new ManagedBean();
108
109 MBeanInfo mbi=dmb.getMBeanInfo();
110
111 try {
112 MBeanAttributeInfo attInfo[]=mbi.getAttributes();
113 for( int i=0; i<attInfo.length; i++ ) {
114 MBeanAttributeInfo mai=attInfo[i];
115 String name=mai.getName();
116
117 AttributeInfo ai=new AttributeInfo();
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 opInfo[]=mbi.getOperations();
128 for( int i=0; i<opInfo.length; i++ ) {
129 MBeanOperationInfo moi=opInfo[i];
130 OperationInfo op=new OperationInfo();
131
132 op.setName(moi.getName());
133 op.setReturnType(moi.getReturnType());
134
135 MBeanParameterInfo parms[]=moi.getSignature();
136 for(int j=0; j<parms.length; j++ ) {
137 ParameterInfo pi=new ParameterInfo();
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 ex ) {
152 ex.printStackTrace();
153 return null;
154 }
155 }
156
157 }