001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.lenya.ac.impl;
020:
021: import java.util.HashMap;
022: import java.util.Map;
023:
024: import org.apache.avalon.framework.activity.Disposable;
025: import org.apache.avalon.framework.configuration.Configurable;
026: import org.apache.avalon.framework.configuration.Configuration;
027: import org.apache.avalon.framework.configuration.ConfigurationException;
028: import org.apache.avalon.framework.service.ServiceException;
029: import org.apache.avalon.framework.service.ServiceSelector;
030: import org.apache.lenya.ac.AccessControlException;
031: import org.apache.lenya.ac.AccessController;
032: import org.apache.lenya.ac.AccessControllerResolver;
033:
034: /**
035: * Access controller resolver composed of other access controller resolvers.
036: * The member resolvers are called one after the other to resolve the access controllers.
037: *
038: * @version $Id: ComposableAccessControllerResolver.java 473861 2006-11-12 03:51:14Z gregor $
039: */
040: public class ComposableAccessControllerResolver extends
041: AbstractAccessControllerResolver implements Configurable,
042: Disposable {
043:
044: /**
045: * @see org.apache.lenya.ac.impl.AbstractAccessControllerResolver#doResolveAccessController(java.lang.String)
046: */
047: public AccessController doResolveAccessController(String url)
048: throws AccessControlException {
049:
050: AccessController controller = null;
051:
052: try {
053:
054: if (this .selector == null) {
055: this .selector = (ServiceSelector) getManager().lookup(
056: AccessControllerResolver.ROLE + "Selector");
057: }
058:
059: String[] types = getResolverTypes();
060: int i = 0;
061: while (controller == null && i < types.length) {
062:
063: getLogger().debug(
064: "Trying to resolve AC resolver for type ["
065: + types[i] + "]");
066: AccessControllerResolver resolver = (AccessControllerResolver) this .selector
067: .select(types[i]);
068: controller = resolver.resolveAccessController(url);
069: setResolver(controller, resolver);
070: getLogger().debug(
071: "Resolved access controller [" + controller
072: + "]");
073: i++;
074: }
075:
076: } catch (ServiceException e) {
077: throw new AccessControlException(e);
078: }
079:
080: return controller;
081: }
082:
083: private Map controllerToResolver = new HashMap();
084:
085: /**
086: * @see org.apache.lenya.ac.AccessControllerResolver#release(org.apache.lenya.ac.AccessController)
087: */
088: public void release(AccessController controller) {
089: assert controller != null;
090: AccessControllerResolver resolver = getResolver(controller);
091: resolver.release(controller);
092: this .selector.release(resolver);
093: }
094:
095: /**
096: * Returns the access controller resolver that was used to resolve a
097: * specific access controller.
098: * @param controller The access controller.
099: * @return An AC resolver.
100: */
101: protected AccessControllerResolver getResolver(
102: AccessController controller) {
103: AccessControllerResolver resolver = (AccessControllerResolver) this .controllerToResolver
104: .get(controller);
105: return resolver;
106: }
107:
108: /**
109: * Sets the access controller resolver that was used to resolve a
110: * specific access controller.
111: * @param controller The access controller.
112: * @param resolver An AC resolver.
113: */
114: protected void setResolver(AccessController controller,
115: AccessControllerResolver resolver) {
116: this .controllerToResolver.put(controller, resolver);
117: }
118:
119: protected static final String RESOLVER_ELEMENT = "resolver";
120: protected static final String TYPE_ATTRIBUTE = "type";
121:
122: private String[] resolverTypes;
123: private ServiceSelector selector;
124:
125: /**
126: * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
127: */
128: public void configure(Configuration configuration)
129: throws ConfigurationException {
130: Configuration[] accessControllerConfigs = configuration
131: .getChildren(RESOLVER_ELEMENT);
132: this .resolverTypes = new String[accessControllerConfigs.length];
133: for (int i = 0; i < accessControllerConfigs.length; i++) {
134: this .resolverTypes[i] = accessControllerConfigs[i]
135: .getAttribute(TYPE_ATTRIBUTE);
136: }
137: }
138:
139: /**
140: * Returns the access controller types.
141: * @return A string array.
142: */
143: protected String[] getResolverTypes() {
144: return this .resolverTypes;
145: }
146:
147: /**
148: * @see org.apache.avalon.framework.activity.Disposable#dispose()
149: */
150: public void dispose() {
151: if (this.selector != null) {
152: getManager().release(this.selector);
153: }
154: }
155:
156: }
|