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

UsersFileRepository

public class UsersFileRepository extends org.apache.avalon.framework.logger.AbstractLogEnabled implements org.apache.avalon.framework.service.Serviceable, org.apache.avalon.framework.activity.Initializable, org.apache.avalon.framework.configuration.Configurable, org.apache.james.services.UsersRepository
Implementation of a Repository to store users on the File System. Requires a configuration element in the .conf.xml file of the form: <repository destinationURL="file://path-to-root-dir-for-repository" type="USERS" model="SYNCHRONOUS"/> Requires a logger called UsersRepository.
version
CVS $Revision: 494012 $

Fields Summary
protected static boolean
DEEP_DEBUG
Whether 'deep debugging' is turned on.
private org.apache.avalon.cornerstone.services.store.Store
store
private org.apache.avalon.cornerstone.services.store.ObjectRepository
or
private String
destination
The destination URL used to define the repository.
Constructors Summary
Methods Summary
public synchronized booleanaddUser(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 added.
return
true if successful.

        String username = user.getUserName();
        if (contains(username)) {
            return false;
        }
        try {
            or.put(username, user);
        } catch (Exception e) {
            throw new RuntimeException("Exception caught while storing user: " + e );
        }
        return true;
    
public voidaddUser(java.lang.String name, java.lang.Object attributes)

        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)

        User newbie = new DefaultJamesUser(username, "SHA");
        newbie.setPassword(password);
        return addUser(newbie);
    
public voidconfigure(org.apache.avalon.framework.configuration.Configuration configuration)

see
org.apache.avalon.framework.configuration.Configurable#configure(Configuration)


        destination = configuration.getChild( "destination" ).getAttribute( "URL" );

        if (!destination.endsWith(File.separator)) {
            destination += File.separator;
        }
    
public booleancontains(java.lang.String name)

        return or.containsKey(name);
    
public booleancontainsCaseInsensitive(java.lang.String name)

        Iterator it = list();
        while (it.hasNext()) {
            if (name.equalsIgnoreCase((String)it.next())) {
                return true;
            }
        }
        return false;
    
public intcountUsers()

        int count = 0;
        for (Iterator it = list(); it.hasNext(); it.next()) {
            count++;
        }
        return count;
    
public java.lang.StringgetRealName(java.lang.String name)

        Iterator it = list();
        while (it.hasNext()) {
            String temp = (String) it.next();
            if (name.equalsIgnoreCase(temp)) {
                return temp;
            }
        }
        return null;
    
public synchronized org.apache.james.services.UsergetUserByName(java.lang.String name)

        if (contains(name)) {
            try {
                return (User)or.get(name);
            } catch (Exception e) {
                throw new RuntimeException("Exception while retrieving user: "
                                           + e.getMessage());
            }
        } else {
            return null;
        }
    
public org.apache.james.services.UsergetUserByNameCaseInsensitive(java.lang.String name)

        String realName = getRealName(name);
        if (realName == null ) {
            return null;
        }
        return getUserByName(realName);
    
public voidinitialize()

see
org.apache.avalon.framework.activity.Initializable#initialize()


        try {
            //prepare Configurations for object and stream repositories
            final DefaultConfiguration objectConfiguration
                = new DefaultConfiguration( "repository",
                                            "generated:UsersFileRepository.compose()" );

            objectConfiguration.setAttribute( "destinationURL", destination );
            objectConfiguration.setAttribute( "type", "OBJECT" );
            objectConfiguration.setAttribute( "model", "SYNCHRONOUS" );

            or = (ObjectRepository)store.select( objectConfiguration );
            if (getLogger().isDebugEnabled()) {
                StringBuffer logBuffer =
                    new StringBuffer(192)
                            .append(this.getClass().getName())
                            .append(" created in ")
                            .append(destination);
                getLogger().debug(logBuffer.toString());
            }
        } catch (Exception e) {
            if (getLogger().isErrorEnabled()) {
                getLogger().error("Failed to initialize repository:" + e.getMessage(), e );
            }
            throw e;
        }
    
public java.util.Iteratorlist()
List users in repository.

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

        return or.list();
    
public synchronized voidremoveUser(java.lang.String name)

        or.remove(name);
    
public voidservice(org.apache.avalon.framework.service.ServiceManager componentManager)

see
org.apache.avalon.framework.service.Serviceable#service(ServiceManager)


           
          
          

        try {
            store = (Store)componentManager.lookup( Store.ROLE );
        } catch (Exception e) {
            final String message = "Failed to retrieve Store component:" + e.getMessage();
            getLogger().error( message, e );
            throw new ServiceException ("", message, e );
        }
    
public booleantest(java.lang.String name, java.lang.String password)

        User user;
        try {
            if (contains(name)) {
                user = (User) or.get(name);
            } else {
               return false;
            }
        } catch (Exception e) {
            throw new RuntimeException("Exception retrieving User" + e);
        }
        return user.verifyPassword(password);
    
public booleanupdateUser(org.apache.james.services.User user)

        String username = user.getUserName();
        if (!contains(username)) {
            return false;
        }
        try {
            or.put(username, user);
        } catch (Exception e) {
            throw new RuntimeException("Exception caught while storing user: " + e );
        }
        return true;