📄 abstractvariable.java
字号:
smiSyntax);
}
try {
Variable variable = (Variable) c.newInstance();
return variable;
}
catch (IllegalAccessException aex) {
throw new RuntimeException("Could not access variable syntax class for: " +
c.getName());
}
catch (InstantiationException iex) {
throw new RuntimeException(
"Could not instantiate variable syntax class for: " +
c.getName());
}
}
/**
* Register SNMP syntax classes from a properties file. The registered
* syntaxes are used by the {@link createFromBER} method to type-safe
* instantiate sub-classes from <code>Variable</code> from an BER encoded
* <code>InputStream</code>.
*/
private synchronized static void registerSyntaxes() {
String syntaxes = System.getProperty(SMISYNTAXES_PROPERTIES,
SMISYNTAXES_PROPERTIES_DEFAULT);
InputStream is = Variable.class.getResourceAsStream(syntaxes);
if (is == null) {
throw new InternalError("Could not read '" + syntaxes +
"' from classpath!");
}
Properties props = new Properties();
try {
props.load(is);
Hashtable regSyntaxes = new Hashtable(props.size());
for (Enumeration en = props.propertyNames(); en.hasMoreElements(); ) {
String id = (String) en.nextElement();
String className = props.getProperty(id);
try {
Class c = Class.forName(className);
regSyntaxes.put(new Integer(id), c);
}
catch (ClassNotFoundException cnfe) {
logger.error(cnfe);
}
}
// atomic syntax registration
registeredSyntaxes = regSyntaxes;
}
catch (IOException iox) {
String txt = "Could not read '" + syntaxes + "': " +
iox.getMessage();
logger.error(txt);
throw new InternalError(txt);
}
finally {
try {
is.close();
}
catch (IOException ex) {
logger.warn(ex);
}
}
}
/**
* Gets the ASN.1 syntax identifier value of this SNMP variable.
* @return
* an integer value < 128 for regular SMI objects and a value >= 128
* for exception values like noSuchObject, noSuchInstance, and
* endOfMibView.
*/
public abstract int getSyntax();
/**
* Checks whether this variable represents an exception like
* noSuchObject, noSuchInstance, and endOfMibView.
* @return
* <code>true</code> if the syntax of this variable is an instance of
* <code>Null</code> and its syntax equals one of the following:
* <UL>
* <LI>{@link SMIConstants#EXCEPTION_NO_SUCH_OBJECT}</LI>
* <LI>{@link SMIConstants#EXCEPTION_NO_SUCH_INSTANCE}</LI>
* <LI>{@link SMIConstants#EXCEPTION_END_OF_MIB_VIEW}</LI>
* </UL>
*/
public boolean isException() {
return Null.isExceptionSyntax(getSyntax());
}
/**
* Gets a string representation of the variable.
* @return
* a string representation of the variable's value.
*/
public abstract String toString();
/**
* Returns an integer representation of this variable if
* such a representation exists.
* @return
* an integer value (if the native representation of this variable
* would be a long, then the long value will be casted to int).
* @throws UnsupportedOperationException if an integer representation
* does not exists for this Variable.
* @since 1.7
*/
public abstract int toInt();
/**
* Returns a long representation of this variable if
* such a representation exists.
* @return
* a long value.
* @throws UnsupportedOperationException if a long representation
* does not exists for this Variable.
* @since 1.7
*/
public abstract long toLong();
public abstract Object clone();
/**
* Gets a textual description of the supplied syntax type.
* @param syntax
* the BER code of the syntax.
* @return
* a textual description like 'Integer32' for <code>syntax</code>
* as used in the Structure of Management Information (SMI) modules.
* '?' is returned if the supplied syntax is unknown.
*/
public static String getSyntaxString(int syntax) {
switch (syntax) {
case BER.INTEGER:
return "Integer32";
case BER.BITSTRING:
return "BIT STRING";
case BER.OCTETSTRING:
return "OCTET STRING";
case BER.OID:
return "OBJECT IDENTIFIER";
case BER.TIMETICKS:
return "TimeTicks";
case BER.COUNTER:
return "Counter";
case BER.COUNTER64:
return "Counter64";
case BER.ENDOFMIBVIEW:
return "EndOfMibView";
case BER.GAUGE32:
return "Gauge";
case BER.IPADDRESS:
return "IpAddress";
case BER.NOSUCHINSTANCE:
return "NoSuchInstance";
case BER.NOSUCHOBJECT:
return "NoSuchObject";
case BER.NULL:
return "Null";
case BER.OPAQUE:
return "Opaque";
}
return "?";
}
/**
* Gets a textual description of this Variable.
* @return
* a textual description like 'Integer32'
* as used in the Structure of Management Information (SMI) modules.
* '?' is returned if the syntax is unknown.
* @since 1.7
*/
public final String getSyntaxString() {
return getSyntaxString(getSyntax());
}
/**
* Returns the BER syntax ID for the supplied syntax string (as returned
* by {@link #getSyntaxString(int)}).
* @param syntaxString
* the textual representation of the syntax.
* @return
* the corresponding BER ID.
* @since 1.6
*/
public static int getSyntaxFromString(String syntaxString) {
for (int i=0; i<SYNTAX_NAME_MAPPING.length; i++) {
if (SYNTAX_NAME_MAPPING[i][0].equals(syntaxString)) {
return ((Integer)SYNTAX_NAME_MAPPING[i][1]).intValue();
}
}
return BER.NULL;
}
/**
* Converts the value of this <code>Variable</code> to a (sub-)index
* value.
* @param impliedLength
* specifies if the sub-index has an implied length. This parameter applies
* to variable length variables only (e.g. {@link OctetString} and
* {@link OID}). For other variables it has no effect.
* @return
* an OID that represents this value as an (sub-)index.
* @throws UnsupportedOperationException
* if this variable cannot be used in an index.
* @since 1.7
*/
public abstract OID toSubIndex(boolean impliedLength);
/**
* Sets the value of this <code>Variable</code> from the supplied (sub-)index.
* @param subIndex
* the sub-index OID.
* @param impliedLength
* specifies if the sub-index has an implied length. This parameter applies
* to variable length variables only (e.g. {@link OctetString} and
* {@link OID}). For other variables it has no effect.
* @throws UnsupportedOperationException
* if this variable cannot be used in an index.
* @since 1.7
*/
public abstract void fromSubIndex(OID subIndex, boolean impliedLength);
/**
* Indicates whether this variable is dynamic, which means that it might
* change its value while it is being (BER) serialized. If a variable is
* dynamic, it will be cloned on-the-fly when it is added to a {@link PDU}
* with {@link PDU#add(VariableBinding)}. By cloning the value, it is
* ensured that there are no inconsistent changes between determining the
* length with {@link #getBERLength()} for encoding enclosing SEQUENCES and
* the actual encoding of the Variable itself with {@link #encodeBER}.
*
* @return
* <code>false</code> by default. Derived classes may override this
* if implementing dynamic {@link Variable} instances.
* @since 1.8
*/
public boolean isDynamic() {
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -