1. /*
  2. * @(#)file SnmpParameters.java
  3. * @(#)author Sun Microsystems, Inc.
  4. * @(#)version 4.17
  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. // Copyright (c) 1995-96 by Cisco Systems, Inc.
  12. package com.sun.jmx.snmp ;
  13. import java.io.Serializable;
  14. import java.io.UnsupportedEncodingException;
  15. import com.sun.jmx.snmp.SnmpDefinitions;
  16. import com.sun.jmx.snmp.SnmpStatusException;
  17. /**
  18. * Contains a set of resources that are used by while sending or receiving
  19. * packets to and from an <CODE>SnmpPeer</CODE>. An <CODE>SnmpPeer</CODE> can
  20. * be configured explicitly to use a specific <CODE>SnmpParameter</CODE>.
  21. * A number of <CODE>SnmpPeer</CODE> objects can share a single parameter
  22. * object.
  23. * <P>
  24. * <B>Note</B>: Changing values for an <CODE>SnmpParameter</CODE> object
  25. * affects all <CODE>SnmpPeer</CODE> objects that share the parameter object.
  26. *
  27. * @version 4.17 12/19/03
  28. * @author Sun Microsystems, Inc
  29. * @author Cisco Systems, Inc.
  30. * @see com.sun.jmx.snmp.SnmpPeer
  31. *
  32. * <p><b>This API is a Sun Microsystems internal API and is subject
  33. * to change without notice.</b></p>
  34. */
  35. public class SnmpParameters extends SnmpParams implements Cloneable, Serializable {
  36. /**
  37. * Creates an <CODE>SnmpParameters</CODE> object with defaults set up.
  38. * By default, <CODE>set</CODE> operations are not allowed, the read community and
  39. * the inform community strings to use is "public" and the SNMP version is V1.
  40. */
  41. public SnmpParameters() {
  42. _readCommunity = defaultRdCommunity ;
  43. _informCommunity = defaultRdCommunity ;
  44. }
  45. /**
  46. * Creates an <CODE>SnmpParameters</CODE> object.
  47. * This constructor allows the specification of the read/write community strings.
  48. * The inform community string to use is "public".
  49. *
  50. * @param rdc community string to use for <CODE>get</CODE> operations.
  51. * @param wrc community string to use for <CODE>set</CODE> operations.
  52. */
  53. public SnmpParameters(String rdc, String wrc) {
  54. _readCommunity = rdc ;
  55. _writeCommunity = wrc ;
  56. _informCommunity = defaultRdCommunity ;
  57. }
  58. /**
  59. * Creates an <CODE>SnmpParameters</CODE> object.
  60. * This constructor allows the specification of the read/write/inform community strings.
  61. *
  62. * @param rdc community string to use for <CODE>get</CODE> operations.
  63. * @param wrc community string to use for <CODE>set</CODE> operations.
  64. * @param inform community string to use for <CODE>inform</CODE> requests.
  65. */
  66. public SnmpParameters(String rdc, String wrc, String inform) {
  67. _readCommunity = rdc ;
  68. _writeCommunity = wrc ;
  69. _informCommunity = inform ;
  70. }
  71. /**
  72. * Gets the community to be used when issuing <CODE>get</CODE> operations.
  73. * @return The community string.
  74. */
  75. public String getRdCommunity() {
  76. return _readCommunity ;
  77. }
  78. /**
  79. * Sets the community string to use when performing <CODE>get</CODE> operations.
  80. * @param read The community string.
  81. */
  82. public synchronized void setRdCommunity(String read) {
  83. if (read == null)
  84. _readCommunity = defaultRdCommunity ;
  85. else
  86. _readCommunity = read ;
  87. }
  88. /**
  89. * Gets the community to be used when issuing <CODE>set</CODE> operations.
  90. * @return The community string.
  91. */
  92. public String getWrCommunity() {
  93. return _writeCommunity ;
  94. }
  95. /**
  96. * Sets the community to be used when issuing <CODE>set</CODE> operations.
  97. * @param write The community string.
  98. */
  99. public void setWrCommunity(String write) {
  100. _writeCommunity = write;
  101. }
  102. /**
  103. * Gets the community to be used when issuing <CODE>inform</CODE> requests.
  104. * @return The community string.
  105. */
  106. public String getInformCommunity() {
  107. return _informCommunity ;
  108. }
  109. /**
  110. * Sets the community string to use when performing <CODE>inform</CODE> requests.
  111. * @param inform The community string.
  112. */
  113. public void setInformCommunity(String inform) {
  114. if (inform == null)
  115. _informCommunity = defaultRdCommunity ;
  116. else
  117. _informCommunity = inform ;
  118. }
  119. /**
  120. * Checks whether parameters are in place for an SNMP <CODE>set</CODE> operation.
  121. * @return <CODE>true</CODE> if parameters are in place, <CODE>false</CODE> otherwise.
  122. */
  123. public boolean allowSnmpSets() {
  124. return _writeCommunity != null ;
  125. }
  126. /**
  127. * Compares two objects.
  128. * Two <CODE>SnmpParameters</CODE> are equal if they correspond to the same protocol version,
  129. * read community and write community.
  130. * @param obj The object to compare <CODE>this</CODE> with.
  131. * @return <CODE>true</CODE> if <CODE>this</CODE> and the specified object are equal, <CODE>false</CODE> otherwise.
  132. */
  133. public synchronized boolean equals(Object obj) {
  134. if (!( obj instanceof SnmpParameters))
  135. return false;
  136. if (this == obj)
  137. return true ;
  138. SnmpParameters param = (SnmpParameters) obj ;
  139. if (_protocolVersion == param._protocolVersion)
  140. if (_readCommunity.equals(param._readCommunity))
  141. return true ;
  142. return false ;
  143. }
  144. /**
  145. * Clones the object and its content.
  146. * @return The object clone.
  147. */
  148. public synchronized Object clone() {
  149. SnmpParameters par = null ;
  150. try {
  151. par = (SnmpParameters) super.clone() ;
  152. //par._retryPolicy = _retryPolicy ;
  153. par._readCommunity = _readCommunity ;
  154. par._writeCommunity = _writeCommunity ;
  155. par._informCommunity = _informCommunity ;
  156. } catch (CloneNotSupportedException e) {
  157. throw new InternalError() ; // VM bug.
  158. }
  159. return par ;
  160. }
  161. /**
  162. * For SNMP Runtime internal use only.
  163. */
  164. public byte[] encodeAuthentication(int snmpCmd)
  165. throws SnmpStatusException {
  166. //
  167. // Returns the community string associated to the specified command.
  168. //
  169. try {
  170. if (snmpCmd == pduSetRequestPdu)
  171. return _writeCommunity.getBytes("8859_1");
  172. else if (snmpCmd == pduInformRequestPdu)
  173. return _informCommunity.getBytes("8859_1") ;
  174. else
  175. return _readCommunity.getBytes("8859_1") ;
  176. }catch(UnsupportedEncodingException e) {
  177. throw new SnmpStatusException(e.getMessage());
  178. }
  179. }
  180. /**
  181. * Specify the default community string to use for <CODE>get</CODE> operations.
  182. * By default, the value is "public".
  183. */
  184. final static String defaultRdCommunity = "public" ;
  185. /**
  186. * The protocol version.
  187. * By default, the value is SNMP version 1.
  188. * @serial
  189. */
  190. private int _protocolVersion = snmpVersionOne ;
  191. /**
  192. * The community to be used when issuing <CODE>get</CODE> operations.
  193. * @serial
  194. */
  195. private String _readCommunity ;
  196. /**
  197. * The community to be used when issuing <CODE>set</CODE> operations.
  198. * @serial
  199. */
  200. private String _writeCommunity ;
  201. /**
  202. * The community to be used when issuing <CODE>inform</CODE> requests.
  203. * @serial
  204. */
  205. private String _informCommunity ;
  206. /**
  207. */
  208. //private int _retryPolicy ; // not implemented as yet.
  209. }