KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > security > UsmUserEntry


1 /*_############################################################################
2   _##
3   _## SNMP4J - UsmUserEntry.java
4   _##
5   _## Copyright 2003-2007 Frank Fock and Jochen Katz (SNMP4J.org)
6   _##
7   _## Licensed under the Apache License, Version 2.0 (the "License");
8   _## you may not use this file except in compliance with the License.
9   _## You may obtain a copy of the License at
10   _##
11   _## http://www.apache.org/licenses/LICENSE-2.0
12   _##
13   _## Unless required by applicable law or agreed to in writing, software
14   _## distributed under the License is distributed on an "AS IS" BASIS,
15   _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   _## See the License for the specific language governing permissions and
17   _## limitations under the License.
18   _##
19   _##########################################################################*/

20
21 package org.snmp4j.security;
22
23 import java.io.Serializable JavaDoc;
24 import org.snmp4j.smi.OctetString;
25 import org.snmp4j.smi.OID;
26
27 /**
28  * The <code>UsmUserEntry</code> class represents a user in the
29  * Local Configuration Datastore (LCD).
30  *
31  * @author Frank Fock
32  * @version 1.1
33  */

34 public class UsmUserEntry implements Serializable JavaDoc, Comparable JavaDoc {
35
36   private static final long serialVersionUID = -3021438367015187166L;
37
38   private OctetString engineID;
39   private OctetString userName;
40   private UsmUser usmUser;
41   private byte[] authenticationKey;
42   private byte[] privacyKey;
43
44   /**
45    * Creates a new user entry with empty engine ID and empty user.
46    */

47   public UsmUserEntry() {
48     engineID = new OctetString();
49     userName = new OctetString();
50     usmUser = new UsmUser(new OctetString(), null, null, null, null);
51   }
52
53   /**
54    * Creates a user with user name and associated {@link UsmUser}.
55    * @param userName
56    * the user name of the new entry.
57    * @param user
58    * the <code>UsmUser</code> representing the security information of the
59    * user.
60    */

61   public UsmUserEntry(OctetString userName, UsmUser user) {
62     this.userName = userName;
63     this.usmUser = user;
64     if (user.isLocalized()) {
65       if ((user.getAuthenticationProtocol() != null) &&
66           (user.getAuthenticationPassphrase() != null)) {
67         authenticationKey = user.getAuthenticationPassphrase().getValue();
68         if ((user.getPrivacyProtocol() != null) &&
69             (user.getPrivacyPassphrase() != null)) {
70           privacyKey = user.getPrivacyPassphrase().getValue();
71         }
72       }
73     }
74   }
75
76   /**
77    * Creates a user with user name and associated {@link UsmUser}.
78    * @param userName
79    * the user name of the new entry.
80    * @param engineID
81    * the authoritative engine ID associated with the user.
82    * @param user
83    * the <code>UsmUser</code> representing the security information of the
84    * user.
85    */

86   public UsmUserEntry(OctetString userName,
87                       OctetString engineID,
88                       UsmUser user) {
89     this(userName, user);
90     this.engineID = engineID;
91   }
92
93   /**
94    * Creates a localized user entry.
95    * @param engineID
96    * the engine ID for which the users has bee localized.
97    * @param securityName
98    * the user and security name of the new entry.
99    * @param authProtocol
100    * the authentication protocol ID.
101    * @param authKey
102    * the authentication key.
103    * @param privProtocol
104    * the privacy protocol ID.
105    * @param privKey
106    * the privacy key.
107    */

108   public UsmUserEntry(byte[] engineID, OctetString securityName,
109                       OID authProtocol, byte[] authKey,
110                       OID privProtocol, byte[] privKey) {
111     this.engineID = (engineID == null) ? null : new OctetString(engineID);
112     this.userName = securityName;
113     this.authenticationKey = authKey;
114     this.privacyKey = privKey;
115     this.usmUser =
116         new UsmUser(userName, authProtocol,
117                     ((authenticationKey != null) ?
118                      new OctetString(authenticationKey) : null),
119                     privProtocol,
120                     ((privacyKey != null) ?
121                      new OctetString(privacyKey) : null), this.engineID);
122   }
123
124   public OctetString getEngineID() {
125     return engineID;
126   }
127   public void setEngineID(OctetString engineID) {
128     this.engineID = engineID;
129   }
130   public void setUserName(OctetString userName) {
131     this.userName = userName;
132   }
133   public OctetString getUserName() {
134     return userName;
135   }
136   public void setUsmUser(UsmUser usmUser) {
137     this.usmUser = usmUser;
138   }
139   public UsmUser getUsmUser() {
140     return usmUser;
141   }
142   public void setAuthenticationKey(byte[] authenticationKey) {
143     this.authenticationKey = authenticationKey;
144   }
145   public byte[] getAuthenticationKey() {
146     return authenticationKey;
147   }
148   public void setPrivacyKey(byte[] privacyKey) {
149     this.privacyKey = privacyKey;
150   }
151   public byte[] getPrivacyKey() {
152     return privacyKey;
153   }
154
155   /**
156    * Compares this user entry with another one by engine ID then by their user
157    * names.
158    *
159    * @param o
160    * a <code>UsmUserEntry</code> instance.
161    * @return
162    * a negative integer, zero, or a positive integer as this object is
163    * less than, equal to, or greater than the specified object.
164    */

165   public int compareTo(Object JavaDoc o) {
166     UsmUserEntry other = (UsmUserEntry)o;
167     int result = 0;
168     if ((engineID != null) && (other.engineID != null)) {
169       result = engineID.compareTo(other.engineID);
170     }
171     else if ((engineID != null) && (other.engineID == null)) {
172       result = 1;
173     }
174     else if ((engineID == null) && (other.engineID != null)) {
175       result = -1;
176     }
177     if (result == 0) {
178       result = userName.compareTo(other.userName);
179       if (result == 0) {
180         result = usmUser.compareTo(other.usmUser);
181       }
182     }
183     return result;
184   }
185
186   public String JavaDoc toString() {
187     return "UsmUserEntry[userName="+userName+",usmUser="+usmUser+"]";
188   }
189
190 }
191
192
Popular Tags