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

📄 abstractvariable.java

📁 snmp4j 1.8.2版 The org.snmp4j classes are capable of creating, sending, and receiving SNMPv1/v2c/v3
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*_############################################################################
  _## 
  _##  SNMP4J - AbstractVariable.java  
  _## 
  _##  Copyright 2003-2007  Frank Fock and Jochen Katz (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.smi;

import java.io.*;
import java.util.*;
import org.snmp4j.log.*;
import org.snmp4j.asn1.*;
import org.snmp4j.SNMP4JSettings;
// For JavaDoc:
import org.snmp4j.PDU;

/**
 * The <code>Variable</code> abstract class is the base class for all SNMP
 * variables.
 * <p>
 * All derived classes need to be registered with their SMI BER type in the
 * <code>smisyntaxes.properties</code>so that the
 * {@link #createFromBER(BERInputStream inputStream)} method
 * is able to decode a variable from a BER encoded stream.
 * <p>
 * To register additional syntaxes, set the system property
 * {@link #SMISYNTAXES_PROPERTIES} before decoding a Variable for the first
 * time. The path of the property file must be accessible from the classpath
 * and it has to be specified relative to the <code>Variable</code> class.
 *
 * @author Jochen Katz & Frank Fock
 * @version 1.8
 * @since 1.8
 */
public abstract class AbstractVariable implements Variable, Serializable {

  private static final long serialVersionUID = 1395840752909725320L;

  public static final String SMISYNTAXES_PROPERTIES =
      "org.snmp4j.smisyntaxes";
  private static final String SMISYNTAXES_PROPERTIES_DEFAULT =
      "smisyntaxes.properties";

  private static final Object[][] SYNTAX_NAME_MAPPING = {
      { "Integer32", new Integer(BER.INTEGER32) },
      { "BIT STRING", new Integer(BER.BITSTRING) },
      { "OCTET STRING", new Integer(BER.OCTETSTRING) },
      { "OBJECT IDENTIFIER", new Integer(BER.OID) },
      { "TimeTicks", new Integer(BER.TIMETICKS) },
      { "Counter", new Integer(BER.COUNTER) },
      { "Counter64", new Integer(BER.COUNTER64) },
      { "EndOfMibView", new Integer(BER.ENDOFMIBVIEW) },
      { "Gauge", new Integer(BER.GAUGE32) },
      { "IpAddress", new Integer(BER.IPADDRESS) },
      { "NoSuchInstance", new Integer(BER.NOSUCHINSTANCE) },
      { "NoSuchObject", new Integer(BER.NOSUCHOBJECT) },
      { "Null", new Integer(BER.NULL) },
      { "Opaque", new Integer(BER.OPAQUE) }
  };

  private static Hashtable registeredSyntaxes = null;

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

  /**
   * The abstract <code>Variable</code> class serves as the base class for all
   * specific SNMP syntax types.
   */
  public AbstractVariable() {
  }

  public abstract boolean equals(Object o);

  public abstract int compareTo(Object o);

  public abstract int hashCode();

  /**
   * Returns the length of this <code>Variable</code> in bytes when encoded
   * according to the Basic Encoding Rules (BER).
   * @return
   *    the BER encoded length of this variable.
   */
  public abstract int getBERLength();

  public int getBERPayloadLength() {
    return getBERLength();
  }

  /**
   * Decodes a <code>Variable</code> from an <code>InputStream</code>.
   * @param inputStream
   *    an <code>InputStream</code> containing a BER encoded byte stream.
   * @throws IOException
   *    if the stream could not be decoded by using BER rules.
   */
  public abstract void decodeBER(BERInputStream inputStream) throws IOException;

  /**
   * Encodes a <code>Variable</code> to an <code>OutputStream</code>.
   * @param outputStream
   *    an <code>OutputStream</code>.
   * @throws IOException
   *    if an error occurs while writing to the stream.
   */
  public abstract void encodeBER(OutputStream outputStream) throws IOException;

