1 17 18 package org.apache.lenya.ac.impl; 19 20 import java.util.HashMap ; 21 import java.util.Map ; 22 23 import org.apache.avalon.framework.activity.Disposable; 24 import org.apache.avalon.framework.configuration.Configurable; 25 import org.apache.avalon.framework.configuration.Configuration; 26 import org.apache.avalon.framework.configuration.ConfigurationException; 27 import org.apache.avalon.framework.service.ServiceException; 28 import org.apache.avalon.framework.service.ServiceSelector; 29 import org.apache.lenya.ac.AccessControlException; 30 import org.apache.lenya.ac.AccessController; 31 import org.apache.lenya.ac.AccessControllerResolver; 32 33 39 public class ComposableAccessControllerResolver 40 extends AbstractAccessControllerResolver 41 implements Configurable, Disposable { 42 43 46 public AccessController doResolveAccessController(String url) throws AccessControlException { 47 48 AccessController controller = null; 49 50 try { 51 52 if (selector == null) { 53 selector = 54 (ServiceSelector) getManager().lookup(AccessControllerResolver.ROLE + "Selector"); 55 } 56 57 String [] types = getResolverTypes(); 58 int i = 0; 59 while (controller == null && i < types.length) { 60 61 getLogger().debug("Trying to resolve AC resolver for type [" + types[i] + "]"); 62 AccessControllerResolver resolver = 63 (AccessControllerResolver) selector.select(types[i]); 64 controller = resolver.resolveAccessController(url); 65 setResolver(controller, resolver); 66 getLogger().debug("Resolved access controller [" + controller + "]"); 67 i++; 68 } 69 70 } catch (ServiceException e) { 71 throw new AccessControlException(e); 72 } 73 74 return controller; 75 } 76 77 private Map controllerToResolver = new HashMap (); 78 79 82 public void release(AccessController controller) { 83 assert controller != null; 84 AccessControllerResolver resolver = getResolver(controller); 85 resolver.release(controller); 86 selector.release(resolver); 87 } 88 89 95 protected AccessControllerResolver getResolver(AccessController controller) { 96 AccessControllerResolver resolver = 97 (AccessControllerResolver) controllerToResolver.get(controller); 98 return resolver; 99 } 100 101 107 protected void setResolver(AccessController controller, AccessControllerResolver resolver) { 108 controllerToResolver.put(controller, resolver); 109 } 110 111 protected static final String RESOLVER_ELEMENT = "resolver"; 112 protected static final String TYPE_ATTRIBUTE = "type"; 113 114 private String [] resolverTypes; 115 private ServiceSelector selector; 116 117 120 public void configure(Configuration configuration) throws ConfigurationException { 121 Configuration[] accessControllerConfigs = configuration.getChildren(RESOLVER_ELEMENT); 122 resolverTypes = new String [accessControllerConfigs.length]; 123 for (int i = 0; i < accessControllerConfigs.length; i++) { 124 resolverTypes[i] = accessControllerConfigs[i].getAttribute(TYPE_ATTRIBUTE); 125 } 126 } 127 128 132 protected String [] getResolverTypes() { 133 return resolverTypes; 134 } 135 136 139 public void dispose() { 140 if (selector != null) { 141 getManager().release(selector); 142 } 143 } 144 145 } 146 | Popular Tags |