KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jmx > snmp > SnmpPduFactoryBER


1 /*
2  * @(#)file SnmpPduFactoryBER.java
3  * @(#)author Sun Microsystems, Inc.
4  * @(#)version 3.30
5  * @(#)date 08/02/09
6  *
7  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
8  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
9  *
10  */

11
12
13 package com.sun.jmx.snmp;
14
15
16 // java imports
17
//
18
import java.io.Serializable JavaDoc;
19
20 // jmx import
21
//
22
import com.sun.jmx.snmp.SnmpPduFactory;
23 import com.sun.jmx.snmp.SnmpMessage;
24 import com.sun.jmx.snmp.SnmpPduPacket;
25 import com.sun.jmx.snmp.SnmpPdu;
26 import com.sun.jmx.snmp.SnmpMsg;
27 import com.sun.jmx.snmp.SnmpStatusException;
28 import com.sun.jmx.snmp.SnmpTooBigException;
29 import com.sun.jmx.snmp.SnmpDefinitions;
30
31 // SNMP Runtime import
32
//
33
import com.sun.jmx.snmp.SnmpV3Message;
34
35 /**
36  * Default implementation of the {@link com.sun.jmx.snmp.SnmpPduFactory SnmpPduFactory} interface.
37  * <BR>It uses the BER (basic encoding rules) standardized encoding scheme associated with ASN.1.
38  * <P>
39  * This implementation of the <CODE>SnmpPduFactory</CODE> is very
40  * basic: it simply calls encoding and decoding methods from
41  * {@link com.sun.jmx.snmp.SnmpMsg}.
42  * <BLOCKQUOTE>
43  * <PRE>
44  * public SnmpPdu decodeSnmpPdu(SnmpMsg msg)
45  * throws SnmpStatusException {
46  * return msg.decodeSnmpPdu() ;
47  * }
48  *
49  * public SnmpMsg encodeSnmpPdu(SnmpPdu pdu, int maxPktSize)
50  * throws SnmpStatusException, SnmpTooBigException {
51  * SnmpMsg result = new SnmpMessage() ; // for SNMP v1/v2
52  * <I>or</I>
53  * SnmpMsg result = new SnmpV3Message() ; // for SNMP v3
54  * result.encodeSnmpPdu(pdu, maxPktSize) ;
55  * return result ;
56  * }
57  * </PRE>
58  * </BLOCKQUOTE>
59  * To implement your own object, you can implement <CODE>SnmpPduFactory</CODE>
60  * or extend <CODE>SnmpPduFactoryBER</CODE>.
61  * <p><b>This API is a Sun Microsystems internal API and is subject
62  * to change without notice.</b></p>
63  */

64
65 public class SnmpPduFactoryBER implements SnmpPduFactory, Serializable JavaDoc {
66    /**
67      * Calls {@link com.sun.jmx.snmp.SnmpMsg#decodeSnmpPdu SnmpMsg.decodeSnmpPdu}
68      * on the specified message and returns the resulting <CODE>SnmpPdu</CODE>.
69      *
70      * @param msg The SNMP message to be decoded.
71      * @return The resulting SNMP PDU packet.
72      * @exception SnmpStatusException If the encoding is invalid.
73      *
74      * @since 1.5
75      */

76     public SnmpPdu decodeSnmpPdu(SnmpMsg msg) throws SnmpStatusException {
77     return msg.decodeSnmpPdu();
78     }
79
80     /**
81      * Encodes the specified <CODE>SnmpPdu</CODE> and
82      * returns the resulting <CODE>SnmpMsg</CODE>. If this
83      * method returns null, the specified <CODE>SnmpPdu</CODE>
84      * will be dropped and the current SNMP request will be
85      * aborted.
86      *
87      * @param p The <CODE>SnmpPdu</CODE> to be encoded.
88      * @param maxDataLength The size limit of the resulting encoding.
89      * @return Null or a fully encoded <CODE>SnmpMsg</CODE>.
90      * @exception SnmpStatusException If <CODE>pdu</CODE> contains
91      * illegal values and cannot be encoded.
92      * @exception SnmpTooBigException If the resulting encoding does not
93      * fit into <CODE>maxPktSize</CODE> bytes.
94      *
95      * @since 1.5
96      */

97     public SnmpMsg encodeSnmpPdu(SnmpPdu p, int maxDataLength)
98     throws SnmpStatusException, SnmpTooBigException {
99     switch(p.version) {
100     case SnmpDefinitions.snmpVersionOne:
101     case SnmpDefinitions.snmpVersionTwo: {
102         SnmpMessage result = new SnmpMessage();
103         result.encodeSnmpPdu((SnmpPduPacket) p, maxDataLength);
104         return result;
105     }
106     case SnmpDefinitions.snmpVersionThree: {
107         SnmpV3Message result = new SnmpV3Message();
108         result.encodeSnmpPdu(p, maxDataLength);
109         return result;
110     }
111     default:
112         return null;
113     }
114     }
115 }
116
117
Popular Tags