KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)file SnmpParameters.java
3  * @(#)author Sun Microsystems, Inc.
4  * @(#)version 4.17
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 // Copyright (c) 1995-96 by Cisco Systems, Inc.
12

13 package com.sun.jmx.snmp ;
14
15
16 import java.io.Serializable JavaDoc;
17 import java.io.UnsupportedEncodingException JavaDoc;
18
19 import com.sun.jmx.snmp.SnmpDefinitions;
20 import com.sun.jmx.snmp.SnmpStatusException;
21
22
23 /**
24  * Contains a set of resources that are used by while sending or receiving
25  * packets to and from an <CODE>SnmpPeer</CODE>. An <CODE>SnmpPeer</CODE> can
26  * be configured explicitly to use a specific <CODE>SnmpParameter</CODE>.
27  * A number of <CODE>SnmpPeer</CODE> objects can share a single parameter
28  * object.
29  * <P>
30  * <B>Note</B>: Changing values for an <CODE>SnmpParameter</CODE> object
31  * affects all <CODE>SnmpPeer</CODE> objects that share the parameter object.
32  *
33  * @version 4.17 12/19/03
34  * @author Sun Microsystems, Inc
35  * @author Cisco Systems, Inc.
36  * @see com.sun.jmx.snmp.SnmpPeer
37  *
38  * <p><b>This API is a Sun Microsystems internal API and is subject
39  * to change without notice.</b></p>
40  */

41
42
43 public class SnmpParameters extends SnmpParams implements Cloneable JavaDoc, Serializable JavaDoc {
44
45     /**
46      * Creates an <CODE>SnmpParameters</CODE> object with defaults set up.
47      * By default, <CODE>set</CODE> operations are not allowed, the read community and
48      * the inform community strings to use is "public" and the SNMP version is V1.
49      */

50     public SnmpParameters() {
51         _readCommunity = defaultRdCommunity ;
52         _informCommunity = defaultRdCommunity ;
53     }
54
55     /**
56      * Creates an <CODE>SnmpParameters</CODE> object.
57      * This constructor allows the specification of the read/write community strings.
58      * The inform community string to use is "public".
59      *
60      * @param rdc community string to use for <CODE>get</CODE> operations.
61      * @param wrc community string to use for <CODE>set</CODE> operations.
62      */

63     public SnmpParameters(String JavaDoc rdc, String JavaDoc wrc) {
64         _readCommunity = rdc ;
65         _writeCommunity = wrc ;
66         _informCommunity = defaultRdCommunity ;
67     }
68
69     /**
70      * Creates an <CODE>SnmpParameters</CODE> object.
71      * This constructor allows the specification of the read/write/inform community strings.
72      *
73      * @param rdc community string to use for <CODE>get</CODE> operations.
74      * @param wrc community string to use for <CODE>set</CODE> operations.
75      * @param inform community string to use for <CODE>inform</CODE> requests.
76      */

77     public SnmpParameters(String JavaDoc rdc, String JavaDoc wrc, String JavaDoc inform) {
78         _readCommunity = rdc ;
79         _writeCommunity = wrc ;
80         _informCommunity = inform ;
81     }
82   
83     /**
84      * Gets the community to be used when issuing <CODE>get</CODE> operations.
85      * @return The community string.
86      */

87     public String JavaDoc getRdCommunity() {
88         return _readCommunity ;
89     }
90
91     /**
92      * Sets the community string to use when performing <CODE>get</CODE> operations.
93      * @param read The community string.
94      */

95     public synchronized void setRdCommunity(String JavaDoc read) {
96         if (read == null)
97             _readCommunity = defaultRdCommunity ;
98         else
99             _readCommunity = read ;
100     }
101
102     /**
103      * Gets the community to be used when issuing <CODE>set</CODE> operations.
104      * @return The community string.
105      */

106     public String JavaDoc getWrCommunity() {
107         return _writeCommunity ;
108     }
109
110     /**
111      * Sets the community to be used when issuing <CODE>set</CODE> operations.
112      * @param write The community string.
113      */

114     public void setWrCommunity(String JavaDoc write) {
115         _writeCommunity = write;
116     }
117
118     /**
119      * Gets the community to be used when issuing <CODE>inform</CODE> requests.
120      * @return The community string.
121      */

122     public String JavaDoc getInformCommunity() {
123         return _informCommunity ;
124     }
125
126     /**
127      * Sets the community string to use when performing <CODE>inform</CODE> requests.
128      * @param inform The community string.
129      */

130     public void setInformCommunity(String JavaDoc inform) {
131         if (inform == null)
132             _informCommunity = defaultRdCommunity ;
133         else
134             _informCommunity = inform ;
135     }
136     
137     /**
138      * Checks whether parameters are in place for an SNMP <CODE>set</CODE> operation.
139      * @return <CODE>true</CODE> if parameters are in place, <CODE>false</CODE> otherwise.
140      */

141     public boolean allowSnmpSets() {
142         return _writeCommunity != null ;
143     }
144
145     /**
146      * Compares two objects.
147      * Two <CODE>SnmpParameters</CODE> are equal if they correspond to the same protocol version,
148      * read community and write community.
149      * @param obj The object to compare <CODE>this</CODE> with.
150      * @return <CODE>true</CODE> if <CODE>this</CODE> and the specified object are equal, <CODE>false</CODE> otherwise.
151      */

152     public synchronized boolean equals(Object JavaDoc obj) {
153         if (!( obj instanceof SnmpParameters))
154             return false;
155
156         if (this == obj)
157             return true ;
158         SnmpParameters param = (SnmpParameters) obj ;
159         if (_protocolVersion == param._protocolVersion)
160             if (_readCommunity.equals(param._readCommunity))
161                 return true ;
162         return false ;
163     }
164   
165     /**
166      * Clones the object and its content.
167      * @return The object clone.
168      */

169     public synchronized Object JavaDoc clone() {
170         SnmpParameters par = null ;
171         try {
172             par = (SnmpParameters) super.clone() ;
173             //par._retryPolicy = _retryPolicy ;
174
par._readCommunity = _readCommunity ;
175             par._writeCommunity = _writeCommunity ;
176             par._informCommunity = _informCommunity ;
177         } catch (CloneNotSupportedException JavaDoc e) {
178             throw new InternalError JavaDoc() ; // VM bug.
179
}
180         return par ;
181     }
182         
183     /**
184      * For SNMP Runtime internal use only.
185      */

186     public byte[] encodeAuthentication(int snmpCmd)
187     throws SnmpStatusException {
188         //
189
// Returns the community string associated to the specified command.
190
//
191
try {
192             if (snmpCmd == pduSetRequestPdu)
193                 return _writeCommunity.getBytes("8859_1");
194             else if (snmpCmd == pduInformRequestPdu)
195                 return _informCommunity.getBytes("8859_1") ;
196             else
197                 return _readCommunity.getBytes("8859_1") ;
198         }catch(UnsupportedEncodingException JavaDoc e) {
199             throw new SnmpStatusException(e.getMessage());
200         }
201     }
202
203     /**
204      * Specify the default community string to use for <CODE>get</CODE> operations.
205      * By default, the value is "public".
206      */

207     final static String JavaDoc defaultRdCommunity = "public" ;
208
209     /**
210      * The protocol version.
211      * By default, the value is SNMP version 1.
212      * @serial
213      */

214     private int _protocolVersion = snmpVersionOne ;
215     /**
216      * The community to be used when issuing <CODE>get</CODE> operations.
217      * @serial
218      */

219     private String JavaDoc _readCommunity ;
220     /**
221      * The community to be used when issuing <CODE>set</CODE> operations.
222      * @serial
223      */

224     private String JavaDoc _writeCommunity ;
225     /**
226      * The community to be used when issuing <CODE>inform</CODE> requests.
227      * @serial
228      */

229     private String JavaDoc _informCommunity ;
230     /**
231      */

232     //private int _retryPolicy ; // not implemented as yet.
233
}
234
Popular Tags