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 | 160 | super(); |
41 | 160 | this.server = server; |
42 | 160 | this.mBeanName = mBeanName; |
43 | 160 | this.mapping = mapping; |
44 | 160 | } |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
public ObjectName getMBeanName () { |
51 | 61 | return this.mBeanName; |
52 | |
} |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
public SnmpOid getOid () { |
59 | 32 | return this.mapping.getOid(); |
60 | |
} |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
public SnmpDataType getSnmpDataType () { |
67 | 1 | return this.mapping.getSnmpDataType(); |
68 | |
} |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
public Class<?> getJmxDataType () { |
75 | 1 | return this.mapping.getJmxDataType(); |
76 | |
} |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
public Object getValue () throws Exception { |
83 | 4 | 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 | 1 | this.server.setAttribute(this.mBeanName, new Attribute(this.mapping.getAttributeName(), value)); |
92 | 1 | } |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
public boolean isReadable () { |
99 | 1 | return this.mapping.isReadable(); |
100 | |
} |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
public boolean isWritable () { |
107 | 1 | return this.mapping.isWritable(); |
108 | |
} |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
@Override |
115 | |
public String toString () { |
116 | |
Object value; |
117 | |
try { |
118 | 3 | value = getValue(); |
119 | |
} |
120 | 2 | catch (final Throwable e) { |
121 | 2 | value = "ERROR: " + (e.getMessage() != null ? e.getMessage() : e.getClass().getName()); |
122 | 1 | } |
123 | 3 | return this.mapping.getOid() + ": (" + this.mapping.getSnmpDataType() + ") " + value; |
124 | |
} |
125 | |
|
126 | |
} |