Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
CompositeDataAttributeAccessor |
|
| 1.3333333333333333;1,333 |
1 | package net.sf.snmpadaptor4j.core.accessor; | |
2 | ||
3 | import javax.management.MBeanServer; | |
4 | import javax.management.ObjectName; | |
5 | import javax.management.openmbean.CompositeData; | |
6 | import net.sf.snmpadaptor4j.api.AttributeAccessor; | |
7 | import net.sf.snmpadaptor4j.object.SnmpDataType; | |
8 | import net.sf.snmpadaptor4j.object.SnmpOid; | |
9 | ||
10 | /** | |
11 | * Object representing an accessor to an attribute for {@link CompositeData}. | |
12 | * @author <a href="http://fr.linkedin.com/in/jpminetti/">Jean-Philippe MINETTI</a> | |
13 | */ | |
14 | public final class CompositeDataAttributeAccessor | |
15 | implements AttributeAccessor { | |
16 | ||
17 | /** | |
18 | * <b>O</b>bject <b>ID</b>entifier (OID) to locate the attribute in the MIB. | |
19 | */ | |
20 | private final SnmpOid oid; | |
21 | ||
22 | /** | |
23 | * JMX agent where the MBean was registered. | |
24 | */ | |
25 | private final MBeanServer server; | |
26 | ||
27 | /** | |
28 | * MBean name. | |
29 | */ | |
30 | private final ObjectName mBeanName; | |
31 | ||
32 | /** | |
33 | * Attribute name of {@link CompositeData}. | |
34 | */ | |
35 | private final String attributeName; | |
36 | ||
37 | /** | |
38 | * Attribute key of value. | |
39 | */ | |
40 | private final String attributeKey; | |
41 | ||
42 | /** | |
43 | * SNMP data type of attribute. | |
44 | */ | |
45 | private final SnmpDataType snmpDataType; | |
46 | ||
47 | /** | |
48 | * Data type of JMX attribute. | |
49 | */ | |
50 | private final Class<?> jmxDataType; | |
51 | ||
52 | /** | |
53 | * Constructor. | |
54 | * @param oid <b>O</b>bject <b>ID</b>entifier (OID) to locate the attribute in the MIB. | |
55 | * @param server JMX agent where the MBean was registered. | |
56 | * @param mBeanName MBean name. | |
57 | * @param attributeName Attribute name of {@link CompositeData}. | |
58 | * @param attributeKey Attribute key of value. | |
59 | * @param snmpDataType SNMP data type of attribute. | |
60 | * @param jmxDataType Data type of JMX attribute. | |
61 | */ | |
62 | public CompositeDataAttributeAccessor (final SnmpOid oid, final MBeanServer server, final ObjectName mBeanName, final String attributeName, | |
63 | final String attributeKey, final SnmpDataType snmpDataType, final Class<?> jmxDataType) { | |
64 | 0 | super(); |
65 | 0 | this.oid = oid; |
66 | 0 | this.server = server; |
67 | 0 | this.mBeanName = mBeanName; |
68 | 0 | this.attributeName = attributeName; |
69 | 0 | this.attributeKey = attributeKey; |
70 | 0 | this.snmpDataType = snmpDataType; |
71 | 0 | this.jmxDataType = jmxDataType; |
72 | 0 | } |
73 | ||
74 | /* | |
75 | * {@inheritDoc} | |
76 | * @see net.sf.snmpadaptor4j.api.AttributeAccessor#getOid() | |
77 | */ | |
78 | public SnmpOid getOid () { | |
79 | 0 | return this.oid; |
80 | } | |
81 | ||
82 | /* | |
83 | * {@inheritDoc} | |
84 | * @see net.sf.snmpadaptor4j.api.AttributeAccessor#getSnmpDataType() | |
85 | */ | |
86 | public SnmpDataType getSnmpDataType () { | |
87 | 0 | return this.snmpDataType; |
88 | } | |
89 | ||
90 | /* | |
91 | * {@inheritDoc} | |
92 | * @see net.sf.snmpadaptor4j.api.AttributeAccessor#getJmxDataType() | |
93 | */ | |
94 | public Class<?> getJmxDataType () { | |
95 | 0 | return this.jmxDataType; |
96 | } | |
97 | ||
98 | /* | |
99 | * {@inheritDoc} | |
100 | * @see net.sf.snmpadaptor4j.api.AttributeAccessor#getValue() | |
101 | */ | |
102 | public Object getValue () throws Exception { | |
103 | 0 | Object value = null; |
104 | 0 | final CompositeData compositeData = (CompositeData) this.server.getAttribute(this.mBeanName, this.attributeName); |
105 | 0 | if (compositeData != null) { |
106 | 0 | value = compositeData.get(this.attributeKey); |
107 | } | |
108 | 0 | return value; |
109 | } | |
110 | ||
111 | /* | |
112 | * {@inheritDoc} | |
113 | * @see net.sf.snmpadaptor4j.api.AttributeAccessor#setValue(java.lang.Object) | |
114 | */ | |
115 | public void setValue (final Object value) { | |
116 | // NOP | |
117 | 0 | } |
118 | ||
119 | /* | |
120 | * {@inheritDoc} | |
121 | * @see net.sf.snmpadaptor4j.api.AttributeAccessor#isReadable() | |
122 | */ | |
123 | public boolean isReadable () { | |
124 | 0 | return true; |
125 | } | |
126 | ||
127 | /* | |
128 | * {@inheritDoc} | |
129 | * @see net.sf.snmpadaptor4j.api.AttributeAccessor#isWritable() | |
130 | */ | |
131 | public boolean isWritable () { | |
132 | 0 | return false; |
133 | } | |
134 | ||
135 | /* | |
136 | * {@inheritDoc} | |
137 | * @see java.lang.Object#toString() | |
138 | */ | |
139 | @Override | |
140 | public String toString () { | |
141 | Object value; | |
142 | try { | |
143 | 0 | value = getValue(); |
144 | } | |
145 | 0 | catch (final Throwable e) { |
146 | 0 | value = "ERROR: " + (e.getMessage() != null ? e.getMessage() : e.getClass().getName()); |
147 | 0 | } |
148 | 0 | return this.oid + ": (" + getSnmpDataType() + ") " + value; |
149 | } | |
150 | ||
151 | } |