KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > agent > mo > jmx > MBeanAttributeMOInfo


1 /*_############################################################################
2   _##
3   _## SNMP4J-AgentJMX - MBeanAttributeMOInfo.java
4   _##
5   _## Copyright (C) 2006-2007 Frank Fock (SNMP4J.org)
6   _##
7   _## This program is free software; you can redistribute it and/or modify
8   _## it under the terms of the GNU General Public License version 2 as
9   _## published by the Free Software Foundation.
10   _##
11   _## This program is distributed in the hope that it will be useful,
12   _## but WITHOUT ANY WARRANTY; without even the implied warranty of
13   _## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14   _## GNU General Public License for more details.
15   _##
16   _## You should have received a copy of the GNU General Public License
17   _## along with this program; if not, write to the Free Software
18   _## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19   _## MA 02110-1301 USA
20   _##
21   _##########################################################################*/

22
23 package org.snmp4j.agent.mo.jmx;
24
25 import javax.management.*;
26 import java.io.IOException JavaDoc;
27 import org.snmp4j.agent.mo.jmx.types.*;
28
29 /**
30  * The <code>MBeanAttributeMOInfo</code> describes an attribute of a MBean.
31  *
32  * @author Frank Fock
33  * @version 1.0
34  */

35 public class MBeanAttributeMOInfo extends MBeanMOInfo {
36
37   protected TypedAttribute attribute;
38
39   /**
40    * Creates an attribute description with the attributes name and type.
41    * @param name
42    * the MBean's <code>ObjectName</code>.
43    * @param attributeName
44    * the name of the attribute.
45    * @param attributeType
46    * the class of the attributes value objects.
47    */

48   public MBeanAttributeMOInfo(ObjectName name, String JavaDoc attributeName,
49                               Class JavaDoc attributeType) {
50     super(name);
51     this.attribute = new TypedAttribute(attributeName, attributeType);
52   }
53
54   /**
55    * Creates an attribute description with a <code>TypedAttribute</code>.
56    * @param name
57    * the MBean's <code>ObjectName</code>.
58    * @param attribute
59    * a <code>TypedAttribute</code> describing the attribute. The value is
60    * by reference.
61    */

62   public MBeanAttributeMOInfo(ObjectName name, TypedAttribute attribute) {
63     super(name);
64     this.attribute = attribute;
65   }
66
67   public String JavaDoc getAttributeName() {
68     return attribute.getName();
69   }
70
71   public Class JavaDoc getAttributeType() {
72     return attribute.getType();
73   }
74
75   public TypedAttribute getAttribute() {
76     return attribute;
77   }
78
79   public Object JavaDoc getAttribute(MBeanServerConnection server) throws ReflectionException,
80       InstanceNotFoundException, AttributeNotFoundException, MBeanException,
81       IOException JavaDoc {
82     return getAttribute(server, getObjectName(), getAttribute());
83   }
84
85   public static Object JavaDoc getAttribute(MBeanServerConnection server,
86                                     ObjectName name,
87                                     TypedAttribute attribute) throws
88       ReflectionException,
89       InstanceNotFoundException, AttributeNotFoundException, MBeanException,
90       IOException JavaDoc
91   {
92     Object JavaDoc nativeValue = server.getAttribute(name, attribute.getName());
93     return attribute.transformFromNative(nativeValue, name);
94   }
95
96
97   public void setAttribute(MBeanServerConnection server, Object JavaDoc value) throws
98       ReflectionException, MBeanException, InvalidAttributeValueException,
99       AttributeNotFoundException, InstanceNotFoundException, IOException JavaDoc {
100     setAttribute(server, getObjectName(), getAttribute(), value);
101   }
102
103   public static void setAttribute(MBeanServerConnection server,
104                                   ObjectName name,
105                                   TypedAttribute attribute,
106                                   Object JavaDoc value) throws
107       ReflectionException, MBeanException, InvalidAttributeValueException,
108       AttributeNotFoundException, InstanceNotFoundException, IOException JavaDoc
109   {
110     Object JavaDoc nativeValue;
111     if (attribute.isNativeValueAlwaysNeeded()) {
112       nativeValue =
113           server.getAttribute(name, attribute.getName());
114       nativeValue = attribute.transformToNative(value, nativeValue, name);
115     }
116     else {
117       nativeValue = attribute.transformToNative(value, null, name);
118     }
119     server.setAttribute(name, new Attribute(attribute.getName(), nativeValue));
120   }
121
122 }
123
Popular Tags