001: /*_############################################################################
002: _##
003: _## SNMP4J - UsmUserTable.java
004: _##
005: _## Copyright (C) 2003-2008 Frank Fock and Jochen Katz (SNMP4J.org)
006: _##
007: _## Licensed under the Apache License, Version 2.0 (the "License");
008: _## you may not use this file except in compliance with the License.
009: _## You may obtain a copy of the License at
010: _##
011: _## http://www.apache.org/licenses/LICENSE-2.0
012: _##
013: _## Unless required by applicable law or agreed to in writing, software
014: _## distributed under the License is distributed on an "AS IS" BASIS,
015: _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: _## See the License for the specific language governing permissions and
017: _## limitations under the License.
018: _##
019: _##########################################################################*/
020:
021: package org.snmp4j.security;
022:
023: import java.io.Serializable;
024: import java.util.TreeMap;
025:
026: import org.snmp4j.smi.OctetString;
027: import java.util.Iterator;
028: import java.util.Map;
029: import java.util.List;
030: import java.util.LinkedList;
031: import java.util.Collection;
032: import org.snmp4j.log.*;
033:
034: /**
035: * The <code>UsmUserTable</code> class stores USM user
036: * information as part of the Local Configuration Datastore (LCD).
037: *
038: * @author Frank Fock
039: * @version 1.1
040: */
041: public class UsmUserTable implements Serializable {
042:
043: private static final long serialVersionUID = 6936547777550957622L;
044:
045: private static final LogAdapter logger = LogFactory
046: .getLogger(UsmUserTable.class);
047:
048: private Map table = new TreeMap();
049:
050: public UsmUserTable() {
051: }
052:
053: public synchronized UsmUserEntry addUser(UsmUserEntry user) {
054: if (logger.isDebugEnabled()) {
055: logger.debug("Adding user " + user.getUserName() + " = "
056: + user.getUsmUser());
057: }
058: return (UsmUserEntry) table.put(new UsmUserKey(user), user);
059: }
060:
061: public synchronized void setUsers(Collection c) {
062: if (logger.isDebugEnabled()) {
063: logger.debug("Setting users to " + c);
064: }
065: table.clear();
066: for (Iterator it = c.iterator(); it.hasNext();) {
067: UsmUserEntry user = (UsmUserEntry) it.next();
068: table.put(new UsmUserKey(user), user);
069: }
070: }
071:
072: /**
073: * Gets all user entries with the supplied user name.
074: * @param userName
075: * an <code>OctetString</code> denoting the user name.
076: * @return
077: * a possibly empty <code>List</code> containing all user entries with
078: * the specified <code>userName</code>.
079: */
080: public synchronized List getUserEntries(OctetString userName) {
081: LinkedList users = new LinkedList();
082: for (Iterator it = table.values().iterator(); it.hasNext();) {
083: UsmUserEntry value = (UsmUserEntry) it.next();
084: if (userName.equals(value.getUserName())) {
085: users.add(value);
086: }
087: }
088: if (logger.isDebugEnabled()) {
089: logger.debug("Returning user entries for " + userName
090: + " = " + users);
091: }
092: return users;
093: }
094:
095: public synchronized List getUserEntries() {
096: LinkedList l = new LinkedList();
097: for (Iterator it = table.values().iterator(); it.hasNext();) {
098: l.add(it.next());
099: }
100: return l;
101: }
102:
103: public synchronized UsmUserEntry removeUser(OctetString engineID,
104: OctetString securityName) {
105: UsmUserEntry entry = (UsmUserEntry) table
106: .remove(new UsmUserKey(engineID, securityName));
107: if (logger.isDebugEnabled()) {
108: logger.debug("Removed user with secName=" + securityName
109: + " and engineID=" + engineID);
110: }
111: return entry;
112: }
113:
114: public synchronized UsmUserEntry getUser(OctetString engineID,
115: OctetString securityName) {
116: return (UsmUserEntry) table.get(new UsmUserKey(engineID,
117: securityName));
118: }
119:
120: public synchronized UsmUserEntry getUser(OctetString securityName) {
121: return (UsmUserEntry) table.get(new UsmUserKey(
122: new OctetString(), securityName));
123: }
124:
125: public synchronized void clear() {
126: table.clear();
127: if (logger.isDebugEnabled()) {
128: logger.debug("Cleared UsmUserTable");
129: }
130: }
131:
132: public static class UsmUserKey implements Comparable {
133: OctetString engineID;
134: OctetString securityName;
135:
136: public UsmUserKey(UsmUserEntry entry) {
137: setEngineID(entry.getEngineID());
138: this .securityName = entry.getUsmUser().getSecurityName();
139: }
140:
141: public UsmUserKey(OctetString engineID, OctetString securityName) {
142: setEngineID(engineID);
143: this .securityName = securityName;
144: }
145:
146: private void setEngineID(OctetString engineID) {
147: if (engineID == null) {
148: this .engineID = new OctetString();
149: } else {
150: this .engineID = engineID;
151: }
152: }
153:
154: public int hashCode() {
155: return engineID.hashCode() ^ 2 + securityName.hashCode();
156: }
157:
158: public boolean equals(Object o) {
159: if ((o instanceof UsmUserEntry)
160: || (o instanceof UsmUserKey)) {
161: return (compareTo(o) == 0);
162: }
163: return false;
164: }
165:
166: public int compareTo(Object o) {
167: if (o instanceof UsmUserEntry) {
168: return compareTo(new UsmUserKey((UsmUserEntry) o));
169: }
170: UsmUserKey other = (UsmUserKey) o;
171: int result = 0;
172: if ((engineID != null) && (other.engineID != null)) {
173: result = engineID.compareTo(other.engineID);
174: } else if ((engineID != null) && (other.engineID == null)) {
175: result = 1;
176: } else if ((engineID == null) && (other.engineID != null)) {
177: result = -1;
178: }
179: if (result == 0) {
180: result = securityName.compareTo(other.securityName);
181: }
182: return result;
183: }
184: }
185: }
|