001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.acl;
017:
018: import org.acegisecurity.Authentication;
019:
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022:
023: import org.springframework.beans.factory.InitializingBean;
024:
025: import org.springframework.util.Assert;
026:
027: import java.util.Iterator;
028: import java.util.List;
029:
030: /**
031: * Iterates through a list of {@link AclProvider}s to locate the ACLs that apply to a given domain object instance.<P>If
032: * no compatible provider is found, it is assumed that no ACLs apply for the specified domain object instance and
033: * <code>null</code> is returned.</p>
034: *
035: * @author Ben Alex
036: * @version $Id: AclProviderManager.java 1496 2006-05-23 13:38:33Z benalex $
037: */
038: public class AclProviderManager implements AclManager, InitializingBean {
039: //~ Static fields/initializers =====================================================================================
040:
041: private static final Log logger = LogFactory
042: .getLog(AclProviderManager.class);
043:
044: //~ Instance fields ================================================================================================
045:
046: private List providers;
047:
048: //~ Methods ========================================================================================================
049:
050: public void afterPropertiesSet() throws Exception {
051: checkIfValidList(this .providers);
052: }
053:
054: private void checkIfValidList(List listToCheck) {
055: Assert.notEmpty(listToCheck,
056: "A list of AclManagers is required");
057: }
058:
059: public AclEntry[] getAcls(Object domainInstance) {
060: Assert
061: .notNull(domainInstance,
062: "domainInstance is null - violating interface contract");
063:
064: Iterator iter = providers.iterator();
065:
066: while (iter.hasNext()) {
067: AclProvider provider = (AclProvider) iter.next();
068:
069: if (provider.supports(domainInstance)) {
070: if (logger.isDebugEnabled()) {
071: logger.debug("ACL lookup using "
072: + provider.getClass().getName());
073: }
074:
075: return provider.getAcls(domainInstance);
076: }
077: }
078:
079: if (logger.isDebugEnabled()) {
080: logger.debug("No AclProvider found for "
081: + domainInstance.toString());
082: }
083:
084: return null;
085: }
086:
087: public AclEntry[] getAcls(Object domainInstance,
088: Authentication authentication) {
089: Assert
090: .notNull(domainInstance,
091: "domainInstance is null - violating interface contract");
092: Assert
093: .notNull(authentication,
094: "authentication is null - violating interface contract");
095:
096: Iterator iter = providers.iterator();
097:
098: while (iter.hasNext()) {
099: AclProvider provider = (AclProvider) iter.next();
100:
101: if (provider.supports(domainInstance)) {
102: if (logger.isDebugEnabled()) {
103: logger.debug("ACL lookup using "
104: + provider.getClass().getName());
105: }
106:
107: return provider.getAcls(domainInstance, authentication);
108: } else {
109: if (logger.isDebugEnabled()) {
110: logger.debug("Provider " + provider.toString()
111: + " does not support " + domainInstance);
112: }
113: }
114: }
115:
116: if (logger.isDebugEnabled()) {
117: logger.debug("No AclProvider found for "
118: + domainInstance.toString());
119: }
120:
121: return null;
122: }
123:
124: public List getProviders() {
125: return this .providers;
126: }
127:
128: /**
129: * Sets the {@link AclProvider} objects to be used for ACL determinations.
130: *
131: * @param newList that should be used for ACL determinations
132: *
133: * @throws IllegalArgumentException if an invalid provider was included in the list
134: */
135: public void setProviders(List newList) {
136: checkIfValidList(newList);
137:
138: Iterator iter = newList.iterator();
139:
140: while (iter.hasNext()) {
141: Object currentObject = null;
142:
143: try {
144: currentObject = iter.next();
145:
146: AclProvider attemptToCast = (AclProvider) currentObject;
147: } catch (ClassCastException cce) {
148: throw new IllegalArgumentException("AclProvider "
149: + currentObject.getClass().getName()
150: + " must implement AclProvider");
151: }
152: }
153:
154: this.providers = newList;
155: }
156: }
|