KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snmp > SNMPSequence


1 /*
2  * SNMP Package
3  *
4  * Copyright (C) 2004, Jonathan Sevy <jsevy@mcs.drexel.edu>
5  *
6  * This is free software. Redistribution and use in source and binary forms, with
7  * or without modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  * derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
19  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
21  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
23  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */

29
30
31 package snmp;
32
33 import java.util.*;
34 import java.io.*;
35
36
37 /**
38 * One of the most important SNMP classes. Represents a sequence of other SNMP data types.
39 * Virtually all compound structures are subclasses of SNMPSequence - for example, the
40 * top-level SNMPMessage, and the SNMPPDU it contains, are both just specializations of
41 * SNMPSequence. Sequences are frequently nested within other sequences.
42 */

43
44 public class SNMPSequence extends SNMPObject
45 {
46     protected Vector sequence; // Vector of whatever is in sequence
47

48     protected byte tag = SNMPBERCodec.SNMPSEQUENCE;
49         
50     
51     /**
52     * Create a new empty sequence.
53     */

54     
55     public SNMPSequence()
56     {
57         sequence = new Vector();
58     }
59     
60     
61     
62     
63     /**
64     * Create a new SNMP sequence from the supplied Vector of SNMPObjects.
65     * @throws SNMPBadValueException Thrown if non-SNMP object supplied in Vector v.
66     */

67     
68     public SNMPSequence(Vector v)
69         throws SNMPBadValueException
70     {
71         
72         Enumeration e = v.elements();
73         
74         while (e.hasMoreElements())
75         {
76             if (!(e.nextElement() instanceof SNMPObject))
77                 throw new SNMPBadValueException("Non-SNMPObject supplied to SNMPSequence.");
78         }
79         
80         
81         sequence = v;
82     }
83     
84     
85     
86     
87     /**
88     * Construct an SNMPMessage from a received ASN.1 byte representation.
89     * @throws SNMPBadValueException Indicates invalid SNMP sequence encoding supplied.
90     */

91     
92     protected SNMPSequence(byte[] enc)
93         throws SNMPBadValueException
94     {
95         extractFromBEREncoding(enc);
96     }
97     
98     
99     
100     
101     
102     /**
103     * Returns a Vector containing the SNMPObjects in the sequence.
104     */

105     
106     public Object JavaDoc getValue()
107     {
108         return sequence;
109     }
110     
111     
112     
113     
114     
115     /**
116     * Used to set the contained SNMP objects from a supplied Vector.
117     * @throws SNMPBadValueException Indicates an incorrect object type supplied, or that the supplied
118     * Vector contains non-SNMPObjects.
119     */

120     
121     public void setValue(Object JavaDoc newSequence)
122         throws SNMPBadValueException
123     {
124         if (newSequence instanceof Vector)
125         {
126             
127             // check that all objects in vector are SNMPObjects
128

129             Enumeration e = ((Vector)newSequence).elements();
130             
131             while (e.hasMoreElements())
132             {
133                 if (!(e.nextElement() instanceof SNMPObject))
134                     throw new SNMPBadValueException("Non-SNMPObject supplied to SNMPSequence.");
135             }
136             
137             
138             this.sequence = (Vector)newSequence;
139         }
140         else
141             throw new SNMPBadValueException(" Sequence: bad object supplied to set value ");
142     }
143     
144     
145     
146     /**
147     * Return the number of SNMPObjects contained in the sequence.
148     */

149     
150     public int size()
151     {
152         return sequence.size();
153     }
154     
155     
156     
157     /**
158     * Add the SNMP object to the end of the sequence.
159     * @throws SNMPBadValueException Relevant only in subclasses
160     */

161     
162     public void addSNMPObject(SNMPObject newObject)
163         throws SNMPBadValueException
164     {
165         sequence.insertElementAt(newObject, sequence.size());
166     }
167     
168     
169     
170     
171     /**
172     * Insert the SNMP object at the specified position in the sequence.
173     * @throws SNMPBadValueException Relevant only in subclasses
174     */

175     
176     public void insertSNMPObjectAt(SNMPObject newObject, int index)
177         throws SNMPBadValueException
178     {
179         sequence.insertElementAt(newObject, index);
180     }
181     
182     
183     
184     
185     
186     /**
187     * Return the SNMP object at the specified index. Indices are 0-based.
188     */

189     
190     public SNMPObject getSNMPObjectAt(int index)
191     {
192         return (SNMPObject)(sequence.elementAt(index));
193     }
194     
195     
196     
197     
198     /**
199     * Return the BER encoding for the sequence.
200     */

201     
202     protected byte[] getBEREncoding()
203     {
204         ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
205         
206             
207         // recursively write contents of Vector
208
byte[] data = encodeVector();
209         
210         // calculate encoding for length of data
211
byte[] len = SNMPBERCodec.encodeLength(data.length);
212         
213         // encode T,L,V info
214
outBytes.write(tag);
215         outBytes.write(len, 0, len.length);
216         outBytes.write(data, 0, data.length);
217         
218         return outBytes.toByteArray();
219     }
220     
221     
222     
223     private byte[] encodeVector()
224     {
225         ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
226         
227         int numElements = sequence.size();
228         for (int i = 0; i < numElements; ++i)
229         {
230             byte[] nextBytes = ((SNMPObject)(sequence.elementAt(i))).getBEREncoding();
231             outBytes.write(nextBytes, 0, nextBytes.length);
232         }
233         
234         return outBytes.toByteArray();
235     }
236     
237     
238     
239     protected void extractFromBEREncoding(byte[] enc)
240         throws SNMPBadValueException
241     {
242         Vector newVector = new Vector();
243         
244         int totalLength = enc.length;
245         int position = 0;
246         
247         while (position < totalLength)
248         {
249             SNMPTLV nextTLV = SNMPBERCodec.extractNextTLV(enc, position);
250             newVector.insertElementAt(SNMPBERCodec.extractEncoding(nextTLV), newVector.size());
251             position += nextTLV.totalLength;
252         }
253         
254         sequence = newVector;
255         
256     }
257     
258     
259     
260     
261     
262     /**
263     * Return a sequence of representations of the contained objects, separated by spaces
264     * and enclosed in parentheses.
265     */

266     
267     public String JavaDoc toString()
268     {
269         StringBuffer JavaDoc valueStringBuffer = new StringBuffer JavaDoc("(");
270         
271         
272         for (int i = 0; i < sequence.size(); ++i)
273         {
274             valueStringBuffer.append(" ");
275             valueStringBuffer.append(((SNMPObject)sequence.elementAt(i)).toString());
276             valueStringBuffer.append(" ");
277         }
278         
279         valueStringBuffer.append(")");
280         return valueStringBuffer.toString();
281     }
282     
283     
284     
285 }
Popular Tags