swingtable.java
上传用户:aonuowh
上传日期:2021-05-23
资源大小:35390k
文件大小:8k
源码类别:

SNMP编程

开发平台:

C/C++

  1. /*$Id: swingtable.src,v 1.3 2002/09/09 05:35:19 tonyjpaul Exp $*/
  2. /*
  3.  * @(#)swingtable.java
  4.  * Copyright (c) 1996-2002 AdventNet, Inc. All Rights Reserved.
  5.  * Please read the COPYRIGHTS file for more details.
  6.  */
  7. /** 
  8.  *  An example of using the SnmpTableModel class with the JFC table component.
  9.  *  This enables easy ways of working with the JFC and SNMP tables. 
  10.   *<img SRC="images/v2candv3only.jpg" ALT="v2c and v3 only"> [-v] <version>      - version
  11.  ifdef(VERSION3){
  12.   (v1 / v2/ v3). By default v1.
  13.  * [-d] <debug>        - Debug output.By default off. 
  14.  * [-c] <community>    - community String. By default "public".
  15.  * [-p] <port>         - remote port no. By default 161.
  16.  * [-r] <Retries>      - Retries. By default 0.      
  17.  * [-t] <Timeout>      - Timeout. By default 5000ms.
  18.  *<img SRC="images/v3only.jpg" ALT="v3 only"> [-u] <username>     - The v3 principal/userName
  19.  * [-a] <autProtocol>  - The authProtocol(MD5/SHA). Mandatory if authPassword is specified
  20.  * [-w] <authPassword> - The authentication password.
  21.  * [-s] <privPassword> - The privacy protocol password. Must be accompanied with auth password and authProtocol fields.
  22.  * [-n] <contextName>  - The snmpv3 contextName to be used.
  23.  * [-i] <contextID>    - The snmpv3 contextID to be used.
  24.  * MIB_file Mandatory  - The mibs to be loaded.
  25.  * host Mandatory      - The RemoteHost (agent).Format (string without double qoutes/IpAddress).
  26.  * tableOID  Mandatory - Give the Object Identifier of a Table.
  27.  **/
  28. import javax.swing.*;
  29. import javax.swing.table.*;
  30. import java.awt.*;
  31. import java.awt.event.*;
  32. import com.adventnet.snmp.ui.*;
  33. public class swingtable extends JFrame {
  34.     private static final int VERSION = 0;
  35.     private static final int COMMUNITY = 1;
  36.     private static final int PORT = 2;
  37.     private static final int RETRIES = 3;
  38.     private static final int TIMEOUT = 4;
  39.     private static final int DEBUG = 5;
  40.     private static final int USER_NAME = 6;
  41.     private static final int AUTH_PROTOCOL = 7;
  42.     private static final int AUTH_PASSWORD = 8;
  43.     private static final int PRIV_PASSWORD = 9;
  44.     private static final int CONTEXTNAME = 10;
  45.     private static final int CONTEXTID = 11;
  46.  /** <img SRC="images/v3only.jpg" ALT="v3 only"> Supply command line arguments to this constructor **/
  47.     public swingtable(String host, String community, String mibs,
  48.                     String tableOID, int port, int retries, int timeout,String debug) {
  49.       
  50.              this("v1", host, community, mibs, tableOID, port, retries, timeout, debug, "", "", "", "","","");
  51.   } /*<img SRC="images/v3only.jpg" ALT="v3 only"> swingtable */
  52.   
  53.  /** <img SRC="images/v3only.jpg" ALT="v3 only"> Supply command line arguments to this constructor **/
  54.     public swingtable(String version, String host, String community, String mibs, String tableOID, int port, int retries,int timeout, String debug, String userName, String authProtocol, String authPassword, String privPassword,String contextName,String contextID) {
  55.                                  
  56.         super("MainWindow");
  57.     SnmpTableModel dataTable = new SnmpTableModel();
  58.     // set the properties fo the table model
  59.     dataTable.setTargetHost (host);
  60.     dataTable.setCommunity (community);
  61.     dataTable.setTargetPort (port);
  62.     dataTable.setRetries (retries);
  63.     dataTable.setTimeout (timeout);
  64.  
  65.  //To load MIBs from compiled file
  66.  dataTable.setLoadFromCompiledMibs(true);
  67.  
  68.  if(debug.equals("set"))
  69.  dataTable.setDebug(true);
  70.     if(version.equals("v3"))
  71.         dataTable.setSnmpVersion(dataTable.VERSION3);
  72.     else if(version.equals("v2"))
  73.         dataTable.setSnmpVersion(dataTable.VERSION2C);
  74.     else dataTable.setSnmpVersion(dataTable.VERSION1);
  75.     if(version.equals("v3")){
  76.         dataTable.setPrincipal(userName);
  77.         if(authProtocol.compareTo("MD5") == 0)
  78.              dataTable.setAuthProtocol(dataTable.MD5_AUTH);
  79.         else if(authProtocol.compareTo("SHA") == 0)
  80.              dataTable.setAuthProtocol(dataTable.SHA_AUTH);
  81.         else dataTable.setAuthProtocol(dataTable.NO_AUTH);
  82.         dataTable.setAuthPassword(authPassword);
  83.         dataTable.setPrivPassword(privPassword);
  84.         dataTable.setContextName(contextName);
  85.         dataTable.setContextID(contextID);
  86.     }
  87.     try {
  88.         System.out.println("Loading MIBs: "+mibs);
  89.         dataTable.loadMibs(mibs);
  90.         System.out.println("Done.");
  91. if(dataTable.getSnmpVersion() == dataTable.VERSION3){
  92. dataTable.create_v3_tables();
  93. }
  94.         dataTable.setTableOID (tableOID);
  95.     } catch (Exception ex) {
  96.         System.err.println("Error: "+ex);
  97.         System.exit(1);
  98.     }
  99.     // Instantiate the JFC table with the SnmpTableModel instance as model
  100.     JTable table = new JTable(dataTable);
  101.       table.sizeColumnsToFit(-1);  
  102.     JScrollPane scrollpane = new JScrollPane(table);
  103.     scrollpane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
  104.     scrollpane.setViewportView(table);
  105.     getContentPane().add(scrollpane);
  106.     } /* swingtable */
  107.     public static void main(String[] args) {
  108.     // Take care of getting options
  109.         String usage = "swingtable [-v version(v1/v2/v3)] [-c community] [-p port] [-r retries] [-t timeout] [-d] [-u userName] [-a authProtocol] [-w authPassword] [-s privPassword] [-n contextname] [-i contextID] host tableOID MIB_file";
  110.         String options[] = { "-v", "-c", "-p", "-r", "-t", "-d", "-u", "-a", "-w", "-s","-n","-i" };
  111.         String values[] = { null, null, null, null, null, "None", null, null, null, null, null,null,null};
  112.     ParseOptions opt = new ParseOptions(args,options,values, usage);
  113.     // check for at least hostname and one OID in remaining arguments
  114.     if (opt.remArgs.length<3) opt.usage_error();
  115.     String host = opt.remArgs[0];
  116.     String oidTable = opt.remArgs[1];
  117.     String community = "public";
  118.     String mibs = opt.remArgs[2];
  119.     String debug = new String("");
  120.     String version ="v1";
  121.     String userName = "";
  122.     String authProtocol = new String("NO_AUTH");
  123.     String authPassword = new String ("");
  124.     String privPassword = new String ("");
  125. String contextName = new String (""); 
  126. String contextID = new String (""); 
  127.     if (values[COMMUNITY] != null) community = values[COMMUNITY];
  128.     
  129.     if(values[DEBUG].equals("Set"))
  130.     debug="set";
  131.     int port = 161, retries=0,timeout = 5;
  132.     try {
  133.         if (values[PORT] != null) 
  134.             port = ( Integer.parseInt(values[PORT]) );
  135.         if (values[RETRIES] != null) 
  136.             retries = ( Integer.parseInt(values[RETRIES]) );
  137.         if (values[TIMEOUT] != null) 
  138.             timeout = ( Integer.parseInt(values[TIMEOUT]) );
  139.     } catch (NumberFormatException ex) {
  140.         System.err.println("Invalid Integer Argument "+ex);
  141.     }
  142.     if(values[VERSION] != null)
  143.          version = values[VERSION];
  144.     if ((version.equals("v3")) && (values[USER_NAME] == null)) opt.usage_error();
  145.     if(values[USER_NAME] != null)
  146.          userName = values[USER_NAME];
  147.     if(values[AUTH_PROTOCOL] != null)
  148.         authProtocol = values[AUTH_PROTOCOL];
  149.     if(values[AUTH_PASSWORD] != null)
  150.         authPassword = values[AUTH_PASSWORD];
  151.     if(values[PRIV_PASSWORD] != null)
  152.         privPassword = values[PRIV_PASSWORD];
  153.     if(values[CONTEXTNAME] != null)
  154.         contextName = values[CONTEXTNAME];
  155.     if(values[CONTEXTID] != null)
  156.         contextID = values[CONTEXTID];
  157.         try { //setup the look and feel for native platform L&F
  158.             UIManager.setLookAndFeel(
  159.                 UIManager.getSystemLookAndFeelClassName());
  160.         } catch (Exception e) {
  161.             System.err.println("Couldn't use the system "
  162.                              + "look and feel: " + e);
  163.         }
  164.       JFrame frame = new swingtable(version, host, community, mibs, oidTable, port, retries, timeout,debug,
  165.           userName, authProtocol, authPassword, privPassword,contextName,contextID);
  166.         WindowListener l = new WindowAdapter() {
  167.             public void windowClosing(WindowEvent e) {
  168.                 System.exit(0);
  169.             }
  170.         };
  171.         frame.addWindowListener(l);
  172.         frame.setSize(600,300);
  173.         frame.setVisible(true);
  174.     }
  175. }