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    
022    import javax.management.Attribute;
023    import javax.management.MBeanAttributeInfo;
024    import javax.management.MBeanException;
025    
026    import org.apache.commons.beanutils.PropertyUtilsBean;
027    
028    /**
029     * A simple holder for an Attribute and a PropertyDescriptor
030     * 
031     * @version $Revision: 564607 $
032     */
033    public class CachedAttribute {
034        private Object bean;
035        private String name;
036        private Attribute attribute;
037        private MBeanAttributeInfo attributeInfo;
038        private PropertyDescriptor propertyDescriptor;
039    
040        /**
041         * Constructor
042         * 
043         * @param attr
044         */
045        public CachedAttribute(Attribute attr) {
046            this.attribute = attr;
047            this.name = attr.getName();
048        }
049    
050        /**
051         * @return Returns the name.
052         */
053        public String getName() {
054            return name;
055        }
056    
057        /**
058         * @param name The name to set.
059         */
060        public void setName(String name) {
061            this.name = name;
062        }
063    
064        /**
065         * @return Returns the attribute.
066         */
067        public Attribute getAttribute() {
068            return attribute;
069        }
070    
071        /**
072         * Set the Attribute
073         * 
074         * @param attribute
075         */
076        public void setAttribute(Attribute attribute) {
077            this.attribute = attribute;
078        }
079    
080        /**
081         * Ensdure attribute value is up to date
082         * 
083         * @param beanUtil
084         * @throws MBeanException
085         */
086        public void updateValue(PropertyUtilsBean beanUtil) throws MBeanException {
087            try {
088                Object value = beanUtil.getProperty(bean, getName());
089                if (value != attribute.getValue()) {
090                    this.attribute = new Attribute(getName(), value);
091                }
092            } catch (IllegalAccessException e) {
093                throw new MBeanException(e);
094            } catch (InvocationTargetException e) {
095                throw new MBeanException(e);
096            } catch (NoSuchMethodException e) {
097                throw new MBeanException(e);
098            }
099        }
100    
101        /**
102         * Update the Attribute
103         * 
104         * @param beanUtils
105         * @param attr The attribute to set.
106         * @throws IllegalAccessException
107         * @throws InvocationTargetException
108         * @throws NoSuchMethodException
109         */
110        public void updateAttribute(PropertyUtilsBean beanUtils, Attribute attr) throws IllegalAccessException,
111                InvocationTargetException, NoSuchMethodException {
112            if (this.attribute != null && propertyDescriptor != null) {
113                // update object value
114                beanUtils.setProperty(bean, getName(), attr.getValue());
115            }
116            this.attribute = attr;
117        }
118    
119        /**
120         * Update that attribute value
121         * 
122         * @param value
123         */
124        public void updateAttributeValue(Object value) {
125            this.attribute = new Attribute(this.attribute.getName(), value);
126        }
127    
128        /**
129         * @return Returns the bean.
130         */
131        public Object getBean() {
132            return bean;
133        }
134    
135        /**
136         * @param bean The bean to set.
137         */
138        public void setBean(Object bean) {
139            this.bean = bean;
140        }
141    
142        /**
143         * @return Returns the propertyDescriptor.
144         */
145        public PropertyDescriptor getPropertyDescriptor() {
146            return propertyDescriptor;
147        }
148    
149        /**
150         * @param propertyDescriptor The propertyDescriptor to set.
151         */
152        public void setPropertyDescriptor(PropertyDescriptor propertyDescriptor) {
153            this.propertyDescriptor = propertyDescriptor;
154        }
155    
156        /**
157         * @return Returns the attributeInfo.
158         */
159        public MBeanAttributeInfo getAttributeInfo() {
160            return attributeInfo;
161        }
162    
163        /**
164         * @param attributeInfo The attributeInfo to set.
165         */
166        public void setAttributeInfo(MBeanAttributeInfo attributeInfo) {
167            this.attributeInfo = attributeInfo;
168        }
169    }