KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jmx > export > assembler > AbstractMBeanInfoAssembler


1 /*
2  * Copyright 2002-2007 the original author or authors.
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 package org.springframework.jmx.export.assembler;
18
19 import javax.management.Descriptor JavaDoc;
20 import javax.management.JMException JavaDoc;
21 import javax.management.modelmbean.ModelMBeanAttributeInfo JavaDoc;
22 import javax.management.modelmbean.ModelMBeanConstructorInfo JavaDoc;
23 import javax.management.modelmbean.ModelMBeanInfo JavaDoc;
24 import javax.management.modelmbean.ModelMBeanInfoSupport JavaDoc;
25 import javax.management.modelmbean.ModelMBeanNotificationInfo JavaDoc;
26 import javax.management.modelmbean.ModelMBeanOperationInfo JavaDoc;
27
28 import org.springframework.aop.support.AopUtils;
29 import org.springframework.jmx.support.JmxUtils;
30
31 /**
32  * Abstract implementation of the <code>MBeanInfoAssembler</code> interface
33  * that encapsulates the creation of a <code>ModelMBeanInfo</code> instance
34  * but delegates the creation of metadata to subclasses.
35  *
36  * <p>This class offers two flavors of Class extraction from a managed bean
37  * instance: {@link #getTargetClass}, extracting the target class behind
38  * any kind of AOP proxy, and {@link #getClassToExpose}, returning the
39  * class or interface that will be searched for annotations and exposed
40  * to the JMX runtime.
41  *
42  * @author Rob Harrop
43  * @author Juergen Hoeller
44  * @since 1.2
45  */

