1 11 12 13 package com.sun.jmx.snmp.daemon; 14 15 16 17 import java.util.Vector ; 20 import java.util.Enumeration ; 21 22 import com.sun.jmx.snmp.SnmpOid; 25 26 import com.sun.jmx.snmp.agent.SnmpMibAgent; 29 30 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 94 public void printTree() { 95 root.printTree(">"); 96 } 97 98 private SnmpMibAgent defaultAgent; 99 private TreeNode root; 100 101 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 return node; 118 } 119 if( cursor + 1 == oid.length) { 120 return node; 123 } 124 125 TreeNode n = node.retrieveMatchingBranch(oid, cursor + 1); 126 return n.agent == null ? this : n; 130 } 131 132 SnmpMibAgent getAgentMib() { 133 return agent; 134 } 135 136 public void printTree(String ident) { 137 138 StringBuffer buff= new StringBuffer (); 139 if (agents == null) { 140 return; 141 } 142 143 for(Enumeration 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 e= children.elements(); e.hasMoreElements(); ) { 155 TreeNode node= (TreeNode) e.nextElement(); 156 node.printTree(ident); 157 } 158 } 159 160 163 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 v = new Vector (); 175 for(Enumeration 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 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 return; 210 TreeNode child = retrieveChild(oid, cursor); 211 if (child == null) { 212 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 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 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 children= new Vector (); 248 private Vector agents= new Vector (); 249 private long nodeValue; 250 protected SnmpMibAgent agent; 251 private TreeNode parent; 252 253 }; } 255 | Popular Tags |