FileDocCategorySizeDatePackage
MemoryRealm.javaAPI DocGlassfish v2 API10471Fri May 04 22:32:18 BST 2007org.apache.catalina.realm

MemoryRealm

public class MemoryRealm extends RealmBase
Simple implementation of Realm that reads an XML file to configure the valid users, passwords, and roles. The file format (and default file location) are identical to those currently supported by Tomcat 3.X.

IMPLEMENTATION NOTE: It is assumed that the in-memory collection representing our defined users (and their roles) is initialized at application startup and never modified again. Therefore, no thread synchronization is performed around accesses to the principals collection.

author
Craig R. McClanahan
version
$Revision: 1.5 $ $Date: 2007/05/05 05:32:17 $

Fields Summary
private static com.sun.org.apache.commons.logging.Log
log
private static com.sun.org.apache.commons.digester.Digester
digester
The Digester we will use to process in-memory database files.
protected final String
info
Descriptive information about this Realm implementation.
protected static final String
name
Descriptive information about this Realm implementation.
private String
pathname
The pathname (absolute or relative to Catalina's current working directory) of the XML file containing our database information.
private HashMap
principals
The set of valid Principals for this Realm, keyed by user name.
private static org.apache.catalina.util.StringManager
sm
The string manager for this package.
private boolean
started
Has this component been started?
Constructors Summary
Methods Summary
voidaddUser(java.lang.String username, java.lang.String password, java.lang.String roles)
Add a new user to the in-memory database.

param
username User's username
param
password User's password (clear text)
param
roles Comma-delimited set of roles associated with this user


        // Accumulate the list of roles for this user
        ArrayList list = new ArrayList();
        roles += ",";
        while (true) {
            int comma = roles.indexOf(',");
            if (comma < 0)
                break;
            String role = roles.substring(0, comma).trim();
            list.add(role);
            roles = roles.substring(comma + 1);
        }

        // Construct and cache the Principal for this user
        GenericPrincipal principal =
            new GenericPrincipal(this, username, password, list);
        principals.put(username, principal);

    
public java.security.Principalauthenticate(java.lang.String username, java.lang.String credentials)
Return the Principal associated with the specified username and credentials, if there is one; otherwise return null.

param
username Username of the Principal to look up
param
credentials Password or other credentials to use in authenticating this username


        GenericPrincipal principal =
            (GenericPrincipal) principals.get(username);

        boolean validated = false;
        if (principal != null) {
            if (hasMessageDigest()) {
                // Hex hashes should be compared case-insensitive
                validated = (digest(credentials)
                             .equalsIgnoreCase(principal.getPassword()));
            } else {
                validated =
                    (digest(credentials).equals(principal.getPassword()));
            }
        }

        if (validated) {
            if (log.isDebugEnabled())
                log.debug(sm.getString("memoryRealm.authenticateSuccess", username));
            return (principal);
        } else {
            if (log.isDebugEnabled())
                log.debug(sm.getString("memoryRealm.authenticateFailure", username));
            return (null);
        }

    
protected synchronized com.sun.org.apache.commons.digester.DigestergetDigester()
Return a configured Digester to use for processing the XML input file, creating a new one if necessary.


        if (digester == null) {
            digester = new Digester();
            digester.setDebug(this.debug);
            digester.setValidating(false);
            digester.addRuleSet(new MemoryRuleSet());
        }
        return (digester);

    
public java.lang.StringgetInfo()
Return descriptive information about this Realm implementation and the corresponding version number, in the format <description>/<version>.



    // ------------------------------------------------------------- Properties


                         
       

        return info;

    
protected java.lang.StringgetName()
Return a short name for this Realm implementation.


        return (this.name);

    
protected java.lang.StringgetPassword(java.lang.String username)
Return the password associated with the given principal's user name.


        GenericPrincipal principal =
            (GenericPrincipal) principals.get(username);
        if (principal != null) {
            return (principal.getPassword());
        } else {
            return (null);
        }

    
public java.lang.StringgetPathname()
Return the pathname of our XML file containing user definitions.


        return pathname;

    
protected java.security.PrincipalgetPrincipal(java.lang.String username)
Return the Principal associated with the given user name.


        return (Principal) principals.get(username);

    
public voidsetPathname(java.lang.String pathname)
Set the pathname of our XML file containing user definitions. If a relative pathname is specified, it is resolved against "catalina.base".

param
pathname The new pathname


        this.pathname = pathname;

    
public synchronized voidstart()
Prepare for active use of the public methods of this Component.

exception
LifecycleException if this component detects a fatal error that prevents it from being started


        // Validate the existence of our database file
        File file = new File(pathname);
        if (!file.isAbsolute())
            file = new File(System.getProperty("catalina.base"), pathname);
        if (!file.exists() || !file.canRead())
            throw new LifecycleException
                (sm.getString("memoryRealm.loadExist",
                              file.getAbsolutePath()));

        // Load the contents of the database file
        if (log.isDebugEnabled())
            log.debug(sm.getString("memoryRealm.loadPath",
                             file.getAbsolutePath()));
        Digester digester = getDigester();
        try {
            synchronized (digester) {
                digester.push(this);
                digester.parse(file);
            }
        } catch (Exception e) {
            throw new LifecycleException("memoryRealm.readXml", e);
        }

        // Perform normal superclass initialization
        super.start();

    
public synchronized voidstop()
Gracefully shut down active use of the public methods of this Component.

exception
LifecycleException if this component detects a fatal error that needs to be reported


        // Perform normal superclass finalization
        super.stop();

        // No shutdown activities required