KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jmx > snmp > daemon > SnmpMibTree


1 /*
2  * @(#)file SnmpMibTree.java
3  * @(#)author Sun Microsystems, Inc.
4  * @(#)version 4.7
5  * @(#)date 01/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.daemon;
14
15
16
17 // java imports
18
//
19
import java.util.Vector JavaDoc;
20 import java.util.Enumeration JavaDoc;
21
22 // jmx imports
23
//
24
import com.sun.jmx.snmp.SnmpOid;
25
26 // SNMP Runtime imports
27
//
28
import com.sun.jmx.snmp.agent.SnmpMibAgent;
29   
30 /**
31  * The class is used for building a tree representation of the different
32  * root oids of the supported MIBs. Each node is associated to a specific MIB.
33  */

34 final class SnmpMibTree {
35     
36     public SnmpMibTree() {
37       defaultAgent= null;
38       root= new TreeNode(-1, null, null);
39     }
40     
41     public void setDefaultAgent(SnmpMibAgent def) {
42         defaultAgent= def;
43         root.agent= def;
44     }
45   
46     public SnmpMibAgent getDefaultAgent() {
47         return defaultAgent;
48     }
49   
50     public void register(SnmpMibAgent agent) {
51         root.registerNode(agent);
52     }
53
54     public void register(SnmpMibAgent agent, long[] oid) {
55       root.registerNode(oid, 0, agent);
56     }
57   
58     public SnmpMibAgent getAgentMib(SnmpOid oid) {
59         TreeNode node= root.retrieveMatchingBranch(oid.longValue(), 0);
60         if (node == null)
61             return defaultAgent;
62         else
63         if(node.getAgentMib() == null)
64         return defaultAgent;
65         else
66         return node.getAgentMib();
67     }
68
69     public void unregister(SnmpMibAgent agent, SnmpOid[] oids) {
70     for(int i = 0; i < oids.length; i++) {
71         long[] oid = oids[i].longValue();
72         TreeNode node = root.retrieveMatchingBranch(oid, 0);
73         if (node == null)
74         continue;
75         node.removeAgent(agent);
76     }
77     }
78
79     
80     public void unregister(SnmpMibAgent agent) {
81     
82     root.removeAgentFully(agent);
83     }
84
85     /*
86     public void unregister(SnmpMibAgent agent) {
87         long[] oid= agent.getRootOid();
88         TreeNode node= root.retrieveMatchingBranch(oid, 0);
89         if (node == null)
90             return;
91         node.removeAgent(agent);
92     }
93     */

94     public void printTree() {
95         root.printTree(">");
96     }
97    
98     private SnmpMibAgent defaultAgent;
99     private TreeNode root;
100     
101     // A SnmpMibTree object is a tree of TreeNode
102
//
103
final class TreeNode {
104           
105         void registerNode(SnmpMibAgent agent) {
106             long[] oid= agent.getRootOid();
107             registerNode(oid, 0, agent);
108         }
109       
110         TreeNode retrieveMatchingBranch(long[] oid, int cursor) {
111         TreeNode node= retrieveChild(oid, cursor);
112             if (node == null)
113                 return this;
114             if (children.size() == 0) {
115                 // In this case, the node does not have any children. So no point to
116
// continue the search ...
117
return node;
118             }
119             if( cursor + 1 == oid.length) {
120                 // In this case, the oid does not have any more element. So the search
121
// is over.
122
return node;
123             }
124         
125             TreeNode n = node.retrieveMatchingBranch(oid, cursor + 1);
126         //If the returned node got a null agent, we have to replace it by
127
//the current one (in case it is not null)
128
//
129
return n.agent == null ? this : n;
130         }
131       
132         SnmpMibAgent getAgentMib() {
133             return agent;
134         }
135       
136         public void printTree(String JavaDoc ident) {
137     
138             StringBuffer JavaDoc buff= new StringBuffer JavaDoc();
139             if (agents == null) {
140                 return;
141             }
142       
143             for(Enumeration JavaDoc e= agents.elements(); e.hasMoreElements(); ) {
144                 SnmpMibAgent mib= (SnmpMibAgent) e.nextElement();
145                 if (mib == null)
146                     buff.append("empty ");
147                 else
148                     buff.append(mib.getMibName() + " ");
149             }
150             ident+= " ";
151             if (children == null) {
152                 return;
153             }
154             for(Enumeration JavaDoc e= children.elements(); e.hasMoreElements(); ) {
155                 TreeNode node= (TreeNode) e.nextElement();
156                 node.printTree(ident);
157             }
158         }
159     
160         // PRIVATE STUFF
161
//--------------
162

163         /**
164          * Only the treeNode class can create an instance of treeNode.
165          * The creation occurs when registering a new oid.
166          */

167         private TreeNode(long nodeValue, SnmpMibAgent agent, TreeNode sup) {
168             this.nodeValue= nodeValue;
169             this.parent= sup;
170             agents.addElement(agent);
171         }
172     
173     private void removeAgentFully(SnmpMibAgent agent) {
174         Vector JavaDoc v = new Vector JavaDoc();
175         for(Enumeration JavaDoc e= children.elements(); e.hasMoreElements(); ) {
176         
177                 TreeNode node= (TreeNode) e.nextElement();
178                 node.removeAgentFully(agent);
179         if(node.agents.isEmpty())
180             v.add(node);
181         
182             }
183         for(Enumeration JavaDoc e= v.elements(); e.hasMoreElements(); ) {
184         children.removeElement(e.nextElement());
185         }
186             removeAgent(agent);
187         
188     }
189
190         private void removeAgent(SnmpMibAgent mib) {
191             if (!agents.contains(mib))
192                 return;
193             agents.removeElement(mib);
194         
195         if (!agents.isEmpty())
196         agent= (SnmpMibAgent)agents.firstElement();
197        
198         }
199
200       private void setAgent(SnmpMibAgent agent) {
201     this.agent = agent;
202       }
203
204         private void registerNode(long[] oid, int cursor, SnmpMibAgent agent) {
205
206             if (cursor >= oid.length)
207                 //That's it !
208
//
209
return;
210             TreeNode child = retrieveChild(oid, cursor);
211             if (child == null) {
212                 // Create a child and register it !
213
//
214
long theValue= oid[cursor];
215         child= new TreeNode(theValue, agent, this);
216                 children.addElement(child);
217             }
218         else
219         if (agents.contains(agent) == false) {
220             agents.addElement(agent);
221         }
222         
223         // We have to set the agent attribute
224
//
225
if(cursor == (oid.length - 1)) {
226           child.setAgent(agent);
227         }
228         else
229           child.registerNode(oid, cursor+1, agent);
230         }
231     
232         private TreeNode retrieveChild(long[] oid, int current) {
233             long theValue= oid[current];
234     
235             for(Enumeration JavaDoc e= children.elements(); e.hasMoreElements(); ) {
236                 TreeNode node= (TreeNode) e.nextElement();
237                 if (node.match(theValue))
238                     return node;
239             }
240             return null;
241         }
242       
243         final private boolean match(long value) {
244             return (nodeValue == value) ? true : false;
245         }
246       
247         private Vector JavaDoc children= new Vector JavaDoc();
248         private Vector JavaDoc agents= new Vector JavaDoc();
249         private long nodeValue;
250         protected SnmpMibAgent agent;
251         private TreeNode parent;
252         
253     }; // end of class TreeNode
254
}
255
Popular Tags