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

📄 propertymoinput.java

📁 用snmp4j实现的agent,代码比较多,但是很值得一看,尤其是对于要用SNMP监控信息的编程者,可以仔细研究一下里面的代码.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
   * @return
   *    a LinkedHashMap with properties whose key starts with
   *    {@link #CONFIG_PREFIX}.
   */
  private static SortedMap scanProperties(Properties props) {
    SortedMap map = new TreeMap();
    for (Iterator it = props.entrySet().iterator(); it.hasNext();) {
      Entry e = (Entry) it.next();
      if (e.getKey().toString().startsWith(CONFIG_PREFIX)) {
        map.put(e.getKey(), e.getValue());
      }
    }
    return map;
  }

  /**
   * Returns the update mode, which might be one of the constants defined by
   * {@link ImportModes}. By default, {@link ImportModes#REPLACE_CREATE} is
   * returned.
   *
   * @return the constant denoting the update mode that should be used by a
   *   <code>SerializableManagedObject</code> to import its content from
   *   persistent storage.
   */
  public int getImportMode() {
    return importMode;
  }

  public Context readContext() throws IOException {
    if ((state == STATE_CTX_SEQ) && (contexts != null)) {
      return (Context) contexts.iterator.next();
    }
    else {
      throw new IOException();
    }
  }

  public IndexedVariables readIndexedVariables() throws IOException {
    if (data == null) {
      data = scanData(properties, oids.curContext, oids.curOID.getOID(), true);
    }
    IndexedVariables ivar = (IndexedVariables) data.next();
    if (logger.isDebugEnabled()) {
      logger.debug("Read indexed variables "+ivar+" for OID "+
                   oids.curOID.getOID()+" in context "+oids.curContext);
    }
    return ivar;
  }

  public MOInfo readManagedObject() throws IOException {
    MOInfo info = (MOInfo) oids.next();
    data = null;
    if (logger.isDebugEnabled()) {
      logger.debug("Read MO "+info);
    }
    return info;
  }

  public Sequence readSequence() throws IOException {
    switch (state) {
      case STATE_ALL_CTX_DATA_SEQ: {
        state++;
        oids = scanOIDs(properties, null);
        return new Sequence(oids.numElements);
      }
      case STATE_ALL_CTX_DATA:
//        state++;
        if (data != null && !oids.iterator.hasNext()) {
          state++;
        }
        else {
          data = scanData(properties, oids.curContext, oids.curOID.getOID(), true);
          return new Sequence(data.numElements);
        }
      case STATE_CTX_SEQ: {
        state++;
        return new Sequence(contexts.numContexts);
      }
    }
    return null;
  }

  public Variable readVariable() throws IOException {
    if (data == null) {
      data = scanData(properties, oids.curContext, oids.curOID.getOID(), false);
    }
    Variable v =(Variable) data.next();
    if (logger.isDebugEnabled()) {
      logger.debug("Read variable "+v+" for OID "+oids.curOID.getOID()+
                   " in context "+oids.curContext);
    }
    return v;
  }

  public void skipContext(Context context) throws IOException {

  }

  public void skipManagedObject(MOInfo mo) throws IOException {
  }

  /**
   * Parses a string of the format
   * <pre>
   * OID={type}value where &lt;type&gt; is one of
   * the following single characters enclosed by '{' and '}':
   *  i                     Integer32
   *  u                     UnsingedInteger32, Gauge32
   *  s                     OCTET STRING
   *  x                     OCTET STRING specified as hex string where
   *                        bytes separated by colons (':').
   *  d                     OCTET STRING specified as decimal string
   *                        where bytes are separated by dots ('.').
   *  n                     Null
   *  o                     OBJECT IDENTIFIER
   *  t                     TimeTicks
   *  a                     IpAddress
   *  b                     OCTET STRING specified as binary string where
   *                        bytes are separated by spaces.
   *  $&lt;variableName&gt;     where <variableName> is the name of a predefined
   *                        variable or the OID of a variable of the agent's
   *                        MIB.
   * </pre>
   * and returns the corresponding variable.
   *
   * @param value
   *    the variable value string.
   * @param returnType
   *    the expected Variable class to return.
   * @return
   *    <code>null</code> if <code>value</code> is <code>null</code> and
   *    the <code>Variable</code> corresponding to <code>value</code> otherwise.
   */
  public Variable createVariableFromString(String value, Class returnType) {
    if (value != null) {
      char type = ' ';
      String varName = null;
      if (value.length() >= 3) {
        type = value.charAt(1);
        int pos = value.indexOf('}');
        if (type == '$') {
          varName = value.substring(2, pos);
        }
        value = value.substring(pos+1);
      }
      Variable variable;
      switch (type) {
        case 'i':
          variable = new Integer32(Integer.parseInt(value));
          break;
        case 'u':
          variable = new UnsignedInteger32(Long.parseLong(value));
          break;
        case 's':
          variable = new OctetString(value);
          break;
        case 'x':
          variable = OctetString.fromString(value, ':', 16);
          break;
        case 'd':
          variable = OctetString.fromString(value, '.', 10);
          break;
        case 'b':
          variable = OctetString.fromString(value, ' ', 2);
          break;
        case 'n':
          variable = new Null();
          break;
        case 'o':
          try {
            variable = new OID(value);
          }
          catch (Exception ex) {
            // does oid contain variable reference?
            Pattern p = Pattern.compile("(\\$#?\\{[^\\}]*\\})");
            Matcher m = p.matcher(value);
            StringBuffer result = new StringBuffer();
            while (m.find()) {
              String group = m.group();
              boolean impliedLength = true;
              if (group.charAt(1) == '#') {
                impliedLength = false;
              }
              group = group.substring(group.indexOf('{')+1, group.length()-1);
              Variable replacementValue = (this.variables == null) ?
                  new OID() : this.variables.getVariable(group);
              OID oid = replacementValue.toSubIndex(impliedLength);
              m.appendReplacement(result, oid.toString());
            }
            m.appendTail(result);
            variable = new OID(result.toString());
          }
          break;
        case 't':
          variable = new TimeTicks(Long.parseLong(value));
          break;
        case 'a':
          variable = new IpAddress(value);
          break;
        case '$':
          variable = (this.variables == null) ?
              null : this.variables.getVariable(varName);
          break;
        case ' ':
          return null;
        default:
          throw new IllegalArgumentException("Variable type "+type+
                                             " not supported");
       }
       if (!returnType.isInstance(variable)) {
         /**@todo make conversion*/
       }
       return variable;
     }
     return null;
  }

  public void close() throws IOException {
  }

  private class ContextInfo implements Iterator {
    int numContexts;
    OctetString curContext;
    Iterator iterator;

    ContextInfo(int numContexts, Iterator contexts) {
      this.numContexts = numContexts;
      this.iterator = contexts;
    }

    public void remove() {
      throw new UnsupportedOperationException();
    }

    public boolean hasNext() {
      return iterator.hasNext();
    }

    public Object next() {
      curContext = (OctetString) iterator.next();
      return curContext;
    }
  }

  private class OIDInfo implements Iterator {
    int numElements;
    MOInfo curOID;
    OctetString curContext;
    Iterator iterator;

    OIDInfo(OctetString context, int numElements, Iterator data) {
      this.numElements = numElements;
      this.iterator = data;
      this.curContext = context;
    }

    public void remove() {
      throw new UnsupportedOperationException();
    }

    public boolean hasNext() {
      return iterator.hasNext();
    }

    public Object next() {
      curOID = (MOInfo) iterator.next();
      return curOID;
    }
  }

  private class DataInfo implements Iterator {
    int numElements;
    OctetString context;
    Iterator iterator;

    DataInfo(OctetString context, int numElements, Iterator data) {
      this.numElements = numElements;
      this.iterator = data;
    }

    public void remove() {
      throw new UnsupportedOperationException();
    }

    public boolean hasNext() {
      return iterator.hasNext();
    }

    public Object next() {
      return iterator.next();
    }
  }

}

⌨️ 快捷键说明

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