TrapBrowserApplication.java.txt
上传用户:aonuowh
上传日期:2021-05-23
资源大小:35390k
文件大小:6k
源码类别:
SNMP编程
开发平台:
C/C++
- /* $Id: TrapBrowserApplication.java,v 1.4 2002/09/09 05:35:19 tonyjpaul Exp $ */
- /*
- * @(#)TrapBrowserApplication.java
- * Copyright (c) 1996-2003 AdventNet, Inc. All Rights Reserved.
- * Please read the associated COPYRIGHTS file for more details.
- */
- /**
- * This is a example program for TrapBrowser bean .
- * The TrapBrowser is used to display the traps parsed by the TrapParserBean.
- * TrapParser file has set of match criteria to parse the traps.
- * We can specify any number of TrapParsers in TrapParser file.
- */
- import javax.swing.*;
- import java.awt.event.*;
- import java.io.*;
- import java.awt.*;
- import javax.swing.event.*;
- import com.adventnet.snmp.beans.*;
- import com.adventnet.snmp.ui.*;
- //import ParseOptions;
- public class TrapBrowserApplication extends JFrame implements TrapListener,ChangeListener
- ,TrapParserListener {
- JCheckBox combobox;
- public TrapBrowserApplication() {
- this.setTitle("TrapBrowserApplication");
- //Instantiate TrapParserBean
- trapparserbean= new com.adventnet.snmp.ui.TrapParserBean();
- //Instantiate SnmpTrapReceiver
- trapreceiver= new com.adventnet.snmp.beans.SnmpTrapReceiver();
- if(mibs != null) {
- try {
- trapreceiver.loadMibs(mibs);
- }
- catch(Exception e) {
- System.out.println("Mib Loading Failed: " + e.getMessage());
- }
- }
- JPanel detailPanel = new JPanel();
- GridBagLayout g = new GridBagLayout();
- GridBagConstraints c = new GridBagConstraints();
- GridBagLayout dg = new GridBagLayout();
- GridBagConstraints dc = new GridBagConstraints();
- JLabel portLabel = new JLabel("Port");
- portText = new JTextField(7);
- portText.setText(""+port);
- JLabel commLabel = new JLabel("Community");
- commText = new JTextField(community, 15);
- JButton restart = new JButton("Restart");
- restart.addActionListener( new restartListener());
- detailPanel.setLayout(dg);
- dc.fill = GridBagConstraints.BOTH;
- dc.gridx = 1;
- dc.weightx = 1;
- dg.setConstraints(portLabel,dc);
- detailPanel.add(portLabel);
- dc.gridx = 2;
- dg.setConstraints(portText,dc);
- detailPanel.add(portText);
- dc.gridx = 3;
- dg.setConstraints(commLabel,dc);
- detailPanel.add(commLabel);
- dc.gridx = 4;
- dg.setConstraints(commText,dc);
- detailPanel.add(commText);
- dc.gridx = 5;
- dg.setConstraints(restart,dc);
- detailPanel.add(restart);
- //Instantiate TrapBrowser
- trapbrowser= new TrapBrowser();
- String[] str={"Class","Type","Source","Date","Severity"};
- trapbrowser.setColumnNames(str);
- getContentPane().setLayout(g);
- c.gridy = 1;
- c.weighty = 1;
- c.weightx = 1;
- c.fill = GridBagConstraints.BOTH;
- g.setConstraints(trapbrowser,c);
- getContentPane().add(trapbrowser);
- c.gridy = 3;
- c.weighty = 0;
- c.weightx = 0;
- g.setConstraints(detailPanel,c);
- getContentPane().add(detailPanel);
- c.gridy = 2;
- c.weighty = 0;
- c.weightx = 0;
- combobox =new JCheckBox("Color severity column");
- combobox.addChangeListener(this);
- g.setConstraints(combobox,c);
- getContentPane().add(combobox);
- // set the port for SnmpTrapReceiver
- trapreceiver.setPort(port);
- // set the parser file for TrapParserBean
- trapparserbean.setFileName("new_trap_parser.parser");
- trapbrowser.loadMibs("../mibs/RFC1213-MIB");
- // add listener for incoming traps
- trapreceiver.addTrapListener(this);
- // add listener for ParsedTrapEvent
- trapparserbean.addParserListener( this);
- this.setSize(600,350);
- }
- /**
- * this should be implemented for TrapListener.The TrapParserBean gets
- * trapEvent and parses it and generates ParsedTrapEvent if it matches
- * the Match Criteria in Trap parser file.
- */
- public void stateChanged(ChangeEvent l)
- {
- if(combobox.isSelected())
- {
- trapbrowser.setShowSeverityColumnColor(true);
- }
- else
- {
- trapbrowser.setShowSeverityColumnColor(false);
- }
- }
- public void receivedTrap(TrapEvent e) {
- trapparserbean.parseEvtAndFire(e);
- }
- /**
- * this should be implemented for TrapParserListener.The TrapBrowser gets
- * ParsedTrapEvent and dispalyes it in TrapBrowser Table.
- */
- public void eventParsed(ParsedTrapEvent evt) {
- trapbrowser.displayEvent(evt);
- }
- // the main method for this application
- public static void main(String [] args) {
- String usage = "java TrapBrowserApplication [-p port] [-c community] [-m MIB_files]";
- String options[] = { "-p", "-c", "-m"};
- String values[] = { null, null, null};
- ParseOptions opt = new ParseOptions(args,options,values, usage);
- if (opt.remArgs.length > 0) opt.usage_error();
- try {
- if (values[0] != null) port = Integer.parseInt(values[0]);
- } catch (NumberFormatException ex) {
- System.err.println("Invalid port : "+ex);
- }
- if (values[1] != null) community = values[1];
- if (values[2] != null) mibs = values[2];
- TrapBrowserApplication frame = new TrapBrowserApplication();
- frame.show();
- frame.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent evt) {
- System.exit(0);
- }
- });
- }
- class restartListener implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- if( portText.getText().equals(""))
- return;
- try {
- port = Integer.parseInt(portText.getText());
- trapreceiver.setPort(port);
- }
- catch(NumberFormatException ee) {
- System.out.println(" Error in Setting port: "+ee.toString());
- return;
- }
- trapreceiver.setCommunity( commText.getText());
- }
- }
- //Declare the beans
- TrapParserBean trapparserbean = null;
- SnmpTrapReceiver trapreceiver = null;
- TrapBrowser trapbrowser = null;
- JTextField portText = null;
- JTextField commText = null;
- static String community = "";
- static String mibs = null;
- static int port = 162;
- }