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

📄 moscalar.java

📁 你个snmp的源码
💻 JAVA
字号:
/*_############################################################################
  _##
  _##  SNMP4J-Agent - MOScalar.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;

import java.util.*;

import org.snmp4j.agent.*;
import org.snmp4j.agent.request.*;
import org.snmp4j.mp.*;
import org.snmp4j.smi.*;
import org.snmp4j.agent.io.MOInput;
import java.io.IOException;
import org.snmp4j.agent.io.MOOutput;

/**
 * The <code>MOScalar</code> class represents scalar SNMP managed objects.
 *
 * @author Frank Fock
 * @version 1.0
 */
public class MOScalar implements ManagedObject, MOScope,
    SerializableManagedObject {

  private OID oid;
  private Variable value;
  private MOAccess access;
  private boolean isVolatile;
  private transient Vector moValueValidationListeners;

  /**
   * Creates a scalar MO instance with OID, maximum access level and initial
   * value.
   * @param id
   *    the instance OID of the scalar instance (last sub-identifier should be
   *    zero).
   * @param access
   *    the maximum access level supported by this instance.
   * @param value
   *    the initial value of the scalar instance. If the initial value is
   *    <code>null</code> or a Counter syntax, the scalar is created as a
   *    volatile (non-persistent) instance by default.
   */
  public MOScalar(OID id, MOAccess access, Variable value) {
    this.oid = id;
    this.access = access;
    this.value = value;
    this.isVolatile = isVolatileByDefault(value);
  }

  private static boolean isVolatileByDefault(Variable value) {
    if (value == null) {
      return true;
    }
    switch (value.getSyntax()) {
      case SMIConstants.SYNTAX_COUNTER32:
      case SMIConstants.SYNTAX_COUNTER64: {
        return true;
      }
      default:
        return false;
    }
  }

  public MOScope getScope() {
    return this;
  }

  public OID find(MOScope range) {
    if (access.isAccessibleForRead() && range.isCovered(this)) {
      return oid;
    }
    return null;
  }

  public void get(SubRequest request) {
    RequestStatus status = request.getStatus();
    if (access.isAccessibleForRead()) {
      VariableBinding vb = request.getVariableBinding();
      vb.setOid(getOid());
      vb.setVariable((Variable)getValue().clone());
      request.completed();
    }
    else {
      status.setErrorStatus(SnmpConstants.SNMP_ERROR_NO_ACCESS);
    }
  }

  public boolean next(SubRequest request) {
    if (access.isAccessibleForRead() && (request.getScope().isCovered(this))) {
      VariableBinding vb = request.getVariableBinding();
      vb.setOid(getOid());
      vb.setVariable((Variable)getValue().clone());
      request.completed();
      return true;
    }
    return false;
  }

  /**
   * Checks whether the new value contained in the supplied sub-request is a
   * valid value for this object. The checks are performed by firing a
   * {@link MOValueValidationEvent} the registered listeners.
   *
   * @param request
   *    the <code>SubRequest</code> with the new value.
   * @return
   *    {@link SnmpConstants#SNMP_ERROR_SUCCESS} if the new value is OK,
   *    any other appropriate SNMPv2/v3 error status if not.
   */
  public int isValueOK(SubRequest request) {
    if (moValueValidationListeners != null) {
      Variable oldValue = value;
      Variable newValue =
           request.getVariableBinding().getVariable();
      MOValueValidationEvent event =
          new MOValueValidationEvent(this, oldValue, newValue);
      fireValidate(event);
      return event.getValidationStatus();
    }
    return SnmpConstants.SNMP_ERROR_SUCCESS;
  }

  public void prepare(SubRequest request) {
    RequestStatus status = request.getStatus();
    if (access.isAccessibleForWrite()) {
      VariableBinding vb = request.getVariableBinding();
      if (vb.getVariable().getSyntax() != getValue().getSyntax()) {
        status.setErrorStatus(SnmpConstants.SNMP_ERROR_WRONG_TYPE);
        return;
      }
      int valueOK = isValueOK(request);
      status.setErrorStatus(valueOK);
      status.setPhaseComplete(true);
    }
    else {
      status.setErrorStatus(SnmpConstants.SNMP_ERROR_NOT_WRITEABLE);
    }
  }

  public void commit(SubRequest request) {
    RequestStatus status = request.getStatus();
    VariableBinding vb = request.getVariableBinding();
    request.setUndoValue(getValue());
    setValue(vb.getVariable());
    status.setPhaseComplete(true);
  }

  public void undo(SubRequest request) {
    RequestStatus status = request.getStatus();
    if ((request.getUndoValue() != null) &&
        (request.getUndoValue() instanceof Variable)) {
      int errorStatus = setValue((Variable)request.getUndoValue());
      status.setErrorStatus(errorStatus);
      status.setPhaseComplete(true);
    }
    else {
      status.setErrorStatus(SnmpConstants.SNMP_ERROR_UNDO_FAILED);
    }
  }

  public void cleanup(SubRequest request) {
    request.setUndoValue(null);
    request.getStatus().setPhaseComplete(true);
  }

  /**
   * Gets the instance OID of this scalar managed object.
   * @return
   *    the instance OID (by reference).
   */
  public OID getOid() {
    return oid;
  }

  public OID getLowerBound() {
    return oid;
  }

  public OID getUpperBound() {
    return oid;
  }

  public boolean isCovered(MOScope other) {
    return (other.isLowerIncluded() && other.isUpperIncluded() &&
            oid.equals(other.getLowerBound()) &&
            oid.equals(other.getUpperBound()));
  }

  public boolean isLowerIncluded() {
    return true;
  }

  public boolean isUpperIncluded() {
    return true;
  }

  /**
   * Returns the actual value of this scalar managed object. For a basic
   * instrumentation, overwrite this method to provide always the actual
   * value and/or to update the internal <code>value</code> member and
   * call <code>super.</code>{@link #getValue()}.
   *
   * @return
   *    a non <code>null</code> Variable with the same syntax defined for
   *    this scalar object.
   */
  public Variable getValue() {
    return value;
  }

  public boolean isVolatile() {
    return isVolatile;
  }

  /**
   * Sets the value of this scalar managed object without checking it for
   * the correct syntax.
   * @param value
   *    a Variable with the with the same syntax defined for
   *    this scalar object (not checked).
   * @return
   *    a SNMP error code (zero indicating success by default).
   */
  public int setValue(Variable value) {
    this.value = value;
    return SnmpConstants.SNMP_ERROR_SUCCESS;
  }

  /**
   * Sets the volatile flag for this instance.
   * @param isVolatile
   *    if <code>true</code> the state of this object will not be persistently
   *    stored, otherwise the agent may save the state of this object
   *    persistently.
   */
  public void setVolatile(boolean isVolatile) {
    this.isVolatile = isVolatile;
  }

  public boolean isOverlapping(MOScope other) {
    return DefaultMOScope.overlaps(this, other);
  }

  /**
   * Adds a value validation listener to check new values.
   * @param l
   *   a <code>MOValueValidationListener</code> instance.
   */
  public synchronized void addMOValueValidationListener(
      MOValueValidationListener l) {
      if (moValueValidationListeners == null) {
        moValueValidationListeners = new Vector(2);
      }
      moValueValidationListeners.add(l);
  }

  /**
   * Removes a value validation listener
   * @param l
   *    a <code>MOValueValidationListener</code> instance.
   */
  public synchronized void removeMOValueValidationListener(
      MOValueValidationListener l) {
      if (moValueValidationListeners != null) {
        moValueValidationListeners.remove(l);
      }
  }

  protected void fireValidate(MOValueValidationEvent validationEvent) {
    if (moValueValidationListeners != null) {
      Vector listeners = moValueValidationListeners;
      int count = listeners.size();
      for (int i = 0; i < count; i++) {
        ((MOValueValidationListener) listeners.elementAt(i)).validate(
            validationEvent);
      }
    }
  }

  public OID getID() {
    return getOid();
  }

  public synchronized void load(MOInput input) throws IOException {
    Variable v = input.readVariable();
    setValue(v);
  }

  public synchronized void save(MOOutput output) throws IOException {
    output.writeVariable(value);
  }

  public boolean covers(OID oid) {
    return this.oid.equals(oid);
  }

}

⌨️ 快捷键说明

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