swingtable.java.txt
上传用户:aonuowh
上传日期:2021-05-23
资源大小:35390k
文件大小:8k
- /*$Id: swingtable.src,v 1.3 2002/09/09 05:35:19 tonyjpaul Exp $*/
- /*
- * @(#)swingtable.java
- * Copyright (c) 1996-2003 AdventNet, Inc. All Rights Reserved.
- * Please read the COPYRIGHTS file for more details.
- */
- /**
- * An example of using the SnmpTableModel class with the JFC table component.
- * This enables easy ways of working with the JFC and SNMP tables.
- *<img SRC="images/v2candv3only.jpg" ALT="v2c and v3 only"> [-v] <version> - version
- ifdef(VERSION3){
- (v1 / v2/ v3). By default v1.
- * [-d] <debug> - Debug output.By default off.
- * [-c] <community> - community String. By default "public".
- * [-p] <port> - remote port no. By default 161.
- * [-r] <Retries> - Retries. By default 0.
- * [-t] <Timeout> - Timeout. By default 5000ms.
- *<img SRC="images/v3only.jpg" ALT="v3 only"> [-u] <username> - The v3 principal/userName
- * [-a] <autProtocol> - The authProtocol(MD5/SHA). Mandatory if authPassword is specified
- * [-w] <authPassword> - The authentication password.
- * [-s] <privPassword> - The privacy protocol password. Must be accompanied with auth password and authProtocol fields.
- * [-n] <contextName> - The snmpv3 contextName to be used.
- * [-i] <contextID> - The snmpv3 contextID to be used.
- * MIB_file Mandatory - The mibs to be loaded.
- * host Mandatory - The RemoteHost (agent).Format (string without double qoutes/IpAddress).
- * tableOID Mandatory - Give the Object Identifier of a Table.
- **/
- import javax.swing.*;
- import javax.swing.table.*;
- import java.awt.*;
- import java.awt.event.*;
- import com.adventnet.snmp.ui.*;
- public class swingtable extends JFrame {
- private static final int VERSION = 0;
- private static final int COMMUNITY = 1;
- private static final int PORT = 2;
- private static final int RETRIES = 3;
- private static final int TIMEOUT = 4;
- private static final int DEBUG = 5;
- private static final int USER_NAME = 6;
- private static final int AUTH_PROTOCOL = 7;
- private static final int AUTH_PASSWORD = 8;
- private static final int PRIV_PASSWORD = 9;
- private static final int CONTEXTNAME = 10;
- private static final int CONTEXTID = 11;
- /** <img SRC="images/v3only.jpg" ALT="v3 only"> Supply command line arguments to this constructor **/
- public swingtable(String host, String community, String mibs,
- String tableOID, int port, int retries, int timeout,String debug) {
-
- this("v1", host, community, mibs, tableOID, port, retries, timeout, debug, "", "", "", "","","");
- } /*<img SRC="images/v3only.jpg" ALT="v3 only"> swingtable */
-
- /** <img SRC="images/v3only.jpg" ALT="v3 only"> Supply command line arguments to this constructor **/
- 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) {
-
- super("MainWindow");
- SnmpTableModel dataTable = new SnmpTableModel();
- // set the properties fo the table model
- dataTable.setTargetHost (host);
- dataTable.setCommunity (community);
- dataTable.setTargetPort (port);
- dataTable.setRetries (retries);
- dataTable.setTimeout (timeout);
-
- //To load MIBs from compiled file
- dataTable.setLoadFromCompiledMibs(true);
-
- if(debug.equals("set"))
- dataTable.setDebug(true);
- if(version.equals("v3"))
- dataTable.setSnmpVersion(dataTable.VERSION3);
- else if(version.equals("v2"))
- dataTable.setSnmpVersion(dataTable.VERSION2C);
- else dataTable.setSnmpVersion(dataTable.VERSION1);
- if(version.equals("v3")){
- dataTable.setPrincipal(userName);
- if(authProtocol.compareTo("MD5") == 0)
- dataTable.setAuthProtocol(dataTable.MD5_AUTH);
- else if(authProtocol.compareTo("SHA") == 0)
- dataTable.setAuthProtocol(dataTable.SHA_AUTH);
- else dataTable.setAuthProtocol(dataTable.NO_AUTH);
- dataTable.setAuthPassword(authPassword);
- dataTable.setPrivPassword(privPassword);
- dataTable.setContextName(contextName);
- dataTable.setContextID(contextID);
- }
- try {
- System.out.println("Loading MIBs: "+mibs);
- dataTable.loadMibs(mibs);
- System.out.println("Done.");
- if(dataTable.getSnmpVersion() == dataTable.VERSION3){
- dataTable.create_v3_tables();
- }
- dataTable.setTableOID (tableOID);
- } catch (Exception ex) {
- System.err.println("Error: "+ex);
- System.exit(1);
- }
- // Instantiate the JFC table with the SnmpTableModel instance as model
- JTable table = new JTable(dataTable);
- table.sizeColumnsToFit(-1);
- JScrollPane scrollpane = new JScrollPane(table);
- scrollpane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
- scrollpane.setViewportView(table);
- getContentPane().add(scrollpane);
- } /* swingtable */
- public static void main(String[] args) {
- // Take care of getting options
- 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";
- String options[] = { "-v", "-c", "-p", "-r", "-t", "-d", "-u", "-a", "-w", "-s","-n","-i" };
- String values[] = { null, null, null, null, null, "None", null, null, null, null, null,null,null};
- ParseOptions opt = new ParseOptions(args,options,values, usage);
- // check for at least hostname and one OID in remaining arguments
- if (opt.remArgs.length<3) opt.usage_error();
- String host = opt.remArgs[0];
- String oidTable = opt.remArgs[1];
- String community = "public";
- String mibs = opt.remArgs[2];
- String debug = new String("");
- String version ="v1";
- String userName = "";
- String authProtocol = new String("NO_AUTH");
- String authPassword = new String ("");
- String privPassword = new String ("");
- String contextName = new String ("");
- String contextID = new String ("");
- if (values[COMMUNITY] != null) community = values[COMMUNITY];
-
- if(values[DEBUG].equals("Set"))
- debug="set";
- int port = 161, retries=0,timeout = 5;
- try {
- if (values[PORT] != null)
- port = ( Integer.parseInt(values[PORT]) );
- if (values[RETRIES] != null)
- retries = ( Integer.parseInt(values[RETRIES]) );
- if (values[TIMEOUT] != null)
- timeout = ( Integer.parseInt(values[TIMEOUT]) );
- } catch (NumberFormatException ex) {
- System.err.println("Invalid Integer Argument "+ex);
- }
- if(values[VERSION] != null)
- version = values[VERSION];
- if ((version.equals("v3")) && (values[USER_NAME] == null)) opt.usage_error();
- if(values[USER_NAME] != null)
- userName = values[USER_NAME];
- if(values[AUTH_PROTOCOL] != null)
- authProtocol = values[AUTH_PROTOCOL];
- if(values[AUTH_PASSWORD] != null)
- authPassword = values[AUTH_PASSWORD];
- if(values[PRIV_PASSWORD] != null)
- privPassword = values[PRIV_PASSWORD];
- if(values[CONTEXTNAME] != null)
- contextName = values[CONTEXTNAME];
- if(values[CONTEXTID] != null)
- contextID = values[CONTEXTID];
- try { //setup the look and feel for native platform L&F
- UIManager.setLookAndFeel(
- UIManager.getSystemLookAndFeelClassName());
- } catch (Exception e) {
- System.err.println("Couldn't use the system "
- + "look and feel: " + e);
- }
- JFrame frame = new swingtable(version, host, community, mibs, oidTable, port, retries, timeout,debug,
- userName, authProtocol, authPassword, privPassword,contextName,contextID);
- WindowListener l = new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- };
- frame.addWindowListener(l);
- frame.setSize(600,300);
- frame.setVisible(true);
- }
- }