| 1 22 23 package org.snmp4j.agent.mo.jmx; 24 25 import java.util.Iterator ; 26 import javax.management.MBeanServerConnection ; 27 28 import org.snmp4j.agent.mo.MOTableRow; 29 import org.snmp4j.smi.OID; 30 import java.util.Set ; 31 import javax.management.ObjectName ; 32 import java.util.Collection ; 33 import java.util.TreeMap ; 34 import java.util.SortedMap ; 35 import java.util.List ; 36 import org.snmp4j.PDU; 37 import org.snmp4j.agent.mo.jmx.types.SMIVariant; 38 import org.snmp4j.agent.mo.jmx.types.*; 39 import javax.management.ObjectInstance ; 40 import javax.management.Attribute ; 41 import org.snmp4j.agent.mo.MOTable; 43 44 51 public class MBeanAttributeMOTableSupport extends AbstractMBeanSupport 52 implements JMXTableSupport { 53 54 public MBeanAttributeMOTableSupport(MBeanServerConnection server) { 55 super(server); 56 } 57 58 66 public synchronized void add(OID tableOID, 67 MBeanAttributeMOTableInfo mBeanInfo) { 68 oid2MBeanMap.put(tableOID, mBeanInfo); 69 } 70 71 81 public synchronized void addAll(Object [][] tableDescriptions) { 82 for (Object [] tableDescr : tableDescriptions) { 83 MBeanAttributeMOTableInfo mBeanInfo = 84 (MBeanAttributeMOTableInfo)tableDescr[1]; 85 oid2MBeanMap.put((OID)tableDescr[0], mBeanInfo); 86 } 87 } 88 89 public OID getLastIndex(OID tableOID) { 90 MBeanAttributeMOTableInfo mBeanInfo = 91 (MBeanAttributeMOTableInfo) getMBeanMOInfo(tableOID); 92 if (mBeanInfo != null) { 93 try { 94 if (mBeanInfo.getKeyProvider() == null) { 95 OID maxIndex = new OID(); 96 Set <ObjectInstance > mBeans = mBeanInfo.getMBeanNames(server); 97 for (ObjectInstance mBean : mBeans) { 98 Object key = mBeanInfo.getKey(server, mBean.getObjectName()); 99 OID index = mBeanInfo.getIndexSupport().mapToIndex(key); 100 if (index.compareTo(maxIndex) > 0) { 101 maxIndex = index; 102 } 103 } 104 return maxIndex; 105 } 106 else { 107 MBeanAttributeMOInfo keyProvider = mBeanInfo.getKeyProvider(); 108 Object keys = keyProvider.getAttribute(server); 109 if (keys instanceof Object []) { 110 Object [] k = (Object [])keys; 111 if (k.length == 0) { 112 return null; 113 } 114 return mapToIndex(tableOID, k[k.length-1], k.length-1); 115 } 116 else if (keys instanceof List ) { 117 List k = (List )keys; 118 if (k.isEmpty()) { 119 return null; 120 } 121 int lastID = k.size()-1; 122 return mapToIndex(tableOID, k.get(lastID), lastID); 123 } 124 else if (keys instanceof Collection ) { 125 int i=0; 126 for (Iterator it = ((Collection )keys).iterator(); it.hasNext(); i++) { 127 Object k = it.next(); 128 if (!it.hasNext()) { 129 return mapToIndex(tableOID, k, i); 130 } 131 } 132 } 133 else { 134 throw new ClassCastException (keys.getClass()+ 135 " is not a supported list"); 136 } 137 } 138 } 139 catch (Exception ex) { 140 throw new RuntimeException (ex); 141 } 142 } 143 return null; 144 } 145 146 public int getRow(OID tableOID, MOTableRow row) { 147 MBeanAttributeMOTableInfo mBeanInfo = 148 (MBeanAttributeMOTableInfo) getMBeanMOInfo(tableOID); 149 if (mBeanInfo != null) { 150 Object key = 151 mBeanInfo.getIndexSupport().mapToRowIdentifier(row.getIndex()); 152 ObjectName oname = mBeanInfo.getIndexSupport().mapToRowMBean(key); 153 if (oname == null) { 154 try { 155 TypedAttribute[] columns = mBeanInfo.getColumns(); 156 Object value = 157 mBeanInfo.getKeyProvider().getRowValues(server, key); 158 if (value instanceof Object []) { 159 for (int i=0; i<((Object [])value).length && (i<row.size()); i++) { 160 TypedAttribute col = columns[i]; 161 Object v = ((Object [])value)[i]; 162 SMIVariant smiValue = new SMIVariant(row.getValue(i)); 163 v = col.transformFromNative(v, null); 164 smiValue.setValue(v); 165 } 166 } 167 else { 168 for (int i=0; ((columns == null) || (i<columns.length)) && 169 (i<row.size()); i++) { 170 SMIVariant smiValue = new SMIVariant(row.getValue(i)); 171 Object v; 172 if (columns == null) { 173 v = value; 174 } 175 else { 176 v = columns[i].transformFromNative(value, null); 177 } 178 smiValue.setValue(v); 179 } 180 } 181 return PDU.noError; 182 } 183 catch (Exception ex) { 184 ex.printStackTrace(); 185 } 186 return PDU.resourceUnavailable; 187 } 188 TypedAttribute[] columns = mBeanInfo.getColumns(); 189 for (int i=0; (i<columns.length) && (i<row.size()); i++) { 190 TypedAttribute col = columns[i]; 191 SMIVariant colVariant = new SMIVariant(row.getValue(i)); 192 try { 193 Object colValue = server.getAttribute(oname, col.getName()); 194 colValue = col.transformFromNative(colValue, oname); 195 int status = colVariant.setValue(colValue); 196 if (status != PDU.noError) { 197 return status; 198 } 199 } 200 catch (Exception ex) { 201 } 203 } 204 return PDU.noError; 205 } 206 return PDU.resourceUnavailable; 207 } 208 209 public int setRow(OID tableOID, MOTableRow row, int column) { 210 MBeanAttributeMOTableInfo mBeanInfo = 211 (MBeanAttributeMOTableInfo) getMBeanMOInfo(tableOID); 212 if (mBeanInfo != null) { 213 Object key = 214 mBeanInfo.getIndexSupport().mapToRowIdentifier(row.getIndex()); 215 if ((mBeanInfo.getKeyProvider() == null) && (key == null)) { 216 try { 217 Set <ObjectInstance > mBeans = mBeanInfo.getMBeanNames(server); 218 for (ObjectInstance mBean : mBeans) { 219 Object k = mBeanInfo.getKey(server, mBean.getObjectName()); 220 OID i = mBeanInfo.getIndexSupport().mapToIndex(k); 221 if (row.getIndex().equals(i)) { 222 key = k; 223 break; 224 } 225 } 226 } 227 catch (Exception ex1) { 228 ex1.printStackTrace(); 229 } 230 } 231 if (key == null) { 232 return PDU.noSuchName; 233 } 234 ObjectName oname = mBeanInfo.getIndexSupport().mapToRowMBean(key); 235 TypedAttribute[] columns = mBeanInfo.getColumns(); 236 if (column<columns.length) { 237 TypedAttribute col = columns[column]; 238 SMIVariant colVariant = new SMIVariant(row.getValue(column)); 239 Object colValue = colVariant.getValue(col.getType()); 240 try { 241 colValue = col.transformToNative(colValue, 242 (col.isNativeValueAlwaysNeeded() ? 243 server.getAttribute(oname, col.getName()): 244 null), 245 oname); 246 if (colValue != null) { 247 server.setAttribute(oname, new Attribute (col.getName(), colValue)); 248 } 249 } 250 catch (Exception ex) { 251 ex.printStackTrace(); 252 } 253 } 254 return PDU.noError; 255 } 256 return PDU.resourceUnavailable; 257 } 258 259 260 public int getRowCount(OID tableOID) { 261 MBeanAttributeMOTableInfo mBeanInfo = 262 (MBeanAttributeMOTableInfo) getMBeanMOInfo(tableOID); 263 if (mBeanInfo != null) { 264 try { 265 if (mBeanInfo.getKeyProvider() == null) { 266 Set <ObjectInstance > mBeans = mBeanInfo.getMBeanNames(server); 267 return mBeans.size(); 268 } 269 else { 270 MBeanAttributeKeyProvider keyProvider = mBeanInfo.getKeyProvider(); 271 return keyProvider.getKeyCount(server); 272 } 273 } 274 catch (Exception ex) { 275 ex.printStackTrace(); 276 } 277 } 278 return 0; 279 } 280 281 public Iterator rowIdIterator(OID tableOID) { 282 MBeanAttributeMOTableInfo mBeanInfo = 283 (MBeanAttributeMOTableInfo) getMBeanMOInfo(tableOID); 284 if (mBeanInfo != null) { 285 try { 286 if (mBeanInfo.getKeyProvider() == null) { 287 Set <ObjectInstance > mBeans = mBeanInfo.getMBeanNames(server); 288 SortedMap indexes = new TreeMap (); 289 for (ObjectInstance mBean : mBeans) { 290 Object key = mBeanInfo.getKey(server, mBean.getObjectName()); 291 OID index = mBeanInfo.getIndexSupport().mapToIndex(key); 292 indexes.put(index, key); 293 } 294 return indexes.values().iterator(); 295 } 296 else { 297 MBeanAttributeKeyProvider keyProvider = mBeanInfo.getKeyProvider(); 298 return keyProvider.keyIterator(server); 299 } 300 } 301 catch (Exception ex) { 302 ex.printStackTrace(); 303 } 304 } 305 return null; 306 } 307 308 public OID mapToIndex(OID tableOID, Object nativeRowId, int nativeIndex) { 309 MBeanAttributeMOTableInfo mBeanInfo = 310 (MBeanAttributeMOTableInfo) getMBeanMOInfo(tableOID); 311 if (mBeanInfo != null) { 312 Object rowId = 313 mBeanInfo.getIndexSupport().getRowIdentifier(nativeRowId, nativeIndex); 314 return mBeanInfo.getIndexSupport().mapToIndex(rowId); 315 } 316 return null; 317 } 318 319 public Object mapToRowId(OID tableOID, OID rowIndex) { 320 MBeanAttributeMOTableInfo mBeanInfo = 321 (MBeanAttributeMOTableInfo) getMBeanMOInfo(tableOID); 322 if (mBeanInfo != null) { 323 return mBeanInfo.getIndexSupport().mapToRowIdentifier(rowIndex); 324 } 325 return null; 326 } 327 328 public Iterator rowIdTailIterator(OID tableOID, Object firstRowId) { 329 MBeanAttributeMOTableInfo mBeanInfo = 330 (MBeanAttributeMOTableInfo) getMBeanMOInfo(tableOID); 331 if (mBeanInfo != null) { 332 try { 333 if (mBeanInfo.getKeyProvider() == null) { 334 Set <ObjectInstance > mBeans = mBeanInfo.getMBeanNames(server); 335 TreeMap indexes = new TreeMap (); 336 OID firstIndex = null; 337 for (ObjectInstance mBean : mBeans) { 338 Object key = mBeanInfo.getKey(server, mBean.getObjectName()); 339 OID index = mBeanInfo.getIndexSupport().mapToIndex(key); 340 if (key.equals(firstRowId)) { 341 firstIndex = index; 342 } 343 indexes.put(index, key); 344 } 345 if (firstIndex == null) { 346 return indexes.values().iterator(); 347 } 348 return indexes.tailMap(firstIndex).values().iterator(); 349 } 350 else { 351 MBeanAttributeKeyProvider keyProvider = mBeanInfo.getKeyProvider(); 352 return keyProvider.keyTailIterator(server, firstRowId); 353 } 354 } 355 catch (Exception ex) { 356 ex.printStackTrace(); 357 } 358 } 359 return null; 360 } 361 } 362 | Popular Tags |