cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jo...@apache.org
Subject cvs commit: cocoon-2.0/src/java/org/apache/cocoon/generation DirectoryGenerator.java
Date Sat, 21 Jun 2003 11:47:08 GMT
joerg       2003/06/21 04:47:08

  Modified:    src/java/org/apache/cocoon/generation
                        DirectoryGenerator.java
  Log:
  backporting the caching from Cocoon 2.1 (thanks to Jorg Heymans),
  fixed cache key generation,
  fixed the root pattern handling (if not matching directory found leave out the ancestors)
  fixed empty src attribute handling
  
  Revision  Changes    Path
  1.4       +244 -165  cocoon-2.0/src/java/org/apache/cocoon/generation/DirectoryGenerator.java
  
  Index: DirectoryGenerator.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.0/src/java/org/apache/cocoon/generation/DirectoryGenerator.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- DirectoryGenerator.java	31 May 2003 01:22:05 -0000	1.3
  +++ DirectoryGenerator.java	21 Jun 2003 11:47:08 -0000	1.4
  @@ -51,8 +51,11 @@
   package org.apache.cocoon.generation;
   
   import org.apache.avalon.framework.parameters.Parameters;
  +import org.apache.cocoon.caching.CacheValidity;
  +import org.apache.cocoon.caching.Cacheable;
   import org.apache.cocoon.ProcessingException;
   import org.apache.cocoon.ResourceNotFoundException;
  +import org.apache.cocoon.util.HashUtil;
   import org.apache.cocoon.environment.Source;
   import org.apache.cocoon.environment.SourceResolver;
   import org.apache.regexp.RE;
  @@ -64,7 +67,9 @@
   import java.io.IOException;
   import java.net.URL;
   import java.text.SimpleDateFormat;
  +import java.util.ArrayList;
   import java.util.Date;
  +import java.util.List;
   import java.util.Map;
   import java.util.Stack;
   
  @@ -72,9 +77,9 @@
    * Generates an XML directory listing.
    * <p>
    * The root node of the generated document will normally be a
  - * <code>directory</code> node, and a directory node can contain zero
  - * or more <code>file</code> or directory nodes. A file node has no
  - * children. Each node will contain the following attributes:
  + * <code>directory</code> node, and a directory node can contain zero or more
  + * <code>file</code> or directory nodes. A file node has no children. Each
