⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rowstatus.java

📁 你个snmp的源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*_############################################################################
  _##
  _##  SNMP4J-Agent - RowStatus.java
  _##
  _##  Copyright 2005-2006  Frank Fock (SNMP4J.org)
  _##
  _##  Licensed under the Apache License, Version 2.0 (the "License");
  _##  you may not use this file except in compliance with the License.
  _##  You may obtain a copy of the License at
  _##
  _##      http://www.apache.org/licenses/LICENSE-2.0
  _##
  _##  Unless required by applicable law or agreed to in writing, software
  _##  distributed under the License is distributed on an "AS IS" BASIS,
  _##  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  _##  See the License for the specific language governing permissions and
  _##  limitations under the License.
  _##
  _##########################################################################*/


package org.snmp4j.agent.mo.snmp;

import java.util.*;

import org.snmp4j.*;
import org.snmp4j.agent.*;
import org.snmp4j.agent.mo.*;
import org.snmp4j.agent.request.*;
import org.snmp4j.smi.*;
import org.snmp4j.log.LogAdapter;
import org.snmp4j.log.LogFactory;

/**
 * The <code>RowStatus</code> class implements the columnar object TC RowStatus.
 * The RowStatus textual convention is used to manage the creation and deletion
 * of conceptual rows, and is used as the value of the SYNTAX clause for the
 * status column of a conceptual row. See RFC 2579.
 *
 * The RowStatus column controls row creation and deletion in SNMP tables with
 * READ-CREATE maximum access. Since the state of a dynamic row is/may be
 * important to dependent rows / other objects of an agent, row status change
 * events can be propagated to other objects through registering
 * {@link RowStatusListener}s.
 *
 * @author Frank Fock
 * @version 1.0
 */
