001: /*_############################################################################
002: _##
003: _## SNMP4J - VariableBinding.java
004: _##
005: _## Copyright (C) 2003-2008 Frank Fock and Jochen Katz (SNMP4J.org)
006: _##
007: _## Licensed under the Apache License, Version 2.0 (the "License");
008: _## you may not use this file except in compliance with the License.
009: _## You may obtain a copy of the License at
010: _##
011: _## http://www.apache.org/licenses/LICENSE-2.0
012: _##
013: _## Unless required by applicable law or agreed to in writing, software
014: _## distributed under the License is distributed on an "AS IS" BASIS,
015: _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: _## See the License for the specific language governing permissions and
017: _## limitations under the License.
018: _##
019: _##########################################################################*/
020:
021: package org.snmp4j.smi;
022:
023: import java.io.Serializable;
024: import org.snmp4j.asn1.*;
025: import java.io.IOException;
026: import java.io.OutputStream;
027:
028: /**
029: * A <code>VariableBinding</code> is an association of a object instance
030: * identifier ({@link OID}) and the instance's value ({@link Variable}).
031: *
032: * @author Frank Fock
033: * @version 1.9
034: */
035: public class VariableBinding implements Serializable, BERSerializable,
036: Cloneable {
037:
038: private static final long serialVersionUID = 1032709950031514113L;
039:
040: private OID oid;
041: private Variable variable;
042:
043: /**
044: * Creates a variable binding with a zero length OID and a {@link Null} value.
045: */
046: public VariableBinding() {
047: oid = new OID();
048: this .variable = new Null();
049: }
050:
051: /**
052: * Creates a variable binding with the supplied object instance identifier
053: * and a {@link Null} value.
054: * @param oid
055: * the OID for the new variable binding.
056: */
057: public VariableBinding(OID oid) {
058: setOid(oid);
059: this .variable = new Null();
060: }
061:
062: /**
063: * Creates a variable binding with the supplied OID and value.
064: * @param oid
065: * the OID for the new variable binding (must not be <code>null</code>).
066: * @param variable
067: * the value for the new variable binding (must not be <code>null</code>).
068: */
069: public VariableBinding(OID oid, Variable variable) {
070: setOid(oid);
071: setVariable(variable);
072: }
073:
074: /**
075: * Gets the object instance identifier of the variable binding.
076: * @return
077: * an <code>OID</code>.
078: */
079: public OID getOid() {
080: return oid;
081: }
082:
083: /**
084: * Sets the object instance identifier for the variable binding.
085: * @param oid
086: * an OID (must not be <code>null</code>) that is cloned when added to
087: * this binding.
088: */
089: public void setOid(OID oid) {
090: if (oid == null) {
091: throw new IllegalArgumentException(
092: "OID of a VariableBinding must not be null");
093: }
094: this .oid = (OID) oid.clone();
095: }
096:
097: /**
098: * Sets the value of the variable binding.
099: *
100: * @param variable
101: * a <code>Variable</code> (must not be <code>null</code>) that is cloned
102: * when added to this binding.
103: */
104: public void setVariable(Variable variable) {
105: if (variable == null) {
106: throw new IllegalArgumentException(
107: "Variable of a VariableBinding must not be null");
108: }
109: this .variable = (Variable) variable.clone();
110: }
111:
112: /**
113: * Gets the value of the variable binding.
114: * @return
115: * a <code>Variable</code> instance.
116: */
117: public Variable getVariable() {
118: return variable;
119: }
120:
121: /**
122: * Gets the syntax of the variable bindings value.
123: * @return
124: * a SMI syntax identifier (see {@link SMIConstants}).
125: */
126: public final int getSyntax() {
127: return variable.getSyntax();
128: }
129:
130: /**
131: * Returns whether the variable bindings value has an exception syntax.
132: * @see Variable
133: * @return
134: * <code>true</code> if the syntax of this variable is an instance of
135: * <code>Null</code> and its syntax equals one of the following:
136: * <UL>
137: * <LI>{@link SMIConstants#EXCEPTION_NO_SUCH_OBJECT}</LI>
138: * <LI>{@link SMIConstants#EXCEPTION_NO_SUCH_INSTANCE}</LI>
139: * <LI>{@link SMIConstants#EXCEPTION_END_OF_MIB_VIEW}</LI>
140: * </UL>
141: */
142: public boolean isException() {
143: return variable.isException();
144: }
145:
146: public final int getBERPayloadLength() {
147: return oid.getBERLength() + variable.getBERLength();
148: }
149:
150: public final int getBERLength() {
151: int length = getBERPayloadLength();
152: // add type byte and length of length
153: length += BER.getBERLengthOfLength(length) + 1;
154: return length;
155: }
156:
157: public final void decodeBER(BERInputStream inputStream)
158: throws IOException {
159: BER.MutableByte type = new BER.MutableByte();
160: int length = BER.decodeHeader(inputStream, type);
161: long startPos = inputStream.getPosition();
162: if (type.getValue() != BER.SEQUENCE) {
163: throw new IOException("Invalid sequence encoding: "
164: + type.getValue());
165: }
166: oid.decodeBER(inputStream);
167: variable = AbstractVariable.createFromBER(inputStream);
168: if (BER.isCheckSequenceLength()) {
169: BER.checkSequenceLength(length, (int) (inputStream
170: .getPosition() - startPos), this );
171: }
172: }
173:
174: public final void encodeBER(OutputStream outputStream)
175: throws IOException {
176: int length = getBERPayloadLength();
177: BER.encodeHeader(outputStream, BER.SEQUENCE, length);
178: oid.encodeBER(outputStream);
179: variable.encodeBER(outputStream);
180: }
181:
182: /**
183: * Gets a string representation of this variable binding.
184: * @return
185: * a string of the form <code><OID> = <Variable></code>.
186: */
187: public String toString() {
188: return oid.toString() + " = " + variable;
189: }
190:
191: public Object clone() {
192: return new VariableBinding((OID) oid.clone(),
193: (Variable) variable.clone());
194: }
195: }
|