KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*_############################################################################
2   _##
3   _## SNMP4J - UsmUserTable.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 java.util.TreeMap JavaDoc;
25
26 import org.snmp4j.smi.OctetString;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.LinkedList JavaDoc;
31 import java.util.Collection JavaDoc;
32 import org.snmp4j.log.*;
33
34 /**
35  * The <code>UsmUserTable</code> class stores USM user
36  * information as part of the Local Configuration Datastore (LCD).
37  *
38  * @author Frank Fock
39  * @version 1.1
40  */

41 public class UsmUserTable implements Serializable JavaDoc {
42
43   private static final long serialVersionUID = 6936547777550957622L;
44
45   private static final LogAdapter logger = LogFactory.getLogger(UsmUserTable.class);
46
47   private Map JavaDoc table = new TreeMap JavaDoc();
48
49   public UsmUserTable() {
50   }
51
52   public synchronized UsmUserEntry addUser(UsmUserEntry user) {
53     if (logger.isDebugEnabled()) {
54       logger.debug("Adding user "+user.getUserName()+" = "+user.getUsmUser());
55     }
56     return (UsmUserEntry) table.put(new UsmUserKey(user), user);
57   }
58
59   public synchronized void setUsers(Collection JavaDoc c) {
60     if (logger.isDebugEnabled()) {
61       logger.debug("Setting users to "+c);
62     }
63     table.clear();
64     for (Iterator JavaDoc it = c.iterator(); it.hasNext(); ) {
65       UsmUserEntry user = (UsmUserEntry) it.next();
66       table.put(new UsmUserKey(user), user);
67     }
68   }
69
70   /**
71    * Gets all user entries with the supplied user name.
72    * @param userName
73    * an <code>OctetString</code> denoting the user name.
74    * @return
75    * a possibly empty <code>List</code> containing all user entries with
76    * the specified <code>userName</code>.
77    */

78   public synchronized List JavaDoc getUserEntries(OctetString userName) {
79     LinkedList JavaDoc users = new LinkedList JavaDoc();
80     for (Iterator JavaDoc it = table.values().iterator(); it.hasNext();) {
81       UsmUserEntry value = (UsmUserEntry) it.next();
82       if (userName.equals(value.getUserName())) {
83         users.add(value);
84       }
85     }
86     if (logger.isDebugEnabled()) {
87       logger.debug("Returning user entries for "+userName+" = "+users);
88     }
89     return users;
90   }
91
92   public synchronized List JavaDoc getUserEntries() {
93     LinkedList JavaDoc l = new LinkedList JavaDoc();
94     for (Iterator JavaDoc it = table.values().iterator(); it.hasNext(); ) {
95       l.add(it.next());
96     }
97     return l;
98   }
99
100   public synchronized UsmUserEntry removeUser(OctetString engineID,
101                                               OctetString securityName) {
102     UsmUserEntry entry =
103         (UsmUserEntry) table.remove(new UsmUserKey(engineID, securityName));
104     if (logger.isDebugEnabled()) {
105       logger.debug("Removed user with secName="+securityName+
106                    " and engineID="+engineID);
107     }
108     return entry;
109   }
110
111   public synchronized UsmUserEntry getUser(OctetString engineID,
112                                            OctetString securityName) {
113     return (UsmUserEntry)table.get(new UsmUserKey(engineID, securityName));
114   }
115
116   public synchronized UsmUserEntry getUser(OctetString securityName) {
117     return (UsmUserEntry)table.get(new UsmUserKey(new OctetString(), securityName));
118   }
119
120   public synchronized void clear() {
121     table.clear();
122     if (logger.isDebugEnabled()) {
123       logger.debug("Cleared UsmUserTable");
124     }
125   }
126
127   public static class UsmUserKey implements Comparable JavaDoc {
128     OctetString engineID;
129     OctetString securityName;
130
131     public UsmUserKey(UsmUserEntry entry) {
132       setEngineID(entry.getEngineID());
133       this.securityName = entry.getUsmUser().getSecurityName();
134     }
135
136     public UsmUserKey(OctetString engineID, OctetString securityName) {
137       setEngineID(engineID);
138       this.securityName = securityName;
139     }
140
141     private void setEngineID(OctetString engineID) {
142       if (engineID == null) {
143         this.engineID = new OctetString();
144       }
145       else {
146         this.engineID = engineID;
147       }
148     }
149
150     public int hashCode() {
151       return engineID.hashCode()^2 + securityName.hashCode();
152     }
153
154     public boolean equals(Object JavaDoc o) {
155       if ((o instanceof UsmUserEntry) || (o instanceof UsmUserKey)) {
156         return (compareTo(o) == 0);
157       }
158       return false;
159     }
160
161     public int compareTo(Object JavaDoc o) {
162       if (o instanceof UsmUserEntry) {
163         return compareTo(new UsmUserKey((UsmUserEntry)o));
164       }
165       UsmUserKey other = (UsmUserKey)o;
166       int result = 0;
167       if ((engineID != null) && (other.engineID != null)) {
168         result = engineID.compareTo(other.engineID);
169       }
170       else if ((engineID != null) && (other.engineID == null)) {
171         result = 1;
172       }
173       else if ((engineID == null) && (other.engineID != null)) {
174         result = -1;
175       }
176       if (result == 0) {
177         result = securityName.compareTo(other.securityName);
178       }
179       return result;
180     }
181   }
182 }
183
184
Popular Tags