KickJava   Java API By Example, From Geeks To Geeks.

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


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.IntrospectionException JavaDoc;
22 import javax.management.MBeanAttributeInfo JavaDoc;
23 import javax.management.ReflectionException JavaDoc;
24
25 import java.beans.PropertyDescriptor JavaDoc;
26 import java.lang.reflect.InvocationTargetException JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.List JavaDoc;
29
30 /**
31  * A Helper class to build a list of MBeanAttributInfos
32  *
33  * @version $Revision: 426415 $
34  */

35 public class AttributeInfoHelper {
36     private PropertyUtilsBean beanUtil = new PropertyUtilsBean();
37     private List JavaDoc list = new ArrayList JavaDoc();
38
39     /**
40      * Add an attribute
41      *
42      * @param theObject
43      * @param name
44      * @param description
45      * @throws ReflectionException
46      
47      */

48     public void addAttribute(Object JavaDoc theObject, String JavaDoc name, String JavaDoc description) throws ReflectionException JavaDoc {
49         PropertyDescriptor JavaDoc pd;
50         try {
51             pd = beanUtil.getPropertyDescriptor(theObject, name);
52             MBeanAttributeInfo JavaDoc info = new MBeanAttributeInfo JavaDoc(name, description, pd.getReadMethod(), pd.getWriteMethod());
53             list.add(info);
54         }
55         catch(IntrospectionException JavaDoc e){
56             throw new ReflectionException JavaDoc(e);
57         }
58         catch (IllegalAccessException JavaDoc e) {
59             throw new ReflectionException JavaDoc(e);
60         }
61         catch (InvocationTargetException JavaDoc e) {
62             throw new ReflectionException JavaDoc(e);
63         }
64         catch (NoSuchMethodException JavaDoc e) {
65             throw new ReflectionException JavaDoc(e);
66         }
67         
68         
69     }
70
71     /**
72      * Get array of MBeanAttriubteInfos registered
73      *
74      * @return MBeanAttributeInfos
75      */

76     public MBeanAttributeInfo JavaDoc[] getAttributeInfos() {
77         MBeanAttributeInfo JavaDoc[] result = new MBeanAttributeInfo JavaDoc[list.size()];
78         list.toArray(result);
79         return result;
80     }
81
82     /**
83      * clear the internal list
84      */

85     public void clear() {
86         list.clear();
87     }
88     
89     /**
90      * Join two MBeanAttributeInfo[] arrays
91      * @param attrs1
92      * @param attrs2
93      * @return new MBeanAttributeInfo array containing contents of attr1 and attr2
94      */

95     public static MBeanAttributeInfo JavaDoc[] join(MBeanAttributeInfo JavaDoc[] attrs1,MBeanAttributeInfo JavaDoc[] attrs2){
96         MBeanAttributeInfo JavaDoc[] result = null;
97         int length = 0;
98         int startPos = 0;
99         if (attrs1 != null){
100             length = attrs1.length;
101         }
102         if (attrs2 != null){
103             length += attrs2.length;
104         }
105         
106         result = new MBeanAttributeInfo JavaDoc[length];
107         if (attrs1 != null){
108             System.arraycopy(attrs1, 0, result, startPos, attrs1.length);
109             startPos = attrs1.length;
110         }
111         if(attrs2 != null){
112             System.arraycopy(attrs2, 0, result, startPos, attrs2.length);
113         }
114         return result;
115     }
116 }
Popular Tags