KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*_############################################################################
2   _##
3   _## SNMP4J-AgentJMX - MBeanMultiAttributeMOInfo.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  * There are cases where a single SNMP variable is mapped to several
31  * MBean attributes. <code>MBeanMultiAttributeMOInfo</code> provides means
32  * for such a mapping.
33  *
34  * @author Frank Fock
35  * @version 1.0
36  */

37 public class MBeanMultiAttributeMOInfo extends MBeanAttributeMOInfo {
38
39   public MBeanMultiAttributeMOInfo(ObjectName name,
40                                    CombinedTypedAttribute attributes) {
41     super(name, attributes);
42   }
43
44   public CombinedTypedAttribute getAttributes() {
45     return (CombinedTypedAttribute) super.getAttribute();
46   }
47
48   public Object JavaDoc getAttribute(MBeanServerConnection server) throws ReflectionException,
49       InstanceNotFoundException, AttributeNotFoundException, MBeanException,
50       IOException JavaDoc {
51     return MBeanMultiAttributeMOInfo.getAttribute(server, getObjectName(),
52                                                   getAttributes());
53   }
54
55   public static Object JavaDoc getAttribute(MBeanServerConnection server,
56                                     ObjectName name,
57                                     CombinedTypedAttribute attributes) throws
58       ReflectionException,
59       InstanceNotFoundException, AttributeNotFoundException, MBeanException,
60       IOException JavaDoc
61   {
62     TypedAttribute[] attr = attributes.getAttributes();
63     Object JavaDoc[] values = new Object JavaDoc[attr.length];
64     for (int i=0; i<values.length; i++) {
65       values[i] = server.getAttribute(name, attr[i].getName());
66       values[i] = attr[i].transformFromNative(values[i], name);
67     }
68     return attributes.transformFromNative(values, name);
69   }
70
71
72   public void setAttribute(MBeanServerConnection server, Object JavaDoc value) throws
73       ReflectionException, MBeanException, InvalidAttributeValueException,
74       AttributeNotFoundException, InstanceNotFoundException, IOException JavaDoc {
75     MBeanMultiAttributeMOInfo.setAttribute(server, getObjectName(),
76                                            getAttributes(), value);
77   }
78
79   public static void setAttribute(MBeanServerConnection server,
80                                   ObjectName name,
81                                   CombinedTypedAttribute attributes,
82                                   Object JavaDoc value) throws
83       ReflectionException, MBeanException, InvalidAttributeValueException,
84       AttributeNotFoundException, InstanceNotFoundException, IOException JavaDoc
85   {
86     TypedAttribute[] attr = attributes.getAttributes();
87     Object JavaDoc[] values = new Object JavaDoc[attr.length];
88     Object JavaDoc[] nativeValue;
89     if (attributes.isNativeValueAlwaysNeeded()) {
90       for (int i=0; i<values.length; i++) {
91         values[i] = server.getAttribute(name, attr[i].getName());
92       }
93       nativeValue = (Object JavaDoc[]) attributes.transformToNative(value, values, null);
94     }
95     else {
96       nativeValue = (Object JavaDoc[]) attributes.transformToNative(value, null, null);
97     }
98     for (int i=0; (i<nativeValue.length) && (i<attr.length); i++) {
99       Object JavaDoc v = attr[i].transformToNative(nativeValue[i], values[i], null);
100       if (v != null) {
101         server.setAttribute(name, new Attribute(attr[i].getName(), v));
102       }
103     }
104   }
105 }
106
Popular Tags