J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1998-2006 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | |
| 27 | package com.sun.jmx.snmp; |
| 28 | |
| 29 | |
| 30 | // java imports |
| 31 | // |
| 32 | import java.io.Serializable; |
| 33 | |
| 34 | // jmx import |
| 35 | // |
| 36 | import com.sun.jmx.snmp.SnmpPduFactory; |
| 37 | import com.sun.jmx.snmp.SnmpMessage; |
| 38 | import com.sun.jmx.snmp.SnmpPduPacket; |
| 39 | import com.sun.jmx.snmp.SnmpPdu; |
| 40 | import com.sun.jmx.snmp.SnmpMsg; |
| 41 | import com.sun.jmx.snmp.SnmpStatusException; |
| 42 | import com.sun.jmx.snmp.SnmpTooBigException; |
| 43 | import com.sun.jmx.snmp.SnmpDefinitions; |
| 44 | |
| 45 | // SNMP Runtime import |
| 46 | // |
| 47 | import com.sun.jmx.snmp.SnmpV3Message; |
| 48 | |
| 49 | /** |
| 50 | * Default implementation of the {@link com.sun.jmx.snmp.SnmpPduFactory SnmpPduFactory} interface. |
| 51 | * <BR>It uses the BER (basic encoding rules) standardized encoding scheme associated with ASN.1. |
| 52 | * <P> |
| 53 | * This implementation of the <CODE>SnmpPduFactory</CODE> is very |
| 54 | * basic: it simply calls encoding and decoding methods from |
| 55 | * {@link com.sun.jmx.snmp.SnmpMsg}. |
| 56 | * <BLOCKQUOTE> |
| 57 | * <PRE> |
| 58 | * public SnmpPdu decodeSnmpPdu(SnmpMsg msg) |
| 59 | * throws SnmpStatusException { |
| 60 | * return msg.decodeSnmpPdu() ; |
| 61 | * } |
| 62 | * |
| 63 | * public SnmpMsg encodeSnmpPdu(SnmpPdu pdu, int maxPktSize) |
| 64 | * throws SnmpStatusException, SnmpTooBigException { |
| 65 | * SnmpMsg result = new SnmpMessage() ; // for SNMP v1/v2 |
| 66 | * <I>or</I> |
| 67 | * SnmpMsg result = new SnmpV3Message() ; // for SNMP v3 |
| 68 | * result.encodeSnmpPdu(pdu, maxPktSize) ; |
| 69 | * return result ; |
| 70 | * } |
| 71 | * </PRE> |
| 72 | * </BLOCKQUOTE> |
| 73 | * To implement your own object, you can implement <CODE>SnmpPduFactory</CODE> |
| 74 | * or extend <CODE>SnmpPduFactoryBER</CODE>. |
| 75 | * <p><b>This API is a Sun Microsystems internal API and is subject |
| 76 | * to change without notice.</b></p> |
| 77 | */ |
| 78 | |
| 79 | public class SnmpPduFactoryBER implements SnmpPduFactory, Serializable { |
| 80 | private static final long serialVersionUID = -3525318344000547635L; |
| 81 | |
| 82 | /** |
| 83 | * Calls {@link com.sun.jmx.snmp.SnmpMsg#decodeSnmpPdu SnmpMsg.decodeSnmpPdu} |
| 84 | * on the specified message and returns the resulting <CODE>SnmpPdu</CODE>. |
| 85 | * |
| 86 | * @param msg The SNMP message to be decoded. |
| 87 | * @return The resulting SNMP PDU packet. |
| 88 | * @exception SnmpStatusException If the encoding is invalid. |
| 89 | * |
| 90 | * @since 1.5 |
| 91 | */ |
| 92 | public SnmpPdu decodeSnmpPdu(SnmpMsg msg) throws SnmpStatusException { |
| 93 | return msg.decodeSnmpPdu(); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Encodes the specified <CODE>SnmpPdu</CODE> and |
| 98 | * returns the resulting <CODE>SnmpMsg</CODE>. If this |
| 99 | * method returns null, the specified <CODE>SnmpPdu</CODE> |
| 100 | * will be dropped and the current SNMP request will be |
| 101 | * aborted. |
| 102 | * |
| 103 | * @param p The <CODE>SnmpPdu</CODE> to be encoded. |
| 104 | * @param maxDataLength The size limit of the resulting encoding. |
| 105 | * @return Null or a fully encoded <CODE>SnmpMsg</CODE>. |
| 106 | * @exception SnmpStatusException If <CODE>pdu</CODE> contains |
| 107 | * illegal values and cannot be encoded. |
| 108 | * @exception SnmpTooBigException If the resulting encoding does not |
| 109 | * fit into <CODE>maxPktSize</CODE> bytes. |
| 110 | * |
| 111 | * @since 1.5 |
| 112 | */ |
| 113 | public SnmpMsg encodeSnmpPdu(SnmpPdu p, int maxDataLength) |
| 114 | throws SnmpStatusException, SnmpTooBigException { |
| 115 | switch(p.version) { |
| 116 | case SnmpDefinitions.snmpVersionOne: |
| 117 | case SnmpDefinitions.snmpVersionTwo: { |
| 118 | SnmpMessage result = new SnmpMessage(); |
| 119 | result.encodeSnmpPdu((SnmpPduPacket) p, maxDataLength); |
| 120 | return result; |
| 121 | } |
| 122 | case SnmpDefinitions.snmpVersionThree: { |
| 123 | SnmpV3Message result = new SnmpV3Message(); |
| 124 | result.encodeSnmpPdu(p, maxDataLength); |
| 125 | return result; |
| 126 | } |
| 127 | default: |
| 128 | return null; |
| 129 | } |
| 130 | } |
| 131 | } |