  /**
   * Creates a <code>Variable</code> from a BER encoded <code>InputStream</code>.
   * Subclasses of <code>Variable</code> are registered using the properties file
   * <code>smisyntaxes.properties</code> in this package. The properties are
   * read when this method is called first.
   *
   * @param inputStream
   *    an <code>BERInputStream</code> containing a BER encoded byte stream.
   * @return
   *    an instance of a subclass of <code>Variable</code>.
   * @throws IOException
   */
  public static Variable createFromBER(BERInputStream inputStream) throws
      IOException {
    if (!inputStream.markSupported()) {
      throw new IOException(
          "InputStream for decoding a Variable must support marks");
    }
    if (SNMP4JSettings.isExtensibilityEnabled() &&
        (registeredSyntaxes == null)) {
      registerSyntaxes();
    }
    inputStream.mark(2);
    int type = inputStream.read();
    Variable variable;
    if (SNMP4JSettings.isExtensibilityEnabled()) {
      Class c = (Class) registeredSyntaxes.get(new Integer(type));
      if (c == null) {
        throw new IOException("Encountered unsupported variable syntax: " +
                              type);
      }
      try {
        variable = (Variable) c.newInstance();
      }
      catch (IllegalAccessException aex) {
        throw new IOException("Could not access variable syntax class for: " +
                              c.getName());
      }
      catch (InstantiationException iex) {
        throw new IOException(
            "Could not instantiate variable syntax class for: " +
            c.getName());
      }
    }
    else {
      variable = createVariable(type);
    }
    inputStream.reset();
    variable.decodeBER(inputStream);
    return variable;
  }

  private static Variable createVariable(int smiSyntax) {
    switch (smiSyntax) {
      case SMIConstants.SYNTAX_OBJECT_IDENTIFIER: {
        return new OID();
      }
      case SMIConstants.SYNTAX_INTEGER: {
        return new Integer32();
      }
      case SMIConstants.SYNTAX_OCTET_STRING: {
        return new OctetString();
      }
      case SMIConstants.SYNTAX_GAUGE32: {
        return new Gauge32();
      }
      case SMIConstants.SYNTAX_COUNTER32: {
        return new Counter32();
      }
      case SMIConstants.SYNTAX_COUNTER64: {
        return new Counter64();
      }
      case SMIConstants.SYNTAX_NULL: {
        return new Null();
      }
      case SMIConstants.SYNTAX_TIMETICKS: {
        return new TimeTicks();
      }
      case SMIConstants.EXCEPTION_END_OF_MIB_VIEW: {
        return new Null(SMIConstants.EXCEPTION_END_OF_MIB_VIEW);
      }
      case SMIConstants.EXCEPTION_NO_SUCH_INSTANCE: {
        return new Null(SMIConstants.EXCEPTION_NO_SUCH_INSTANCE);
      }
      case SMIConstants.EXCEPTION_NO_SUCH_OBJECT: {
        return new Null(SMIConstants.EXCEPTION_NO_SUCH_OBJECT);
      }
      case SMIConstants.SYNTAX_OPAQUE: {
        return new Opaque();
      }
      case SMIConstants.SYNTAX_IPADDRESS: {
        return new IpAddress();
      }
      default: {
        throw new IllegalArgumentException("Unsupported variable syntax: " +
                                           smiSyntax);
      }
    }
  }

  /**
   * Creates a <code>Variable</code> from the supplied SMI syntax identifier.
   * Subclasses of <code>Variable</code> are registered using the properties
   * file <code>smisyntaxes.properties</code> in this package. The properties
   * are read when this method is called for the first time.
   *
   * @param smiSyntax
   *    an SMI syntax identifier of the registered types, which is typically
   *    defined by {@link SMIConstants}.
   * @return
   *    a <code>Variable</code> variable instance of the supplied SMI syntax.
   */
  public static Variable createFromSyntax(int smiSyntax) {
    if (!SNMP4JSettings.isExtensibilityEnabled()) {
      return createVariable(smiSyntax);
    }
    if (registeredSyntaxes == null) {
      registerSyntaxes();
    }
    Class c = (Class) registeredSyntaxes.get(new Integer(smiSyntax));
    if (c == null) {
      throw new IllegalArgumentException("Unsupported variable syntax: " +

⌨️ 快捷键说明

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