001// Copyright 2005 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//     http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.hivemind.management.mbeans;
016
017import javax.management.Attribute;
018import javax.management.AttributeList;
019import javax.management.AttributeNotFoundException;
020import javax.management.DynamicMBean;
021import javax.management.InvalidAttributeValueException;
022import javax.management.MBeanAttributeInfo;
023import javax.management.MBeanConstructorInfo;
024import javax.management.MBeanException;
025import javax.management.MBeanInfo;
026import javax.management.MBeanNotificationInfo;
027import javax.management.MBeanOperationInfo;
028import javax.management.MBeanRegistration;
029import javax.management.MBeanServer;
030import javax.management.ObjectName;
031import javax.management.ReflectionException;
032
033/**
034 * Ancestor for MBeans. Eases implementation of the {@link javax.management.DynamicMBean} interface.
035 * Provides empty method implementations and implements {@link #getAttributes(String[])} and
036 * {@link #setAttributes(AttributeList)}
037 * 
038 * @author Achim Huegen
039 */
040public abstract class AbstractDynamicMBean implements MBeanRegistration, DynamicMBean
041{
042
043    private MBeanInfo _mBeanInfo;
044
045    private MBeanServer _mbeanServer;
046
047    /**
048     * @see javax.management.DynamicMBean#getMBeanInfo()
049     */
050    public MBeanInfo getMBeanInfo()
051    {
052        if (_mBeanInfo == null)
053            setMBeanInfo(createMBeanInfo());
054        return _mBeanInfo;
055    }
056
057    /**
058     * Sets the MBeanInfo
059     * 
060     * @param info
061     *            the info
062     */
063    protected void setMBeanInfo(MBeanInfo info)
064    {
065        _mBeanInfo = info;
066    }
067
068    /**
069     * Delegates the MBeanInfo retrieval to various methods
070     * 
071     * @return the MBeanInfo of the MBean
072     */
073    private MBeanInfo createMBeanInfo()
074    {
075        MBeanAttributeInfo attrs[] = createMBeanAttributeInfo();
076        MBeanConstructorInfo ctors[] = createMBeanConstructorInfo();
077        MBeanOperationInfo opers[] = createMBeanOperationInfo();
078        MBeanNotificationInfo notifs[] = createMBeanNotificationInfo();
079        String className = getMBeanClassName();
080        String description = getMBeanDescription();
081        return new MBeanInfo(className, description, attrs, ctors, opers, notifs);
082    }
083
084    /**
085     * Provides the info which attributes the MBean has. Should be overwritten by the descendants
086     */
087    protected MBeanAttributeInfo[] createMBeanAttributeInfo()
088    {
089        return null;
090    }
091
092    /**
093     * Provides the info which constructors MBean has. Should be overwritten by the descendants
094     */
095    protected MBeanConstructorInfo[] createMBeanConstructorInfo()
096    {
097        return null;
098    }
099
100    /**
101     * Provides the info which operations can be called on the MBean. Should be overwritten by the
102     * descendants
103     */
104    protected MBeanOperationInfo[] 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[] createMBeanNotificationInfo()
114    {
115        return null;
116    }
117
118    protected String getMBeanClassName()
119    {
120        return getClass().getName();
121    }
122
123    /**
124     * @return Textual description of the MBean
125     */
126    protected String getMBeanDescription()
127    {
128        return null;
129    }
130
131    /**
132     * @see javax.management.DynamicMBean#getAttribute(java.lang.String)
133     */
134    public Object getAttribute(String name) throws AttributeNotFoundException, MBeanException,
135            ReflectionException
136    {
137        return null;
138    }
139
140    /**
141     * @see javax.management.DynamicMBean#setAttribute(javax.management.Attribute)
142     */
143    public void setAttribute(Attribute attribute) throws AttributeNotFoundException,
144            InvalidAttributeValueException, MBeanException, ReflectionException
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 getAttributes(String[] attributes)
154    {
155        AttributeList list = new AttributeList();
156        if (attributes != null)
157        {
158            for (int i = 0; i < attributes.length; i++)
159            {
160                String attribute = attributes[i];
161                try
162                {
163                    Object result = getAttribute(attribute);
164                    list.add(new Attribute(attribute, result));
165                }
166                catch (AttributeNotFoundException ignored)
167                {
168                }
169                catch (MBeanException ignored)
170                {
171                }
172                catch (ReflectionException 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 setAttributes(AttributeList attributes)
185    {
186        AttributeList list = new AttributeList();
187
188        if (attributes != null)
189        {
190            for (int i = 0; i < attributes.size(); ++i)
191            {
192                Attribute attribute = (Attribute) attributes.get(i);
193                try
194                {
195                    setAttribute(attribute);
196                    list.add(attribute);
197                }
198                catch (AttributeNotFoundException ignored)
199                {
200                }
201                catch (InvalidAttributeValueException ignored)
202                {
203                }
204                catch (MBeanException ignored)
205                {
206                }
207                catch (ReflectionException 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 invoke(String method, Object[] arguments, String[] params) throws MBeanException,
221            ReflectionException
222    {
223        return null;
224    }
225
226    public ObjectName preRegister(MBeanServer mbeanserver, ObjectName objectname)
227    {
228        _mbeanServer = mbeanserver;
229        return objectname;
230    }
231
232    public void postRegister(Boolean registrationDone)
233    {
234    }
235
236    public void preDeregister() throws Exception
237    {
238    }
239
240    public void postDeregister()
241    {
242    }
243
244    protected MBeanServer getMBeanServer()
245    {
246        return _mbeanServer;
247    }
248
249
250}