public class RowStatus extends MOMutableColumn implements MOChangeListener,
    MOTableRowListener
{

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

  public static final int notExistant = 0;
  public static final int active = 1;
  public static final int notInService = 2;
  public static final int notReady = 3;
  public static final int createAndGo = 4;
  public static final int createAndWait = 5;
  public static final int destroy = 6;

  private OID oid;
  private int columnIndex;

  private transient Vector rowStatusListeners;

  /**
   * Creates a RowStatus column with the specified column sub-identifier and
   * maximum access of 'read-create'.
   * @param columnID
   *    a column sub-identifier.
   */
  public RowStatus(int columnID) {
    super(columnID, SMIConstants.SYNTAX_INTEGER,
          MOAccessImpl.ACCESS_READ_CREATE);
    this.addMOValueValidationListener(new RowStatusValidator());
  }

  /**
   * Creates a RowStatus column with the specified column sub-identifier.
   * @param columnID
   *    a column sub-identifier.
   * @param access
   *    the maximum access for the RowStatus column (should be READ-CREATE).
   */
  public RowStatus(int columnID, MOAccess access) {
    super(columnID, SMIConstants.SYNTAX_INTEGER, access);
    this.addMOValueValidationListener(new RowStatusValidator());
  }

  /**
   * Sets the table instance this columnar object is contained in. This method
   * should be called by {@link MOTable} instance to register the table with
   * the RowStatus column. When called, this RowStatus registers itself as
   * {@link MOChangeListener} and {@link MOTableRowListener}.
   * @param table
   *    the <code>MOTable</code> instance where this column is contained in.
   */
  public void setTable(MOTable table) {
    super.setTable(table);
    oid = new OID(table.getOID());
    oid.append(getColumnID());
    columnIndex = table.getColumnIndex(getColumnID());

    table.addMOChangeListener(this);
    table.addMOTableRowListener(this);
  }

  /**
   * Unsets the table instance and thus unregisters itself as
   * {@link MOChangeListener} and {@link MOTableRowListener}.
   * @param table
   *    the <code>MOTable</code> instance where this column was part of.
   */
  public void unsetTable(MOTable table) {
    columnIndex = 0;
    table.removeMOChangeListener(this);
    table.removeMOTableRowListener(this);
  }

  protected boolean isReady(MOTableRow row, int rowStatusColumn) {
    return isReady(row, rowStatusColumn, null);
  }

  protected boolean isReady(MOTableRow row, int rowStatusColumn,
                            MOTableRow changeSet) {
    MOColumn[] columns = getTable().getColumns();
    for (int i=0; i<columns.length; i++) {
      if (i == rowStatusColumn) {
        continue;
      }
      if (columns[i] instanceof MOMutableColumn) {
        MOMutableColumn mcol = (MOMutableColumn) columns[i];
        if ((mcol.isMandatory()) &&
            ((row.getValue(i) == null) &&
             ((changeSet == null) || (changeSet.getValue(i) == null)))) {
          if (logger.isDebugEnabled()) {
            logger.debug("Row "+row+" is not ready because column "+i+
                         " is not set properly");
          }
          return false;
        }
      }
    }
    return true;
  }

  public void prepare(SubRequest subRequest, MOTableRow row,
                      MOTableRow changeSet, int column) {
    super.prepare(subRequest, row, null, column);
    if (subRequest.getStatus().getErrorStatus() == PDU.noError) {
      int newValue =
          ((Integer32)subRequest.getVariableBinding().getVariable()).getValue();
      int oldValue = notExistant;
      if (row.getValue(column) != null) {
        oldValue = ((Integer32) row.getValue(column)).getValue();
      }
      if ((oldValue == notExistant) || (oldValue == notReady) ||
          (oldValue == createAndGo)) {
        switch (newValue) {
          case createAndGo:
          case notInService:
          case active: {
            if (!isReady(row, column, changeSet)) {
              if (logger.isDebugEnabled()) {
                logger.debug(toString()+": Row '"+
                             row.getIndex() +
                             " is not ready! Cannot change status to from "+
                             oldValue+" to "+newValue);
              }
              subRequest.getStatus().setErrorStatus(PDU.inconsistentValue);
            }
            break;
          }
        }
      }
      RowStatusEvent rowStatusEvent =
          new RowStatusEvent(this, getTable(), row, changeSet,
                             oldValue, newValue, true);
      fireRowStatusChanged(rowStatusEvent);
      if (rowStatusEvent.getDenyReason() != PDU.noError) {
        subRequest.getStatus().setErrorStatus(rowStatusEvent.getDenyReason());
      }
    }
  }

  public void commit(SubRequest subRequest, MOTableRow row,
                     MOTableRow changeSet, int column) {
    int oldValue = ((Integer32)row.getValue(column)).getValue();
    super.commit(subRequest, row, null, column);
    if (!subRequest.hasError()) {
      int newValue = ((Integer32)row.getValue(column)).getValue();
      assignNewValue(subRequest, row, column, newValue);
      RowStatusEvent rowStatusEvent =
          new RowStatusEvent(this, getTable(), row, changeSet,
                             oldValue, newValue);
      fireRowStatusChanged(rowStatusEvent);
    }
  }

  protected void assignNewValue(SubRequest subRequest,
                                MOTableRow row, int column,
                                int newValue) {
    switch (newValue) {
      case destroy: {
        MOTableRow deleted = getTable().removeRow(row.getIndex());
        if (deleted == null) {
          subRequest.getStatus().setErrorStatus(PDU.commitFailed);
        }
        else {
          subRequest.setUndoValue(deleted);
        }
        break;
      }
      case createAndWait: {
        if (isReady(row, column)) {
          ((Integer32)row.getValue(column)).setValue(RowStatus.notInService);
        }
        else {
          ((Integer32) row.getValue(column)).setValue(RowStatus.notReady);
        }
        break;
      }
      case createAndGo: {
        ((Integer32)row.getValue(column)).setValue(RowStatus.active);
        break;
      }
    }
  }

  public void undo(SubRequest subRequest, MOTableRow row, int column) {
    super.undo(subRequest, row, column);
    if (!subRequest.hasError()) {
      int newStatus = ((Integer32)row.getValue(column)).getValue();
      switch (newStatus) {
        case destroy: {
          MOTableRow oldRow = (MOTableRow)subRequest.getUndoValue();
          Integer32 oldValue = (Integer32) oldRow.getValue(column);
          boolean added =
              getTable().addRow((MOTableRow)subRequest.getUndoValue());
          if (!added) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -