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