001: package com.dwipal;
002:
003: import javax.swing.tree.DefaultMutableTreeNode;
004: import javax.swing.JTree;
005: import javax.swing.*;
006: import javax.swing.tree.DefaultTreeModel;
007: import javax.swing.tree.TreeNode;
008: import javax.swing.event.TreeSelectionListener;
009: import javax.swing.event.TreeSelectionEvent;
010: import javax.swing.tree.TreeSelectionModel;
011: import java.util.Vector;
012: import java.util.Hashtable;
013: import java.util.Enumeration;
014: import java.io.File;
015:
016: public class DwSnmpOidSupport {
017:
018: DwSnmpMibTreeHash oidResolveHash;
019: DwSnmpMibOutputHandler output = null;
020:
021: DwSnmpOidSupport() {
022: oidResolveHash = new DwSnmpMibTreeHash();
023: outputText("OID Support Library initialized");
024: }
025:
026: public String getNodeOid(DefaultMutableTreeNode node) {
027: String strPath = "";
028: DwSnmpMibRecord nodeInfo = (DwSnmpMibRecord) node
029: .getUserObject();
030: if (nodeInfo.recordType == nodeInfo.recVariable)
031: return (nodeInfo.name + "-" + nodeInfo.syntax);
032: try {
033: TreeNode[] nodePath = node.getPath();
034: DwSnmpMibRecord recTemp;
035: if (nodePath.length < 2)
036: return (".");
037: for (int i = 2; i < nodePath.length; i++) {
038: recTemp = (DwSnmpMibRecord) (((DefaultMutableTreeNode) nodePath[i])
039: .getUserObject());
040: //System.out.println(recTemp.name + " " + recTemp.number);
041: strPath = strPath.concat("."
042: + String.valueOf(recTemp.number));
043: }
044: } catch (Exception e1) {
045: return ("Error in getting path..\n" + e1.toString());
046: }
047: // Node OID Obtained, now check if it is in a table
048: // For Table Element
049: //System.out.println("Getting OID Name...");
050: if (nodeInfo.recordType == 3) {
051: DwSnmpMibRecord recParTemp = (DwSnmpMibRecord) ((DefaultMutableTreeNode) node
052: .getParent()).getUserObject();
053: if (recParTemp.tableEntry == -1)
054: strPath = strPath.concat(".(" + 1 + " - " + "n" + ")");
055: else
056: strPath = strPath.concat(".(" + 1 + " - "
057: + String.valueOf(recParTemp.tableEntry) + ")");
058: } else if (nodeInfo.recordType == 2) {
059: if (nodeInfo.tableEntry == -1)
060: strPath = strPath.concat(".(1-" + node.getChildCount()
061: + ")" + ".(1-" + "n)");
062: else
063: strPath = strPath.concat(".(1-" + node.getChildCount()
064: + ")" + ".(1-"
065: + String.valueOf(nodeInfo.tableEntry) + ")");
066: } else if (node.isLeaf() == true)
067: strPath = strPath.concat(".0");
068: else
069: strPath = strPath.concat(".*");
070: //System.out.println(strPath);
071: return strPath;
072:
073: }
074:
075: /** END OF getNodeOid
076: */
077:
078: /** getNodeOidQuery : RETURNS OID VALUES SUCH THAT THEY CAN
079: * BE STRAIGHTAWAY USED FOR QUERIES
080: */
081:
082: public String getNodeOidQuery(DefaultMutableTreeNode node) {
083:
084: String strPath = "";
085: DwSnmpMibRecord nodeInfo = (DwSnmpMibRecord) node
086: .getUserObject();
087: try {
088: TreeNode[] nodePath = node.getPath();
089: DwSnmpMibRecord recTemp;
090: if (nodePath.length < 2)
091: return (".");
092: for (int i = 2; i < nodePath.length; i++) {
093: recTemp = (DwSnmpMibRecord) (((DefaultMutableTreeNode) nodePath[i])
094: .getUserObject());
095: //System.out.println(recTemp.name + " " + recTemp.number);
096: strPath = strPath.concat("."
097: + String.valueOf(recTemp.number));
098: }
099: } catch (Exception e1) {
100: outputError("Error in getting path..\n" + e1.toString());
101: return "";
102: }
103: // Node OID Obtained, now check if it is in a table
104: // For Table Element
105: if (nodeInfo.recordType == 3) {
106: DwSnmpMibRecord recParTemp = (DwSnmpMibRecord) ((DefaultMutableTreeNode) node
107: .getParent()).getUserObject();
108: if (recParTemp.tableEntry == -1)
109: strPath = strPath.concat(".65535");
110: else
111: strPath = strPath.concat("."
112: + String.valueOf(recParTemp.tableEntry));
113: } else if (nodeInfo.recordType == 2) {
114: if (nodeInfo.tableEntry == -1)
115: strPath = strPath.concat(".1.1");
116: else
117: strPath = strPath.concat(".1."
118: + String.valueOf(nodeInfo.tableEntry));
119: } else if (node.isLeaf() == true)
120: strPath = strPath.concat(".0");
121: else
122: strPath = strPath.concat(".0");
123: return strPath;
124: }
125:
126: /** END OF getNodeOidQuery
127: */
128:
129: /** getNodeOidActual RETURNS THE ACTUAL OID, WITHOUT
130: * APPENDING ANYTHING. MAINLY USED
131: * FOR OID TO NAME RESOLVING.
132: *
133: */
134: public String getNodeOidActual(DefaultMutableTreeNode node) {
135:
136: String strPath = "";
137: DwSnmpMibRecord nodeInfo = (DwSnmpMibRecord) node
138: .getUserObject();
139: try {
140: TreeNode[] nodePath = node.getPath();
141: DwSnmpMibRecord recTemp;
142: if (nodePath.length < 2)
143: return (".");
144: for (int i = 2; i < nodePath.length; i++) {
145: recTemp = (DwSnmpMibRecord) (((DefaultMutableTreeNode) nodePath[i])
146: .getUserObject());
147: //System.out.println(recTemp.name + " " + recTemp.number);
148: strPath = strPath.concat("."
149: + String.valueOf(recTemp.number));
150: }
151: } catch (Exception e1) {
152: outputError("Error in getting path..\n" + e1.toString());
153: return ("Error, cannot resolve OID name");
154: }
155: return strPath;
156: }
157:
158: void buildOidToNameResolutionTable(DefaultMutableTreeNode treeRoot) {
159: try {
160: DefaultMutableTreeNode node;
161: DwSnmpMibRecord nodeRec;
162: Enumeration enu = treeRoot.breadthFirstEnumeration();
163: while (enu.hasMoreElements()) {
164: node = (DefaultMutableTreeNode) enu.nextElement();
165: nodeRec = (DwSnmpMibRecord) node.getUserObject();
166: String sRec = getNodeOidActual(node);
167: oidResolveHash.put(sRec, nodeRec.name);
168: }
169: } catch (Exception e) {
170: outputError("Error in building OID table..\n"
171: + e.toString());
172: }
173: }
174:
175: public String resolveOidName(String oid) {
176: String objName = null;
177: String oidCopy;
178:
179: if (oid.startsWith("."))
180: oidCopy = oid.toString();
181: else
182: oidCopy = "." + oid.toString();
183:
184: try {
185: oidCopy = oidCopy.substring(0, oidCopy.lastIndexOf('.'));
186:
187: while (objName == null && oidCopy.length() > 2) {
188: objName = (String) oidResolveHash.get(oidCopy);
189: oidCopy = oidCopy
190: .substring(0, oidCopy.lastIndexOf('.'));
191: }
192: if (objName == null)
193: return ("***");
194: } catch (Exception e) {
195: System.out.println("Error in Resolving OID Name :\n "
196: + e.toString());
197: }
198: return (objName + oid.substring(oid.indexOf(".", oidCopy
199: .length() + 1)));
200: }
201:
202: public void setOutput(DwSnmpMibOutputHandler output) {
203: this .output = output;
204: }
205:
206: void outputText(String s) {
207: try {
208: output.println(s);
209: } catch (Exception e) {
210: System.out.println(s);
211: }
212: }
213:
214: void outputError(String s) {
215: try {
216: output.printError(s);
217: } catch (Exception e) {
218: System.out.println(s);
219: }
220: }
221: }
|