X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=security%2Fusermgt%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fsecurity%2Fauthentication%2FInMemoryUserSet.java;fp=security%2Fusermgt%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fsecurity%2Fauthentication%2FInMemoryUserSet.java;h=59129213a4837ad41d8b04f55f253a73c750aadf;hb=9449ea0f360f6e9c14057db57f3ee0bfba947ab4;hp=0000000000000000000000000000000000000000;hpb=e8b988e92306a4aea2f047af1b48588147288831;p=utils diff --git a/security/usermgt/src/main/java/org/wamblee/security/authentication/InMemoryUserSet.java b/security/usermgt/src/main/java/org/wamblee/security/authentication/InMemoryUserSet.java new file mode 100644 index 00000000..59129213 --- /dev/null +++ b/security/usermgt/src/main/java/org/wamblee/security/authentication/InMemoryUserSet.java @@ -0,0 +1,108 @@ +/* + * Copyright 2005-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wamblee.security.authentication; + +import org.wamblee.security.encryption.MessageDigester; + +import java.util.Set; +import java.util.TreeSet; + +/** + * In-memory user set. + * + * @author Erik Brakkee + */ +public class InMemoryUserSet extends AbstractUserSet { + /** + * Users. All users in this set have their password validator and encoder + * set. + */ + private Set users; + + /** + * Constructs an empty user set. + */ + public InMemoryUserSet(NameValidator aPasswordValidator, + MessageDigester aPasswordEncoder) { + super(aPasswordValidator, aPasswordEncoder); + users = new TreeSet(); + } + + @Override + public void userModified(User aUser) { + users.remove(aUser); + setPasswordInfo(aUser); + users.add(aUser); + } + + @Override + public User find(String aName) { + for (User user : users) { + if (user.getName().equals(aName)) { + return user; + } + } + + return null; + } + + @Override + public boolean add(User aUser) { + setPasswordInfo(aUser); + + return users.add(aUser); + } + + @Override + public boolean contains(User aUser) { + return users.contains(aUser); + } + + @Override + public boolean remove(User aUser) { + return users.remove(aUser); + } + + @Override + public Set list() { + Set list = new TreeSet(); + list.addAll(users); + return list; + } + + @Override + public Set list(Group aGroup) { + Set result = new TreeSet(); + + for (User user : users) { + if (user.getGroups().contains(aGroup)) { + result.add(user); + } + } + + return result; + } + + @Override + public int size() { + return users.size(); + } + + @Override + public void clearCache() { + // Empty + } +}