1 | |
package net.sf.snmpadaptor4j.core; |
2 | |
|
3 | |
import java.math.BigInteger; |
4 | |
import java.net.InetAddress; |
5 | |
import java.util.Iterator; |
6 | |
import java.util.List; |
7 | |
import java.util.Set; |
8 | |
import java.util.SortedMap; |
9 | |
import java.util.TreeMap; |
10 | |
import java.util.TreeSet; |
11 | |
import java.util.Map.Entry; |
12 | |
import javax.management.MBeanServer; |
13 | |
import javax.management.ObjectName; |
14 | |
import net.sf.snmpadaptor4j.api.SnmpMib; |
15 | |
import net.sf.snmpadaptor4j.api.AttributeAccessor; |
16 | |
import net.sf.snmpadaptor4j.core.accessor.MBeanAttributeAccessor; |
17 | |
import net.sf.snmpadaptor4j.core.mapping.MBeanAttributeMapping; |
18 | |
import net.sf.snmpadaptor4j.object.SnmpDataType; |
19 | |
import net.sf.snmpadaptor4j.object.SnmpOid; |
20 | |
import org.apache.log4j.Logger; |
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
public class JmxSnmpMib |
27 | |
implements SnmpMib { |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | 22 | protected final Logger logger = Logger.getLogger(JmxSnmpMib.class); |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
private final SnmpMib other; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | 22 | private final SortedMap<SnmpOid, AttributeAccessor> mib = new TreeMap<SnmpOid, AttributeAccessor>(); |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
public JmxSnmpMib (final SnmpMib other) { |
49 | 22 | super(); |
50 | 22 | this.other = other; |
51 | 22 | } |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
protected final SortedMap<SnmpOid, AttributeAccessor> getMib () { |
58 | 154 | return this.mib; |
59 | |
} |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
void unregisterAllAttributes () { |
65 | |
MBeanAttributeAccessor accessor; |
66 | 2 | synchronized (this.mib) { |
67 | 22 | while (!this.mib.isEmpty()) { |
68 | 20 | accessor = (MBeanAttributeAccessor) this.mib.get(this.mib.firstKey()); |
69 | 20 | unregisterAttributes(accessor.getMBeanName(), accessor.getOid()); |
70 | |
} |
71 | 2 | } |
72 | 2 | } |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
void registerAttributes (final MBeanServer server, final ObjectName mBeanName, final List<MBeanAttributeMapping> mBeanAttributeMappingList) throws Exception { |
82 | 2 | for (final MBeanAttributeMapping mapping : mBeanAttributeMappingList) { |
83 | 26 | if (this.logger.isInfoEnabled()) { |
84 | 13 | this.logger.info("SNMP attribute registering at " + mapping.getOid() + " (" + mapping.getSnmpDataType() + ") = [" + mBeanName + "]." |
85 | |
+ mapping.getAttributeName() + " (" + mapping.getJmxDataType().getSimpleName() + ")"); |
86 | |
} |
87 | 26 | checkDataType(mapping.getSnmpDataType(), mapping.getJmxDataType()); |
88 | 26 | synchronized (this.mib) { |
89 | 26 | this.mib.put(mapping.getOid(), new MBeanAttributeAccessor(server, mBeanName, mapping)); |
90 | 26 | } |
91 | |
} |
92 | 2 | } |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
void unregisterAttributes (final MBeanServer server, final ObjectName mBeanName) { |
100 | 4 | final Set<SnmpOid> oidSet = new TreeSet<SnmpOid>(); |
101 | 4 | synchronized (this.mib) { |
102 | 4 | for (final AttributeAccessor node : this.mib.values()) { |
103 | 40 | if (mBeanName.equals(((MBeanAttributeAccessor) node).getMBeanName())) { |
104 | 10 | oidSet.add(node.getOid()); |
105 | |
} |
106 | |
} |
107 | 4 | for (final SnmpOid oid : oidSet) { |
108 | 10 | unregisterAttributes(mBeanName, oid); |
109 | |
} |
110 | 4 | } |
111 | 4 | } |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
private void unregisterAttributes (final ObjectName mBeanName, final SnmpOid oid) { |
119 | 30 | if (this.logger.isInfoEnabled()) { |
120 | 15 | this.logger.info("SNMP attribute unregistering at " + oid + " for [" + mBeanName + "]"); |
121 | |
} |
122 | 30 | this.mib.remove(oid); |
123 | 30 | } |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
protected static final void checkDataType (final SnmpDataType snmpDataType, final Class<?> jmxDataType) throws Exception { |
132 | 158 | if (snmpDataType == SnmpDataType.integer32) { |
133 | 19 | if (!int.class.equals(jmxDataType) && !Integer.class.equals(jmxDataType) && !byte.class.equals(jmxDataType) && !Byte.class.equals(jmxDataType) |
134 | |
&& !short.class.equals(jmxDataType) && !Short.class.equals(jmxDataType) && !boolean.class.equals(jmxDataType) |
135 | |
&& !Boolean.class.equals(jmxDataType)) { |
136 | 7 | throw new Exception(snmpDataType + " is inconsistent with " + jmxDataType.getName()); |
137 | |
} |
138 | |
} |
139 | 139 | else if ((snmpDataType == SnmpDataType.unsigned32) || (snmpDataType == SnmpDataType.gauge32) || (snmpDataType == SnmpDataType.counter32)) { |
140 | 43 | if (!long.class.equals(jmxDataType) && !Long.class.equals(jmxDataType) && !int.class.equals(jmxDataType) && !Integer.class.equals(jmxDataType) |
141 | |
&& !byte.class.equals(jmxDataType) && !Byte.class.equals(jmxDataType) && !short.class.equals(jmxDataType) && !Short.class.equals(jmxDataType)) { |
142 | 15 | throw new Exception(snmpDataType + " is inconsistent with " + jmxDataType.getName()); |
143 | |
} |
144 | |
} |
145 | 96 | else if (snmpDataType == SnmpDataType.counter64) { |
146 | 15 | if (!BigInteger.class.equals(jmxDataType) && !long.class.equals(jmxDataType) && !Long.class.equals(jmxDataType)) { |
147 | 10 | throw new Exception(snmpDataType + " is inconsistent with " + jmxDataType.getName()); |
148 | |
} |
149 | |
} |
150 | 81 | else if (snmpDataType == SnmpDataType.timeTicks) { |
151 | 15 | if (!long.class.equals(jmxDataType) && !Long.class.equals(jmxDataType)) { |
152 | 11 | throw new Exception(snmpDataType + " is inconsistent with " + jmxDataType.getName()); |
153 | |
} |
154 | |
} |
155 | 66 | else if (snmpDataType == SnmpDataType.ipAddress) { |
156 | 15 | if (!String.class.equals(jmxDataType) && !InetAddress.class.equals(jmxDataType)) { |
157 | 11 | throw new Exception(snmpDataType + " is inconsistent with " + jmxDataType.getName()); |
158 | |
} |
159 | |
} |
160 | 51 | else if (snmpDataType == SnmpDataType.objectIdentifier) { |
161 | 13 | if (!String.class.equals(jmxDataType) && !SnmpOid.class.equals(jmxDataType)) { |
162 | 11 | throw new Exception(snmpDataType + " is inconsistent with " + jmxDataType.getName()); |
163 | |
} |
164 | |
} |
165 | 93 | } |
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
public final AttributeAccessor find (final SnmpOid oid) { |
172 | |
AttributeAccessor node; |
173 | 3 | synchronized (this.mib) { |
174 | 3 | node = this.mib.get(oid); |
175 | 3 | } |
176 | 3 | if (node == null) { |
177 | 2 | node = this.other.find(oid); |
178 | |
} |
179 | 3 | return node; |
180 | |
} |
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
public final AttributeAccessor next (final SnmpOid oid) { |
187 | |
AttributeAccessor node; |
188 | 2 | final Iterator<Entry<SnmpOid, AttributeAccessor>> entryIterator = nextSet(oid).entrySet().iterator(); |
189 | 2 | if (entryIterator.hasNext()) { |
190 | 1 | node = entryIterator.next().getValue(); |
191 | |
} |
192 | |
else { |
193 | 1 | node = null; |
194 | |
} |
195 | 2 | return node; |
196 | |
} |
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
public final SortedMap<SnmpOid, AttributeAccessor> nextSet (final SnmpOid oid) { |
203 | 3 | final SortedMap<SnmpOid, AttributeAccessor> nodeMap = new TreeMap<SnmpOid, AttributeAccessor>(); |
204 | 3 | synchronized (this.mib) { |
205 | 3 | nodeMap.putAll(this.mib.tailMap(SnmpOid.newInstance(oid.getOid(), 0))); |
206 | 3 | } |
207 | 3 | nodeMap.putAll(this.other.nextSet(oid)); |
208 | 3 | return nodeMap; |
209 | |
} |
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
@Override |
216 | |
public final String toString () { |
217 | 1 | return "JmxSnmpMib" + this.mib.values(); |
218 | |
} |
219 | |
|
220 | |
} |