1. /*
  2. * @(#)file SnmpPduFactoryBER.java
  3. * @(#)author Sun Microsystems, Inc.
  4. * @(#)version 3.30
  5. * @(#)date 04/09/15
  6. *
  7. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  8. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  9. *
  10. */
  11. package com.sun.jmx.snmp;
  12. // java imports
  13. //
  14. import java.io.Serializable;
  15. // jmx import
  16. //
  17. import com.sun.jmx.snmp.SnmpPduFactory;
  18. import com.sun.jmx.snmp.SnmpMessage;
  19. import com.sun.jmx.snmp.SnmpPduPacket;
  20. import com.sun.jmx.snmp.SnmpPdu;
  21. import com.sun.jmx.snmp.SnmpMsg;
  22. import com.sun.jmx.snmp.SnmpStatusException;
  23. import com.sun.jmx.snmp.SnmpTooBigException;
  24. import com.sun.jmx.snmp.SnmpDefinitions;
  25. // SNMP Runtime import
  26. //
  27. import com.sun.jmx.snmp.SnmpV3Message;
  28. /**
  29. * Default implementation of the {@link com.sun.jmx.snmp.SnmpPduFactory SnmpPduFactory} interface.
  30. * <BR>It uses the BER (basic encoding rules) standardized encoding scheme associated with ASN.1.
  31. * <P>
  32. * This implementation of the <CODE>SnmpPduFactory</CODE> is very
  33. * basic: it simply calls encoding and decoding methods from
  34. * {@link com.sun.jmx.snmp.SnmpMsg}.
  35. * <BLOCKQUOTE>
  36. * <PRE>
  37. * public SnmpPdu decodeSnmpPdu(SnmpMsg msg)
  38. * throws SnmpStatusException {
  39. * return msg.decodeSnmpPdu() ;
  40. * }
  41. *
  42. * public SnmpMsg encodeSnmpPdu(SnmpPdu pdu, int maxPktSize)
  43. * throws SnmpStatusException, SnmpTooBigException {
  44. * SnmpMsg result = new SnmpMessage() ; // for SNMP v1/v2
  45. * <I>or</I>
  46. * SnmpMsg result = new SnmpV3Message() ; // for SNMP v3
  47. * result.encodeSnmpPdu(pdu, maxPktSize) ;
  48. * return result ;
  49. * }
  50. * </PRE>
  51. * </BLOCKQUOTE>
  52. * To implement your own object, you can implement <CODE>SnmpPduFactory</CODE>
  53. * or extend <CODE>SnmpPduFactoryBER</CODE>.
  54. * <p><b>This API is a Sun Microsystems internal API and is subject
  55. * to change without notice.</b></p>
  56. */
  57. public class SnmpPduFactoryBER implements SnmpPduFactory, Serializable {
  58. /**
  59. * Calls {@link com.sun.jmx.snmp.SnmpMsg#decodeSnmpPdu SnmpMsg.decodeSnmpPdu}
  60. * on the specified message and returns the resulting <CODE>SnmpPdu</CODE>.
  61. *
  62. * @param msg The SNMP message to be decoded.
  63. * @return The resulting SNMP PDU packet.
  64. * @exception SnmpStatusException If the encoding is invalid.
  65. *
  66. * @since 1.5
  67. */
  68. public SnmpPdu decodeSnmpPdu(SnmpMsg msg) throws SnmpStatusException {
  69. return msg.decodeSnmpPdu();
  70. }
  71. /**
  72. * Encodes the specified <CODE>SnmpPdu</CODE> and
  73. * returns the resulting <CODE>SnmpMsg</CODE>. If this
  74. * method returns null, the specified <CODE>SnmpPdu</CODE>
  75. * will be dropped and the current SNMP request will be
  76. * aborted.
  77. *
  78. * @param p The <CODE>SnmpPdu</CODE> to be encoded.
  79. * @param maxDataLength The size limit of the resulting encoding.
  80. * @return Null or a fully encoded <CODE>SnmpMsg</CODE>.
  81. * @exception SnmpStatusException If <CODE>pdu</CODE> contains
  82. * illegal values and cannot be encoded.
  83. * @exception SnmpTooBigException If the resulting encoding does not
  84. * fit into <CODE>maxPktSize</CODE> bytes.
  85. *
  86. * @since 1.5
  87. */
  88. public SnmpMsg encodeSnmpPdu(SnmpPdu p, int maxDataLength)
  89. throws SnmpStatusException, SnmpTooBigException {
  90. switch(p.version) {
  91. case SnmpDefinitions.snmpVersionOne:
  92. case SnmpDefinitions.snmpVersionTwo: {
  93. SnmpMessage result = new SnmpMessage();
  94. result.encodeSnmpPdu((SnmpPduPacket) p, maxDataLength);
  95. return result;
  96. }
  97. case SnmpDefinitions.snmpVersionThree: {
  98. SnmpV3Message result = new SnmpV3Message();
  99. result.encodeSnmpPdu(p, maxDataLength);
  100. return result;
  101. }
  102. default:
  103. return null;
  104. }
  105. }
  106. }