KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > ScopedPDU


1 /*_############################################################################
2   _##
3   _## SNMP4J - ScopedPDU.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;
22
23 import org.snmp4j.smi.OctetString;
24 import org.snmp4j.asn1.BER;
25 import java.io.OutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import org.snmp4j.asn1.BERInputStream;
28 import org.snmp4j.asn1.BER.MutableByte;
29
30 /**
31  * The <code>ScopedPDU</code> class represents a SNMPv3 scoped PDU.
32  *
33  * @author Frank Fock
34  * @version 1.0
35  */

36
37 public class ScopedPDU extends PDU {
38
39   private static final long serialVersionUID = 4343157159110407279L;
40
41   private OctetString contextEngineID = new OctetString();
42   private OctetString contextName = new OctetString();
43
44   /**
45    * Create a empty ScopedPDU.
46    */

47   public ScopedPDU() {
48   }
49
50   /**
51    * Copy constructor.
52    * @param other
53    * a <code>ScopedPDU</code> instance.
54    */

55   public ScopedPDU(ScopedPDU other) {
56     super(other);
57     this.contextEngineID = (OctetString) other.contextEngineID.clone();
58     this.contextName = (OctetString) other.contextName.clone();
59   }
60
61   /**
62    * Sets the context engine ID field of the scoped PDU.
63    * @param contextEngineID
64    * an <code>OctetString</code> instance (must not be <code>null</code>).
65    * @throws NullPointerException if contextEngineID == null
66    */

67   public void setContextEngineID(OctetString contextEngineID) {
68     if (contextEngineID == null) {
69       throw new NullPointerException JavaDoc("Context engine ID must not be null");
70     }
71     this.contextEngineID = contextEngineID;
72   }
73
74   /**
75    * Gets the context engine ID of this scoped PDU.
76    * @return
77    * an <code>OctetString</code> instance.
78    */

79   public OctetString getContextEngineID() {
80     return contextEngineID;
81   }
82
83   /**
84    * Sets the context name field of this scoped PDU.
85    * @param contextName
86    * an <code>OctetString</code> instance (must not be <code>null</code>).
87    */

88   public void setContextName(OctetString contextName) {
89     if (contextName == null) {
90       throw new NullPointerException JavaDoc("Context name must not be null");
91     }
92     this.contextName = contextName;
93   }
94
95   /**
96    * Gets the context name of this scoped PDU.
97    * @return
98    * an <code>OctetString</code> instance.
99    */

100   public OctetString getContextName() {
101     return contextName;
102   }
103
104   public int getBERLength() {
105     int length = getBERPayloadLength();
106     length += 1 + BER.getBERLengthOfLength(length);
107     return length;
108   }
109
110   public int getBERPayloadLength() {
111     int length = super.getBERLength();
112     int cid = (contextEngineID == null) ? 0 : contextEngineID.length();
113     int cn = (contextName == null) ? 0 : contextName.length();
114     length += BER.getBERLengthOfLength(cid) + 1
115         + cid + BER.getBERLengthOfLength(cn) + 1 + cn;
116     return length;
117   }
118
119   public void encodeBER(OutputStream JavaDoc outputStream) throws IOException JavaDoc {
120     BER.encodeHeader(outputStream, BER.SEQUENCE, getBERPayloadLength());
121     contextEngineID.encodeBER(outputStream);
122     contextName.encodeBER(outputStream);
123     super.encodeBER(outputStream);
124   }
125
126
127
128   public Object JavaDoc clone() {
129     return new ScopedPDU(this);
130   }
131
132   /**
133    * Decodes a <code>Variable</code> from an <code>InputStream</code>.
134    *
135    * @param inputStream an <code>InputStream</code> containing a BER encoded
136    * byte stream.
137    * @throws IOException
138    */

139   public void decodeBER(BERInputStream inputStream) throws IOException JavaDoc {
140     MutableByte mutableByte = new MutableByte();
141     int length = BER.decodeHeader(inputStream, mutableByte);
142     long startPos = inputStream.getPosition();
143     contextEngineID.decodeBER(inputStream);
144     contextName.decodeBER(inputStream);
145     super.decodeBER(inputStream);
146     if (BER.isCheckSequenceLength()) {
147       BER.checkSequenceLength(length,
148                               (int) (inputStream.getPosition() - startPos),
149                               this);
150     }
151   }
152
153   /**
154    * Returns a string representation of the object.
155    *
156    * @return a string representation of the object.
157    */

158   public String JavaDoc toString() {
159     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
160     buf.append(getTypeString(type));
161     buf.append("[reqestID=");
162     buf.append(requestID);
163     buf.append(", errorStatus=");
164     buf.append(errorStatus);
165     buf.append(", errorIndex=");
166     buf.append(errorIndex);
167     buf.append(", VBS[");
168     for (int i = 0; i < variableBindings.size(); i++) {
169       buf.append(variableBindings.get(i));
170       if (i + 1 < variableBindings.size()) {
171         buf.append("; ");
172       }
173     }
174     buf.append("]]");
175     return buf.toString();
176   }
177
178 }
179
Popular Tags