Package org.snmp4j.agent.mo.jmx

Source Code of org.snmp4j.agent.mo.jmx.JMXMutableTableModel

/*_############################################################################
  _##
  _##  SNMP4J-AgentJMX - JMXMutableTableModel.java 
  _##
  _##  Copyright (C) 2006-2009  Frank Fock (SNMP4J.org)
  _## 
  _##  This program is free software; you can redistribute it and/or modify
  _##  it under the terms of the GNU General Public License version 2 as
  _##  published by the Free Software Foundation.
  _##
  _##  This program is distributed in the hope that it will be useful,
  _##  but WITHOUT ANY WARRANTY; without even the implied warranty of
  _##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  _##  GNU General Public License for more details.
  _##
  _##  You should have received a copy of the GNU General Public License
  _##  along with this program; if not, write to the Free Software
  _##  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  _##  MA  02110-1301  USA
  _## 
  _##########################################################################*/

package org.snmp4j.agent.mo.jmx;

import org.snmp4j.agent.mo.*;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.Variable;
import org.snmp4j.PDU;
import org.snmp4j.log.LogFactory;
import org.snmp4j.log.LogAdapter;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.List;

/**
* The <code>JMXMutableTableModel</code> extends the {@link JMXTableModel} by
* adding support for row creation and deletion.
*
* @author Frank Fock
* @version 1.0
*/
public class JMXMutableTableModel extends JMXTableModel
    implements MOMutableTableModel {

  private static LogAdapter logger =
      LogFactory.getLogger(JMXMutableTableModel.class);

  /**
   * Creates a JMX based table model for a table with the specified OID and
   * columns using the supplied {@link JMXMutableTableSupport} to map
   * between SNMP and JMX.
   *
   * @param tableOID
   *    the OID of the table (e.g., ifEntry).
   * @param tableSupport
   *    the JMXMutableTableSupport instance that maps between SNMP values and
   *    value organisation to JMX MBean(s).
   * @param columns
   *    the columns defined for the table.
   */
  public JMXMutableTableModel(OID tableOID,
                              JMXMutableTableSupport tableSupport,
                              MOColumn[] columns) {
    super(tableOID, tableSupport, columns);
  }

  public MOTableRow createRow(OID index, Variable[] values) throws
      UnsupportedOperationException {
    MOTableRow row = rowFactory.createRow(index, values);
    return row;
  }

  public void freeRow(MOTableRow row) {
    //nothing to do by default
  }

  public MOTableRow addRow(MOTableRow row) {
    MOTableRow oldRow =
        rowFactory.createRow(row.getIndex(), getInitialRowValues());
    if (table.getRow(tableOID, oldRow) != PDU.noError) {
      oldRow = null;
    }
    else {
      removeRow(row.getIndex());
    }
    int status = ((JMXMutableTableSupport)table).createRow(tableOID, row);
    if (status != PDU.noError) {
      throw new UnsupportedOperationException(PDU.toErrorStatusText(status));
    }
    return oldRow;
  }

  public MOTableRow removeRow(OID index) {
    MOTableRow row = rowFactory.createRow(index, getInitialRowValues());
    if (row != null) {
      table.getRow(tableOID, row);
      int status = ((JMXMutableTableSupport)table).removeRow(tableOID, index);
      if (status != PDU.noError) {
        logger.debug("Row removal failed for index="+index+" and table "+
                     tableOID+" with "+PDU.toErrorStatusText(status));
        return null;
      }
    }
    return row;
  }

  public void clear() {
    ((JMXMutableTableSupport)table).clear(tableOID);
  }

  public void clear(MOTableRowFilter filter) {
    Iterator it = table.rowIdIterator(tableOID);
    List<OID> toRemove = new ArrayList<OID>();
    for (int i=0; it != null && it.hasNext(); i++) {
      Object rowID = it.next();
      OID index = table.mapToIndex(tableOID, rowID, i);
      MOTableRow row = getRow(index);
      if (filter.passesFilter(row)) {
        toRemove.add(index);
      }
    }
    for (int i=0; i<toRemove.size(); i++) {
      ((JMXMutableTableSupport)table).removeRow(tableOID, toRemove.get(i));
    }
  }

  public void setRowFactory(MOTableRowFactory rowFactory) {
    this.rowFactory = rowFactory;
  }

}
TOP

Related Classes of org.snmp4j.agent.mo.jmx.JMXMutableTableModel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.