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

UserDatabaseRealm

public class UserDatabaseRealm extends RealmBase

Implementation of {@link Realm} that is based on an implementation of {@link UserDatabase} made available through the global JNDI resources configured for this instance of Catalina. Set the resourceName parameter to the global JNDI resources name for the configured instance of UserDatabase that we should consult.

author
Craig R. McClanahan
version
$Revision: 1.4 $ $Date: 2007/05/05 05:32:17 $
since
4.1

Fields Summary
protected org.apache.catalina.UserDatabase
database
The UserDatabase we will use to authenticate users and identify associated roles.
protected final String
info
Descriptive information about this Realm implementation.
protected static final String
name
Descriptive information about this Realm implementation.
protected String
resourceName
The global JNDI name of the UserDatabase resource we will be utilizing.
private static org.apache.catalina.util.StringManager
sm
The string manager for this package.
Constructors Summary
Methods Summary
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


        // Does a user with this username exist?
        User user = database.findUser(username);
        if (user == null) {
            return (null);
        }

        // Do the credentials specified by the user match?
        // FIXME - Update all realms to support encoded passwords
        boolean validated = false;
        if (hasMessageDigest()) {
            // Hex hashes should be compared case-insensitive
            validated = (digest(credentials)
                         .equalsIgnoreCase(user.getPassword()));
        } else {
            validated =
                (digest(credentials).equals(user.getPassword()));
        }
        if (!validated) {
            if (debug >= 2) {
                log(sm.getString("userDatabaseRealm.authenticateFailure",
                                 username));
            }
            return (null);
        }

        // Construct a GenericPrincipal that represents this user
        if (debug >= 2) {
            log(sm.getString("userDatabaseRealm.authenticateSuccess",
                             username));
        }
        ArrayList combined = new ArrayList();
        Iterator roles = user.getRoles();
        while (roles.hasNext()) {
            Role role = (Role) roles.next();
            String rolename = role.getRolename();
            if (!combined.contains(rolename)) {
                combined.add(rolename);
            }
        }
        Iterator groups = user.getGroups();
        while (groups.hasNext()) {
            Group group = (Group) groups.next();
            roles = group.getRoles();
            while (roles.hasNext()) {
                Role role = (Role) roles.next();
                String rolename = role.getRolename();
                if (!combined.contains(rolename)) {
                    combined.add(rolename);
                }
            }
        }
        return (new GenericPrincipal(this, user.getUsername(),
                                     user.getPassword(), combined));

    
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.


        return (null);

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


        return (null);

    
public java.lang.StringgetResourceName()
Return the global JNDI name of the UserDatabase resource we will be using.


        return resourceName;

    
public voidsetResourceName(java.lang.String resourceName)
Set the global JNDI name of the UserDatabase resource we will be using.

param
resourceName The new global JNDI name


        this.resourceName = resourceName;

    
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


        try {
            StandardServer server = (StandardServer) ServerFactory.getServer();
            Context context = server.getGlobalNamingContext();
            database = (UserDatabase) context.lookup(resourceName);
        } catch (Throwable e) {
            e.printStackTrace();
            log(sm.getString("userDatabaseRealm.lookup", resourceName), e);
            database = null;
        }
        if (database == null) {
            throw new LifecycleException
                (sm.getString("userDatabaseRealm.noDatabase", resourceName));
        }

        // 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();

        // Release reference to our user database
        database = null;