View Javadoc

    1   /* Copyright 2004 Acegi Technology Pty Limited
    2    *
    3    * Licensed under the Apache License, Version 2.0 (the "License");
    4    * you may not use this file except in compliance with the License.
    5    * You may obtain a copy of the License at
    6    *
    7    *     http://www.apache.org/licenses/LICENSE-2.0
    8    *
    9    * Unless required by applicable law or agreed to in writing, software
    10   * distributed under the License is distributed on an "AS IS" BASIS,
    11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12   * See the License for the specific language governing permissions and
    13   * limitations under the License.
    14   */
    15
    16  package org.acegisecurity.acl;
    17
    18  import org.acegisecurity.Authentication;
    19
    20  import org.apache.commons.logging.Log;
    21  import org.apache.commons.logging.LogFactory;
    22
    23  import org.springframework.beans.factory.InitializingBean;
    24  import org.springframework.util.Assert;
    25
    26  import java.util.Iterator;
    27  import java.util.List;
    28
    29
    30  /***
    31   * Iterates through a list of {@link AclProvider}s to locate the ACLs that
    32   * apply to a given domain object instance.
    33   *
    34   * <P>
    35   * If no compatible provider is found, it is assumed that no ACLs apply for the
    36   * specified domain object instance and <code>null</code> is returned.
    37   * </p>
    38   *
    39   * @author Ben Alex
    40   * @version $Id: AclProviderManager.java,v 1.4 2005/11/17 00:55:51 benalex Exp $
    41   */
    42  public class AclProviderManager implements AclManager, InitializingBean {
    43      //~ Static fields/initializers =============================================
    44
    45      private static final Log logger = LogFactory.getLog(AclProviderManager.class);
    46
    47      //~ Instance fields ========================================================
    48
    49      private List providers;
    50
    51      //~ Methods ================================================================
    52
    53      public AclEntry[] getAcls(Object domainInstance) {
    54          Assert.notNull(domainInstance, "domainInstance is null - violating interface contract");
    55
    56          Iterator iter = providers.iterator();
    57
    58          while (iter.hasNext()) {
    59              AclProvider provider = (AclProvider) iter.next();
    60
    61              if (provider.supports(domainInstance)) {
    62                  if (logger.isDebugEnabled()) {
    63                      logger.debug("ACL lookup using "
    64                              + provider.getClass().getName());
    65                  }
    66
    67                  return provider.getAcls(domainInstance);
    68              }
    69          }
    70
    71          if (logger.isDebugEnabled()) {
    72              logger.debug("No AclProvider found for "
    73                      + domainInstance.toString());
    74          }
    75
    76          return null;
    77      }
    78
    79      public AclEntry[] getAcls(Object domainInstance,
    80          Authentication authentication) {
    81          Assert.notNull(domainInstance, "domainInstance is null - violating interface contract");
    82          Assert.notNull(authentication, "authentication is null - violating interface contract");
    83
    84          Iterator iter = providers.iterator();
    85
    86          while (iter.hasNext()) {
    87              AclProvider provider = (AclProvider) iter.next();
    88
    89              if (provider.supports(domainInstance)) {
    90                  if (logger.isDebugEnabled()) {
    91                      logger.debug("ACL lookup using "
    92                              + provider.getClass().getName());
    93                  }
    94
    95                  return provider.getAcls(domainInstance, authentication);
    96              } else {
    97                  if (logger.isDebugEnabled()) {
    98                      logger.debug("Provider " + provider.toString()
    99                              + " does not support " + domainInstance);
    100                 }
    101             }
    102         }
    103
    104         if (logger.isDebugEnabled()) {
    105             logger.debug("No AclProvider found for "
    106                     + domainInstance.toString());
    107         }
    108
    109         return null;
    110     }
    111
    112     /***
    113      * Sets the {@link AclProvider} objects to be used for ACL determinations.
    114      *
    115      * @param newList that should be used for ACL determinations
    116      *
    117      * @throws IllegalArgumentException if an invalid provider was included in
    118      *         the list
    119      */
    120     public void setProviders(List newList) {
    121         checkIfValidList(newList);
    122
    123         Iterator iter = newList.iterator();
    124
    125         while (iter.hasNext()) {
    126             Object currentObject = null;
    127
    128             try {
    129                 currentObject = iter.next();
    130
    131                 AclProvider attemptToCast = (AclProvider) currentObject;
    132             } catch (ClassCastException cce) {
    133                 throw new IllegalArgumentException("AclProvider "
    134                     + currentObject.getClass().getName()
    135                     + " must implement AclProvider");
    136             }
    137         }
    138
    139         this.providers = newList;
    140     }
    141
    142     public List getProviders() {
    143         return this.providers;
    144     }
    145
    146     public void afterPropertiesSet() throws Exception {
    147         checkIfValidList(this.providers);
    148     }
    149
    150     private void checkIfValidList(List listToCheck) {
    151         Assert.notEmpty(listToCheck, "A list of AclManagers is required");
    152     }
    153 }