1 17 18 package org.apache.lenya.ac.impl; 19 20 import org.apache.avalon.framework.logger.AbstractLogEnabled; 21 import org.apache.avalon.framework.service.ServiceException; 22 import org.apache.avalon.framework.service.ServiceManager; 23 import org.apache.avalon.framework.service.Serviceable; 24 import org.apache.avalon.framework.thread.ThreadSafe; 25 import org.apache.excalibur.source.SourceResolver; 26 import org.apache.lenya.ac.AccessControlException; 27 import org.apache.lenya.ac.AccessController; 28 import org.apache.lenya.ac.AccessControllerResolver; 29 import org.apache.lenya.ac.cache.URLKeyUtil; 30 import org.apache.lenya.util.CacheMap; 31 32 36 public abstract class AbstractAccessControllerResolver 37 extends AbstractLogEnabled 38 implements AccessControllerResolver, Serviceable, ThreadSafe { 39 40 protected static final int CAPACITY = 1000; 41 private CacheMap cache = new CacheMap(CAPACITY); 42 43 46 public AccessController resolveAccessController(String webappUrl) 47 throws AccessControlException { 48 49 SourceResolver resolver = null; 50 AccessController controller = null; 51 Object key = null; 52 53 try { 54 resolver = (SourceResolver) getManager().lookup(SourceResolver.ROLE); 55 key = generateCacheKey(webappUrl, resolver); 56 getLogger().debug("Access controller cache key: [" + key + "]"); 57 58 } catch (Exception e) { 59 throw new AccessControlException(e); 60 } finally { 61 if (resolver != null) { 62 getManager().release(resolver); 63 } 64 } 65 66 synchronized (cache) { 67 controller = (AccessController) cache.get(key); 68 if (controller == null) { 69 getLogger().debug("No access controller in cache."); 70 controller = doResolveAccessController(webappUrl); 71 cache.put(key, controller); 72 } else { 73 getLogger().debug("Getting access controller from cache."); 74 } 75 } 76 77 return controller; 78 } 79 80 87 protected Object generateCacheKey(String webappUrl, SourceResolver resolver) 88 throws AccessControlException { 89 Object key; 90 try { 91 key = URLKeyUtil.generateKey(resolver, webappUrl); 92 } catch (Exception e) { 93 throw new AccessControlException(e); 94 } 95 return key; 96 } 97 98 104 protected abstract AccessController doResolveAccessController(String webappUrl) 105 throws AccessControlException; 106 107 110 public void release(AccessController controller) { 111 116 } 117 118 private ServiceManager manager; 119 120 123 public void service(ServiceManager manager) throws ServiceException { 124 getLogger().debug("Servicing [" + getClass().getName() + "]"); 125 this.manager = manager; 126 } 127 128 132 public ServiceManager getManager() { 133 return manager; 134 } 135 136 } 137 | Popular Tags |