sylvain     01/12/11 13:49:33

  Modified:    src/org/apache/cocoon/sitemap Handler.java
                        SitemapManager.java
  Added:       src/org/apache/cocoon/components/source
                        DelayedLastModified.java
  Log:
  Showcase for reduction of calls to getLastModified() using a passive approach.
  
  Revision  Changes    Path
  1.1                  
xml-cocoon2/src/org/apache/cocoon/components/source/DelayedLastModified.java
  
  Index: DelayedLastModified.java
  ===================================================================
  /*****************************************************************************
   * Copyright (C) The Apache Software Foundation. All rights reserved.        *
   * ------------------------------------------------------------------------- *
   * This software is published under the terms of the Apache Software License *
   * version 1.1, a copy of which has been included  with this distribution in *
   * the LICENSE file.                                                         *
   *****************************************************************************/
  package org.apache.cocoon.components.source;
  
  import org.apache.cocoon.environment.Source;
  import org.apache.cocoon.environment.ModifiableSource;
  
  /**
   * A front-end to a <code>Source</code> that reduces the number of calls to
   * <code>Source.getLastModified()</code>, which can be a costly operation.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Sylvain Wallez</a>
   * @version $Id: DelayedLastModified.java,v 1.1 2001/12/11 21:49:33 sylvain Exp $
   */
  public final class DelayedLastModified {
      
      private Source source;
      
      private long delay;
      
      private long nextCheckTime = 0;
      
      private long lastModified = 0;
      
      private boolean isModifiableSource;
  
      /**
       * Creates a new delay for a <code>Source</code> which ensures that
       * <code>Source.getLastModified()</code> won't be called more than once per
       * <code>delay</code> milliseconds period.
       * 
       * @param source the wrapped <code>Source</code>
       * @param delay  the last-modified refresh delay, in milliseconds
       */
      public DelayedLastModified(Source source, long delay) {
          this.source = source;
          this.delay = delay;
          this.isModifiableSource = source instanceof ModifiableSource;
      }
      
      /**
       * Get the last modification time for the resource. The age of the returned
       * information is guaranteed to be lower or equal to the delay specified in
       * {@link #DelayedLastModified(Source, long)}.
       *
       * @return the last modification time.
       */
      public long get() {
          
          long now = System.currentTimeMillis();
          
          // Do we have to refresh lastModified ?
          if (now >= nextCheckTime) {
              // Get a new lastModified value
              this.nextCheckTime = now + this.delay;
              
              // Refresh modifiable sources
              if (this.isModifiableSource) {
                  ((ModifiableSource)this.source).refresh();
              }
              
              this.lastModified = source.getLastModified();
          }
          
          return this.lastModified;
      }
  }
  
  
  
  1.24      +14 -4     xml-cocoon2/src/org/apache/cocoon/sitemap/Handler.java
  
  Index: Handler.java
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/src/org/apache/cocoon/sitemap/Handler.java,v
  retrieving revision 1.23
  retrieving revision 1.24
  diff -u -r1.23 -r1.24
  --- Handler.java      2001/10/25 20:36:40     1.23
  +++ Handler.java      2001/12/11 21:49:33     1.24
  @@ -25,6 +25,7 @@
   import org.apache.cocoon.components.pipeline.StreamPipeline;
   import org.apache.cocoon.components.source.CocoonSourceFactory;
   import org.apache.cocoon.components.source.SourceHandler;
  +import org.apache.cocoon.components.source.DelayedLastModified;
   import org.apache.cocoon.environment.Environment;
   import org.apache.cocoon.environment.ModifiableSource;
   import org.apache.cocoon.environment.Source;
  @@ -41,7 +42,7 @@
    * @author <a href="mailto:[EMAIL PROTECTED]";>Carsten Ziegeler</a>
    * @author <a href="mailto:[EMAIL PROTECTED]";>Giacomo Pati</a>
    * @author <a href="mailto:[EMAIL PROTECTED]";>Stefano Mazzocchi</a>
  - * @version CVS $Revision: 1.23 $ $Date: 2001/10/25 20:36:40 $
  + * @version CVS $Revision: 1.24 $ $Date: 2001/12/11 21:49:33 $
    */
   public class Handler extends AbstractLoggable
   implements Runnable, Contextualizable, Composable, Processor, Disposable, 
