Coverage Report - org.acegisecurity.acl.AclProviderManager
 
Classes in this File Line Coverage Branch Coverage Complexity
AclProviderManager
89% 
100% 
3.333
 
 1  
 /* Copyright 2004, 2005, 2006 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  
 
 25  
 import org.springframework.util.Assert;
 26  
 
 27  
 import java.util.Iterator;
 28  
 import java.util.List;
 29  
 
 30  
 
 31  
 /**
 32  
  * Iterates through a list of {@link AclProvider}s to locate the ACLs that apply to a given domain object instance.<P>If
 33  
  * no compatible provider is found, it is assumed that no ACLs apply for the specified domain object instance and
 34  
  * <code>null</code> is returned.</p>
 35  
  *
 36  
  * @author Ben Alex
 37  
  * @version $Id: AclProviderManager.java 1496 2006-05-23 13:38:33Z benalex $
 38  
  */
 39  9
 public class AclProviderManager implements AclManager, InitializingBean {
 40  
     //~ Static fields/initializers =====================================================================================
 41  
 
 42  2
     private static final Log logger = LogFactory.getLog(AclProviderManager.class);
 43  
 
 44  
     //~ Instance fields ================================================================================================
 45  
 
 46  
     private List providers;
 47  
 
 48  
     //~ Methods ========================================================================================================
 49  
 
 50  
     public void afterPropertiesSet() throws Exception {
 51  2
         checkIfValidList(this.providers);
 52  1
     }
 53  
 
 54  
     private void checkIfValidList(List listToCheck) {
 55  9
         Assert.notEmpty(listToCheck, "A list of AclManagers is required");
 56  7
     }
 57  
 
 58  
     public AclEntry[] getAcls(Object domainInstance) {
 59  4
         Assert.notNull(domainInstance, "domainInstance is null - violating interface contract");
 60  
 
 61  3
         Iterator iter = providers.iterator();
 62  
 
 63  5
         while (iter.hasNext()) {
 64  3
             AclProvider provider = (AclProvider) iter.next();
 65  
 
 66  3
             if (provider.supports(domainInstance)) {
 67  1
                 if (logger.isDebugEnabled()) {
 68  0
                     logger.debug("ACL lookup using " + provider.getClass().getName());
 69  
                 }
 70  
 
 71  1
                 return provider.getAcls(domainInstance);
 72  
             }
 73  2
         }
 74  
 
 75  2
         if (logger.isDebugEnabled()) {
 76  0
             logger.debug("No AclProvider found for " + domainInstance.toString());
 77  
         }
 78  
 
 79  2
         return null;
 80  
     }
 81  
 
 82  
     public AclEntry[] getAcls(Object domainInstance, Authentication authentication) {
 83  4
         Assert.notNull(domainInstance, "domainInstance is null - violating interface contract");
 84  3
         Assert.notNull(authentication, "authentication is null - violating interface contract");
 85  
 
 86  2
         Iterator iter = providers.iterator();
 87  
 
 88  3
         while (iter.hasNext()) {
 89  2
             AclProvider provider = (AclProvider) iter.next();
 90  
 
 91  2
             if (provider.supports(domainInstance)) {
 92  1
                 if (logger.isDebugEnabled()) {
 93  0
                     logger.debug("ACL lookup using " + provider.getClass().getName());
 94  
                 }
 95  
 
 96  1
                 return provider.getAcls(domainInstance, authentication);
 97  
             } else {
 98  1
                 if (logger.isDebugEnabled()) {
 99  0
                     logger.debug("Provider " + provider.toString() + " does not support " + domainInstance);
 100  
                 }
 101  
             }
 102  1
         }
 103  
 
 104  1
         if (logger.isDebugEnabled()) {
 105  0
             logger.debug("No AclProvider found for " + domainInstance.toString());
 106  
         }
 107  
 
 108  1
         return null;
 109  
     }
 110  
 
 111  
     public List getProviders() {
 112  1
         return this.providers;
 113  
     }
 114  
 
 115  
     /**
 116  
      * Sets the {@link AclProvider} objects to be used for ACL determinations.
 117  
      *
 118  
      * @param newList that should be used for ACL determinations
 119  
      *
 120  
      * @throws IllegalArgumentException if an invalid provider was included in the list
 121  
      */
 122  
     public void setProviders(List newList) {
 123  7
         checkIfValidList(newList);
 124  
 
 125  6
         Iterator iter = newList.iterator();
 126  
 
 127  11
         while (iter.hasNext()) {
 128  6
             Object currentObject = null;
 129  
 
 130  
             try {
 131  6
                 currentObject = iter.next();
 132  
 
 133  6
                 AclProvider attemptToCast = (AclProvider) currentObject;
 134  1
             } catch (ClassCastException cce) {
 135  1
                 throw new IllegalArgumentException("AclProvider " + currentObject.getClass().getName()
 136  
                     + " must implement AclProvider");
 137  5
             }
 138  5
         }
 139  
 
 140  5
         this.providers = newList;
 141  5
     }
 142  
 }