1 20 21 package org.snmp4j.security; 22 23 import java.io.Serializable ; 24 import java.util.TreeMap ; 25 26 import org.snmp4j.smi.OctetString; 27 import java.util.Iterator ; 28 import java.util.Map ; 29 import java.util.List ; 30 import java.util.LinkedList ; 31 import java.util.Collection ; 32 import org.snmp4j.log.*; 33 34 41 public class UsmUserTable implements Serializable { 42 43 private static final long serialVersionUID = 6936547777550957622L; 44 45 private static final LogAdapter logger = LogFactory.getLogger(UsmUserTable.class); 46 47 private Map table = new TreeMap (); 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 c) { 60 if (logger.isDebugEnabled()) { 61 logger.debug("Setting users to "+c); 62 } 63 table.clear(); 64 for (Iterator it = c.iterator(); it.hasNext(); ) { 65 UsmUserEntry user = (UsmUserEntry) it.next(); 66 table.put(new UsmUserKey(user), user); 67 } 68 } 69 70 78 public synchronized List getUserEntries(OctetString userName) { 79 LinkedList users = new LinkedList (); 80 for (Iterator 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 getUserEntries() { 93 LinkedList l = new LinkedList (); 94 for (Iterator 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 { 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 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 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 |