KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > management > CachedAttribute


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jbi.management;
18
19 import org.apache.commons.beanutils.PropertyUtilsBean;
20
21 import javax.management.Attribute JavaDoc;
22 import javax.management.MBeanAttributeInfo JavaDoc;
23 import javax.management.MBeanException JavaDoc;
24
25 import java.beans.PropertyDescriptor JavaDoc;
26 import java.lang.reflect.InvocationTargetException JavaDoc;
27
28 /**
29  * A simple holder for an Attribute and a PropertyDescriptor
30  *
31  * @version $Revision: 426415 $
32  */

33 public class CachedAttribute {
34     private Object JavaDoc bean;
35     private String JavaDoc name;
36     private Attribute JavaDoc attribute;
37     private MBeanAttributeInfo JavaDoc attributeInfo;
38     private PropertyDescriptor JavaDoc propertyDescriptor;
39
40     /**
41      * Constructor
42      *
43      * @param attr
44      */

45     public CachedAttribute(Attribute JavaDoc attr) {
46         this.attribute = attr;
47         this.name = attr.getName();
48     }
49
50     /**
51      * @return Returns the name.
52      */

53     public String JavaDoc getName() {
54         return name;
55     }
56
57     /**
58      * @param name The name to set.
59      */

60     public void setName(String JavaDoc name) {
61         this.name = name;
62     }
63
64     /**
65      * @return Returns the attribute.
66      */

67     public Attribute JavaDoc getAttribute() {
68         return attribute;
69     }
70
71     /**
72      * Set the Attribute
73      *
74      * @param attribute
75      */

76     public void setAttribute(Attribute JavaDoc attribute) {
77         this.attribute = attribute;
78     }
79
80     /**
81      * Ensdure attribute value is up to date
82      *
83      * @param beanUtil
84      * @throws MBeanException
85      */

86     public void updateValue(PropertyUtilsBean beanUtil) throws MBeanException JavaDoc {
87         try {
88             Object JavaDoc value = beanUtil.getProperty(bean, getName());
89             if (value != attribute.getValue()) {
90                 this.attribute = new Attribute JavaDoc(getName(), value);
91             }
92         }
93         catch (IllegalAccessException JavaDoc e) {
94             throw new MBeanException JavaDoc(e);
95         }
96         catch (InvocationTargetException JavaDoc e) {
97             throw new MBeanException JavaDoc(e);
98         }
99         catch (NoSuchMethodException JavaDoc e) {
100             throw new MBeanException JavaDoc(e);
101         }
102     }
103
104     /**
105      * Update the Attribute
106      *
107      * @param beanUtils
108      * @param attribute The attribute to set.
109      * @throws IllegalAccessException
110      * @throws InvocationTargetException
111      * @throws NoSuchMethodException
112      */

113     public void updateAttribute(PropertyUtilsBean beanUtils, Attribute JavaDoc attribute) throws IllegalAccessException JavaDoc,
114             InvocationTargetException JavaDoc, NoSuchMethodException JavaDoc {
115         if (this.attribute != null && propertyDescriptor != null) {
116             // update object value
117
beanUtils.setProperty(bean, getName(), attribute.getValue());
118         }
119         this.attribute = attribute;
120     }
121
122     /**
123      * Update that attribute value
124      *
125      * @param value
126      */

127     public void updateAttributeValue(Object JavaDoc value) {
128         this.attribute = new Attribute JavaDoc(this.attribute.getName(), value);
129     }
130
131     /**
132      * @return Returns the bean.
133      */

134     public Object JavaDoc getBean() {
135         return bean;
136     }
137
138     /**
139      * @param bean The bean to set.
140      */

141     public void setBean(Object JavaDoc bean) {
142         this.bean = bean;
143     }
144
145     /**
146      * @return Returns the propertyDescriptor.
147      */

148     public PropertyDescriptor JavaDoc getPropertyDescriptor() {
149         return propertyDescriptor;
150     }
151
152     /**
153      * @param propertyDescriptor The propertyDescriptor to set.
154      */

155     public void setPropertyDescriptor(PropertyDescriptor JavaDoc propertyDescriptor) {
156         this.propertyDescriptor = propertyDescriptor;
157     }
158
159     /**
160      * @return Returns the attributeInfo.
161      */

162     public MBeanAttributeInfo JavaDoc getAttributeInfo() {
163         return attributeInfo;
164     }
165
166     /**
167      * @param attributeInfo The attributeInfo to set.
168      */

169     public void setAttributeInfo(MBeanAttributeInfo JavaDoc attributeInfo) {
170         this.attributeInfo = attributeInfo;
171     }
172 }
Popular Tags