craigmcc    01/03/31 18:29:47

  Modified:    catalina/src/share/org/apache/catalina/realm
                        MemoryRealm.java
  Added:       catalina/src/share/org/apache/catalina/realm
                        GenericPrincipal.java
  Log:
  Refactor the Principal that is synthesized in MemoryRealm into a separate
  class that can be reused by other Realm implementations.  Modify
  MemoryRealm to use the new class.
  
  Revision  Changes    Path
  1.3       +29 -122   
jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/realm/MemoryRealm.java
  
  Index: MemoryRealm.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/realm/MemoryRealm.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- MemoryRealm.java  2000/12/31 16:30:35     1.2
  +++ MemoryRealm.java  2001/04/01 02:29:46     1.3
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/realm/MemoryRealm.java,v
 1.2 2000/12/31 16:30:35 nacho Exp $
  - * $Revision: 1.2 $
  - * $Date: 2000/12/31 16:30:35 $
  + * $Header: 
/home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/realm/MemoryRealm.java,v
 1.3 2001/04/01 02:29:46 craigmcc Exp $
  + * $Revision: 1.3 $
  + * $Date: 2001/04/01 02:29:46 $
    *
    * ====================================================================
    *
  @@ -69,6 +69,7 @@
   import java.beans.PropertyChangeSupport;
   import java.security.Principal;
   import java.io.File;
  +import java.util.ArrayList;
   import java.util.HashMap;
   import org.apache.catalina.Container;
   import org.apache.catalina.Lifecycle;
  @@ -96,7 +97,7 @@
    * synchronization is performed around accesses to the principals collection.
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.2 $ $Date: 2000/12/31 16:30:35 $
  + * @version $Revision: 1.3 $ $Date: 2001/04/01 02:29:46 $
    */
   
   public final class MemoryRealm
  @@ -151,6 +152,7 @@
        */
       private boolean started = false;
   
  +
       // ------------------------------------------------------------- Properties
   
   
  @@ -189,8 +191,8 @@
        */
       public Principal authenticate(String username, String credentials) {
   
  -     MemoryRealmPrincipal principal =
  -         (MemoryRealmPrincipal) principals.get(username);
  +     GenericPrincipal principal =
  +         (GenericPrincipal) principals.get(username);
        if ((principal != null) &&
            (credentials.equals(principal.getPassword()))) {
            if (debug > 1)
  @@ -216,10 +218,12 @@
       public boolean hasRole(Principal principal, String role) {
   
        if ((principal == null) || (role == null) ||
  -         !(principal instanceof MemoryRealmPrincipal))
  +         !(principal instanceof GenericPrincipal))
            return (false);
  -
  -     boolean result = ((MemoryRealmPrincipal) principal).hasRole(role);
  +        GenericPrincipal gp = (GenericPrincipal) principal;
  +        if (!(gp.getRealm() == this))
  +            return (false);
  +     boolean result = gp.hasRole(role);
        if (debug > 1) {
            String name = principal.getName();
            if (result)
  @@ -244,20 +248,23 @@
        */
       void addUser(String username, String password, String roles) {
   
  -     MemoryRealmPrincipal principal =
  -         new MemoryRealmPrincipal(username, password);
  -     principals.put(username, principal);
  -
  -     roles += ",";
  +        // 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();
  -         principal.addRole(role);
  +            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);
  +
       }
   
   
  @@ -268,8 +275,8 @@
        * Return the password associated with the given principal's user name.
        */
       protected String getPassword(String username) {
  -     MemoryRealmPrincipal principal =
  -         (MemoryRealmPrincipal) principals.get(username);
  +     GenericPrincipal principal =
  +         (GenericPrincipal) principals.get(username);
        if (principal != null) {
            return (principal.getPassword());
        } else {
  @@ -287,6 +294,9 @@
   
   
   
  +    // ------------------------------------------------------ Lifecycle Methods
  +
  +
       /**
        * Prepare for active use of the public methods of this Component.
        *
  @@ -352,111 +362,6 @@
   
   
   /**
  - * Private class representing an individual user's Principal object.
  - */
  -
  -final class MemoryRealmPrincipal implements Principal {
  -
  -
  -    /**
  -     * The password for this Principal.
  -     */
  -    private String password = null;
  -
  -
  -    /**
  -     * The role names possessed by this Principal.
  -     */
  -    private String roles[] = new String[0];
  -
  -
  -    /**
  -     * The username for this Principal.
  -     */
  -    private String username = null;
  -
  -
  -    /**
  -     * Construct a new MemoryRealmPrincipal instance.
  -     *
  -     * @param username The username for this Principal
  -     * @param password The password for this Principal
  -     */
  -    public MemoryRealmPrincipal(String username, String password) {
  -
  -     this.username = username;
  -     this.password = password;
  -
  -    }
  -
  -
  -    /**
  -     * Add a new role name to the set possessed by this Principal.
  -     *
  -     * @param role The role to be added
  -     */
  -    public void addRole(String role) {
  -
  -     if (role == null)
  -         return;
  -
  -     for (int i = 0; i < roles.length; i++) {
  -         if (role.equals(roles[i]))
  -             return;
  -     }
  -
  -     String results[] = new String[roles.length + 1];
  -     for (int i = 0; i < roles.length; i++)
  -         results[i] = roles[i];
  -     results[roles.length] = role;
  -
  -        roles = results;
  -
  -    }
  -
  -
  -    /**
  -     * Return the name of this Principal.
  -     */
  -    public String getName() {
  -
  -     return (username);
  -
  -    }
  -
  -
  -    /**
  -     * Return the password of this Principal.
  -     */
  -    public String getPassword() {
  -
  -     return (password);
  -
  -    }
  -
  -
  -    /**
  -     * Does this Principal possess the specified role?
  -     *
  -     * @param role Role to be checked
  -     */
  -    public boolean hasRole(String role) {
  -
  -     if (role == null)
  -         return (false);
  -        for (int i = 0; i < roles.length; i++) {
  -            if (role.equals(roles[i]))
  -             return (true);
  -     }
  -     return (false);
  -
  -    }
  -
  -
  -}
  -
  -
  -/**
    * Private class used when parsing the XML database file.
    */
   final class MemoryRealmUserAction extends XmlAction {
  @@ -479,4 +384,6 @@
        realm.addUser(username, password, roles);
   
       }
  +
  +
   }
  
  
  
  1.1                  
jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/realm/GenericPrincipal.java
  
  Index: GenericPrincipal.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/realm/GenericPrincipal.java,v
 1.1 2001/04/01 02:29:46 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2001/04/01 02:29:46 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights 
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:  
   *       "This product includes software developed by the 
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written 
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */ 
  
  
  package org.apache.catalina.realm;
  
  
  import java.security.Principal;
  import java.util.List;
  import org.apache.catalina.Realm;
  
  
  /**
   * Generic implementation of <strong>java.security.Principal</strong> that
   * is available for use by <code>Realm</code> implementations.  The class
   * and all of its methods (except for <code>getName()</code>) are package
   * private to avoid interference from applications.
   *
   * @author Craig R. McClanahan
   * @version $Revision: 1.1 $ $Date: 2001/04/01 02:29:46 $
   */
  
  class GenericPrincipal implements Principal {
  
  
      // ----------------------------------------------------------- Constructors
  
  
      /**
       * Construct a new Principal, associated with the specified Realm, for the
       * specified username and password.
       *
       * @param realm The Realm that owns this Principal
       * @param name The username of the user represented by this Principal
       * @param password Credentials used to authenticate this user
       */
      GenericPrincipal(Realm realm, String name, String password) {
  
          this(realm, name, password, null);
  
      }
  
  
      /**
       * Construct a new Principal, associated with the specified Realm, for the
       * specified username and password, with the specified role names
       * (as Strings).
       *
       * @param realm The Realm that owns this principal
       * @param name The username of the user represented by this Principal
       * @param password Credentials used to authenticate this user
       * @param roles List of roles (must be Strings) possessed by this user
       */
      GenericPrincipal(Realm realm, String name, String password, List roles) {
  
          super();
          this.realm = realm;
          this.name = name;
          this.password = password;
          if (roles != null) {
              this.roles = new String[roles.size()];
              this.roles = (String[]) roles.toArray(this.roles);
          }
  
      }
  
  
      // ------------------------------------------------------------- Properties
  
  
      /**
       * The username of the user represented by this Principal.
       */
      protected String name = null;
  
      public String getName() {
          return (this.name);
      }
  
  
      /**
       * The authentication credentials for the user represented by
       * this Principal.
       */
      protected String password = null;
  
      String getPassword() {
          return (this.password);
      }
  
  
      /**
       * The Realm with which this Principal is associated.
       */
      protected Realm realm = null;
  
      Realm getRealm() {
          return (this.realm);
      }
  
  
      /**
       * The set of roles associated with this user.
       */
      protected String roles[] = new String[0];
  
      String[] getRoles() {
          return (this.roles);
      }
  
  
      // --------------------------------------------------------- Public Methods
  
  
      /**
       * Return a String representation of this object, which exposes only
       * information that should be public.
       */
      public String toString() {
  
          StringBuffer sb = new StringBuffer("GenericPrincipal[");
          sb.append(this.name);
          sb.append("]");
          return (sb.toString());
  
      }
  
  
      // -------------------------------------------------------- Package Methods
  
  
      /**
       * Does the user represented by this Principal possess the specified role?
       *
       * @param role Role to be tested
       */
      boolean hasRole(String role) {
  
          if (role == null)
              return (false);
          for (int i = 0; i < roles.length; i++) {
              if (role.equals(roles[i]))
                  return (true);
          }
          return (false);
  
      }
  
  
  }
  
  
  

Reply via email to