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

📄 snmp4jconfigmib.java

📁 用snmp4j实现的agent,代码比较多,但是很值得一看,尤其是对于要用SNMP监控信息的编程者,可以仔细研究一下里面的代码.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      if (!(((os.length() >= 0) && (os.length() <= 255)))) {
        validationEvent.setValidationStatus(SnmpConstants.SNMP_ERROR_WRONG_LENGTH);
        return;
      }
     //--AgentGen BEGIN=snmp4jCfgStoragePath::validate
     //--AgentGen END
    }
  }
  /**
   * The <code>Snmp4jCfgStorageFormatValidator</code> implements the value
   * validation for <code>Snmp4jCfgStorageFormat</code>.
   */
  static class Snmp4jCfgStorageFormatValidator implements MOValueValidationListener {

    public void validate(MOValueValidationEvent validationEvent) {
      Variable newValue = validationEvent.getNewValue();
     //--AgentGen BEGIN=snmp4jCfgStorageFormat::validate
     if (((Integer32)newValue).getValue() != Snmp4jCfgStorageFormatEnum.binary) {
       validationEvent.setValidationStatus(PDU.wrongValue);
     }
     //--AgentGen END
    }
  }
  /**
   * The <code>Snmp4jCfgStorageOperationValidator</code> implements the value
   * validation for <code>Snmp4jCfgStorageOperation</code>.
   */
  static class Snmp4jCfgStorageOperationValidator implements MOValueValidationListener {

    public void validate(MOValueValidationEvent validationEvent) {
      Variable newValue = validationEvent.getNewValue();
     //--AgentGen BEGIN=snmp4jCfgStorageOperation::validate
     switch (newValue.toInt()) {
       case Snmp4jCfgStorageOperationEnum.restore:
       case Snmp4jCfgStorageOperationEnum.store:
         break;
       default:
         validationEvent.setValidationStatus(PDU.wrongValue);
     }
     //--AgentGen END
    }
  }

  // Enumerations
  public static final class Snmp4jCfgSecSrcAddrValidationEnum {
    public static final int enabled = 1;
    public static final int disabled = 2;
    public static final int notAvailable = 3;
  }

  public static final class Snmp4jCfgStorageFormatEnum {
    /* -- Default format */
    public static final int binary = 1;
    public static final int xml = 2;
  }
  public static final class Snmp4jCfgStorageRestoreModeEnum {
    public static final int replaceAndCreate = 1;
    public static final int updateAndCreate = 2;
    public static final int updateOnly = 3;
    public static final int createOnly = 4;
  }
  public static final class Snmp4jCfgStorageOperationEnum {
    /* -- no action */
    public static final int idle = 1;
    /* -- (re)store operation in progress */
    public static final int inProgress = 2;
    /* -- store current configuration */
    public static final int store = 3;
    /* -- restore configuration */
    public static final int restore = 4;
  }

  // Rows and Factories
  class Snmp4jCfgStorageEntryRowFactory
        extends DefaultMOMutableRow2PCFactory
  {
    public Snmp4jCfgStorageEntryRowFactory() {}

    public MOTableRow createRow(OID index, Variable[] values)
        throws UnsupportedOperationException
    {
      Snmp4jCfgStorageEntryRow row = new Snmp4jCfgStorageEntryRow(index, values);
     //--AgentGen BEGIN=snmp4jCfgStorageEntry::createRow
     ((Integer32)values[idxSnmp4jCfgStorageOperation]).
         setValue(Snmp4jCfgStorageOperationEnum.idle);
     //--AgentGen END
      return row;
    }

    public void freeRow(MOTableRow row) {
     //--AgentGen BEGIN=snmp4jCfgStorageEntry::freeRow
     //--AgentGen END
    }
  }

  class Snmp4jCfgStorageEntryRow extends DefaultMOMutableRow2PC {

    public Snmp4jCfgStorageEntryRow(OID index, Variable[] values) {
      super(index, values);
    }

    public OctetString getSnmp4jCfgStoragePath() {
      return (OctetString) getValue(idxSnmp4jCfgStoragePath);
    }

    public void setSnmp4jCfgStoragePath(OctetString newValue) {
      setValue(idxSnmp4jCfgStoragePath, newValue);
    }

    public OctetString getSnmp4jCfgStorageID() {
      return (OctetString) getValue(idxSnmp4jCfgStorageID);
    }

    public void setSnmp4jCfgStorageID(OctetString newValue) {
      setValue(idxSnmp4jCfgStorageID, newValue);
    }

    public TimeTicks getSnmp4jCfgStorageLastStore() {
      return (TimeTicks) getValue(idxSnmp4jCfgStorageLastStore);
    }

    public void setSnmp4jCfgStorageLastStore(TimeTicks newValue) {
      setValue(idxSnmp4jCfgStorageLastStore, newValue);
    }

    public TimeTicks getSnmp4jCfgStorageLastRestore() {
      return (TimeTicks) getValue(idxSnmp4jCfgStorageLastRestore);
    }

    public void setSnmp4jCfgStorageLastRestore(TimeTicks newValue) {
      setValue(idxSnmp4jCfgStorageLastRestore, newValue);
    }

    public Integer32 getSnmp4jCfgStorageRestoreMode() {
      return (Integer32) getValue(idxSnmp4jCfgStorageRestoreMode);
    }

    public void setSnmp4jCfgStorageRestoreMode(Integer32 newValue) {
      setValue(idxSnmp4jCfgStorageRestoreMode, newValue);
    }

    public Integer32 getSnmp4jCfgStorageOperation() {
      return (Integer32) getValue(idxSnmp4jCfgStorageOperation);
    }

    public void setSnmp4jCfgStorageOperation(Integer32 newValue) {
      setValue(idxSnmp4jCfgStorageOperation, newValue);
    }

    public Integer32 getSnmp4jCfgStorageStorageType() {
      return (Integer32) getValue(idxSnmp4jCfgStorageStorageType);
    }

    public void setSnmp4jCfgStorageStorageType(Integer32 newValue) {
      setValue(idxSnmp4jCfgStorageStorageType, newValue);
    }

    public Integer32 getSnmp4jCfgStorageStatus() {
      return (Integer32) getValue(idxSnmp4jCfgStorageStatus);
    }

    public void setSnmp4jCfgStorageStatus(Integer32 newValue) {
      setValue(idxSnmp4jCfgStorageStatus, newValue);
    }

    //--AgentGen BEGIN=snmp4jCfgStorageEntry::RowFactory

    public void prepareRow(SubRequest subRequest, MOTableRow changeSet) {
      if (PRIMARY_INDEX.equals(changeSet.getIndex())) {
        if (snmp4jCfgStorageEntryModel.getRow(PRIMARY_INDEX) == null) {
          subRequest.getRequest().setErrorStatus(PDU.noCreation);
        }
      }
    }

    public void commitRow(SubRequest subRequest, MOTableRow changeSet) {
      Integer32 operation = getSnmp4jCfgStorageOperation();
      OctetString providerID = getSnmp4jCfgStorageID();
      MOPersistenceProvider provider =
          getPersistenceProvider(providerID.toString());
      if (provider == null) {
        subRequest.getRequest().setErrorStatus(PDU.commitFailed);
      }
      else {
        Operation op =
            new Operation(this, provider,
                          getSnmp4jCfgStorageRestoreMode().getValue(),
                          operation.getValue());
        setValue(idxSnmp4jCfgStorageOperation,
                 new Integer32(Snmp4jCfgStorageOperationEnum.inProgress));
        op.start();
      }
    }

    //--AgentGen END
  }