SourceResolver {
  @@ -52,6 +53,7 @@
       /** the source of this sitemap */
       private String sourceFileName;
       private ModifiableSource source;
  +    private DelayedLastModified sourceLastModified;
   
       /** the last error */
       private Exception exception;
  @@ -71,11 +73,19 @@
       /** The source handler for the sitemap components */
       private SourceHandler sourceHandler;
   
  +    // FIXME : ugly hack to pass delay information from the main sitemap 
configuration
  +    // (the way to pass it cleanly from SitemapManager isn't obvious).
  +    private static long sitemapCheckDelay = 10000L; // default is 10 secs.
  +    
  +    static void setSitemapCheckDelay(long delay) {
  +        sitemapCheckDelay = delay;
  +    }
  +
       protected Handler(String sourceFileName, boolean check_reload) throws 
FileNotFoundException {
           this.check_reload = check_reload;
           this.sourceFileName = sourceFileName;
       }
  -
  +    
       /**
        * Contextualizable
        */
  @@ -99,8 +109,7 @@
       protected boolean hasChanged() {
           if (available()) {
               if (check_reload) {
  -                this.source.refresh();
  -                return sitemap.modifiedSince(this.source.getLastModified());
  +                return sitemap.modifiedSince(this.sourceLastModified.get());
               }
               return false;
           }
  @@ -119,6 +128,7 @@
           try {
               environment.setSourceHandler(this.sourceHandler);
               this.source = 
(ModifiableSource)environment.resolve(this.sourceFileName);
  +            this.sourceLastModified = new DelayedLastModified(this.source, 
this.sitemapCheckDelay);
               this.contextSource = environment.resolve("");
           } finally {
               environment.setSourceHandler(oldSourceHandler);
  
  
  
  1.2       +5 -1      xml-cocoon2/src/org/apache/cocoon/sitemap/SitemapManager.java
  
  Index: SitemapManager.java
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/src/org/apache/cocoon/sitemap/SitemapManager.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- SitemapManager.java       2001/10/25 10:34:00     1.1
  +++ SitemapManager.java       2001/12/11 21:49:33     1.2
  @@ -38,7 +38,7 @@
    * to Java code.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Sylvain Wallez</a>
  - * @version CVS $Revision: 1.1 $ $Date: 2001/10/25 10:34:00 $
  + * @version CVS $Revision: 1.2 $ $Date: 2001/12/11 21:49:33 $
    */
   public class SitemapManager extends Manager implements Processor, Configurable {
       
  @@ -63,9 +63,13 @@
           value = sconf.getAttribute("reload-method", "asynchron");
           this.reloadSitemapAsynchron = !(value != null && 
value.equalsIgnoreCase("synchron") == true);
           
  +        long checkDelay = sconf.getAttributeAsLong("check-delay", 10L);
  +        Handler.setSitemapCheckDelay(checkDelay * 1000L);
  +        
           getLogger().debug("Sitemap location = " + this.sitemapFileName);
           getLogger().debug("Checking sitemap reload = " + this.checkSitemapReload);
           getLogger().debug("Reloading sitemap asynchron = " + 
this.reloadSitemapAsynchron);
  +        getLogger().debug("Sitemap check delay = " + checkDelay + " seconds");
           
           System.err.println("Sitemap location = " + this.sitemapFileName);
           System.err.println("Checking sitemap reload = " + this.checkSitemapReload);
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     [EMAIL PROTECTED]
To unsubscribe, e-mail:          [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to