KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jmx > snmp > SnmpOidTableSupport


1 /*
2  * @(#)file SnmpOidTableSupport.java
3  * @(#)author Sun Microsystems, Inc.
4  * @(#)version 1.18
5  * @(#)date 08/02/09
6  *
7  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
8  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
9  *
10  */

11
12
13 package com.sun.jmx.snmp;
14
15
16 // java import
17
//
18
import java.util.Vector JavaDoc;
19 import java.util.Enumeration JavaDoc;
20 import java.util.Hashtable JavaDoc;
21
22 //RI import
23
import com.sun.jmx.snmp.SnmpOidTable;
24 import com.sun.jmx.snmp.SnmpOidRecord;
25 import com.sun.jmx.snmp.SnmpStatusException;
26
27 // SNMP Runtime import
28
//
29
import com.sun.jmx.trace.Trace;
30
31 /**
32  * Contains metadata definitions for MIB variables.
33  * A name can be resolved against a table of MIB variables.
34  * Each entry in the table is an <CODE>SnmpOidRecord</CODE> object that contains a name, a dot-separated OID string,
35  * and the corresponding SMI type of the variable.
36  * <P>
37  * If you need to load a specific <CODE>SnmpOidTable</CODE>, just call the static method
38  * {@link com.sun.jmx.snmp.SnmpOid#setSnmpOidTable <CODE>SnmpOid.setSnmpOidTable(<I>myOidTable</I>)</CODE>}.
39  * <P>
40  * <p><b>This API is a Sun Microsystems internal API and is subject
41  * to change without notice.</b></p>
42  * @see com.sun.jmx.snmp.SnmpOidRecord
43  *
44  * @version 1.18 12/19/03
45  * @author Sun Microsystems, Inc
46  */

47
48 public class SnmpOidTableSupport implements SnmpOidTable {
49
50     /**
51      * Creates an <CODE>SnmpOidTableSupport</CODE> with the specified name.
52      * This name identifies the MIB to which belong the MIB variables contained
53      * in this <CODE>SnmpOidTableSupport</CODE> object.
54      * @param name The OID table name.
55      */

56     public SnmpOidTableSupport(String JavaDoc name) {
57         myName=name;
58     }
59   
60     /**
61      * Searches for a MIB variable given its logical name and returns an {@link com.sun.jmx.snmp.SnmpOidRecord} object
62      * containing information on the variable.
63      *
64      * @param name The name of the MIB variable.
65      * @return The <CODE>SnmpOidRecord</CODE> object containing information on the variable.
66      * @exception SnmpStatusException If the variable is not found.
67      */

68     public SnmpOidRecord resolveVarName(String JavaDoc name) throws SnmpStatusException {
69
70         SnmpOidRecord var = (SnmpOidRecord)oidStore.get(name) ;
71         if (var != null) {
72             return var;
73         } else {
74             throw new SnmpStatusException("Variable name <" + name + "> not found in Oid repository") ;
75         }
76     }
77
78     /**
79      * Searches for a MIB variable given its OID and returns an {@link com.sun.jmx.snmp.SnmpOidRecord} object
80      * containing information on the variable.
81      *
82      * @param oid The OID of the MIB variable.
83      * @return The <CODE>SnmpOidRecord</CODE> object containing information on the variable.
84      * @exception SnmpStatusException If the variable is not found.
85      */

86     public SnmpOidRecord resolveVarOid(String JavaDoc oid) throws SnmpStatusException {
87
88         // Try to see if the variable name is actually an OID to resolve.
89
//
90
int index = oid.indexOf('.') ;
91         if (index < 0) {
92             throw new SnmpStatusException("Variable oid <" + oid + "> not found in Oid repository") ;
93         }
94         if (index == 0) {
95             // The oid starts with a '.' ala CMU.
96
//
97
oid= oid.substring(1, oid.length());
98         }
99       
100         // Go through the oidStore ... Good luck !
101
//
102
for(Enumeration JavaDoc list= oidStore.elements(); list.hasMoreElements(); ) {
103             SnmpOidRecord element= (SnmpOidRecord) list.nextElement();
104             if (element.getOid().equals(oid))
105                 return element;
106         }
107
108         throw new SnmpStatusException("Variable oid <" + oid + "> not found in Oid repository") ;
109     }
110
111     /**
112      * Returns a list that can be used to traverse all the entries in this <CODE>SnmpOidTable</CODE>.
113      * @return A vector of {@link com.sun.jmx.snmp.SnmpOidRecord} objects.
114      */

115     public Vector JavaDoc getAllEntries() {
116
117         Vector JavaDoc elementsVector = new Vector JavaDoc();
118         // get the locally defined elements ...
119
for (Enumeration JavaDoc e = oidStore.elements();
120              e.hasMoreElements(); ) {
121             elementsVector.addElement(e.nextElement());
122         }
123         return elementsVector ;
124     }
125
126     /**
127      * Loads a list of variables into the storage area,
128      * which is kept in memory. If you have new MIB variables,
129      * this method can be called to load them.
130      * @param mibs The list of variables to load.
131      */

132     public synchronized void loadMib(SnmpOidRecord[] mibs) {
133         try {
134             for (int i = 0; ; i++) {
135                 SnmpOidRecord s = mibs[i] ;
136                 if (isTraceOn()) {
137                     trace("loadMib", "load " + s.getName());
138                 }
139                 oidStore.put(s.getName(), s) ;
140             }
141         } catch (ArrayIndexOutOfBoundsException JavaDoc e) {
142         }
143     }
144
145     /**
146      * Checks if the specified <CODE>Object</CODE> is equal to this <CODE>SnmpOidTableSupport</CODE>.
147      * @param object The <CODE>Object</CODE> to be compared.
148      * @return <CODE>true</CODE> if <CODE>object</CODE> is an <CODE>SnmpOidTableSupport</CODE> instance and equals to this,
149      * <CODE>false</CODE> otherwise.
150      */

151     public boolean equals(Object JavaDoc object) {
152
153         if (!(object instanceof SnmpOidTableSupport)) {
154             return false;
155         }
156         SnmpOidTableSupport val = (SnmpOidTableSupport) object;
157         return myName.equals(val.getName());
158     }
159
160     /**
161      * Returns the name identifying this <CODE>SnmpOidTableSupport</CODE> object.
162      * @return The OID table name.
163      */

164     public String JavaDoc getName() {
165         return myName;
166     }
167     /*
168      * ------------------------------------------
169      * PRIVATE METHODS
170      * ------------------------------------------
171      */

172
173     // TRACES & DEBUG
174
//---------------
175

176     boolean isTraceOn() {
177         return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_SNMP);
178     }
179
180     void trace(String JavaDoc clz, String JavaDoc func, String JavaDoc info) {
181         Trace.send(Trace.LEVEL_TRACE, Trace.INFO_SNMP, clz, func, info);
182     }
183
184     void trace(String JavaDoc func, String JavaDoc info) {
185         trace(dbgTag, func, info);
186     }
187     
188     boolean isDebugOn() {
189         return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_SNMP);
190     }
191
192     void debug(String JavaDoc clz, String JavaDoc func, String JavaDoc info) {
193         Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_SNMP, clz, func, info);
194     }
195
196     void debug(String JavaDoc func, String JavaDoc info) {
197         debug(dbgTag, func, info);
198     }
199     
200     String JavaDoc dbgTag = "SnmpOidTableSupport";
201     
202     private Hashtable JavaDoc oidStore = new Hashtable JavaDoc() ; // storage area.
203
private String JavaDoc myName;
204 }
205
Popular Tags