KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > management > mbeans > AbstractDynamicMBean


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

15 package org.apache.hivemind.management.mbeans;
16
17 import javax.management.Attribute JavaDoc;
18 import javax.management.AttributeList JavaDoc;
19 import javax.management.AttributeNotFoundException JavaDoc;
20 import javax.management.DynamicMBean JavaDoc;
21 import javax.management.InvalidAttributeValueException JavaDoc;
22 import javax.management.MBeanAttributeInfo JavaDoc;
23 import javax.management.MBeanConstructorInfo JavaDoc;
24 import javax.management.MBeanException JavaDoc;
25 import javax.management.MBeanInfo JavaDoc;
26 import javax.management.MBeanNotificationInfo JavaDoc;
27 import javax.management.MBeanOperationInfo JavaDoc;
28 import javax.management.MBeanRegistration JavaDoc;
29 import javax.management.MBeanServer JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31 import javax.management.ReflectionException JavaDoc;
32
33 /**
34  * Ancestor for MBeans. Eases implementation of the {@link javax.management.DynamicMBean} interface.
35  * Provides empty method implementations and implements {@link #getAttributes(String[])} and
36  * {@link #setAttributes(AttributeList)}
37  *
38  * @author Achim Huegen
39  */

40 public abstract class AbstractDynamicMBean implements MBeanRegistration JavaDoc, DynamicMBean JavaDoc
41 {
42
43     private MBeanInfo JavaDoc _mBeanInfo;
44
45     private MBeanServer JavaDoc _mbeanServer;
46
47     /**
48      * @see javax.management.DynamicMBean#getMBeanInfo()
49      */

50     public MBeanInfo JavaDoc getMBeanInfo()
51     {
52         if (_mBeanInfo == null)
53             setMBeanInfo(createMBeanInfo());
54         return _mBeanInfo;
55     }
56
57     /**
58      * Sets the MBeanInfo
59      *
60      * @param info
61      * the info
62      */

63     protected void setMBeanInfo(MBeanInfo JavaDoc info)
64     {
65         _mBeanInfo = info;
66     }
67
68     /**
69      * Delegates the MBeanInfo retrieval to various methods
70      *
71      * @return the MBeanInfo of the MBean
72      */

73     private MBeanInfo JavaDoc createMBeanInfo()
74     {
75         MBeanAttributeInfo JavaDoc attrs[] = createMBeanAttributeInfo();
76         MBeanConstructorInfo JavaDoc ctors[] = createMBeanConstructorInfo();
77         MBeanOperationInfo JavaDoc opers[] = createMBeanOperationInfo();
78         MBeanNotificationInfo JavaDoc notifs[] = createMBeanNotificationInfo();
79         String JavaDoc className = getMBeanClassName();
80         String JavaDoc description = getMBeanDescription();
81         return new MBeanInfo JavaDoc(className, description, attrs, ctors, opers, notifs);
82     }
83
84     /**
85      * Provides the info which attributes the MBean has. Should be overwritten by the descendants
86      */

87     protected MBeanAttributeInfo JavaDoc[] createMBeanAttributeInfo()
88     {
89         return null;
90     }
91
92     /**
93      * Provides the info which constructors MBean has. Should be overwritten by the descendants
94      */

95     protected MBeanConstructorInfo JavaDoc[] createMBeanConstructorInfo()
96     {
97         return null;
98     }
99
100     /**
101      * Provides the info which operations can be called on the MBean. Should be overwritten by the
102      * descendants
103      */

104     protected MBeanOperationInfo JavaDoc[] createMBeanOperationInfo()
105     {
106         return null;
107     }
108
109     /**
110      * Provides the info which notifications the MBean supports. Should be overwritten by the
111      * descendants
112      */

113     protected MBeanNotificationInfo JavaDoc[] createMBeanNotificationInfo()
114     {
115         return null;
116     }
117
118     protected String JavaDoc getMBeanClassName()
119     {
120         return getClass().getName();
121     }
122
123     /**
124      * @return Textual description of the MBean
125      */

126     protected String JavaDoc getMBeanDescription()
127     {
128         return null;
129     }
130
131     /**
132      * @see javax.management.DynamicMBean#getAttribute(java.lang.String)
133      */

134     public Object JavaDoc getAttribute(String JavaDoc name) throws AttributeNotFoundException JavaDoc, MBeanException JavaDoc,
135             ReflectionException JavaDoc
136     {
137         return null;
138     }
139
140     /**
141      * @see javax.management.DynamicMBean#setAttribute(javax.management.Attribute)
142      */

143     public void setAttribute(Attribute JavaDoc attribute) throws AttributeNotFoundException JavaDoc,
144             InvalidAttributeValueException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc
145     {
146     }
147
148     /**
149      * Gets a list of attributes using {@link #getAttribute(String)}
150      *
151      * @see javax.management.DynamicMBean#getAttributes(java.lang.String[])
152      */

153     public AttributeList JavaDoc getAttributes(String JavaDoc[] attributes)
154     {
155         AttributeList JavaDoc list = new AttributeList JavaDoc();
156         if (attributes != null)
157         {
158             for (int i = 0; i < attributes.length; i++)
159             {
160                 String JavaDoc attribute = attributes[i];
161                 try
162                 {
163                     Object JavaDoc result = getAttribute(attribute);
164                     list.add(new Attribute JavaDoc(attribute, result));
165                 }
166                 catch (AttributeNotFoundException JavaDoc ignored)
167                 {
168                 }
169                 catch (MBeanException JavaDoc ignored)
170                 {
171                 }
172                 catch (ReflectionException JavaDoc ignored)
173                 {
174                 }
175             }
176
177         }
178         return list;
179     }
180
181     /**
182      * @see javax.management.DynamicMBean#setAttributes(javax.management.AttributeList)
183      */

184     public AttributeList JavaDoc setAttributes(AttributeList JavaDoc attributes)
185     {
186         AttributeList JavaDoc list = new AttributeList JavaDoc();
187
188         if (attributes != null)
189         {
190             for (int i = 0; i < attributes.size(); ++i)
191             {
192                 Attribute JavaDoc attribute = (Attribute JavaDoc) attributes.get(i);
193                 try
194                 {
195                     setAttribute(attribute);
196                     list.add(attribute);
197                 }
198                 catch (AttributeNotFoundException JavaDoc ignored)
199                 {
200                 }
201                 catch (InvalidAttributeValueException JavaDoc ignored)
202                 {
203                 }
204                 catch (MBeanException JavaDoc ignored)
205                 {
206                 }
207                 catch (ReflectionException JavaDoc ignored)
208                 {
209                 }
210             }
211         }
212
213         return list;
214     }
215
216     /**
217      * @see javax.management.DynamicMBean#invoke(java.lang.String, java.lang.Object[],
218      * java.lang.String[])
219      */

220     public Object JavaDoc invoke(String JavaDoc method, Object JavaDoc[] arguments, String JavaDoc[] params) throws MBeanException JavaDoc,
221             ReflectionException JavaDoc
222     {
223         return null;
224     }
225
226     public ObjectName JavaDoc preRegister(MBeanServer JavaDoc mbeanserver, ObjectName JavaDoc objectname)
227     {
228         _mbeanServer = mbeanserver;
229         return objectname;
230     }
231
232     public void postRegister(Boolean JavaDoc registrationDone)
233     {
234     }
235
236     public void preDeregister() throws Exception JavaDoc
237     {
238     }
239
240     public void postDeregister()
241     {
242     }
243
244     protected MBeanServer JavaDoc getMBeanServer()
245     {
246         return _mbeanServer;
247     }
248
249
250 }
251
Popular Tags