KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jmx > snmp > agent > SnmpMibNode


1 /*
2  * @(#)file SnmpMibNode.java
3  * @(#)author Sun Microsystems, Inc.
4  * @(#)version 4.20
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.agent;
14
15
16
17 // java imports
18
//
19
import java.io.Serializable JavaDoc;
20 import java.util.Vector JavaDoc;
21 import java.util.Hashtable JavaDoc;
22 import java.util.Enumeration JavaDoc;
23
24 // jmx imports
25
//
26
import com.sun.jmx.snmp.SnmpOid;
27 import com.sun.jmx.snmp.SnmpValue;
28 import com.sun.jmx.snmp.SnmpVarBind;
29 import com.sun.jmx.snmp.SnmpDefinitions;
30 import com.sun.jmx.snmp.SnmpStatusException;
31
32 /**
33  * The <CODE>SnmpMibNode</CODE> class represents a node in an SNMP MIB.
34  * <P>
35  * This class is used internally and by the class generated by
36  * <CODE>mibgen</CODE>.
37  * You should not need to use this class directly.
38  *
39  * <p><b>This API is a Sun Microsystems internal API and is subject
40  * to change without notice.</b></p>
41  * @version 4.20 12/19/03
42  * @author Sun Microsystems, Inc
43  */

44
45 public abstract class SnmpMibNode implements Serializable JavaDoc {
46     
47     // ---------------------------------------------------------------------
48
// PUBLIC METHODS
49
//----------------------------------------------------------------------
50

51     /**
52      * Get the next OID arc corresponding to a readable scalar variable,
53      * a branch leading to a subgroub, or a table.
54      *
55      * @param id Id we start from looking for the next.
56      * @param userData A contextual object containing user-data.
57      * This object is allocated through the <code>
58      * {@link com.sun.jmx.snmp.agent.SnmpUserDataFactory}</code>
59      * for each incoming SNMP request.
60      *
61      * @return The next id in this group.
62      *
63      * @exception SnmpStatusException If no id is found after the given id.
64      */

65     public long getNextVarId(long id, Object JavaDoc userData)
66     throws SnmpStatusException {
67     return getNextIdentifier(varList,id);
68     }
69
70     /**
71      * Get the next OID arc corresponding to a readable scalar variable,
72      * a branch leading to a subgroub, or a table, possibly skipping over
73      * those arcs that must not or cannot be returned.
74      *
75      * Calls {@link #getNextVarId(long,java.lang.Object)} until
76      * {@link #skipVariable(long,java.lang.Object,int)} returns false.
77      *
78      * @param id Id we start from looking for the next.
79      * @param userData A contextual object containing user-data.
80      * This object is allocated through the <code>
81      * {@link com.sun.jmx.snmp.agent.SnmpUserDataFactory}</code>
82      * for each incoming SNMP request.
83      * @param pduVersion Protocol version of the original request PDU.
84      *
85      * @return The next id in this group which can be returned using
86      * the given PDU's protocol version.
87      *
88      * @exception SnmpStatusException If no id is found after the given id.
89      */

90     public long getNextVarId(long id, Object JavaDoc userData, int pduVersion)
91     throws SnmpStatusException {
92     long varid=id;
93     do {
94         varid = getNextVarId(varid,userData);
95     } while (skipVariable(varid,userData,pduVersion));
96
97     return varid;
98     }
99         
100     /**
101      * Hook for subclasses.
102      * The default implementation of this method is to always return
103      * false. Subclasses should redefine this method so that it returns
104      * true when:
105      * <ul><li>the variable is a leaf that is not instantiated,</li>
106      * <li>or the variable is a leaf whose type cannot be returned by that
107      * version of the protocol (e.g. an Counter64 with SNMPv1).</li>
108      * </ul>
109      *
110      * @param id Id we start from looking for the next.
111      * @param userData A contextual object containing user-data.
112      * This object is allocated through the <code>
113      * {@link com.sun.jmx.snmp.agent.SnmpUserDataFactory}</code>
114      * for each incoming SNMP request.
115      * @param pduVersion Protocol version of the original request PDU.
116      *
117      * @return true if the variable must be skipped by the get-next
118      * algorithm.
119      */

120     protected boolean skipVariable(long id, Object JavaDoc userData, int pduVersion) {
121     return false;
122     }
123
124     /**
125      * Find the node which handles a varbind, and register it in the
126      * SnmpRequestTree. This method is a pure internal method. You should
127      * never try to call it directly.
128      *
129      * @param varbind The varbind to be handled
130      *
131      * @param oid The OID array extracted from the varbind
132      *
133      * @param depth The depth reached in the OID at this step of the
134      * processing.
135      *
136      * @param handlers The Hashtable in which the varbind will be registered
137      * with its handling node. This hashtable contains
138      * <CODE>SnmpRequestTree.Handler</CODE> items.
139      *
140      * @exception SnmpStatusException No handling node was found.
141      **/

142     void findHandlingNode(SnmpVarBind varbind,
143               long[] oid, int depth,
144               SnmpRequestTree handlers)
145     throws SnmpStatusException {
146     throw noSuchObjectException;
147     }
148
149     /**
150      * Find the node which handles the leaf that immediately follows the
151      * given varbind OID, and register the it in the SnmpRequestTree.
152      * This method is a pure internal method. You should never try to call
153      * it directly.
154      *
155      * @param varbind The varbind to be handled
156      *
157      * @param oid The OID array extracted from the varbind
158      *
159      * @param depth The depth reached in the OID at this step of the
160      * processing.
161      *
162      * @param handlers The Hashtable in which the varbind will be registered
163      * with its handling node. This hashtable contains
164      * SnmpRequestTree.Handler items.
165      *
166      * @return The SnmpOid of the next leaf.
167      *
168      * @exception SnmpStatusException No handling node was found.
169      **/

170     long[] findNextHandlingNode(SnmpVarBind varbind,
171                  long[] oid, int pos, int depth,
172                  SnmpRequestTree handlers, AcmChecker checker)
173     throws SnmpStatusException {
174     throw noSuchObjectException;
175     }
176
177     /**
178      * Generic handling of the <CODE>get</CODE> operation.
179      *
180      * <p> You can override this method if you need to implement some
181      * specific policies for minimizing the accesses made to some remote
182      * underlying resources.
183      * <p>
184      *
185      * @param req The sub-request that must be handled by this node.
186      *
187      * @param depth The depth reached in the OID tree.
188      *
189      * @exception SnmpStatusException An error occurred while accessing
190      * the MIB node.
191      */

192     public abstract void get(SnmpMibSubRequest req, int depth)
193     throws SnmpStatusException;
194
195     /**
196      * Generic handling of the <CODE>set</CODE> operation.
197      * <p> You can override this method if you need to implement some
198      * specific policies for minimizing the accesses made to some remote
199      * underlying resources.
200      * <p>
201      *
202      * @param req The sub-request that must be handled by this node.
203      *
204      * @param depth The depth reached in the OID tree.
205      *
206      * @exception SnmpStatusException An error occurred while accessing
207      * the MIB node.
208      */

209     public abstract void set(SnmpMibSubRequest req, int depth)
210     throws SnmpStatusException;
211
212     /**
213      * Generic handling of the <CODE>check</CODE> operation.
214      * <p> You can override this method if you need to implement some
215      * specific policies for minimizing the accesses made to some remote
216      * underlying resources, or if you need to implement some consistency
217      * checks between the different values provided in the varbind list.
218      * <p>
219      *
220      * @param req The sub-request that must be handled by this node.
221      *
222      * @param depth The depth reached in the OID tree.
223      *
224      * @exception SnmpStatusException An error occurred while accessing
225      * the MIB node.
226      */

227     public abstract void check(SnmpMibSubRequest req, int depth)
228     throws SnmpStatusException;
229
230     /**
231      * Sorts the specified integer array.
232      *
233      * @param array An integer array.
234      */

235     static public void sort(int array[]) {
236         QuickSort(array, 0, array.length - 1);
237     }
238     
239     /**
240      * Computes the root OID of the MIB.
241      */

242     public void getRootOid(Vector JavaDoc result) {
243         return;
244     }
245     
246     //----------------------------------------------------------------------
247
// PACKAGE METHODS
248
//----------------------------------------------------------------------
249

250     /**
251      * This is a generic version of C.A.R Hoare's Quick Sort
252      * algorithm. This will handle arrays that are already
253      * sorted, and arrays with duplicate keys.
254      *
255      * If you think of a one dimensional array as going from
256      * the lowest index on the left to the highest index on the right
257      * then the parameters to this function are lowest index or
258      * left and highest index or right. The first time you call
259      * this function it will be with the parameters 0, a.length - 1.
260      *
261      * @param a An integer array.
262      * @param lo0 Left boundary of array partition.
263      * @param hi0 Right boundary of array partition.
264      */

265     static void QuickSort(int a[], int lo0, int hi0) {
266         int lo = lo0;
267         int hi = hi0;
268         int mid;
269
270         if ( hi0 > lo0) {
271
272             /* Arbitrarily establishing partition element as the midpoint of
273              * the array.
274              */

275             mid = a[ ( lo0 + hi0 ) / 2 ];
276
277             // loop through the array until indices cross
278
while( lo <= hi ) {
279                 /* find the first element that is greater than or equal to
280                  * the partition element starting from the left Index.
281                  */

282                 while( ( lo < hi0 ) && ( a[lo] < mid ))
283                     ++lo;
284
285                 /* find an element that is smaller than or equal to
286                  * the partition element starting from the right Index.
287                  */

288                 while( ( hi > lo0 ) && ( a[hi] > mid ))
289                     --hi;
290
291                 // if the indexes have not crossed, swap
292
if( lo <= hi ) {
293                     swap(a, lo, hi);
294                     ++lo;
295                     --hi;
296                 }
297             }
298
299             /* If the right index has not reached the left side of array
300              * must now sort the left partition.
301              */

302             if( lo0 < hi )
303                 QuickSort( a, lo0, hi );
304
305             /* If the left index has not reached the right side of array
306              * must now sort the right partition.
307              */

308             if( lo < hi0 )
309                 QuickSort( a, lo, hi0 );
310
311         }
312     }
313
314     //----------------------------------------------------------------------
315
// PROTECTED METHODS
316
//----------------------------------------------------------------------
317

318     /**
319      * This will give the first element greater than <CODE>value</CODE>
320      * in a sorted array.
321      * If there is no element of the array greater than <CODE>value</CODE>,
322      * the method will throw a <CODE>SnmpStatusException</CODE>.
323      *
324      * @param table A sorted integer array.
325      *
326      * @param value The greatest value.
327      *
328      * @exception SnmpStatusException If there is no element greater than
329      * <CODE>value</CODE>.
330      */

331     final static protected int getNextIdentifier(int table[], long value)
332     throws SnmpStatusException {
333  
334     final int[] a = table;
335         final int val= (int) value;
336
337     if (a == null)
338         throw noSuchObjectException;
339
340         int low= 0;
341         int max= a.length;
342         int curr= low + (max-low)/2;
343         int elmt= 0;
344     
345         // Basic check
346
//
347
if (max < 1)
348         throw noSuchObjectException;
349
350         if (a[max-1] <= val)
351             throw noSuchObjectException;
352  
353         while (low <= max) {
354             elmt= a[curr];
355             if (val == elmt) {
356                 // We ned to get the next index ...
357
//
358
curr++;
359                 return a[curr];
360             }
361             if (elmt < val) {
362                 low= curr +1;
363             } else {
364                 max= curr -1;
365             }
366             curr= low + (max-low)/2;
367         }
368         return a[curr];
369     }
370
371     
372     //----------------------------------------------------------------------
373
// PRIVATE METHODS
374
//----------------------------------------------------------------------
375

376     final static private void swap(int a[], int i, int j) {
377         int T;
378         T = a[i];
379         a[i] = a[j];
380         a[j] = T;
381     }
382     
383     //----------------------------------------------------------------------
384
// PROTECTED VARIABLES
385
//----------------------------------------------------------------------
386

387     /**
388      * Contains the list of variable identifiers.
389      */

390     protected int[] varList;
391
392     /**
393      * Contains a predefined exception that is often fired when an
394      * object is not found in the MIB.
395      */

396     static final protected SnmpStatusException noSuchInstanceException =
397         new SnmpStatusException(SnmpStatusException.noSuchInstance);
398     static final protected SnmpStatusException noSuchObjectException =
399         new SnmpStatusException(SnmpStatusException.noSuchObject);
400     static final protected SnmpStatusException noSuchNameException =
401         new SnmpStatusException(SnmpDefinitions.snmpRspNoSuchName);
402 }
403
Popular Tags