1 package com.dwipal; 2 3 import javax.swing.tree.DefaultMutableTreeNode ; 4 import javax.swing.JTree ; 5 import javax.swing.*; 6 import javax.swing.tree.DefaultTreeModel ; 7 import javax.swing.tree.TreeNode ; 8 import javax.swing.event.TreeSelectionListener ; 9 import javax.swing.event.TreeSelectionEvent ; 10 import javax.swing.tree.TreeSelectionModel ; 11 import java.util.Vector ; 12 import java.util.Hashtable ; 13 import java.util.Enumeration ; 14 import java.io.File ; 15 16 public class DwSnmpOidSupport 17 { 18 19 DwSnmpMibTreeHash oidResolveHash; 20 DwSnmpMibOutputHandler output=null; 21 22 DwSnmpOidSupport() { 23 oidResolveHash=new DwSnmpMibTreeHash(); 24 outputText("OID Support Library initialized"); 25 } 26 27 public String getNodeOid(DefaultMutableTreeNode node) { 28 String strPath=""; 29 DwSnmpMibRecord nodeInfo = (DwSnmpMibRecord )node.getUserObject(); 30 if(nodeInfo.recordType==nodeInfo.recVariable) return(nodeInfo.name + "-" + nodeInfo.syntax); 31 try { 32 TreeNode [] nodePath= node.getPath(); 33 DwSnmpMibRecord recTemp; 34 if (nodePath.length < 2 ) return("."); 35 for(int i=2;i<nodePath.length;i++) { 36 recTemp=(DwSnmpMibRecord)(((DefaultMutableTreeNode )nodePath[i]).getUserObject()); 37 strPath=strPath.concat("." + String.valueOf (recTemp.number )); 39 } 40 } 41 catch (Exception e1) { 42 return("Error in getting path..\n"+e1.toString()); 43 } 44 if(nodeInfo.recordType == 3) 48 { 49 DwSnmpMibRecord recParTemp=(DwSnmpMibRecord) ((DefaultMutableTreeNode )node.getParent()).getUserObject(); 50 if(recParTemp.tableEntry==-1) strPath=strPath.concat(".("+1 + " - " + "n"+")"); 51 else 52 strPath=strPath.concat(".(" + 1 + " - " + String.valueOf(recParTemp.tableEntry)+")"); 53 } 54 else 55 if(nodeInfo.recordType == 2) 56 { 57 if(nodeInfo.tableEntry==-1) strPath=strPath.concat(".(1-"+node.getChildCount()+")"+ ".(1-" + "n)"); 58 else 59 strPath=strPath.concat(".(1-"+node.getChildCount()+")"+ ".(1-" + String.valueOf(nodeInfo.tableEntry)+")"); 60 } 61 else 62 if(node.isLeaf() == true) strPath=strPath.concat(".0"); 63 else 64 strPath=strPath.concat(".*"); 65 return strPath; 67 68 69 } 70 72 73 76 77 public String getNodeOidQuery(DefaultMutableTreeNode node) { 78 79 String strPath=""; 80 DwSnmpMibRecord nodeInfo = (DwSnmpMibRecord )node.getUserObject(); 81 try { 82 TreeNode [] nodePath= node.getPath(); 83 DwSnmpMibRecord recTemp; 84 if (nodePath.length < 2 ) return("."); 85 for(int i=2;i<nodePath.length;i++) { 86 recTemp=(DwSnmpMibRecord)(((DefaultMutableTreeNode )nodePath[i]).getUserObject()); 87 strPath=strPath.concat("." + String.valueOf (recTemp.number )); 89 } 90 } 91 catch (Exception e1) { 92 outputError("Error in getting path..\n"+e1.toString()); 93 return ""; 94 } 95 if(nodeInfo.recordType == 3) 98 { 99 DwSnmpMibRecord recParTemp=(DwSnmpMibRecord) ((DefaultMutableTreeNode )node.getParent()).getUserObject(); 100 if(recParTemp.tableEntry==-1) strPath=strPath.concat(".65535"); 101 else 102 strPath=strPath.concat("."+String.valueOf(recParTemp.tableEntry)); 103 } 104 else 105 if(nodeInfo.recordType == 2) 106 { 107 if(nodeInfo.tableEntry==-1) strPath=strPath.concat(".1.1"); 108 else 109 strPath=strPath.concat(".1." + String.valueOf(nodeInfo.tableEntry)); 110 } 111 else 112 if(node.isLeaf() == true) strPath=strPath.concat(".0"); 113 else 114 strPath=strPath.concat(".0"); 115 return strPath; 116 } 117 119 120 121 122 127 public String getNodeOidActual(DefaultMutableTreeNode node) { 128 129 String strPath=""; 130 DwSnmpMibRecord nodeInfo = (DwSnmpMibRecord )node.getUserObject(); 131 try { 132 TreeNode [] nodePath= node.getPath(); 133 DwSnmpMibRecord recTemp; 134 if (nodePath.length < 2 ) return("."); 135 for(int i=2;i<nodePath.length;i++) { 136 recTemp=(DwSnmpMibRecord)(((DefaultMutableTreeNode )nodePath[i]).getUserObject()); 137 strPath=strPath.concat("." + String.valueOf (recTemp.number )); 139 } 140 } 141 catch (Exception e1) { 142 outputError("Error in getting path..\n"+e1.toString()); 143 return("Error, cannot resolve OID name"); 144 } 145 return strPath; 146 } 147 148 void buildOidToNameResolutionTable(DefaultMutableTreeNode treeRoot) 149 { 150 try { 151 DefaultMutableTreeNode node; 152 DwSnmpMibRecord nodeRec; 153 Enumeration enu=treeRoot.breadthFirstEnumeration(); 154 while(enu.hasMoreElements()) { 155 node=(DefaultMutableTreeNode ) enu.nextElement(); 156 nodeRec=(DwSnmpMibRecord) node.getUserObject(); 157 String sRec=getNodeOidActual(node); 158 oidResolveHash.put(sRec,nodeRec.name); 159 } 160 } 161 catch (Exception e) { 162 outputError("Error in building OID table..\n"+e.toString()); 163 } 164 } 165 166 public String resolveOidName(String oid) 167 { 168 String objName=null; 169 String oidCopy; 170 171 if(oid.startsWith(".")) oidCopy=oid.toString(); 172 else oidCopy="." + oid.toString(); 173 174 try { 175 oidCopy=oidCopy.substring(0,oidCopy.lastIndexOf('.')); 176 177 while(objName==null && oidCopy.length()>2) { 178 objName=(String )oidResolveHash.get(oidCopy); 179 oidCopy=oidCopy.substring(0,oidCopy.lastIndexOf('.')); 180 } 181 if(objName==null) return("***"); 182 } 183 catch(Exception e) { System.out.println("Error in Resolving OID Name :\n " + e.toString()); } 184 return(objName+oid.substring(oid.indexOf(".",oidCopy.length()+1))); 185 } 186 187 188 public void setOutput(DwSnmpMibOutputHandler output) { 189 this.output=output; 190 } 191 void outputText(String s) { 192 try { 193 output.println(s); 194 } catch(Exception e) { 195 System.out.println(s); 196 } 197 } 198 void outputError(String s) { 199 try { 200 output.printError(s); 201 } catch(Exception e) { 202 System.out.println(s); 203 } 204 } 205 } 206 207 208 209 210 | Popular Tags |