FileDocCategorySizeDatePackage
AbstractUsersRepository.javaAPI DocApache James 2.3.19552Fri Jan 12 12:56:32 GMT 2007org.apache.james.userrepository

AbstractUsersRepository

public abstract class AbstractUsersRepository extends org.apache.avalon.framework.logger.AbstractLogEnabled implements org.apache.james.services.UsersRepository, org.apache.avalon.framework.component.Component
A partial implementation of a Repository to store users.

This implements common functionality found in different UsersRespository implementations, and makes it easier to create new User repositories.

Fields Summary
Constructors Summary
Methods Summary
public booleanaddUser(org.apache.james.services.User user)
Adds a user to the repository with the specified User object. Users names must be unique-case-insensitive in the repository.

param
user the user to be added
return
true if succesful, false otherwise
since
James 1.2.2

        String username = user.getUserName();

        if ( containsCaseInsensitive(username) ) {
            return false;
        }
        
        doAddUser(user);
        return true;
    
public voidaddUser(java.lang.String name, java.lang.Object attributes)
Adds a user to the repository with the specified attributes. In current implementations, the Object attributes is generally a String password.

param
name the name of the user to be added
param
attributes the password value as a String

        if (attributes instanceof String) {
            User newbie = new DefaultUser(name, "SHA");
            newbie.setPassword( (String) attributes );
            addUser(newbie);
        } else {
            throw new RuntimeException("Improper use of deprecated method" 
                                       + " - use addUser(User user)");
        }
    
public booleanaddUser(java.lang.String username, java.lang.String password)

see
org.apache.james.services.UsersRepository#addUser(java.lang.String, java.lang.String)

        User newbie = new DefaultJamesUser(username, "SHA");
        newbie.setPassword(password);
        return addUser(newbie);
    
public booleancontains(java.lang.String name)
Returns whether or not this user is in the repository

        User user = getUserByName(name, false);
        return ( user != null );
    
public booleancontainsCaseInsensitive(java.lang.String name)
Returns whether or not this user is in the repository. Names are matched on a case insensitive basis.

        User user = getUserByName( name, true );
        return ( user != null );
    
public intcountUsers()
Returns a count of the users in the repository.

return
the number of users in the repository

        List usernames = listUserNames();
        return usernames.size();
    
protected abstract voiddoAddUser(org.apache.james.services.User user)
Adds a user to the underlying Repository. The user name must not clash with an existing user.

protected abstract voiddoRemoveUser(org.apache.james.services.User user)
Removes a user from the underlying repository. If the user doesn't exist, returns ok.

protected abstract voiddoUpdateUser(org.apache.james.services.User user)
Updates a user record to match the supplied User.

public java.lang.StringgetRealName(java.lang.String name)
Returns the user name of the user matching name on an equalsIgnoreCase basis. Returns null if no match.

param
name the name of the user to retrieve
return
the correct case sensitive name of the user

        // Get the user by name, ignoring case, and return the correct name.
        User user = getUserByName(name, true);
        if ( user == null ) {
            return null;
        } else {
            return user.getUserName();
        }
    
public org.apache.james.services.UsergetUserByName(java.lang.String name)
Get the user object with the specified user name. Return null if no such user.

param
name the name of the user to retrieve
return
the user if found, null otherwise
since
James 1.2.2

        return getUserByName(name, false);
    
protected org.apache.james.services.UsergetUserByName(java.lang.String name, boolean ignoreCase)
Gets a user by name, ignoring case if specified. This implementation gets the entire set of users, and scrolls through searching for one matching name.

param
name the name of the user being retrieved
param
ignoreCase whether the name is regarded as case-insensitive
return
the user being retrieved, null if the user doesn't exist

        // Just iterate through all of the users until we find one matching.
        Iterator users = listAllUsers();
        while ( users.hasNext() ) {
            User user = (User)users.next();
            String username = user.getUserName();
            if (( !ignoreCase && username.equals(name) ) ||
                ( ignoreCase && username.equalsIgnoreCase(name) )) {
                return user;
            }
        }
        // Not found - return null
        return null;
    
public org.apache.james.services.UsergetUserByNameCaseInsensitive(java.lang.String name)
Get the user object with the specified user name. Match user naems on a case insensitive basis. Return null if no such user.

param
name the name of the user to retrieve
return
the user if found, null otherwise
since
James 1.2.2

        return getUserByName(name, true);
    
public java.util.Iteratorlist()
List users in repository.

return
Iterator over a collection of Strings, each being one user in the repository.

        return listUserNames().iterator();
    
protected abstract java.util.IteratorlistAllUsers()
Returns a list populated with all of the Users in the repository.

return
an Iterator of Users.

protected java.util.ListlistUserNames()
Produces the complete list of User names, with correct case.

return
a List of Strings representing user names.

        Iterator users = listAllUsers();
        List userNames = new LinkedList();
        while ( users.hasNext() ) {
            User user = (User)users.next();
            userNames.add(user.getUserName());
        }

        return userNames;
    
public voidremoveUser(java.lang.String name)
Removes a user from the repository

param
user the user to be removed

        User user = getUserByName(name);
        if ( user != null ) {
            doRemoveUser(user);
        }
    
public booleantest(java.lang.String name, java.lang.String password)
Test if user with name 'name' has password 'password'.

param
name the name of the user to be tested
param
password the password to be tested
return
true if the test is successful, false if the password is incorrect or the user doesn't exist
since
James 1.2.2

        User user = getUserByName(name, false);
        if ( user == null ) {
            return false;
        } else {
            return user.verifyPassword(password);
        }
    
public booleanupdateUser(org.apache.james.services.User user)
Update the repository with the specified user object. A user object with this username must already exist.

param
user the user to be updated
return
true if successful.

        // Return false if it's not found.
        if ( ! contains(user.getUserName()) ) {
            return false;
        }
        else {
            doUpdateUser(user);
            return true;
        }