46 public abstract class AbstractMBeanInfoAssembler implements MBeanInfoAssembler {
47
48     /**
49      * Create an instance of the <code>ModelMBeanInfoSupport</code> class supplied with all
50      * JMX implementations and populates the metadata through calls to the subclass.
51      * @param managedBean the bean that will be exposed (might be an AOP proxy)
52      * @param beanKey the key associated with the managed bean
53      * @return the populated ModelMBeanInfo instance
54      * @throws JMException in case of errors
55      * @see #getDescription(Object, String)
56      * @see #getAttributeInfo(Object, String)
57      * @see #getConstructorInfo(Object, String)
58      * @see #getOperationInfo(Object, String)
59      * @see #getNotificationInfo(Object, String)
60      * @see #populateMBeanDescriptor(javax.management.Descriptor, Object, String)
61      */

62     public ModelMBeanInfo JavaDoc getMBeanInfo(Object JavaDoc managedBean, String JavaDoc beanKey) throws JMException JavaDoc {
63         checkManagedBean(managedBean);
64         ModelMBeanInfo JavaDoc info = new ModelMBeanInfoSupport JavaDoc(
65                 getClassName(managedBean, beanKey), getDescription(managedBean, beanKey),
66                 getAttributeInfo(managedBean, beanKey), getConstructorInfo(managedBean, beanKey),
67                 getOperationInfo(managedBean, beanKey), getNotificationInfo(managedBean, beanKey));
68         Descriptor JavaDoc desc = info.getMBeanDescriptor();
69         populateMBeanDescriptor(desc, managedBean, beanKey);
70         info.setMBeanDescriptor(desc);
71         return info;
72     }
73
74     /**
75      * Check the given bean instance, throwing an IllegalArgumentException
76      * if it is not eligible for exposure with this assembler.
77      * <p>Default implementation is empty, accepting every bean instance.
78      * @param managedBean the bean that will be exposed (might be an AOP proxy)
79      * @throws IllegalArgumentException the bean is not valid for exposure
80      */

81     protected void checkManagedBean(Object JavaDoc managedBean) throws IllegalArgumentException JavaDoc {
82     }
83
84     /**
85      * Return the actual bean class of the given bean instance.
86      * This is the class exposed to description-style JMX properties.
87      * <p>Default implementation returns the target class for an AOP proxy,
88      * and the plain bean class else.
89      * @param managedBean the bean instance (might be an AOP proxy)
90      * @return the bean class to expose
91      * @see org.springframework.aop.framework.AopProxyUtils#getTargetClass
92      */

93     protected Class JavaDoc getTargetClass(Object JavaDoc managedBean) {
94         return AopUtils.getTargetClass(managedBean);
95     }
96
97     /**
98      * Return the class or interface to expose for the given bean.
99      * This is the class that will be searched for attributes and operations
100      * (for example, checked for annotations).
101      * @param managedBean the bean instance (might be an AOP proxy)
102      * @return the bean class to expose
103      * @see JmxUtils#getClassToExpose(Object)
104      */

105     protected Class JavaDoc getClassToExpose(Object JavaDoc managedBean) {
106         return JmxUtils.getClassToExpose(managedBean);
107     }
108
109     /**
110      * Return the class or interface to expose for the given bean class.
111      * This is the class that will be searched for attributes and operations
112      * @param beanClass the bean class (might be an AOP proxy class)
113      * @return the bean class to expose
114      * @see JmxUtils#getClassToExpose(Class)
115      */

116     protected Class JavaDoc getClassToExpose(Class JavaDoc beanClass) {
117         return JmxUtils.getClassToExpose(beanClass);
118     }
119
120     /**
121      * Get the class name of the MBean resource.
122      * <p>Default implementation returns a simple description for the MBean
123      * based on the class name.
124      * @param managedBean the bean instance (might be an AOP proxy)
125      * @param beanKey the key associated with the MBean in the beans map
126      * of the <code>MBeanExporter</code>
127      * @return the MBean description
128      * @throws JMException in case of errors
129      */

130     protected String JavaDoc getClassName(Object JavaDoc managedBean, String JavaDoc beanKey) throws JMException JavaDoc {
131         return getTargetClass(managedBean).getName();
132     }
133
134     /**
135      * Get the description of the MBean resource.
136      * <p>Default implementation returns a simple description for the MBean
137      * based on the class name.
138      * @param managedBean the bean instance (might be an AOP proxy)
139      * @param beanKey the key associated with the MBean in the beans map
140      * of the <code>MBeanExporter</code>
141      * @throws JMException in case of errors
142      */

143     protected String JavaDoc getDescription(Object JavaDoc managedBean, String JavaDoc beanKey) throws JMException JavaDoc {
144         String JavaDoc targetClassName = getTargetClass(managedBean).getName();
145         if (AopUtils.isAopProxy(managedBean)) {
146             return "Proxy for " + targetClassName;
147         }
148         return targetClassName;
149     }
150
151     /**
152      * Called after the <code>ModelMBeanInfo</code> instance has been constructed but
153      * before it is passed to the <code>MBeanExporter</code>.
154      * <p>Subclasses can implement this method to add additional descriptors to the
155      * MBean metadata. Default implementation is empty.
156      * @param descriptor the <code>Descriptor</code> for the MBean resource.
157      * @param managedBean the bean instance (might be an AOP proxy)
158      * @param beanKey the key associated with the MBean in the beans map
159      * of the <code>MBeanExporter</code>
160      * @throws JMException in case of errors
161      */

162     protected void populateMBeanDescriptor(Descriptor JavaDoc descriptor, Object JavaDoc managedBean, String JavaDoc beanKey)
163             throws JMException JavaDoc {
164     }
165
166     /**
167      * Get the constructor metadata for the MBean resource. Subclasses should implement
168      * this method to return the appropriate metadata for all constructors that should
169      * be exposed in the management interface for the managed resource.
170      * <p>Default implementation returns an empty array of <code>ModelMBeanConstructorInfo</code>.
171      * @param managedBean the bean instance (might be an AOP proxy)
172      * @param beanKey the key associated with the MBean in the beans map
173      * of the <code>MBeanExporter</code>
174      * @return the constructor metadata
175      * @throws JMException in case of errors
176      */

177     protected ModelMBeanConstructorInfo JavaDoc[] getConstructorInfo(Object JavaDoc managedBean, String JavaDoc beanKey)
178             throws JMException JavaDoc {
179         return new ModelMBeanConstructorInfo JavaDoc[0];
180     }
181
182     /**
183      * Get the notification metadata for the MBean resource. Subclasses should implement
184      * this method to return the appropriate metadata for all notifications that should
185      * be exposed in the management interface for the managed resource.
186      * <p>Default implementation returns an empty array of <code>ModelMBeanNotificationInfo</code>.
187      * @param managedBean the bean instance (might be an AOP proxy)
188      * @param beanKey the key associated with the MBean in the beans map
189      * of the <code>MBeanExporter</code>
190      * @return the notification metadata
191      * @throws JMException in case of errors
192      */

193     protected ModelMBeanNotificationInfo JavaDoc[] getNotificationInfo(Object JavaDoc managedBean, String JavaDoc beanKey)
194             throws JMException JavaDoc {
195         return new ModelMBeanNotificationInfo JavaDoc[0];
196     }
197
198
199     /**
200      * Get the attribute metadata for the MBean resource. Subclasses should implement
201      * this method to return the appropriate metadata for all the attributes that should
202      * be exposed in the management interface for the managed resource.
203      * @param managedBean the bean instance (might be an AOP proxy)
204      * @param beanKey the key associated with the MBean in the beans map
205      * of the <code>MBeanExporter</code>
206      * @return the attribute metadata
207      * @throws JMException in case of errors
208      */

209     protected abstract ModelMBeanAttributeInfo JavaDoc[] getAttributeInfo(Object JavaDoc managedBean, String JavaDoc beanKey)
210             throws JMException JavaDoc;
211
212     /**
213      * Get the operation metadata for the MBean resource. Subclasses should implement
214      * this method to return the appropriate metadata for all operations that should
215      * be exposed in the management interface for the managed resource.
216      * @param managedBean the bean instance (might be an AOP proxy)
217      * @param beanKey the key associated with the MBean in the beans map
218      * of the <code>MBeanExporter</code>
219      * @return the operation metadata
220      * @throws JMException in case of errors
221      */

222     protected abstract ModelMBeanOperationInfo JavaDoc[] getOperationInfo(Object JavaDoc managedBean, String JavaDoc beanKey)
223             throws JMException JavaDoc;
224
225 }
226
Popular Tags