1. /*
  2. * @(#)file SnmpProxyMBean.java
  3. * @(#)author Sun Microsystems, Inc.
  4. * @(#)version X.XX
  5. * @(#)date XX/XX/XX
  6. *
  7. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  8. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  9. *
  10. */
  11. package com.sun.jmx.snmp.agent;
  12. // java imports
  13. //
  14. import java.io.Serializable;
  15. import java.util.Enumeration;
  16. // jmx imports
  17. //
  18. import com.sun.jmx.snmp.SnmpStatusException;
  19. import com.sun.jmx.snmp.SnmpDefinitions;
  20. import javax.management.ObjectName;
  21. import javax.management.MBeanServer;
  22. import com.sun.jmx.snmp.SnmpVarBind;
  23. // SNMP Runtime imports
  24. //
  25. import com.sun.jmx.trace.Trace;
  26. /**
  27. * A simple MIB agent that implements SNMP calls (get, set, getnext and getbulk) in a way that only errors or exceptions are returned. Every call done on this agent fails. Error handling is done according to the manager's SNMP protocol version.
  28. * <P>It is used by <CODE>SnmpAdaptorServer</CODE> for its default agent behavior. When a received Oid doesn't match, this agent is called to fill the result list with errors.</P>
  29. * <p><b>This API is a Sun Microsystems internal API and is subject
  30. * to change without notice.</b></p>
  31. * @since 1.5
  32. *
  33. */
  34. public class SnmpErrorHandlerAgent extends SnmpMibAgent
  35. implements Serializable {
  36. public SnmpErrorHandlerAgent() {}
  37. /**
  38. * Initializes the MIB (with no registration of the MBeans into the
  39. * MBean server). Does nothing.
  40. *
  41. * @exception IllegalAccessException The MIB cannot be initialized.
  42. */
  43. public void init() throws IllegalAccessException {
  44. }
  45. /**
  46. * Initializes the MIB but each single MBean representing the MIB
  47. * is inserted into the MBean server.
  48. *
  49. * @param server The MBean server to register the service with.
  50. * @param name The object name.
  51. *
  52. * @return The passed name paramter.
  53. *
  54. * @exception java.lang.Exception
  55. */
  56. public ObjectName preRegister(MBeanServer server, ObjectName name)
  57. throws Exception {
  58. return name;
  59. }
  60. /**
  61. * Gets the root object identifier of the MIB.
  62. * <P>The root object identifier is the object identifier uniquely
  63. * identifying the MIB.
  64. *
  65. * @return The returned oid is null.
  66. */
  67. public long[] getRootOid() {
  68. return null;
  69. }
  70. /**
  71. * Processes a <CODE>get</CODE> operation. It will throw an exception for V1 requests or it will set exceptions within the list for V2 requests.
  72. *
  73. * @param inRequest The SnmpMibRequest object holding the list of variable to be retrieved.
  74. *
  75. * @exception SnmpStatusException An error occured during the operation.
  76. */
  77. public void get(SnmpMibRequest inRequest) throws SnmpStatusException {
  78. if(isDebugOn()) trace("get","Get in Exception");
  79. if(inRequest.getVersion() == SnmpDefinitions.snmpVersionOne)
  80. throw new SnmpStatusException(SnmpStatusException.noSuchName);
  81. Enumeration l = inRequest.getElements();
  82. while(l.hasMoreElements()) {
  83. SnmpVarBind varbind = (SnmpVarBind) l.nextElement();
  84. varbind.setNoSuchObject();
  85. }
  86. }
  87. /**
  88. * Checks if a <CODE>set</CODE> operation can be performed.
  89. * If the operation can not be performed, the method should emit a
  90. * <CODE>SnmpStatusException</CODE>.
  91. *
  92. * @param inRequest The SnmpMibRequest object holding the list of variables to
  93. * be set. This list is composed of
  94. * <CODE>SnmpVarBind</CODE> objects.
  95. *
  96. * @exception SnmpStatusException The <CODE>set</CODE> operation
  97. * cannot be performed.
  98. */
  99. public void check(SnmpMibRequest inRequest) throws SnmpStatusException {
  100. if(isDebugOn()) trace("check","Check in Exception");
  101. throw new SnmpStatusException(SnmpDefinitions.snmpRspNotWritable);
  102. }
  103. /**
  104. * Processes a <CODE>set</CODE> operation. Should never be called (check previously called having failed).
  105. *
  106. * @param inRequest The SnmpMibRequest object holding the list of variable to be set.
  107. *
  108. * @exception SnmpStatusException An error occured during the operation.
  109. */
  110. public void set(SnmpMibRequest inRequest) throws SnmpStatusException {
  111. if(isDebugOn()) trace("set","Set in Exception, CAN't be called");
  112. throw new SnmpStatusException(SnmpDefinitions.snmpRspNotWritable);
  113. }
  114. /**
  115. * Processes a <CODE>getNext</CODE> operation. It will throw an exception for V1 requests or it will set exceptions within the list for V2 requests..
  116. *
  117. * @param inRequest The SnmpMibRequest object holding the list of variables to be retrieved.
  118. *
  119. * @exception SnmpStatusException An error occured during the operation.
  120. */
  121. public void getNext(SnmpMibRequest inRequest) throws SnmpStatusException {
  122. if(isDebugOn()) trace("getNext","GetNext in Exception");
  123. if(inRequest.getVersion() == SnmpDefinitions.snmpVersionOne)
  124. throw new SnmpStatusException(SnmpStatusException.noSuchName);
  125. Enumeration l = inRequest.getElements();
  126. while(l.hasMoreElements()) {
  127. SnmpVarBind varbind = (SnmpVarBind) l.nextElement();
  128. varbind.setEndOfMibView();
  129. }
  130. }
  131. /**
  132. * Processes a <CODE>getBulk</CODE> operation. It will throw an exception if the request is a V1 one or it will set exceptions within the list for V2 ones.
  133. *
  134. * @param inRequest The SnmpMibRequest object holding the list of variable to be retrieved.
  135. *
  136. * @exception SnmpStatusException An error occured during the operation.
  137. */
  138. public void getBulk(SnmpMibRequest inRequest, int nonRepeat, int maxRepeat)
  139. throws SnmpStatusException {
  140. if(isDebugOn()) trace("getBulk","GetBulk in Exception");
  141. if(inRequest.getVersion() == SnmpDefinitions.snmpVersionOne)
  142. throw new SnmpStatusException(SnmpDefinitions.snmpRspGenErr, 0);
  143. Enumeration l = inRequest.getElements();
  144. while(l.hasMoreElements()) {
  145. SnmpVarBind varbind = (SnmpVarBind) l.nextElement();
  146. varbind.setEndOfMibView();
  147. }
  148. }
  149. private boolean isDebugOn() {
  150. return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_ADAPTOR_SNMP);
  151. }
  152. private void debug(String clz, String func, String info) {
  153. Trace.send(Trace.LEVEL_TRACE, Trace.INFO_ADAPTOR_SNMP, clz, func, info);
  154. }
  155. private void trace(String func, String info) {
  156. debug(dbgTag, func, info);
  157. }
  158. private String dbgTag = "SnmpErrorHandlerAgent";
  159. }