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 org.apache.avalon.framework.logger.AbstractLogEnabled;
022: import org.apache.avalon.framework.service.ServiceException;
023: import org.apache.avalon.framework.service.ServiceManager;
024: import org.apache.avalon.framework.service.Serviceable;
025: import org.apache.avalon.framework.thread.ThreadSafe;
026: import org.apache.excalibur.source.SourceResolver;
027: import org.apache.lenya.ac.AccessControlException;
028: import org.apache.lenya.ac.AccessController;
029: import org.apache.lenya.ac.AccessControllerResolver;
030: import org.apache.lenya.ac.cache.URLKeyUtil;
031: import org.apache.lenya.util.CacheMap;
032:
033: /**
034: * Abstract implementation for access controller resolvers.
035: * @version $Id: AbstractAccessControllerResolver.java 531479 2007-04-23 14:21:35Z andreas $
036: */
037: public abstract class AbstractAccessControllerResolver extends
038: AbstractLogEnabled implements AccessControllerResolver,
039: Serviceable, ThreadSafe {
040:
041: protected static final int CAPACITY = 1000;
042: private CacheMap cache;
043:
044: protected CacheMap getCache() {
045: if (this .cache == null) {
046: this .cache = new CacheMap(CAPACITY, getLogger());
047: }
048: return this .cache;
049: }
050:
051: /**
052: * @see org.apache.lenya.ac.AccessControllerResolver#resolveAccessController(java.lang.String)
053: */
054: public AccessController resolveAccessController(String webappUrl)
055: throws AccessControlException {
056:
057: SourceResolver resolver = null;
058: AccessController controller = null;
059: Object key = null;
060:
061: try {
062: resolver = (SourceResolver) getManager().lookup(
063: SourceResolver.ROLE);
064: key = generateCacheKey(webappUrl, resolver);
065: getLogger().debug(
066: "Access controller cache key: [" + key + "]");
067:
068: } catch (Exception e) {
069: throw new AccessControlException(e);
070: } finally {
071: if (resolver != null) {
072: getManager().release(resolver);
073: }
074: }
075:
076: CacheMap cache = getCache();
077:
078: synchronized (cache) {
079: controller = (AccessController) cache.get(key);
080: if (controller == null) {
081: getLogger().debug("No access controller in cache.");
082: controller = doResolveAccessController(webappUrl);
083: cache.put(key, controller);
084: } else {
085: getLogger().debug(
086: "Getting access controller from cache.");
087: }
088: }
089:
090: return controller;
091: }
092:
093: /**
094: * Generates a cache key for the access controller.
095: * @param webappUrl The webapp URL.
096: * @param resolver The source resolver.
097: * @return An object.
098: * @throws AccessControlException when something went wrong.
099: */
100: protected Object generateCacheKey(String webappUrl,
101: SourceResolver resolver) throws AccessControlException {
102: Object key;
103: try {
104: key = URLKeyUtil.generateKey(resolver, webappUrl);
105: } catch (Exception e) {
106: throw new AccessControlException(e);
107: }
108: return key;
109: }
110:
111: /**
112: * The actual resolving method.
113: * @param webappUrl The URL within the web application.
114: * @return An access controller.
115: * @throws AccessControlException when something went wrong.
116: */
117: protected abstract AccessController doResolveAccessController(
118: String webappUrl) throws AccessControlException;
119:
120: /**
121: * @see org.apache.lenya.ac.AccessControllerResolver#release(org.apache.lenya.ac.AccessController)
122: */
123: public void release(AccessController controller) {
124: /*
125: if (controller != null) {
126: getManager().release(controller);
127: }
128: */
129: }
130:
131: protected ServiceManager manager;
132:
133: /**
134: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
135: */
136: public void service(ServiceManager _manager)
137: throws ServiceException {
138: getLogger().debug("Servicing [" + getClass().getName() + "]");
139: this .manager = _manager;
140: }
141:
142: /**
143: * Returns the service manager of this Serviceable.
144: * @return A service manager.
145: */
146: public ServiceManager getManager() {
147: return this.manager;
148: }
149:
150: }
|