You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lenya.apache.org by an...@apache.org on 2003/08/12 17:16:25 UTC

cvs commit: cocoon-lenya/src/java/org/apache/lenya/cms/ac2 PublicationAccessControllerResolver.java AccessControllerResolver.java AbstractAccessControllerResolver.java

andreas     2003/08/12 08:16:25

  Modified:    src/java/org/apache/lenya/cms/ac2
                        PublicationAccessControllerResolver.java
                        AccessControllerResolver.java
                        AbstractAccessControllerResolver.java
  Log:
  added access controller caching
  
  Revision  Changes    Path
  1.6       +70 -37    cocoon-lenya/src/java/org/apache/lenya/cms/ac2/PublicationAccessControllerResolver.java
  
  Index: PublicationAccessControllerResolver.java
  ===================================================================
  RCS file: /home/cvs/cocoon-lenya/src/java/org/apache/lenya/cms/ac2/PublicationAccessControllerResolver.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- PublicationAccessControllerResolver.java	11 Aug 2003 16:03:19 -0000	1.5
  +++ PublicationAccessControllerResolver.java	12 Aug 2003 15:16:25 -0000	1.6
  @@ -56,13 +56,13 @@
   package org.apache.lenya.cms.ac2;
   
   import java.io.File;
  -import java.net.URI;
   
   import org.apache.avalon.framework.configuration.Configurable;
   import org.apache.avalon.framework.configuration.Configuration;
   import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
   import org.apache.excalibur.source.Source;
   import org.apache.excalibur.source.SourceResolver;
  +import org.apache.excalibur.source.SourceUtil;
   import org.apache.lenya.cms.ac.AccessControlException;
   import org.apache.lenya.cms.publication.Publication;
   import org.apache.lenya.cms.publication.PublicationException;
  @@ -80,57 +80,66 @@
       protected static final String TYPE_ATTRIBUTE = "type";
   
       /**
  -     * @see org.apache.lenya.cms.ac2.AbstractAccessControllerResolver#doResolveAccessController(java.lang.String)
  +     * This implementation uses the publication ID in combination with the context path
  +     * as cache key.
  +     * @see org.apache.lenya.cms.ac2.AbstractAccessControllerResolver#generateCacheKey(java.lang.String, org.apache.excalibur.source.SourceResolver)
        */
  -    public AccessController doResolveAccessController(String webappUrl)
  +    protected Object generateCacheKey(String webappUrl, SourceResolver resolver)
           throws AccessControlException {
   
  -        return resolveAccessController(webappUrl, "context:///");
  +        Publication publication = getPublication(webappUrl);
  +        String key = "";
  +
  +        if (publication != null) {
  +            key = publication.getId();
  +            getLogger().debug("Using publication ID as cache key: [" + key + "]");
  +        } else {
  +            getLogger().debug("No publication found - using empty string as cache key.");
  +        }
  +
  +        return super.generateCacheKey(key, resolver);
       }
   
       /**
        * @see org.apache.lenya.cms.ac2.AbstractAccessControllerResolver#doResolveAccessController(java.lang.String)
        */
  -    public AccessController resolveAccessController(String webappUrl, String contextUri)
  +    public AccessController doResolveAccessController(String webappUrl)
           throws AccessControlException {
  -
  -        assert webappUrl.startsWith("/");
  -
           getLogger().debug("Resolving controller for URL [" + webappUrl + "]");
   
           AccessController controller = null;
  +        Publication publication = getPublication(webappUrl);
   
  -        // remove leading slash
  -        webappUrl = webappUrl.substring(1);
  +        if (publication != null) {
  +            String publicationUrl = webappUrl.substring(("/" + publication.getId()).length());
  +            controller = resolveAccessController(publication, publicationUrl);
  +        }
  +        return controller;
  +    }
   
  -        if (webappUrl.length() > 0) {
  +    /**
  +     * Returns the publication for the webapp URL or null if the URL is not included
  +     * in a publication.
  +     * @param webappUrl The webapp URL.
  +     * @return A publication.
  +     * @throws AccessControlException when something went wrong.
  +     */
  +    protected Publication getPublication(String webappUrl) throws AccessControlException {
  +        Publication publication = null;
   
  -            SourceResolver resolver = null;
  -            Source contextSource = null;
  -            File contextDir;
  -            try {
  -                resolver = (SourceResolver) getManager().lookup(SourceResolver.ROLE);
  -                contextSource = resolver.resolveURI(contextUri);
  -                contextDir = new File(new URI(contextSource.getURI()));
  -                assert contextDir.isDirectory();
  -            } catch (Exception e) {
  -                throw new AccessControlException(e);
  -            } finally {
  -                if (resolver != null) {
  -                    if (contextSource != null) {
  -                        resolver.release(contextSource);
  -                    }
  -                    getManager().release(resolver);
  -                }
  -            }
  +        assert webappUrl.startsWith("/");
  +        // remove leading slash
  +        String url = webappUrl.substring(1);
  +
  +        if (url.length() > 0) {
   
  -            String publicationId = webappUrl.split("/")[0];
  +            File contextDir = getContext();
  +            String publicationId = url.split("/")[0];
   
               if (PublicationFactory
                   .existsPublication(publicationId, contextDir.getAbsolutePath())) {
   
                   getLogger().debug("Publication [" + publicationId + "] exists.");
  -                Publication publication;
                   try {
                       publication =
                           PublicationFactory.getPublication(
  @@ -140,14 +149,38 @@
                       throw new AccessControlException(e);
                   }
   
  -                String publicationUrl = webappUrl.substring(("/" + publicationId).length());
  -                controller = resolveAccessController(publication, publicationUrl);
               } else {
                   getLogger().debug("Publication [" + publicationId + "] does not exist.");
               }
           }
  +        return publication;
  +    }
   
  -        return controller;
  +    /**
  +     * Returns the servlet context. 
  +     * @return A file.
  +     * @throws AccessControlException when something went wrong.
  +     */
  +    protected File getContext() throws AccessControlException {
  +        SourceResolver resolver = null;
  +        Source contextSource = null;
  +        File contextDir;
  +        try {
  +            resolver = (SourceResolver) getManager().lookup(SourceResolver.ROLE);
  +            contextSource = resolver.resolveURI("context:///");
  +            contextDir = SourceUtil.getFile(contextSource);
  +            assert contextDir.isDirectory();
  +        } catch (Exception e) {
  +            throw new AccessControlException(e);
  +        } finally {
  +            if (resolver != null) {
  +                if (contextSource != null) {
  +                    resolver.release(contextSource);
  +                }
  +                getManager().release(resolver);
  +            }
  +        }
  +        return contextDir;
       }
   
       /**
  @@ -172,7 +205,7 @@
                   String type = configuration.getAttribute(TYPE_ATTRIBUTE);
   
                   boolean authorized;
  -                
  +
                   accessController =
                       (AccessController) getManager().lookup(AccessController.ROLE + "/" + type);
   
  @@ -187,5 +220,5 @@
   
           return accessController;
       }
  -    
  +
   }
  
  
  
  1.6       +2 -2      cocoon-lenya/src/java/org/apache/lenya/cms/ac2/AccessControllerResolver.java
  
  Index: AccessControllerResolver.java
  ===================================================================
  RCS file: /home/cvs/cocoon-lenya/src/java/org/apache/lenya/cms/ac2/AccessControllerResolver.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- AccessControllerResolver.java	11 Aug 2003 16:03:19 -0000	1.5
  +++ AccessControllerResolver.java	12 Aug 2003 15:16:25 -0000	1.6
  @@ -67,7 +67,7 @@
   public interface AccessControllerResolver extends Component {
       
       String ROLE = AccessControllerResolver.class.getName();
  -    String DEFAULT_RESOLVER = "composable";
  +    String DEFAULT_RESOLVER = "publication";
   
       /**
        * Resolves an access controller for a certain URL.
  
  
  
  1.3       +34 -21    cocoon-lenya/src/java/org/apache/lenya/cms/ac2/AbstractAccessControllerResolver.java
  
  Index: AbstractAccessControllerResolver.java
  ===================================================================
  RCS file: /home/cvs/cocoon-lenya/src/java/org/apache/lenya/cms/ac2/AbstractAccessControllerResolver.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- AbstractAccessControllerResolver.java	11 Aug 2003 16:03:19 -0000	1.2
  +++ AbstractAccessControllerResolver.java	12 Aug 2003 15:16:25 -0000	1.3
  @@ -61,9 +61,9 @@
   import org.apache.avalon.framework.service.ServiceManager;
   import org.apache.avalon.framework.service.Serviceable;
   import org.apache.avalon.framework.thread.ThreadSafe;
  -import org.apache.excalibur.source.Source;
   import org.apache.excalibur.source.SourceResolver;
   import org.apache.lenya.cms.ac.AccessControlException;
  +import org.apache.lenya.cms.ac2.cache.URLKeyUtil;
   import org.apache.lenya.util.CacheMap;
   
   /**
  @@ -83,42 +83,55 @@
           throws AccessControlException {
   
           SourceResolver resolver = null;
  -        Source source = null;
           AccessController controller = null;
  +        Object key = null;
   
           try {
               resolver = (SourceResolver) getManager().lookup(SourceResolver.ROLE);
  -            source = resolver.resolveURI("context:///");
  -            String key = source.getURI() + "_" + webappUrl;
  +            key = generateCacheKey(webappUrl, resolver);
               getLogger().debug("Access controller cache key: [" + key + "]");
   
  -            synchronized (cache) {
  -                controller = (AccessController) cache.get(key);
  -                if (controller == null) {
  -                    getLogger().debug("No access controller in cache.");
  -                    controller = doResolveAccessController(webappUrl);
  -                    cache.put(key, controller);
  -                }
  -                else {
  -                    getLogger().debug("Getting access controller from cache.");
  -                }
  -            }
  -
           } catch (Exception e) {
               throw new AccessControlException(e);
           } finally {
               if (resolver != null) {
  -                if (source != null) {
  -                    resolver.release(source);
  -                }
                   getManager().release(resolver);
               }
           }
   
  +        synchronized (cache) {
  +            controller = (AccessController) cache.get(key);
  +            if (controller == null) {
  +                getLogger().debug("No access controller in cache.");
  +                controller = doResolveAccessController(webappUrl);
  +                cache.put(key, controller);
  +            } else {
  +                getLogger().debug("Getting access controller from cache.");
  +            }
  +        }
  +
           return controller;
       }
   
       /**
  +     * Generates a cache key for the access controller.
  +     * @param webappUrl The webapp URL.
  +     * @param resolver The source resolver.
  +     * @return An object.
  +     * @throws AccessControlException when something went wrong.
  +     */
  +    protected Object generateCacheKey(String webappUrl, SourceResolver resolver)
  +        throws AccessControlException {
  +        Object key;
  +        try {
  +            key = URLKeyUtil.generateKey(resolver, webappUrl);
  +        } catch (Exception e) {
  +            throw new AccessControlException(e);
  +        }
  +        return key;
  +    }
  +
  +    /**
        * The actual resolving method.
        * @param webappUrl The URL within the web application.
        * @return An access controller.
  @@ -137,7 +150,7 @@
       }
   
       private ServiceManager manager;
  -    
  +
       /**
        * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
        */
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: lenya-cvs-unsubscribe@cocoon.apache.org
For additional commands, e-mail: lenya-cvs-help@cocoon.apache.org