001: /*
002: * $Id: Beans.java 6115 2006-01-30 04:50:47Z dfs $
003: *
004: * Copyright 2006 Daniel F. Savarese
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.savarese.org/software/ApacheLicense-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: package org.savarese.unicorn.util;
020:
021: import java.lang.reflect.*;
022: import java.beans.*;
023: import javax.management.*;
024:
025: /**
026: * A singleton class that stores convenience methods for converting
027: * JavaBean information types to MBean information types.
028: */
029: public final class Beans {
030:
031: private Beans() {
032: }
033:
034: /**
035: * Converts a BeanInfo instance into an MBeanInfo instance.
036: *
037: * @param info The BeanInfo instance to convert.
038: * @return The MBeanInfo representation of the BeanInfo.
039: * @throws javax.management.IntrospectionException Should an
040: * unexpected condition arise during the conversion.
041: */
042: public static final MBeanInfo beanInfoToMBeanInfo(BeanInfo info)
043: throws javax.management.IntrospectionException {
044: BeanDescriptor bd = info.getBeanDescriptor();
045: PropertyDescriptor[] properties = info.getPropertyDescriptors();
046: MethodDescriptor[] methods = info.getMethodDescriptors();
047: Constructor[] constructors = bd.getBeanClass()
048: .getConstructors();
049: MBeanAttributeInfo[] mattributes = null;
050: MBeanConstructorInfo[] mconstructors = null;
051: MBeanOperationInfo[] moperations = null;
052:
053: if (properties != null && properties.length > 0) {
054: mattributes = new MBeanAttributeInfo[properties.length];
055: for (int i = 0; i < properties.length; ++i)
056: mattributes[i] = propertyDescriptorToMBeanAttributeInfo(properties[i]);
057: }
058:
059: if (constructors != null && constructors.length > 0) {
060: mconstructors = new MBeanConstructorInfo[constructors.length];
061: for (int i = 0; i < constructors.length; ++i)
062: mconstructors[i] = constructorToMBeanConstructorInfo(constructors[i]);
063: }
064:
065: if (methods != null && methods.length > 0) {
066: moperations = new MBeanOperationInfo[methods.length];
067: for (int i = 0; i < methods.length; ++i)
068: moperations[i] = methodDescriptorToMBeanOperationInfo(methods[i]);
069: }
070:
071: return new MBeanInfo(bd.getName(), bd.getShortDescription(),
072: mattributes, mconstructors, moperations, null);
073: }
074:
075: /**
076: * Converts a PropertyDescriptor instance into an MBeanAttributeInfo
077: * instance.
078: *
079: * @param pd The PropertyDescriptor instance to convert.
080: * @return The MBeanAttributeInfo representation of the PropertyDescriptor.
081: * @throws javax.management.IntrospectionException Should an
082: * unexpected condition arise during the conversion.
083: */
084: public static final MBeanAttributeInfo propertyDescriptorToMBeanAttributeInfo(
085: PropertyDescriptor pd)
086: throws javax.management.IntrospectionException {
087: return new MBeanAttributeInfo(pd.getName(), pd
088: .getShortDescription(), pd.getReadMethod(), pd
089: .getWriteMethod());
090: }
091:
092: /**
093: * Converts a MethodDescriptor instance into an MBeanOperationInfo instance.
094: *
095: * @param md The MethodDescriptor instance to convert.
096: * @return The MBeanOperationInfo representation of the MethodDescriptor.
097: */
098: public static final MBeanOperationInfo methodDescriptorToMBeanOperationInfo(
099: MethodDescriptor md) {
100: return new MBeanOperationInfo(md.getShortDescription(), md
101: .getMethod());
102: }
103:
104: /**
105: * Converts a Constructor instance into an MBeanConstructorInfo instance.
106: *
107: * @param c The Constructor instance to convert.
108: * @return The MBeanConstructorInfo representation of the Constructor.
109: */
110: public static final MBeanConstructorInfo constructorToMBeanConstructorInfo(
111: Constructor c) {
112: return new MBeanConstructorInfo(c.getName(), c);
113: }
114: }
|