001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.servicemix.jbi.management;
018    
019    import java.beans.PropertyDescriptor;
020    import java.lang.reflect.InvocationTargetException;
021    import java.util.ArrayList;
022    import java.util.List;
023    
024    import javax.management.IntrospectionException;
025    import javax.management.MBeanAttributeInfo;
026    import javax.management.ReflectionException;
027    
028    import org.apache.commons.beanutils.PropertyUtilsBean;
029    
030    /**
031     * A Helper class to build a list of MBeanAttributInfos
032     * 
033     * @version $Revision: 564607 $
034     */
035    public class AttributeInfoHelper {
036        private PropertyUtilsBean beanUtil = new PropertyUtilsBean();
037    
038        private List<MBeanAttributeInfo> list = new ArrayList<MBeanAttributeInfo>();
039    
040        /**
041         * Add an attribute
042         * 
043         * @param theObject
044         * @param name
045         * @param description
046         * @throws ReflectionException
047         * 
048         */
049        public void addAttribute(Object theObject, String name, String description) throws ReflectionException {
050            PropertyDescriptor pd;
051            try {
052                pd = beanUtil.getPropertyDescriptor(theObject, name);
053                MBeanAttributeInfo info = new MBeanAttributeInfo(name, description, pd.getReadMethod(), pd.getWriteMethod());
054                list.add(info);
055            } catch (IntrospectionException e) {
056                throw new ReflectionException(e);
057            } catch (IllegalAccessException e) {
058                throw new ReflectionException(e);
059            } catch (InvocationTargetException e) {
060                throw new ReflectionException(e);
061            } catch (NoSuchMethodException e) {
062                throw new ReflectionException(e);
063            }
064    
065        }
066    
067        /**
068         * Get array of MBeanAttriubteInfos registered
069         * 
070         * @return MBeanAttributeInfos
071         */
072        public MBeanAttributeInfo[] getAttributeInfos() {
073            MBeanAttributeInfo[] result = new MBeanAttributeInfo[list.size()];
074            list.toArray(result);
075            return result;
076        }
077    
078        /**
079         * clear the internal list
080         */
081        public void clear() {
082            list.clear();
083        }
084    
085        /**
086         * Join two MBeanAttributeInfo[] arrays
087         * 
088         * @param attrs1
089         * @param attrs2
090         * @return new MBeanAttributeInfo array containing contents of attr1 and
091         *         attr2
092         */
093        public static MBeanAttributeInfo[] join(MBeanAttributeInfo[] attrs1, MBeanAttributeInfo[] attrs2) {
094            MBeanAttributeInfo[] result = null;
095            int length = 0;
096            int startPos = 0;
097            if (attrs1 != null) {
098                length = attrs1.length;
099            }
100            if (attrs2 != null) {
101                length += attrs2.length;
102            }
103    
104            result = new MBeanAttributeInfo[length];
105            if (attrs1 != null) {
106                System.arraycopy(attrs1, 0, result, startPos, attrs1.length);
107                startPos = attrs1.length;
108            }
109            if (attrs2 != null) {
110                System.arraycopy(attrs2, 0, result, startPos, attrs2.length);
111            }
112            return result;
113        }
114    }