View Javadoc

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  public class AclProviderManager implements AclManager, InitializingBean {
40      //~ Static fields/initializers =====================================================================================
41  
42      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          checkIfValidList(this.providers);
52      }
53  
54      private void checkIfValidList(List listToCheck) {
55          Assert.notEmpty(listToCheck, "A list of AclManagers is required");
56      }
57  
58      public AclEntry[] getAcls(Object domainInstance) {
59          Assert.notNull(domainInstance, "domainInstance is null - violating interface contract");
60  
61          Iterator iter = providers.iterator();
62  
63          while (iter.hasNext()) {
64              AclProvider provider = (AclProvider) iter.next();
65  
66              if (provider.supports(domainInstance)) {
67                  if (logger.isDebugEnabled()) {
68                      logger.debug("ACL lookup using " + provider.getClass().getName());
69                  }
70  
71                  return provider.getAcls(domainInstance);
72              }
73          }
74  
75          if (logger.isDebugEnabled()) {
76              logger.debug("No AclProvider found for " + domainInstance.toString());
77          }
78  
79          return null;
80      }
81  
82      public AclEntry[] getAcls(Object domainInstance, Authentication authentication) {
83          Assert.notNull(domainInstance, "domainInstance is null - violating interface contract");
84          Assert.notNull(authentication, "authentication is null - violating interface contract");
85  
86          Iterator iter = providers.iterator();
87  
88          while (iter.hasNext()) {
89              AclProvider provider = (AclProvider) iter.next();
90  
91              if (provider.supports(domainInstance)) {
92                  if (logger.isDebugEnabled()) {
93                      logger.debug("ACL lookup using " + provider.getClass().getName());
94                  }
95  
96                  return provider.getAcls(domainInstance, authentication);
97              } else {
98                  if (logger.isDebugEnabled()) {
99                      logger.debug("Provider " + provider.toString() + " does not support " + domainInstance);
100                 }
101             }
102         }
103 
104         if (logger.isDebugEnabled()) {
105             logger.debug("No AclProvider found for " + domainInstance.toString());
106         }
107 
108         return null;
109     }
110 
111     public List getProviders() {
112         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         checkIfValidList(newList);
124 
125         Iterator iter = newList.iterator();
126 
127         while (iter.hasNext()) {
128             Object currentObject = null;
129 
130             try {
131                 currentObject = iter.next();
132 
133                 AclProvider attemptToCast = (AclProvider) currentObject;
134             } catch (ClassCastException cce) {
135                 throw new IllegalArgumentException("AclProvider " + currentObject.getClass().getName()
136                     + " must implement AclProvider");
137             }
138         }
139 
140         this.providers = newList;
141     }
142 }