KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > smi > VariableBinding


1 /*_############################################################################
2   _##
3   _## SNMP4J - VariableBinding.java
4   _##
5   _## Copyright 2003-2007 Frank Fock and Jochen Katz (SNMP4J.org)
6   _##
7   _## Licensed under the Apache License, Version 2.0 (the "License");
8   _## you may not use this file except in compliance with the License.
9   _## You may obtain a copy of the License at
10   _##
11   _## http://www.apache.org/licenses/LICENSE-2.0
12   _##
13   _## Unless required by applicable law or agreed to in writing, software
14   _## distributed under the License is distributed on an "AS IS" BASIS,
15   _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   _## See the License for the specific language governing permissions and
17   _## limitations under the License.
18   _##
19   _##########################################################################*/

20
21 package org.snmp4j.smi;
22
23 import java.io.Serializable JavaDoc;
24 import org.snmp4j.asn1.*;
25 import java.io.IOException JavaDoc;
26 import java.io.OutputStream JavaDoc;
27
28 /**
29  * A <code>VariableBinding</code> is an association of a object instance
30  * identifier ({@link OID}) and the instance's value ({@link Variable}).
31  *
32  * @author Frank Fock
33  * @version 1.0
34  */

35 public class VariableBinding
36     implements Serializable JavaDoc, BERSerializable, Cloneable JavaDoc {
37
38   private static final long serialVersionUID = 1032709950031514113L;
39
40   private OID oid;
41   private Variable variable;
42
43
44   /**
45    * Creates a variable binding with a zero length OID and a {@link Null} value.
46    */

47   public VariableBinding() {
48     oid = new OID();
49     this.variable = new Null();
50   }
51
52   /**
53    * Creates a variable binding with the supplied object instance identifier
54    * and a {@link Null} value.
55    * @param oid
56    * the OID for the new variable binding.
57    */

58   public VariableBinding(OID oid) {
59     setOid(oid);
60     this.variable = new Null();
61   }
62
63   /**
64    * Creates a variable binding with the supplied OID and value.
65    * @param oid
66    * the OID for the new variable binding (must not be <code>null</code>).
67    * @param variable
68    * the value for the new variable binding (must not be <code>null</code>).
69    */

70   public VariableBinding(OID oid, Variable variable) {
71     setOid(oid);
72     setVariable(variable);
73   }
74
75   /**
76    * Gets the object instance identifier of the variable binding.
77    * @return
78    * an <code>OID</code>.
79    */

80   public OID getOid() {
81     return oid;
82   }
83
84   /**
85    * Sets the object instance identifier for the variable binding.
86    * @param oid
87    * an OID (must not be <code>null</code>).
88    */

89   public void setOid(OID oid) {
90     if (oid == null) {
91       throw new IllegalArgumentException JavaDoc(
92           "OID of a VariableBinding must not be null");
93     }
94     this.oid = oid;
95   }
96
97   /**
98    * Sets the value of the variable binding.
99    * @param variable
100    * a <code>Variable</code> (must not be <code>null</code>).
101    */

102   public void setVariable(Variable variable) {
103     if (variable == null) {
104       throw new IllegalArgumentException JavaDoc(
105           "Variable of a VariableBinding must not be null");
106     }
107     this.variable = variable;
108   }
109
110   /**
111    * Gets the value of the variable binding.
112    * @return
113    * a <code>Variable</code> instance.
114    */

115   public Variable getVariable() {
116     return variable;
117   }
118
119   /**
120    * Gets the syntax of the variable bindings value.
121    * @return
122    * a SMI syntax identifier (see {@link SMIConstants}).
123    */

124   public final int getSyntax() {
125     return variable.getSyntax();
126   }
127
128   /**
129    * Returns whether the variable bindings value has an exception syntax.
130    * @see Variable
131    * @return
132    * <code>true</code> if the syntax of this variable is an instance of
133    * <code>Null</code> and its syntax equals one of the following:
134    * <UL>
135    * <LI>{@link SMIConstants#EXCEPTION_NO_SUCH_OBJECT}</LI>
136    * <LI>{@link SMIConstants#EXCEPTION_NO_SUCH_INSTANCE}</LI>
137    * <LI>{@link SMIConstants#EXCEPTION_END_OF_MIB_VIEW}</LI>
138    * </UL>
139    */

140   public boolean isException() {
141     return variable.isException();
142   }
143
144   public final int getBERPayloadLength() {
145     return oid.getBERLength() + variable.getBERLength();
146   }
147
148   public final int getBERLength() {
149     int length = getBERPayloadLength();
150     // add type byte and length of length
151
length += BER.getBERLengthOfLength(length) + 1;
152     return length;
153   }
154
155   public final void decodeBER(BERInputStream inputStream) throws IOException JavaDoc {
156     BER.MutableByte type = new BER.MutableByte();
157     int length = BER.decodeHeader(inputStream, type);
158     long startPos = inputStream.getPosition();
159     if (type.getValue() != BER.SEQUENCE) {
160       throw new IOException JavaDoc("Invalid sequence encoding: " + type.getValue());
161     }
162     oid.decodeBER(inputStream);
163     variable = AbstractVariable.createFromBER(inputStream);
164     if (BER.isCheckSequenceLength()) {
165       BER.checkSequenceLength(length,
166                               (int) (inputStream.getPosition() - startPos),
167                               this);
168     }
169   }
170
171   public final void encodeBER(OutputStream JavaDoc outputStream) throws IOException JavaDoc {
172     int length = getBERPayloadLength();
173     BER.encodeHeader(outputStream, BER.SEQUENCE,
174                      length);
175     oid.encodeBER(outputStream);
176     variable.encodeBER(outputStream);
177   }
178
179   /**
180    * Gets a string representation of this variable binding.
181    * @return
182    * a string of the form <code>&lt;OID&gt; = &lt;Variable&gt;</code>.
183    */

184   public String JavaDoc toString() {
185     return oid.toString()+" = "+variable;
186   }
187
188   public Object JavaDoc clone() {
189     return new VariableBinding((OID)oid.clone(), (Variable)variable.clone());
190   }
191 }
192
193
Popular Tags