/**
* <a href="http://servicemix.org">ServiceMix: The open source ESB</a>
*
* Copyright 2005 RAJD Consultancy Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**/
package org.servicemix.jbi.management;
import org.apache.commons.beanutils.PropertyUtilsBean;
import javax.management.DynamicMBean;
import javax.management.IntrospectionException;
import javax.management.JMException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanOperationInfo;
import javax.management.ReflectionException;
import javax.management.StandardMBean;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
/**
* Builds a DynamicMBean wrappers for existing objects
*
* @version $Revision: 657 $
*/
class MBeanBuilder {
/**
* Build an MBean
*
* @param theObject
* @param interfaceMBean
* @param description
* @return the MBean wrapper
* @throws JMException
*/
static DynamicMBean buildStandardMBean(Object theObject, Class interfaceMBean, String description) throws JMException {
DynamicMBean result = null;
if (theObject != null) {
if (theObject instanceof MBeanInfoProvider) {
MBeanInfoProvider info = (MBeanInfoProvider) theObject;
result = new BaseStandardMBean(info.getObjectToManage(),interfaceMBean, description, info.getAttributeInfos(), info
.getOperationInfos());
info.setPropertyChangeListener((BaseStandardMBean)result);
}
else {
return new StandardMBean(theObject, interfaceMBean);
}
}
return result;
}
private static MBeanAttributeInfo[] getAttributes(Object theObject) throws ReflectionException {
MBeanAttributeInfo[] result = null;
PropertyUtilsBean beanUtil = new PropertyUtilsBean();
PropertyDescriptor[] pds = beanUtil.getPropertyDescriptors(theObject);
if (pds != null) {
result = new MBeanAttributeInfo[pds.length];
for (int i = 0;i < pds.length;i++) {
try {
result[i] = new MBeanAttributeInfo(pds[i].getName(), pds[i].getShortDescription(), pds[i]
.getReadMethod(), pds[i].getWriteMethod());
}
catch (IntrospectionException e) {
throw new ReflectionException(e);
}
}
}
return result;
}
private static MBeanOperationInfo[] getOperations(MBeanAttributeInfo[] attrs, Object theObject) throws ReflectionException {
MBeanOperationInfo[] result = null;
Method[] methods = theObject.getClass().getDeclaredMethods();
if (methods != null) {
List tmpList = new ArrayList();
for (int i = 0;i < methods.length;i++) {
Method method = methods[i];
if (method.isAccessible() && !doesMethodExist(method.getName(), attrs)) {
tmpList.add(method);
}
}
result = new MBeanOperationInfo[tmpList.size()];
for (int i = 0;i < tmpList.size();i++) {
Method method = (Method) tmpList.get(i);
result[i] = new MBeanOperationInfo(method.getName(), method);
}
}
return result;
}
private static boolean doesMethodExist(String name, MBeanAttributeInfo[] attrs) {
boolean result = false;
if (attrs != null) {
for (int i = 0;i < attrs.length;i++) {
MBeanAttributeInfo info = attrs[i];
if (info.getName().equals(name)) {
result = true;
break;
}
}
}
return result;
}
}