| 1 22 23 package org.snmp4j.agent.mo.jmx; 24 25 import javax.management.ObjectName ; 26 import java.util.Collection ; 27 import java.util.Arrays ; 28 import java.util.List ; 29 import javax.management.MBeanServerConnection ; 30 import java.util.Iterator ; 31 import java.io.IOException ; 32 import javax.management.MBeanException ; 33 import javax.management.AttributeNotFoundException ; 34 import javax.management.InstanceNotFoundException ; 35 import javax.management.ReflectionException ; 36 import org.snmp4j.agent.mo.jmx.util.JMXArrayIndexKey; 37 import org.snmp4j.agent.mo.jmx.types.*; 38 import java.util.AbstractList ; 39 import java.util.Collections ; 40 import java.util.ArrayList ; 41 42 52 public class MBeanAttributeKeyProvider extends MBeanAttributeMOInfo { 53 54 private boolean keysNeedSorting; 55 56 64 public MBeanAttributeKeyProvider(ObjectName name, TypedAttribute attribute) { 65 super(name, attribute); 66 } 67 68 78 public MBeanAttributeKeyProvider(ObjectName name, TypedAttribute attribute, 79 boolean keysNeedSorting) { 80 super(name, attribute); 81 this.keysNeedSorting = keysNeedSorting; 82 } 83 84 96 public Iterator keyIterator(MBeanServerConnection server) throws IOException , 97 MBeanException , AttributeNotFoundException , InstanceNotFoundException , 98 ReflectionException  99 { 100 Object keys = getAttribute(server); 101 if (keys instanceof Collection ) { 102 if (keysNeedSorting) { 103 List l = new ArrayList ((Collection )keys); 104 Collections.sort(l); 105 keys = l; 106 } 107 return ((Collection )keys).iterator(); 108 } 109 else if (keys instanceof Object []) { 110 List l = Arrays.asList((Object [])keys); 111 return getAscendingIterator(l); 112 } 113 else if (keys instanceof long[]) { 114 List l = asList((long[])keys); 115 return getAscendingIterator(l); 116 } 117 else if (keys instanceof int[]) { 118 List l = asList((int[])keys); 119 return getAscendingIterator(l); 120 } 121 else { 122 throw new ClassCastException (keys.getClass()+ 123 " is not a supported list"); 124 } 125 } 126 127 private Iterator getAscendingIterator(List l) { 128 if (keysNeedSorting) { 129 l = new ArrayList (l); 130 Collections.sort(l); 131 } 132 return l.iterator(); 133 } 134 135 public static List <Integer > asList(final int[] a) { 136 return new AbstractList <Integer >() { 137 public Integer get(int i) { return a[i]; } 138 public Integer set(int i, Integer val) { 140 Integer oldVal = a[i]; 141 a[i] = val; 142 return oldVal; 143 } 144 public int size() { return a.length; } 145 }; 146 } 147 148 public static List <Long > asList(final long[] a) { 149 return new AbstractList <Long >() { 150 public Long get(int i) { return a[i]; } 151 public Long set(int i, Integer val) { 153 Long oldVal = a[i]; 154 a[i] = val; 155 return oldVal; 156 } 157 public int size() { return a.length; } 158 }; 159 } 160 161 177 public Iterator keyTailIterator(MBeanServerConnection server, 178 Object firstRowId) throws IOException , 179 MBeanException , AttributeNotFoundException , InstanceNotFoundException , 180 ReflectionException  181 { 182 Object keys = getAttribute(server); 183 if (keys instanceof Collection ) { 184 keys = ((Collection )keys).toArray(); 185 } 186 if (keys instanceof Object []) { 187 if (keysNeedSorting) { 188 Arrays.sort((Object [])keys); 189 } 190 List l = Arrays.asList((Object [])keys); 191 int pos = 0; 192 if (firstRowId instanceof JMXArrayIndexKey) { 193 pos = ((JMXArrayIndexKey)firstRowId).getIndex(); 194 } 195 else { 196 pos = Arrays.binarySearch((Object []) keys, firstRowId); 197 } 198 if (Math.abs(pos) >= l.size()) { 199 return Collections.emptyList().iterator(); 200 } 201 return createTailIterator(l.listIterator(Math.abs(pos)), pos); 202 } 203 else if (keys instanceof long[]) { 204 if (keysNeedSorting) { 205 Arrays.sort((long[])keys); 206 } 207 List l = asList((long[])keys); 208 int pos = 0; 209 if (firstRowId instanceof JMXArrayIndexKey) { 210 pos = ((JMXArrayIndexKey)firstRowId).getIndex(); 211 } 212 else { 213 pos = Arrays.binarySearch((long[]) keys, (Long )firstRowId); 214 } 215 if (Math.abs(pos) >= l.size()) { 216 return Collections.emptyList().iterator(); 217 } 218 return createTailIterator(l.listIterator(Math.abs(pos)), pos); 219 } 220 else if (keys instanceof int[]) { 221 if (keysNeedSorting) { 222 Arrays.sort((int[])keys); 223 } 224 List l = asList((int[])keys); 225 int pos = 0; 226 if (firstRowId instanceof JMXArrayIndexKey) { 227 pos = ((JMXArrayIndexKey)firstRowId).getIndex(); 228 } 229 else { 230 pos = Arrays.binarySearch((int[]) keys, (Integer )firstRowId); 231 } 232 if (Math.abs(pos) >= l.size()) { 233 return Collections.emptyList().iterator(); 234 } 235 return createTailIterator(l.listIterator(Math.abs(pos)), pos); 236 } 237 else { 238 throw new ClassCastException (keys.getClass()+ 239 " is not a supported list"); 240 } 241 } 242 243 protected Iterator createTailIterator(Iterator it, int indexPos) { 244 return it; 245 } 246 247 259 public int getKeyCount(MBeanServerConnection server) throws IOException , 260 MBeanException , AttributeNotFoundException , InstanceNotFoundException , 261 ReflectionException  262 { 263 Object keys = getAttribute(server); 264 if (keys instanceof Collection ) { 265 return ((Collection )keys).size(); 266 } 267 else if (keys instanceof Object []) { 268 return ((Object [])keys).length; 269 } 270 else if (keys instanceof long[]) { 271 return ((long[])keys).length; 272 } 273 else if (keys instanceof int[]) { 274 return ((int[])keys).length; 275 } 276 return 0; 277 } 278 279 public Object getRowValues(MBeanServerConnection server, 280 Object indexObject) throws IOException , 281 MBeanException , AttributeNotFoundException , InstanceNotFoundException , 282 ReflectionException  283 { 284 return indexObject; 285 } 286 287 } 288 | Popular Tags |