//--AgentGen BEGIN=_METHODS

  public void setSnmpCommunityMIB(SnmpCommunityMIB snmpCommunityMIB) {
    this.snmpCommunityMIB = snmpCommunityMIB;
  }

  public void setPrimaryProvider(MOPersistenceProvider persistenceProvider) {
    this.primaryPersistence = persistenceProvider;

    Snmp4jCfgStorageEntryRow primary = (Snmp4jCfgStorageEntryRow)
        snmp4jCfgStorageEntryModel.getRow(PRIMARY_INDEX);
    if (primary == null) {
      Variable[] vbs = snmp4jCfgStorageEntry.getDefaultValues();
      vbs[idxSnmp4jCfgStorageStatus] = new Integer32(RowStatus.active);
      primary = (Snmp4jCfgStorageEntryRow)
          snmp4jCfgStorageEntry.createRow(PRIMARY_INDEX, vbs);
      primary.setSnmp4jCfgStorageStorageType(
          new Integer32(StorageType.permanent));
      primary.setSnmp4jCfgStorageOperation(
          new Integer32(Snmp4jCfgStorageOperationEnum.idle));
      snmp4jCfgStorageEntry.addRow(primary);
    }
    primary.setSnmp4jCfgStoragePath(
      new OctetString(primaryPersistence.getDefaultURI()));
    addPersistenceProvider(persistenceProvider);
  }

  public void addPersistenceProvider(MOPersistenceProvider provider) {
    persistenceProvider.put(provider.getPersistenceProviderID(), provider);
  }

  public MOPersistenceProvider getPersistenceProvider(String id) {
    return (MOPersistenceProvider) persistenceProvider.get(id);
  }

  public SnmpCommunityMIB getCoexistenceInfoProvider() {
    return this.snmpCommunityMIB;
  }

//--AgentGen END

//--AgentGen BEGIN=_CLASSES

  private class Operation extends Thread {

    private Snmp4jCfgStorageEntryRow row;
    private int operation;
    private int restoreType;
    private MOPersistenceProvider provider;

    public Operation(Snmp4jCfgStorageEntryRow row,
                     MOPersistenceProvider provider,
                     int restoreType,
                     int operation) {
      this.operation = operation;
      this.provider = provider;
      this.restoreType = restoreType;
      this.row = row;
    }

    public void run() {
      switch (operation) {
        case Snmp4jCfgStorageOperationEnum.store: {
          String path = row.getValue(idxSnmp4jCfgStoragePath).toString();
          try {
            provider.store(path);
            row.setValue(idxSnmp4jCfgStorageLastStore, sysUpTime.get());
            row.setValue(idxSnmp4jCfgStorageOperation,
                         new Integer32(Snmp4jCfgStorageOperationEnum.idle));
          }
          catch (Exception iox) {
            logger.warn("Failed to store config to '"+path+"': "+iox.getMessage());
            row.setValue(idxSnmp4jCfgStorageOperation,
                         new Integer32(Snmp4jCfgStorageOperationEnum.idle));
          }
          break;
        }
        case Snmp4jCfgStorageOperationEnum.restore: {
          String f = row.getValue(idxSnmp4jCfgStoragePath).toString();
          try {
            provider.restore(f, restoreType);
            row.setValue(idxSnmp4jCfgStorageLastRestore, sysUpTime.get());
            row.setValue(idxSnmp4jCfgStorageOperation,
                         new Integer32(Snmp4jCfgStorageOperationEnum.idle));
          }
          catch (Exception iox) {
            logger.warn("Failed to restore config from '" + f + "': "+
                        iox.getMessage());
            row.setValue(idxSnmp4jCfgStorageOperation,
                         new Integer32(Snmp4jCfgStorageOperationEnum.idle));
          }
          break;
        }
      }
    }
  }

//--AgentGen END

//--AgentGen BEGIN=_END
//--AgentGen END
}


⌨️ 快捷键说明

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