1 10 11 package com.sun.jmx.snmp.agent; 12 13 14 import java.util.Vector ; 17 import java.util.Enumeration ; 18 import java.util.Iterator ; 19 20 import javax.management.AttributeList ; 23 import javax.management.Attribute ; 24 import javax.management.MBeanException ; 25 import javax.management.MBeanServer ; 26 import javax.management.ObjectName ; 27 import javax.management.ReflectionException ; 28 import javax.management.InstanceNotFoundException ; 29 import javax.management.InvalidAttributeValueException ; 30 import javax.management.InstanceAlreadyExistsException ; 31 import javax.management.MBeanRegistrationException ; 32 import javax.management.NotCompliantMBeanException ; 33 import javax.management.RuntimeOperationsException ; 34 import com.sun.jmx.snmp.SnmpOid; 35 import com.sun.jmx.snmp.SnmpValue; 36 import com.sun.jmx.snmp.SnmpVarBind; 37 import com.sun.jmx.snmp.SnmpStatusException; 38 39 40 60 61 public class SnmpGenericObjectServer { 62 63 69 72 protected final MBeanServer server; 73 74 80 87 public SnmpGenericObjectServer(MBeanServer server) { 88 this.server = server; 89 } 90 91 124 public void get(SnmpGenericMetaServer meta, ObjectName name, 125 SnmpMibSubRequest req, int depth) 126 throws SnmpStatusException { 127 128 130 final int size = req.getSize(); 131 final Object data = req.getUserData(); 132 final String [] nameList = new String [size]; 133 final SnmpVarBind[] varList = new SnmpVarBind[size]; 134 final long[] idList = new long[size]; 135 int i = 0; 136 137 for (Enumeration e=req.getElements(); e.hasMoreElements();) { 138 final SnmpVarBind var= (SnmpVarBind) e.nextElement(); 139 try { 140 final long id = var.oid.getOidArc(depth); 141 nameList[i] = meta.getAttributeName(id); 142 varList[i] = var; 143 idList[i] = id; 144 145 meta.checkGetAccess(id,data); 150 151 i++; 153 } catch(SnmpStatusException x) { 154 req.registerGetException(var,x); 157 } 158 } 159 160 AttributeList result = null; 161 int errorCode = SnmpStatusException.noSuchInstance; 162 163 try { 164 result = server.getAttributes(name,nameList); 165 } catch (InstanceNotFoundException f) { 166 result = new AttributeList (); 169 } catch (ReflectionException r) { 170 result = new AttributeList (); 173 } catch (Exception x) { 174 result = new AttributeList (); 175 } 176 177 178 final Iterator it = result.iterator(); 179 180 for (int j=0; j < i; j++) { 181 if (!it.hasNext()) { 182 final SnmpStatusException x = 185 new SnmpStatusException(errorCode); 186 req.registerGetException(varList[j],x); 187 continue; 188 } 189 190 final Attribute att = (Attribute ) it.next(); 191 192 while ((j < i) && (! nameList[j].equals(att.getName()))) { 193 final SnmpStatusException x = 196 new SnmpStatusException(errorCode); 197 req.registerGetException(varList[j],x); 198 j++; 199 } 200 201 if ( j == i) break; 202 203 try { 204 varList[j].value = 205 meta.buildSnmpValue(idList[j],att.getValue()); 206 } catch (SnmpStatusException x) { 207 req.registerGetException(varList[j],x); 208 } 209 } 211 } 213 214 236 public SnmpValue get(SnmpGenericMetaServer meta, ObjectName name, 237 long id, Object data) 238 throws SnmpStatusException { 239 final String attname = meta.getAttributeName(id); 240 Object result = null; 241 242 try { 243 result = server.getAttribute(name,attname); 244 } catch (MBeanException m) { 245 Exception t = m.getTargetException(); 246 if (t instanceof SnmpStatusException) 247 throw (SnmpStatusException) t; 248 throw new SnmpStatusException(SnmpStatusException.noSuchInstance); 249 } catch (Exception e) { 250 throw new SnmpStatusException(SnmpStatusException.noSuchInstance); 251 } 252 253 return meta.buildSnmpValue(id,result); 254 } 255 256 289 public void set(SnmpGenericMetaServer meta, ObjectName name, 290 SnmpMibSubRequest req, int depth) 291 throws SnmpStatusException { 292 293 final int size = req.getSize(); 294 final AttributeList attList = new AttributeList (size); 295 final String [] nameList = new String [size]; 296 final SnmpVarBind[] varList = new SnmpVarBind[size]; 297 final long[] idList = new long[size]; 298 int i = 0; 299 300 for (Enumeration e=req.getElements(); e.hasMoreElements();) { 301 final SnmpVarBind var= (SnmpVarBind) e.nextElement(); 302 try { 303 final long id = var.oid.getOidArc(depth); 304 final String attname = meta.getAttributeName(id); 305 final Object attvalue= 306 meta.buildAttributeValue(id,var.value); 307 final Attribute att = new Attribute (attname,attvalue); 308 attList.add(att); 309 nameList[i] = attname; 310 varList[i] = var; 311 idList[i] = id; 312 i++; 313 } catch(SnmpStatusException x) { 314 req.registerSetException(var,x); 315 } 316 } 317 318 AttributeList result = null; 319 int errorCode = SnmpStatusException.noAccess; 320 321 try { 322 result = server.setAttributes(name,attList); 323 } catch (InstanceNotFoundException f) { 324 result = new AttributeList (); 325 errorCode = SnmpStatusException.snmpRspInconsistentName; 326 } catch (ReflectionException r) { 327 errorCode = SnmpStatusException.snmpRspInconsistentName; 328 result = new AttributeList (); 329 } catch (Exception x) { 330 result = new AttributeList (); 331 } 332 333 final Iterator it = result.iterator(); 334 335 for (int j=0; j < i; j++) { 336 if (!it.hasNext()) { 337 final SnmpStatusException x = 338 new SnmpStatusException(errorCode); 339 req.registerSetException(varList[j],x); 340 continue; 341 } 342 343 final Attribute att = (Attribute ) it.next(); 344 345 while ((j < i) && (! nameList[j].equals(att.getName()))) { 346 final SnmpStatusException x = 347 new SnmpStatusException(SnmpStatusException.noAccess); 348 req.registerSetException(varList[j],x); 349 j++; 350 } 351 352 if ( j == i) break; 353 354 try { 355 varList[j].value = 356 meta.buildSnmpValue(idList[j],att.getValue()); 357 } catch (SnmpStatusException x) { 358 req.registerSetException(varList[j],x); 359 } 360 361 } 362 } 363 364 387 public SnmpValue set(SnmpGenericMetaServer meta, ObjectName name, 388 SnmpValue x, long id, Object data) 389 throws SnmpStatusException { 390 final String attname = meta.getAttributeName(id); 391 final Object attvalue= 392 meta.buildAttributeValue(id,x); 393 final Attribute att = new Attribute (attname,attvalue); 394 395 Object result = null; 396 397 try { 398 server.setAttribute(name,att); 399 result = server.getAttribute(name,attname); 400 } catch(InvalidAttributeValueException iv) { 401 throw new 402 SnmpStatusException(SnmpStatusException.snmpRspWrongValue); 403 } catch (InstanceNotFoundException f) { 404 throw new 405 SnmpStatusException(SnmpStatusException.snmpRspInconsistentName); 406 } catch (ReflectionException r) { 407 throw new 408 SnmpStatusException(SnmpStatusException.snmpRspInconsistentName); 409 } catch (MBeanException m) { 410 Exception t = m.getTargetException(); 411 if (t instanceof SnmpStatusException) 412 throw (SnmpStatusException) t; 413 throw new 414 SnmpStatusException(SnmpStatusException.noAccess); 415 } catch (Exception e) { 416 throw new 417 SnmpStatusException(SnmpStatusException.noAccess); 418 } 419 420 return meta.buildSnmpValue(id,result); 421 } 422 423 451 public void check(SnmpGenericMetaServer meta, ObjectName name, 452 SnmpMibSubRequest req, int depth) 453 throws SnmpStatusException { 454 455 final Object data = req.getUserData(); 456 457 for (Enumeration e=req.getElements(); e.hasMoreElements();) { 458 final SnmpVarBind var= (SnmpVarBind) e.nextElement(); 459 try { 460 final long id = var.oid.getOidArc(depth); 461 check(meta,name,var.value,id,data); 463 } catch(SnmpStatusException x) { 464 req.registerCheckException(var,x); 465 } 466 } 467 } 468 469 500 public void check(SnmpGenericMetaServer meta, ObjectName name, 502 SnmpValue x, long id, Object data) 503 throws SnmpStatusException { 504 505 meta.checkSetAccess(x,id,data); 506 try { 507 final String attname = meta.getAttributeName(id); 508 final Object attvalue= meta.buildAttributeValue(id,x); 509 final Object [] params = new Object [1]; 510 final String [] signature = new String [1]; 511 512 params[0] = attvalue; 513 signature[0] = attvalue.getClass().getName(); 514 server.invoke(name,"check"+attname,params,signature); 515 516 } catch( SnmpStatusException e) { 517 throw e; 518 } 519 catch (InstanceNotFoundException i) { 520 throw new 521 SnmpStatusException(SnmpStatusException.snmpRspInconsistentName); 522 } catch (ReflectionException r) { 523 } catch (MBeanException m) { 525 Exception t = m.getTargetException(); 526 if (t instanceof SnmpStatusException) 527 throw (SnmpStatusException) t; 528 throw new SnmpStatusException(SnmpStatusException.noAccess); 529 } catch (Exception e) { 530 throw new 531 SnmpStatusException(SnmpStatusException.noAccess); 532 } 533 } 534 535 public void registerTableEntry(SnmpMibTable meta, SnmpOid rowOid, 536 ObjectName objname, Object entry) 537 throws SnmpStatusException { 538 if (objname == null) 539 throw new 540 SnmpStatusException(SnmpStatusException.snmpRspInconsistentName); 541 try { 542 if (entry != null && !server.isRegistered(objname)) 543 server.registerMBean(entry, objname); 544 } catch (InstanceAlreadyExistsException e) { 545 throw new 546 SnmpStatusException(SnmpStatusException.snmpRspInconsistentName); 547 } catch (MBeanRegistrationException e) { 548 throw new SnmpStatusException(SnmpStatusException.snmpRspNoAccess); 549 } catch (NotCompliantMBeanException e) { 550 throw new SnmpStatusException(SnmpStatusException.snmpRspGenErr); 551 } catch (RuntimeOperationsException e) { 552 throw new SnmpStatusException(SnmpStatusException.snmpRspGenErr); 553 } catch(Exception e) { 554 throw new SnmpStatusException(SnmpStatusException.snmpRspGenErr); 555 } 556 } 557 558 } 559 | Popular Tags |