1 package net.sf.snmpadaptor4j.core.accessor;
2
3 import javax.management.Attribute;
4 import javax.management.MBeanServer;
5 import javax.management.ObjectName;
6 import net.sf.snmpadaptor4j.api.AttributeAccessor;
7 import net.sf.snmpadaptor4j.core.mapping.MBeanAttributeMapping;
8 import net.sf.snmpadaptor4j.object.SnmpDataType;
9 import net.sf.snmpadaptor4j.object.SnmpOid;
10
11
12
13
14
15 public final class MBeanAttributeAccessor
16 implements AttributeAccessor {
17
18
19
20
21 private final MBeanServer server;
22
23
24
25
26 private final ObjectName mBeanName;
27
28
29
30
31 private final MBeanAttributeMapping mapping;
32
33
34
35
36
37
38
39 public MBeanAttributeAccessor (final MBeanServer server, final ObjectName mBeanName, final MBeanAttributeMapping mapping) {
40 super();
41 this.server = server;
42 this.mBeanName = mBeanName;
43 this.mapping = mapping;
44 }
45
46
47
48
49
50 public ObjectName getMBeanName () {
51 return this.mBeanName;
52 }
53
54
55
56
57
58 public SnmpOid getOid () {
59 return this.mapping.getOid();
60 }
61
62
63
64
65
66 public SnmpDataType getSnmpDataType () {
67 return this.mapping.getSnmpDataType();
68 }
69
70
71
72
73
74 public Class<?> getJmxDataType () {
75 return this.mapping.getJmxDataType();
76 }
77
78
79
80
81
82 public Object getValue () throws Exception {
83 return this.server.getAttribute(this.mBeanName, this.mapping.getAttributeName());
84 }
85
86
87
88
89
90 public void setValue (final Object value) throws Exception {
91 this.server.setAttribute(this.mBeanName, new Attribute(this.mapping.getAttributeName(), value));
92 }
93
94
95
96
97
98 public boolean isReadable () {
99 return this.mapping.isReadable();
100 }
101
102
103
104
105
106 public boolean isWritable () {
107 return this.mapping.isWritable();
108 }
109
110
111
112
113
114 @Override
115 public String toString () {
116 Object value;
117 try {
118 value = getValue();
119 }
120 catch (final Throwable e) {
121 value = "ERROR: " + (e.getMessage() != null ? e.getMessage() : e.getClass().getName());
122 }
123 return this.mapping.getOid() + ": (" + this.mapping.getSnmpDataType() + ") " + value;
124 }
125
126 }