Coverage Report - org.acegisecurity.vote.AbstractAccessDecisionManager
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractAccessDecisionManager
78% 
71% 
2
 
 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.vote;
 17  
 
 18  
 import java.util.Iterator;
 19  
 import java.util.List;
 20  
 
 21  
 import org.acegisecurity.AccessDecisionManager;
 22  
 import org.acegisecurity.AccessDeniedException;
 23  
 import org.acegisecurity.AcegiMessageSource;
 24  
 import org.acegisecurity.ConfigAttribute;
 25  
 import org.springframework.beans.factory.InitializingBean;
 26  
 import org.springframework.context.MessageSource;
 27  
 import org.springframework.context.MessageSourceAware;
 28  
 import org.springframework.context.support.MessageSourceAccessor;
 29  
 import org.springframework.util.Assert;
 30  
 
 31  
 /**
 32  
  * Abstract implementation of {@link AccessDecisionManager}.
 33  
  * <p/>
 34  
  * Handles configuration of a bean context defined list of
 35  
  * {@link AccessDecisionVoter}s and the access control behaviour if all voters
 36  
  * abstain from voting (defaults to deny access).
 37  
  * </p>
 38  
  */
 39  21
 public abstract class AbstractAccessDecisionManager implements AccessDecisionManager, InitializingBean,
 40  
         MessageSourceAware {
 41  
     // ~ Instance fields
 42  
     // ================================================================================================
 43  
 
 44  
     private List decisionVoters;
 45  
 
 46  21
     protected MessageSourceAccessor messages = AcegiMessageSource.getAccessor();
 47  
 
 48  21
     private boolean allowIfAllAbstainDecisions = false;
 49  
 
 50  
     // ~ Methods
 51  
     // ========================================================================================================
 52  
 
 53  
     public void afterPropertiesSet() throws Exception {
 54  1
         Assert.notEmpty(this.decisionVoters, "A list of AccessDecisionVoters is required");
 55  1
         Assert.notNull(this.messages, "A message source must be set");
 56  1
     }
 57  
 
 58  
     protected final void checkAllowIfAllAbstainDecisions() {
 59  6
         if (!this.isAllowIfAllAbstainDecisions()) {
 60  3
             throw new AccessDeniedException(messages.getMessage("AbstractAccessDecisionManager.accessDenied",
 61  
                     "Access is denied"));
 62  
         }
 63  3
     }
 64  
 
 65  
     public List getDecisionVoters() {
 66  67
         return this.decisionVoters;
 67  
     }
 68  
 
 69  
     public boolean isAllowIfAllAbstainDecisions() {
 70  12
         return allowIfAllAbstainDecisions;
 71  
     }
 72  
 
 73  
     public void setAllowIfAllAbstainDecisions(boolean allowIfAllAbstainDecisions) {
 74  4
         this.allowIfAllAbstainDecisions = allowIfAllAbstainDecisions;
 75  4
     }
 76  
 
 77  
     public void setDecisionVoters(List newList) {
 78  21
         Assert.notEmpty(newList);
 79  
 
 80  21
         Iterator iter = newList.iterator();
 81  
 
 82  83
         while (iter.hasNext()) {
 83  62
             Object currentObject = iter.next();
 84  63
             Assert.isInstanceOf(AccessDecisionVoter.class, currentObject, "AccessDecisionVoter " + currentObject.getClass().getName()
 85  
                     + " must implement AccessDecisionVoter");
 86  62
         }
 87  
 
 88  21
         this.decisionVoters = newList;
 89  21
     }
 90  
 
 91  
     public void setMessageSource(MessageSource messageSource) {
 92  1
         this.messages = new MessageSourceAccessor(messageSource);
 93  1
     }
 94  
 
 95  
     public boolean supports(ConfigAttribute attribute) {
 96  0
         Iterator iter = this.decisionVoters.iterator();
 97  
 
 98  0
         while (iter.hasNext()) {
 99  0
             AccessDecisionVoter voter = (AccessDecisionVoter) iter.next();
 100  
 
 101  0
             if (voter.supports(attribute)) {
 102  0
                 return true;
 103  
             }
 104  0
         }
 105  
 
 106  0
         return false;
 107  
     }
 108  
 
 109  
     /**
 110  
      * Iterates through all <code>AccessDecisionVoter</code>s and ensures
 111  
      * each can support the presented class.
 112  
      * <p/>
 113  
      * If one or more voters cannot support the presented class,
 114  
      * <code>false</code> is returned.
 115  
      * </p>
 116  
      *
 117  
      * @param clazz DOCUMENT ME!
 118  
      * @return DOCUMENT ME!
 119  
      */
 120  
     public boolean supports(Class clazz) {
 121  1
         Iterator iter = this.decisionVoters.iterator();
 122  
 
 123  3
         while (iter.hasNext()) {
 124  2
             AccessDecisionVoter voter = (AccessDecisionVoter) iter.next();
 125  
 
 126  2
             if (!voter.supports(clazz)) {
 127  0
                 return false;
 128  
             }
 129  2
         }
 130  
 
 131  1
         return true;
 132  
     }
 133  
 }