1. /*
  2. * @(#)file SnmpStandardObjectServer.java
  3. * @(#)author Sun Microsystems, Inc.
  4. * @(#)version 1.10
  5. * @(#)date 04/09/15
  6. *
  7. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  8. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  9. */
  10. package com.sun.jmx.snmp.agent;
  11. // java imports
  12. //
  13. import java.io.Serializable;
  14. import java.util.Hashtable;
  15. import java.util.Enumeration;
  16. import java.util.Vector;
  17. // jmx imports
  18. //
  19. import com.sun.jmx.snmp.SnmpOid;
  20. import com.sun.jmx.snmp.SnmpValue;
  21. import com.sun.jmx.snmp.SnmpVarBind;
  22. import com.sun.jmx.snmp.SnmpStatusException;
  23. // SNMP Runtime imports
  24. //
  25. /**
  26. * <p>
  27. * This class is a utility class that transform SNMP GET / SET requests
  28. * into series of get<i>AttributeName</i>() set<i>AttributeName</i>()
  29. * invoked on the MBean.
  30. * </p>
  31. *
  32. * <p>
  33. * The transformation relies on the metadata information provided by the
  34. * {@link com.sun.jmx.snmp.agent.SnmpStandardMetaServer} object which is
  35. * passed as first parameter to every method. This SnmpStandardMetaServer
  36. * object is usually a Metadata object generated by <code>mibgen</code>.
  37. * </p>
  38. *
  39. * <p>
  40. * The MBean is not invoked directly by this class but through the
  41. * metadata object which holds a reference on it.
  42. * </p>
  43. *
  44. * <p><b><i>
  45. * This class is used internally by mibgen generated metadata objects and
  46. * you should never need to use it directly.
  47. * </b></i></p>
  48. * <p><b>This API is a Sun Microsystems internal API and is subject
  49. * to change without notice.</b></p>
  50. **/
  51. public class SnmpStandardObjectServer implements Serializable {
  52. /**
  53. * Generic handling of the <CODE>get</CODE> operation.
  54. * <p> The default implementation of this method is to loop over the
  55. * varbind list associated with the sub-request and to call
  56. * <CODE>get(var.oid.getOidArc(depth), data);</CODE>
  57. * <pre>
  58. * public void get(SnmpStandardMetaServer meta, SnmpMibSubRequest req,
  59. * int depth)
  60. * throws SnmpStatusException {
  61. *
  62. * final Object data = req.getUserData();
  63. *
  64. * for (Enumeration e= req.getElements(); e.hasMoreElements();) {
  65. *
  66. * final SnmpVarBind var= (SnmpVarBind) e.nextElement();
  67. *
  68. * try {
  69. * // This method will generate a SnmpStatusException
  70. * // if `depth' is out of bounds.
  71. * //
  72. * final long id = var.oid.getOidArc(depth);
  73. * var.value = meta.get(id, data);
  74. * } catch(SnmpStatusException x) {
  75. * req.registerGetException(var,x);
  76. * }
  77. * }
  78. * }
  79. * </pre>
  80. * <p> You can override this method if you need to implement some
  81. * specific policies for minimizing the accesses made to some remote
  82. * underlying resources.
  83. * <p>
  84. *
  85. * @param meta A pointer to the generated meta-data object which
  86. * implements the <code>SnmpStandardMetaServer</code>
  87. * interface.
  88. *
  89. * @param req The sub-request that must be handled by this node.
  90. *
  91. * @param depth The depth reached in the OID tree.
  92. *
  93. * @exception SnmpStatusException An error occurred while accessing
  94. * the MIB node.
  95. */
  96. public void get(SnmpStandardMetaServer meta, SnmpMibSubRequest req,
  97. int depth)
  98. throws SnmpStatusException {
  99. final Object data = req.getUserData();
  100. for (Enumeration e= req.getElements(); e.hasMoreElements();) {
  101. final SnmpVarBind var= (SnmpVarBind) e.nextElement();
  102. try {
  103. final long id = var.oid.getOidArc(depth);
  104. var.value = meta.get(id, data);
  105. } catch(SnmpStatusException x) {
  106. req.registerGetException(var,x);
  107. }
  108. }
  109. }
  110. /**
  111. * Generic handling of the <CODE>set</CODE> operation.
  112. * <p> The default implementation of this method is to loop over the
  113. * varbind list associated with the sub-request and to call
  114. * <CODE>set(var.value, var.oid.getOidArc(depth), data);</CODE>
  115. * <pre>
  116. * public void set(SnmpStandardMetaServer meta, SnmpMibSubRequest req,
  117. * int depth)
  118. * throws SnmpStatusException {
  119. *
  120. * final Object data = req.getUserData();
  121. *
  122. * for (Enumeration e= req.getElements(); e.hasMoreElements();) {
  123. *
  124. * final SnmpVarBind var= (SnmpVarBind) e.nextElement();
  125. *
  126. * try {
  127. * // This method will generate a SnmpStatusException
  128. * // if `depth' is out of bounds.
  129. * //
  130. * final long id = var.oid.getOidArc(depth);
  131. * var.value = meta.set(var.value, id, data);
  132. * } catch(SnmpStatusException x) {
  133. * req.registerSetException(var,x);
  134. * }
  135. * }
  136. * }
  137. * </pre>
  138. * <p> You can override this method if you need to implement some
  139. * specific policies for minimizing the accesses made to some remote
  140. * underlying resources.
  141. * <p>
  142. *
  143. * @param meta A pointer to the generated meta-data object which
  144. * implements the <code>SnmpStandardMetaServer</code>
  145. * interface.
  146. *
  147. * @param req The sub-request that must be handled by this node.
  148. *
  149. * @param depth The depth reached in the OID tree.
  150. *
  151. * @exception SnmpStatusException An error occurred while accessing
  152. * the MIB node.
  153. */
  154. public void set(SnmpStandardMetaServer meta, SnmpMibSubRequest req,
  155. int depth)
  156. throws SnmpStatusException {
  157. final Object data = req.getUserData();
  158. for (Enumeration e= req.getElements(); e.hasMoreElements();) {
  159. SnmpVarBind var = null;
  160. var = (SnmpVarBind) e.nextElement();
  161. try {
  162. // This method will generate a SnmpStatusException
  163. // if `depth' is out of bounds.
  164. //
  165. final long id = var.oid.getOidArc(depth);
  166. var.value = meta.set(var.value, id, data);
  167. } catch(SnmpStatusException x) {
  168. req.registerSetException(var,x);
  169. }
  170. }
  171. }
  172. /**
  173. * Generic handling of the <CODE>check</CODE> operation.
  174. * <p> The default implementation of this method is to loop over the
  175. * varbind list associated with the sub-request and to call
  176. * <CODE>check(var.value, var.oid.getOidArc(depth), data);</CODE>
  177. * <pre>
  178. * public void check(SnmpStandardMetaServer meta, SnmpMibSubRequest req,
  179. * int depth)
  180. * throws SnmpStatusException {
  181. *
  182. * final Object data = req.getUserData();
  183. *
  184. * for (Enumeration e= req.getElements(); e.hasMoreElements();) {
  185. *
  186. * final SnmpVarBind var= (SnmpVarBind) e.nextElement();
  187. *
  188. * try {
  189. * // This method will generate a SnmpStatusException
  190. * // if `depth' is out of bounds.
  191. * //
  192. * final long id = var.oid.getOidArc(depth);
  193. * meta.check(var.value, id, data);
  194. * } catch(SnmpStatusException x) {
  195. * req.registerCheckException(var,x);
  196. * }
  197. * }
  198. * }
  199. * </pre>
  200. * <p> You can override this method if you need to implement some
  201. * specific policies for minimizing the accesses made to some remote
  202. * underlying resources, or if you need to implement some consistency
  203. * checks between the different values provided in the varbind list.
  204. * <p>
  205. *
  206. * @param meta A pointer to the generated meta-data object which
  207. * implements the <code>SnmpStandardMetaServer</code>
  208. * interface.
  209. *
  210. * @param req The sub-request that must be handled by this node.
  211. *
  212. * @param depth The depth reached in the OID tree.
  213. *
  214. * @exception SnmpStatusException An error occurred while accessing
  215. * the MIB node.
  216. */
  217. public void check(SnmpStandardMetaServer meta, SnmpMibSubRequest req,
  218. int depth)
  219. throws SnmpStatusException {
  220. final Object data = req.getUserData();
  221. for (Enumeration e= req.getElements(); e.hasMoreElements();) {
  222. final SnmpVarBind var = (SnmpVarBind) e.nextElement();
  223. try {
  224. // This method will generate a SnmpStatusException
  225. // if `depth' is out of bounds.
  226. //
  227. final long id = var.oid.getOidArc(depth);
  228. meta.check(var.value,id,data);
  229. } catch(SnmpStatusException x) {
  230. req.registerCheckException(var,x);
  231. }
  232. }
  233. }
  234. }