Source Code Cross Referenced for SNMPSequence.java in  » JMX » jmanage » snmp » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » JMX » jmanage » snmp 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * SNMP Package
003:         *
004:         * Copyright (C) 2004, Jonathan Sevy <jsevy@mcs.drexel.edu>
005:         *
006:         * This is free software. Redistribution and use in source and binary forms, with
007:         * or without modification, are permitted provided that the following conditions
008:         * are met:
009:         *
010:         *  1. Redistributions of source code must retain the above copyright notice, this
011:         *     list of conditions and the following disclaimer.
012:         *  2. Redistributions in binary form must reproduce the above copyright notice,
013:         *     this list of conditions and the following disclaimer in the documentation 
014:         *     and/or other materials provided with the distribution.
015:         *  3. The name of the author may not be used to endorse or promote products 
016:         *     derived from this software without specific prior written permission.
017:         *
018:         * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
019:         * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
020:         * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 
021:         * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022:         * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
023:         * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
024:         * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
025:         * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
026:         * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027:         *
028:         */
029:
030:        package snmp;
031:
032:        import java.util.*;
033:        import java.io.*;
034:
035:        /**
036:         *    One of the most important SNMP classes. Represents a sequence of other SNMP data types.
037:         *    Virtually all compound structures are subclasses of SNMPSequence - for example, the 
038:         *    top-level SNMPMessage, and the SNMPPDU it contains, are both just specializations of 
039:         *    SNMPSequence. Sequences are frequently nested within other sequences.
040:         */
041:
042:        public class SNMPSequence extends SNMPObject {
043:            protected Vector sequence; // Vector of whatever is in sequence
044:
045:            protected byte tag = SNMPBERCodec.SNMPSEQUENCE;
046:
047:            /**
048:             *    Create a new empty sequence.
049:             */
050:
051:            public SNMPSequence() {
052:                sequence = new Vector();
053:            }
054:
055:            /**
056:             *    Create a new SNMP sequence from the supplied Vector of SNMPObjects.
057:             *    @throws SNMPBadValueException Thrown if non-SNMP object supplied in Vector v.
058:             */
059:
060:            public SNMPSequence(Vector v) throws SNMPBadValueException {
061:
062:                Enumeration e = v.elements();
063:
064:                while (e.hasMoreElements()) {
065:                    if (!(e.nextElement() instanceof  SNMPObject))
066:                        throw new SNMPBadValueException(
067:                                "Non-SNMPObject supplied to SNMPSequence.");
068:                }
069:
070:                sequence = v;
071:            }
072:
073:            /**
074:             *    Construct an SNMPMessage from a received ASN.1 byte representation.
075:             *    @throws SNMPBadValueException Indicates invalid SNMP sequence encoding supplied.
076:             */
077:
078:            protected SNMPSequence(byte[] enc) throws SNMPBadValueException {
079:                extractFromBEREncoding(enc);
080:            }
081:
082:            /**
083:             *    Returns a Vector containing the SNMPObjects in the sequence.
084:             */
085:
086:            public Object getValue() {
087:                return sequence;
088:            }
089:
090:            /** 
091:             *    Used to set the contained SNMP objects from a supplied Vector.
092:             *     @throws SNMPBadValueException Indicates an incorrect object type supplied, or that the supplied
093:             *    Vector contains non-SNMPObjects.
094:             */
095:
096:            public void setValue(Object newSequence)
097:                    throws SNMPBadValueException {
098:                if (newSequence instanceof  Vector) {
099:
100:                    // check that all objects in vector are SNMPObjects
101:
102:                    Enumeration e = ((Vector) newSequence).elements();
103:
104:                    while (e.hasMoreElements()) {
105:                        if (!(e.nextElement() instanceof  SNMPObject))
106:                            throw new SNMPBadValueException(
107:                                    "Non-SNMPObject supplied to SNMPSequence.");
108:                    }
109:
110:                    this .sequence = (Vector) newSequence;
111:                } else
112:                    throw new SNMPBadValueException(
113:                            " Sequence: bad object supplied to set value ");
114:            }
115:
116:            /** 
117:             *    Return the number of SNMPObjects contained in the sequence.
118:             */
119:
120:            public int size() {
121:                return sequence.size();
122:            }
123:
124:            /** 
125:             *    Add the SNMP object to the end of the sequence.
126:             *    @throws SNMPBadValueException Relevant only in subclasses
127:             */
128:
129:            public void addSNMPObject(SNMPObject newObject)
130:                    throws SNMPBadValueException {
131:                sequence.insertElementAt(newObject, sequence.size());
132:            }
133:
134:            /** 
135:             *    Insert the SNMP object at the specified position in the sequence.
136:             *    @throws SNMPBadValueException Relevant only in subclasses
137:             */
138:
139:            public void insertSNMPObjectAt(SNMPObject newObject, int index)
140:                    throws SNMPBadValueException {
141:                sequence.insertElementAt(newObject, index);
142:            }
143:
144:            /** 
145:             *    Return the SNMP object at the specified index. Indices are 0-based.
146:             */
147:
148:            public SNMPObject getSNMPObjectAt(int index) {
149:                return (SNMPObject) (sequence.elementAt(index));
150:            }
151:
152:            /** 
153:             *    Return the BER encoding for the sequence.
154:             */
155:
156:            protected byte[] getBEREncoding() {
157:                ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
158:
159:                // recursively write contents of Vector
160:                byte[] data = encodeVector();
161:
162:                // calculate encoding for length of data
163:                byte[] len = SNMPBERCodec.encodeLength(data.length);
164:
165:                // encode T,L,V info
166:                outBytes.write(tag);
167:                outBytes.write(len, 0, len.length);
168:                outBytes.write(data, 0, data.length);
169:
170:                return outBytes.toByteArray();
171:            }
172:
173:            private byte[] encodeVector() {
174:                ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
175:
176:                int numElements = sequence.size();
177:                for (int i = 0; i < numElements; ++i) {
178:                    byte[] nextBytes = ((SNMPObject) (sequence.elementAt(i)))
179:                            .getBEREncoding();
180:                    outBytes.write(nextBytes, 0, nextBytes.length);
181:                }
182:
183:                return outBytes.toByteArray();
184:            }
185:
186:            protected void extractFromBEREncoding(byte[] enc)
187:                    throws SNMPBadValueException {
188:                Vector newVector = new Vector();
189:
190:                int totalLength = enc.length;
191:                int position = 0;
192:
193:                while (position < totalLength) {
194:                    SNMPTLV nextTLV = SNMPBERCodec
195:                            .extractNextTLV(enc, position);
196:                    newVector.insertElementAt(SNMPBERCodec
197:                            .extractEncoding(nextTLV), newVector.size());
198:                    position += nextTLV.totalLength;
199:                }
200:
201:                sequence = newVector;
202:
203:            }
204:
205:            /** 
206:             *    Return a sequence of representations of the contained objects, separated by spaces
207:             *    and enclosed in parentheses.
208:             */
209:
210:            public String toString() {
211:                StringBuffer valueStringBuffer = new StringBuffer("(");
212:
213:                for (int i = 0; i < sequence.size(); ++i) {
214:                    valueStringBuffer.append(" ");
215:                    valueStringBuffer.append(((SNMPObject) sequence
216:                            .elementAt(i)).toString());
217:                    valueStringBuffer.append(" ");
218:                }
219:
220:                valueStringBuffer.append(")");
221:                return valueStringBuffer.toString();
222:            }
223:
224:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.