1. /*
  2. * @(#)file SnmpOidTableSupport.java
  3. * @(#)author Sun Microsystems, Inc.
  4. * @(#)version 1.18
  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. */
  11. package com.sun.jmx.snmp;
  12. // java import
  13. //
  14. import java.util.Vector;
  15. import java.util.Enumeration;
  16. import java.util.Hashtable;
  17. //RI import
  18. import com.sun.jmx.snmp.SnmpOidTable;
  19. import com.sun.jmx.snmp.SnmpOidRecord;
  20. import com.sun.jmx.snmp.SnmpStatusException;
  21. // SNMP Runtime import
  22. //
  23. import com.sun.jmx.trace.Trace;
  24. /**
  25. * Contains metadata definitions for MIB variables.
  26. * A name can be resolved against a table of MIB variables.
  27. * Each entry in the table is an <CODE>SnmpOidRecord</CODE> object that contains a name, a dot-separated OID string,
  28. * and the corresponding SMI type of the variable.
  29. * <P>
  30. * If you need to load a specific <CODE>SnmpOidTable</CODE>, just call the static method
  31. * {@link com.sun.jmx.snmp.SnmpOid#setSnmpOidTable <CODE>SnmpOid.setSnmpOidTable(<I>myOidTable</I>)</CODE>}.
  32. * <P>
  33. * <p><b>This API is a Sun Microsystems internal API and is subject
  34. * to change without notice.</b></p>
  35. * @see com.sun.jmx.snmp.SnmpOidRecord
  36. *
  37. * @version 1.18 12/19/03
  38. * @author Sun Microsystems, Inc
  39. */
  40. public class SnmpOidTableSupport implements SnmpOidTable {
  41. /**
  42. * Creates an <CODE>SnmpOidTableSupport</CODE> with the specified name.
  43. * This name identifies the MIB to which belong the MIB variables contained
  44. * in this <CODE>SnmpOidTableSupport</CODE> object.
  45. * @param name The OID table name.
  46. */
  47. public SnmpOidTableSupport(String name) {
  48. myName=name;
  49. }
  50. /**
  51. * Searches for a MIB variable given its logical name and returns an {@link com.sun.jmx.snmp.SnmpOidRecord} object
  52. * containing information on the variable.
  53. *
  54. * @param name The name of the MIB variable.
  55. * @return The <CODE>SnmpOidRecord</CODE> object containing information on the variable.
  56. * @exception SnmpStatusException If the variable is not found.
  57. */
  58. public SnmpOidRecord resolveVarName(String name) throws SnmpStatusException {
  59. SnmpOidRecord var = (SnmpOidRecord)oidStore.get(name) ;
  60. if (var != null) {
  61. return var;
  62. } else {
  63. throw new SnmpStatusException("Variable name <" + name + "> not found in Oid repository") ;
  64. }
  65. }
  66. /**
  67. * Searches for a MIB variable given its OID and returns an {@link com.sun.jmx.snmp.SnmpOidRecord} object
  68. * containing information on the variable.
  69. *
  70. * @param oid The OID of the MIB variable.
  71. * @return The <CODE>SnmpOidRecord</CODE> object containing information on the variable.
  72. * @exception SnmpStatusException If the variable is not found.
  73. */
  74. public SnmpOidRecord resolveVarOid(String oid) throws SnmpStatusException {
  75. // Try to see if the variable name is actually an OID to resolve.
  76. //
  77. int index = oid.indexOf('.') ;
  78. if (index < 0) {
  79. throw new SnmpStatusException("Variable oid <" + oid + "> not found in Oid repository") ;
  80. }
  81. if (index == 0) {
  82. // The oid starts with a '.' ala CMU.
  83. //
  84. oid= oid.substring(1, oid.length());
  85. }
  86. // Go through the oidStore ... Good luck !
  87. //
  88. for(Enumeration list= oidStore.elements(); list.hasMoreElements(); ) {
  89. SnmpOidRecord element= (SnmpOidRecord) list.nextElement();
  90. if (element.getOid().equals(oid))
  91. return element;
  92. }
  93. throw new SnmpStatusException("Variable oid <" + oid + "> not found in Oid repository") ;
  94. }
  95. /**
  96. * Returns a list that can be used to traverse all the entries in this <CODE>SnmpOidTable</CODE>.
  97. * @return A vector of {@link com.sun.jmx.snmp.SnmpOidRecord} objects.
  98. */
  99. public Vector getAllEntries() {
  100. Vector elementsVector = new Vector();
  101. // get the locally defined elements ...
  102. for (Enumeration e = oidStore.elements();
  103. e.hasMoreElements(); ) {
  104. elementsVector.addElement(e.nextElement());
  105. }
  106. return elementsVector ;
  107. }
  108. /**
  109. * Loads a list of variables into the storage area,
  110. * which is kept in memory. If you have new MIB variables,
  111. * this method can be called to load them.
  112. * @param mibs The list of variables to load.
  113. */
  114. public synchronized void loadMib(SnmpOidRecord[] mibs) {
  115. try {
  116. for (int i = 0; ; i++) {
  117. SnmpOidRecord s = mibs[i] ;
  118. if (isTraceOn()) {
  119. trace("loadMib", "load " + s.getName());
  120. }
  121. oidStore.put(s.getName(), s) ;
  122. }
  123. } catch (ArrayIndexOutOfBoundsException e) {
  124. }
  125. }
  126. /**
  127. * Checks if the specified <CODE>Object</CODE> is equal to this <CODE>SnmpOidTableSupport</CODE>.
  128. * @param object The <CODE>Object</CODE> to be compared.
  129. * @return <CODE>true</CODE> if <CODE>object</CODE> is an <CODE>SnmpOidTableSupport</CODE> instance and equals to this,
  130. * <CODE>false</CODE> otherwise.
  131. */
  132. public boolean equals(Object object) {
  133. if (!(object instanceof SnmpOidTableSupport)) {
  134. return false;
  135. }
  136. SnmpOidTableSupport val = (SnmpOidTableSupport) object;
  137. return myName.equals(val.getName());
  138. }
  139. /**
  140. * Returns the name identifying this <CODE>SnmpOidTableSupport</CODE> object.
  141. * @return The OID table name.
  142. */
  143. public String getName() {
  144. return myName;
  145. }
  146. /*
  147. * ------------------------------------------
  148. * PRIVATE METHODS
  149. * ------------------------------------------
  150. */
  151. // TRACES & DEBUG
  152. //---------------
  153. boolean isTraceOn() {
  154. return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_SNMP);
  155. }
  156. void trace(String clz, String func, String info) {
  157. Trace.send(Trace.LEVEL_TRACE, Trace.INFO_SNMP, clz, func, info);
  158. }
  159. void trace(String func, String info) {
  160. trace(dbgTag, func, info);
  161. }
  162. boolean isDebugOn() {
  163. return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_SNMP);
  164. }
  165. void debug(String clz, String func, String info) {
  166. Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_SNMP, clz, func, info);
  167. }
  168. void debug(String func, String info) {
  169. debug(dbgTag, func, info);
  170. }
  171. String dbgTag = "SnmpOidTableSupport";
  172. private Hashtable oidStore = new Hashtable() ; // storage area.
  173. private String myName;
  174. }