node
  + * will contain the following attributes:
    * <blockquote>
    *   <dl>
    *   <dt> name
  @@ -90,126 +95,166 @@
    * <b>Configuration options:</b>
    * <dl>
    * <dt> <i>depth</i> (optional)
  - * <dd> Sets how deep DirectoryGenerator should delve into the
  - * directory structure. If set to 1 (the default), only the starting
  - * directory's immediate contents will be returned.
  + * <dd> Sets how deep DirectoryGenerator should delve into the directory
  + * structure. If set to 1 (the default), only the starting directory's
  + * immediate contents will be returned.
    * <dt> <i>dateFormat</i> (optional)
  - * <dd> Sets the format for the date attribute of each node, as
  - * described in java.text.SimpleDateFormat. If unset, the default
  - * format for the current locale will be used.
  + * <dd> Sets the format for the date attribute of each node as described in
  + * java.text.SimpleDateFormat. If unset, the default format for the current
  + * locale will be used.
  + * <dt> <i>refreshDelay</i> (optional)
  + * <dd> Sets the delay (in seconds) between checks on the filesystem for
  + * changed content. Defaults to 1 second.
    * </dl>
    *
    * @author <a href="mailto:pier@apache.org">Pierpaolo Fumagalli</a>
    *         (Apache Software Foundation)
    * @author <a href="mailto:conny@smb-tec.com">Conny Krappatsch</a>
    *         (SMB GmbH) for Virbus AG
  + * @author <a href="mailto:jh@coeno.com">Jorg Heymans</a>
    * @version CVS $Id$
    */
  -public class DirectoryGenerator extends ComposerGenerator  {
  -  private static final String FILE = "file:";
  +public class DirectoryGenerator extends ComposerGenerator implements Cacheable {
  +
  +    /** Constant for the file protocol. */
  +    private static final String FILE = "file:";
   
       /** The URI of the namespace of this generator. */
  -    protected static final String URI =
  -    "http://apache.org/cocoon/directory/2.0";
  +    protected static final String URI = "http://apache.org/cocoon/directory/2.0";
   
       /** The namespace prefix for this namespace. */
       protected static final String PREFIX = "dir";
   
  -    /* Node and attribute names */
  -    protected static final String DIR_NODE_NAME         = "directory";
  -    protected static final String FILE_NODE_NAME        = "file";
  -
  -    protected static final String FILENAME_ATTR_NAME    = "name";
  -    protected static final String LASTMOD_ATTR_NAME     = "lastModified";
  -    protected static final String DATE_ATTR_NAME        = "date";
  -	protected static final String SIZE_ATTR_NAME        = "size";
  +    /* Node and attribute names. */
  +    protected static final String DIR_NODE_NAME = "directory";
  +    protected static final String FILE_NODE_NAME = "file";
  +
  +    protected static final String FILENAME_ATTR_NAME = "name";
  +    protected static final String LASTMOD_ATTR_NAME = "lastModified";
  +    protected static final String DATE_ATTR_NAME = "date";
  +    protected static final String SIZE_ATTR_NAME = "size";
  +    protected static final String REQUESTED_ATTR_NAME = "requested";
  +
  +    /** The cache validity of a directory. */
  +    protected DirValidity dirvalidity;
  +    /** Convenience object, so we don't need to create an AttributesImpl for every element.
*/
  +    protected AttributesImpl attributes;
  +
  +    /**
  +     * The cache key needs to be generated for the configuration of this
  +     * generator, so storing the parameters for generateKey().
  +     * Using the member variables after setup() would not work I guess. I don't
  +     * know a way from the regular expressions back to the pattern or at least
  +     * a useful string.
  +     */
  +    protected List cacheKeyParList;
   
  -    /*
  -     * Variables set per-request
  -     *
  +    /** The depth parameter determines how deep the DirectoryGenerator should delve. */
  +    protected int depth;
  +    /**
  +     * The dateFormatter determines into which date format the lastModified
  +     * time should be converted.
        * FIXME: SimpleDateFormat is not supported by all locales!
        */
  -    protected int depth;
  -    protected AttributesImpl attributes = new AttributesImpl();
       protected SimpleDateFormat dateFormatter;
  -
  +    /** The delay between checks on updates to the filesystem. */
  +    protected long refreshDelay;
  +    /** The regular expression for the root pattern. */
       protected RE rootRE;
  +    /** The regular expression for the include pattern. */
       protected RE includeRE;
  +    /** The regular expression for the exclude pattern. */
       protected RE excludeRE;
  -
  +    /**
  +     * This is only set to true for the requested directory specified by the
  +     * <code>src</code> attribute on the generator's configuration.
  +     */
       protected boolean isRequestedDirectory;
   
  -
       /**
  -     * Set the request parameters. Must be called before the generate
  -     * method.
  +     * Set the request parameters. Must be called before the generate method.
        *
  -     * @param   resolver
  -     *      the SourceResolver object
  -     * @param   objectModel
  -     *      a <code>Map</code> containing model object
  -     * @param   src
  -     *      the URI for this request (?)
  -     * @param   par
  -     *      configuration parameters
  +     * @param resolver     the SourceResolver object
  +     * @param objectModel  a <code>Map</code> containing model object
  +     * @param src          the directory to be XMLized specified as src attribute on &lt;map:generate/>
  +     * @param par          configuration parameters
        */
       public void setup(SourceResolver resolver, Map objectModel, String src, Parameters
par)
  -        throws ProcessingException, SAXException, IOException {
  +            throws ProcessingException, SAXException, IOException {
  +        if (src == null) {
  +            throw new ProcessingException("No src attribute pointing to a directory to
be XMLized specified.");
  +        }
           super.setup(resolver, objectModel, src, par);
   
  +        this.cacheKeyParList = new ArrayList();
  +        this.cacheKeyParList.add(src);
  +
  +        this.depth = par.getParameterAsInteger("depth", 1);
  +        this.cacheKeyParList.add(String.valueOf(this.depth));
  +
  +        this.refreshDelay = par.getParameterAsLong("refreshDelay", 1L) * 1000L;
  +        this.cacheKeyParList.add(String.valueOf(this.refreshDelay));
  +
           String dateFormatString = par.getParameter("dateFormat", null);
  +        this.cacheKeyParList.add(dateFormatString);
           if (dateFormatString != null) {
               this.dateFormatter = new SimpleDateFormat(dateFormatString);
           } else {
               this.dateFormatter = new SimpleDateFormat();
           }
   
  -        this.depth = par.getParameterAsInteger("depth", 1);
  -        getLogger().debug("depth: " + this.depth);
  +        if (getLogger().isDebugEnabled()) {
  +            getLogger().debug("depth: " + this.depth);
  +            getLogger().debug("refreshDelay: " + this.refreshDelay);
  +            getLogger().debug("dateFormat: " + this.dateFormatter.toPattern());
  +        }
   
  -        String rePattern = par.getParameter("root", null);
  +        String rePattern = null;
           try {
  -            getLogger().debug("root pattern: " + rePattern);
  -            this.rootRE = (rePattern == null)?null:new RE(rePattern);
  +            rePattern = par.getParameter("root", null);
  +            this.cacheKeyParList.add(rePattern);
  +            if (getLogger().isDebugEnabled()) {
  +                getLogger().debug("root pattern: " + rePattern);
  +            }
  +            this.rootRE = (rePattern == null) ? null : new RE(rePattern);
   
               rePattern = par.getParameter("include", null);
  -            getLogger().debug("include pattern: " + rePattern);
  -            this.includeRE = (rePattern == null)?null:new RE(rePattern);
  +            this.cacheKeyParList.add(rePattern);
  +            if (getLogger().isDebugEnabled()) {
  +                getLogger().debug("include pattern: " + rePattern);
  +            }
  +            this.includeRE = (rePattern == null) ? null : new RE(rePattern);
   
               rePattern = par.getParameter("exclude", null);
  -            getLogger().debug("exclude pattern: " + rePattern);
  -            this.excludeRE = (rePattern == null)?null:new RE(rePattern);
  +            this.cacheKeyParList.add(rePattern);
  +            if (getLogger().isDebugEnabled()) {
  +                getLogger().debug("exclude pattern: " + rePattern);
  +            }
  +            this.excludeRE = (rePattern == null) ? null : new RE(rePattern);
           } catch (RESyntaxException rese) {
               getLogger().error("Syntax error in regexp pattern '" + rePattern + "'", rese);
  -            throw new ProcessingException("Syntax error in regexp pattern '"
  -                + rePattern + "'", rese);
  +            throw new ProcessingException("Syntax error in regexp pattern '" + rePattern
+ "'", rese);
           }
  -
           this.isRequestedDirectory = false;
  -
  -        /* Create a reusable attributes for creating nodes */
           this.attributes = new AttributesImpl();
       }
   
  -
       /**
        * Generate XML data.
        *
        * @throws  SAXException
        *      if an error occurs while outputting the document
        * @throws  ProcessingException
  -     *      if the requsted URI isn't a directory on the local
  -     *      filesystem
  +     *      if the requested URI isn't a directory on the local filesystem
        */
  -    public void generate()
  -    throws SAXException, ProcessingException {
  +    public void generate() throws SAXException, ProcessingException {
           String directory = super.source;
           Source inputSource = null;
           try {
               inputSource = this.resolver.resolve(directory);
               String systemId = inputSource.getSystemId();
               if (!systemId.startsWith(FILE)) {
  -              throw new ResourceNotFoundException(systemId + " does not denote a directory");
  +                throw new ResourceNotFoundException(systemId + " does not denote a directory");
               }
               // This relies on systemId being of the form "file://..."
               File directoryFile = new File(new URL(systemId).getFile());
  @@ -218,27 +263,24 @@
               }
   
               this.contentHandler.startDocument();
  -            this.contentHandler.startPrefixMapping(PREFIX,URI);
   
               Stack ancestors = getAncestors(directoryFile);
  -            addPathWithAncestors(directoryFile, ancestors);
  +            addAncestorPath(directoryFile, ancestors);
   
  -            this.contentHandler.endPrefixMapping(PREFIX);
               this.contentHandler.endDocument();
           } catch (IOException ioe) {
               getLogger().warn("Could not read directory " + directory, ioe);
  -            throw new ResourceNotFoundException("Could not read directory "
  -                + directory, ioe);
  +            throw new ResourceNotFoundException("Could not read directory " + directory,
ioe);
           } finally {
  -            if (inputSource != null) inputSource.recycle();
  +            if (inputSource != null) {
  +                inputSource.recycle();
  +            }
           }
       }
   
       /**
  -     * Creates a stack containing the ancestors of File up to specified
  -     * directory.
  +     * Creates a stack containing the ancestors of File up to specified directory.
        * @param path the File whose ancestors shall be retrieved
  -     *
        * @return a Stack containing the ancestors.
        */
       protected Stack getAncestors(File path) {
  @@ -249,49 +291,50 @@
               parent = parent.getParentFile();
               if (parent != null) {
                   ancestors.push(parent);
  +            } else {
  +                // no ancestor matched the root pattern
  +                ancestors.clear();
               }
           }
  -
           return ancestors;
       }
   
  -
  -    protected void addPathWithAncestors(File path, Stack ancestors)
  -            throws SAXException {
  -
  +    /**
  +     * Adds recursively the path from the directory matched by the root pattern
  +     * down to the requested directory.
  +     * @param path       the requested directory.
  +     * @param ancestors  the stack of the ancestors.
  +     * @throws SAXException
  +     */
  +    protected void addAncestorPath(File path, Stack ancestors) throws SAXException {
           if (ancestors.empty()) {
               this.isRequestedDirectory = true;
  -            addPath(path, depth);
  +            addPath(path, this.depth);
           } else {
               startNode(DIR_NODE_NAME, (File)ancestors.pop());
  -            addPathWithAncestors(path, ancestors);
  +            addAncestorPath(path, ancestors);
               endNode(DIR_NODE_NAME);
           }
       }
   
  -
       /**
  -     * Adds a single node to the generated document. If the path is a
  -     * directory, and depth is greater than zero, then recursive calls
  -     * are made to add nodes for the directory's children.
  -     *
  -     * @param   path
  -     *      the file/directory to process
  -     * @param   depth
  -     *      how deep to scan the directory
  +     * Adds a single node to the generated document. If the path is a directory
  +     * and depth is greater than zero, then recursive calls are made to add
  +     * nodes for the directory's children.
        *
  -     * @throws  SAXException
  -     *      if an error occurs while constructing nodes
  +     * @param path   the file/directory to process
  +     * @param depth  how deep to scan the directory
  +     *
  +     * @throws SAXException  if an error occurs while constructing nodes
        */
  -    protected void addPath(File path, int depth)
  -    throws SAXException {
  +    protected void addPath(File path, int depth) throws SAXException {
           if (path.isDirectory()) {
               startNode(DIR_NODE_NAME, path);
  -            if (depth>0) {
  +            if (depth > 0) {
                   File contents[] = path.listFiles();
  -                for (int i=0; i<contents.length; i++) {
  +                for (int i = 0; i < contents.length; i++) {
                       if (isIncluded(contents[i]) && !isExcluded(contents[i])) {
  -                        addPath(contents[i], depth-1);
  +                        addPath(contents[i], depth - 1);
                       }
                   }
               }
  @@ -304,135 +347,171 @@
           }
       }
   
  -
       /**
  -     * Begins a named node, and calls setNodeAttributes to set its
  -     * attributes.
  +     * Begins a named node and calls setNodeAttributes to set its attributes.
        *
  -     * @param   nodeName
  -     *      the name of the new node
  -     * @param   path
  -     *      the file/directory to use when setting attributes
  +     * @param nodeName  the name of the new node
  +     * @param path      the file/directory to use when setting attributes
        *
  -     * @throws  SAXException
  -     *      if an error occurs while creating the node
  +     * @throws SAXException  if an error occurs while creating the node
        */
  -    protected void startNode(String nodeName, File path)
  -    throws SAXException {
  +    protected void startNode(String nodeName, File path) throws SAXException {
           setNodeAttributes(path);
  -        super.contentHandler.startElement(URI, nodeName, PREFIX+':'+nodeName, attributes);
  +        this.dirvalidity.addFile(path);
  +        super.contentHandler.startElement(URI, nodeName, PREFIX + ':' + nodeName, this.attributes);
       }
   
  -
       /**
        * Sets the attributes for a given path. The default method sets attributes
  -     * for the name of thefile/directory and for the last modification time
  +     * for the name of the file/directory and for the last modification time
        * of the path.
        *
  -     * @param path
  -     *        the file/directory to use when setting attributes
  +     * @param path  the file/directory to use when setting attributes
        *
  -     * @throws SAXException
  -     *         if an error occurs while setting the attributes
  +     * @throws SAXException  if an error occurs while setting the attributes
        */
       protected void setNodeAttributes(File path) throws SAXException {
           long lastModified = path.lastModified();
  -        attributes.clear();
  -        attributes.addAttribute("", FILENAME_ATTR_NAME,
  -                    FILENAME_ATTR_NAME, "CDATA",
  -                    path.getName());
  -        attributes.addAttribute("", LASTMOD_ATTR_NAME,
  -                    LASTMOD_ATTR_NAME, "CDATA",
  -                    Long.toString(path.lastModified()));
  -        attributes.addAttribute("", DATE_ATTR_NAME,
  -                    DATE_ATTR_NAME, "CDATA",
  -                    dateFormatter.format(new Date(lastModified)));
  -		attributes.addAttribute("", SIZE_ATTR_NAME,
  -					SIZE_ATTR_NAME, "CDATA",
  -					Long.toString(path.length()));
  +        this.attributes.clear();
  +        this.attributes.addAttribute("", FILENAME_ATTR_NAME, FILENAME_ATTR_NAME, "CDATA",
path.getName());
  +        this.attributes.addAttribute("", LASTMOD_ATTR_NAME, LASTMOD_ATTR_NAME, "CDATA",
Long.toString(path.lastModified()));
  +        this.attributes.addAttribute("", DATE_ATTR_NAME, DATE_ATTR_NAME, "CDATA", this.dateFormatter.format(new
Date(lastModified)));
  +        this.attributes.addAttribute("", SIZE_ATTR_NAME, SIZE_ATTR_NAME, "CDATA", Long.toString(path.length()));
   
           if (this.isRequestedDirectory) {
  -            attributes.addAttribute("", "requested", "requested", "CDATA",
  -                    "true");
  +            this.attributes.addAttribute("", REQUESTED_ATTR_NAME, REQUESTED_ATTR_NAME,
"CDATA", "true");
               this.isRequestedDirectory = false;
           }
       }
   
  -
       /**
        * Ends the named node.
        *
  -     * @param   nodeName
  -     *      the name of the new node
  +     * @param nodeName  the name of the new node
        *
  -     * @throws  SAXException
  -     *      if an error occurs while closing the node
  +     * @throws SAXException  if an error occurs while closing the node
        */
  -    protected void endNode(String nodeName)
  -    throws SAXException {
  -        super.contentHandler.endElement(URI, nodeName, PREFIX+':'+nodeName);
  +    protected void endNode(String nodeName) throws SAXException {
  +        super.contentHandler.endElement(URI, nodeName, PREFIX + ':' + nodeName);
       }
   
  -
       /**
        * Determines if a given File is the defined root.
        *
  -     * @param path the File to check
  +     * @param path  the File to check
        *
        * @return true if the File is the root or the root pattern is not set,
  -     *      false otherwise.
  +     *         false otherwise.
        */
       protected boolean isRoot(File path) {
  -
  -        return (this.rootRE == null)
  -                ? true
  -                : this.rootRE.match(path.getName());
  +        return (this.rootRE == null) ? true : this.rootRE.match(path.getName());
       }
   
  -
       /**
        * Determines if a given File shall be visible.
        *
  -     * @param path the File to check
  +     * @param path  the File to check
        *
  -     * @return true if the File shall be visible or the include Pattern is
  -            <code>null</code>, false otherwise.
  +     * @return true if the File shall be visible or the include Pattern is <code>null</code>,
  +     *         false otherwise.
        */
       protected boolean isIncluded(File path) {
  -
  -        return (this.includeRE == null)
  -                ? true
  -                : this.includeRE.match(path.getName());
  +        return (this.includeRE == null) ? true : this.includeRE.match(path.getName());
       }
   
  -
       /**
        * Determines if a given File shall be excluded from viewing.
        *
  -     * @param path the File to check
  +     * @param path  the File to check
        *
  -     * @return false if the given File shall not be excluded or the
  -     * exclude Pattern is <code>null</code>, true otherwise.
  +     * @return false if the given File shall not be excluded or the exclude
  +     *         Pattern is <code>null</code>, true otherwise.
        */
       protected boolean isExcluded(File path) {
  -
  -        return (this.excludeRE == null)
  -                ? false
  -                : this.excludeRE.match(path.getName());
  +        return (this.excludeRE == null) ? false : this.excludeRE.match(path.getName());
       }
   
       /**
        * Recycle resources
  -     *
        */
  -
       public void recycle() {
  -       super.recycle();
  -       this.attributes = null;
  -         this.dateFormatter = null;
  -       this.rootRE = null;
  -       this.includeRE = null;
  -       this.excludeRE = null;
  +        super.recycle();
  +        this.cacheKeyParList = null;
  +        this.attributes = null;
  +        this.dateFormatter = null;
  +        this.rootRE = null;
  +        this.includeRE = null;
  +        this.excludeRE = null;
  +    }
   
  +    /**
  +     * @return the generator cache state i.e. key
  +     */
  +    public long generateKey() {
  +        StringBuffer buffer = new StringBuffer();
  +        int len = this.cacheKeyParList.size();
  +        for (int i = 0; i < len; i++) {
  +            buffer.append((String)this.cacheKeyParList.get(i) + ":");
  +        }
  +        return HashUtil.hash(buffer);
  +    }
  +
  +    /**
  +     * @return CacheValidity  the CacheValidity is a DirValidity which checks
  +     *                        the timestamp of each file
  +     */
  +    public CacheValidity generateValidity() {
  +        this.dirvalidity = new DirValidity(this.refreshDelay);
  +        return this.dirvalidity;
       }
  +
  +
  +    /**
  +     * DirValidity class providing timestamp checking on each file.
  +     */
  +    public class DirValidity implements CacheValidity {
  +
  +        private long expiry;
  +        private long delay;
  +        List files = new ArrayList();
  +        List fileDates = new ArrayList();
  +
  +        public DirValidity(long delay) {
  +            this.expiry = System.currentTimeMillis() + delay;
  +            this.delay = delay;
  +        }
  +
  +        /**
  +         * @param cv the cachevalidity
  +         * @return true if the cache is valid otherwise false
  +         */
  +        public boolean isValid(CacheValidity cv) {
  +            if (System.currentTimeMillis() <= this.expiry) {
  +                return true;
  +            }
  +            this.expiry = System.currentTimeMillis() + this.delay;
  +            int len = files.size();
  +            for (int i = 0; i < len; i++) {
  +                File f = (File)files.get(i);
  +                if (!f.exists()) {
  +                    return false; // File was removed
  +                }
  +                long oldDate = ((Long)fileDates.get(i)).longValue();
  +                long newDate = f.lastModified();
  +
  +                if (oldDate != newDate) {
  +                    return false;
  +                }
  +            }
  +            // all content is up to date: update the expiry date
  +            this.expiry = System.currentTimeMillis() + this.delay;
  +            return true;
  +        }
  +
  +        public void addFile(File f) {
  +            this.files.add(f);
  +            this.fileDates.add(new Long(f.lastModified()));
  +        }
  +    }
  +
   }
  
  
  

Mime
View raw message