mikeh       01/11/15 07:58:34

  Modified:    src/java/org/apache/turbine TurbineConstants.java
               src/java/org/apache/turbine/pipeline DefaultResolver.java
  Added:       src/java/org/apache/turbine/util DoubleKeyCache.java
  Log:
  Adding a caching mechanism to the DefaultResolver.  Since the resolver
  is the single entry point to get templates and modules, caching is
  best done there.  I will remove the incomplete caching in the ModuleLoader.
  
  The TR props for the resolver are now:
  resolver.default= org.apache.turbine.pipeline.DefaultResolver
  resolver.cache.template = false
  resolver.cache.module   = false
  
  Added a very simple class to handle the double key caching required by
  both templates and modules.
  
  Revision  Changes    Path
  1.7       +5 -0      
jakarta-turbine-3/src/java/org/apache/turbine/TurbineConstants.java
  
  Index: TurbineConstants.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine-3/src/java/org/apache/turbine/TurbineConstants.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- TurbineConstants.java     2001/11/13 19:45:08     1.6
  +++ TurbineConstants.java     2001/11/15 15:58:33     1.7
  @@ -124,6 +124,11 @@
        */
       public static final String RESOLVER = "resolver.default";
   
  +    /**
  +     * what will the resolver cache (replaces the <module.cache> property)
  +     */
  +    public static final String RESOLVER_MODULE_CACHE = "resolver.cache.module";
  +    public static final String RESOLVER_TEMPLATE_CACHE = "resolver.cache.template";
   
       /**
        * JDBC database driver.
  
  
  
  1.4       +107 -18   
jakarta-turbine-3/src/java/org/apache/turbine/pipeline/DefaultResolver.java
  
  Index: DefaultResolver.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine-3/src/java/org/apache/turbine/pipeline/DefaultResolver.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- DefaultResolver.java      2001/11/13 21:35:44     1.3
  +++ DefaultResolver.java      2001/11/15 15:58:33     1.4
  @@ -62,10 +62,13 @@
   import java.util.List;
   import java.util.Iterator;
   import org.apache.turbine.Turbine;
  +import org.apache.turbine.TurbineConstants;
   import org.apache.turbine.Log;
   import org.apache.turbine.Resolver;
   import org.apache.turbine.modules.Module;
   
  +import org.apache.turbine.util.DoubleKeyCache;
  +
   // Given a target template
   
   // 1. find all possible classes that could be used
  @@ -116,6 +119,14 @@
      for finding, caching, and serving up modules.  That functionaly
      should be placed in the pluggable resolver.  I'll leave it for now so
      to make the changes more gradually.
  +
  +   subclasses can override findTemplate and findModule to 
  +   use a different algorithm, while using the caching mechanism
  +   of the DefaultResolver
  +
  +   subclasses should override getTemplate and getModule if they
  +   wish to use a different caching mechanism
  +
   */
   
   /**
  @@ -123,39 +134,124 @@
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Jason van Zyl</a>
    * @author <a href="mailto:[EMAIL PROTECTED]";>Mike Haberman</a>
  - * @version $Id: DefaultResolver.java,v 1.3 2001/11/13 21:35:44 mikeh Exp $
  + * @version $Id: DefaultResolver.java,v 1.4 2001/11/15 15:58:33 mikeh Exp $
    */
   
   public class DefaultResolver
  -       implements Resolver
  +       implements Resolver,
  +                  TurbineConstants
   {
       String defaultTemplate;
   
  +    boolean doTemplateCache;
  +    boolean doModuleCache;
  +
  +    DoubleKeyCache templateCache;
  +    DoubleKeyCache moduleCache;
  +
       public void init()
           throws Exception
       {
           defaultTemplate = 
  -         Turbine.getConfiguration().getString("template.default") + "." +
  -         Turbine.getConfiguration().getString("template.default.extension");
  +            Turbine.getConfiguration().getString("template.default") + 
  +            "." +
  +            Turbine.getConfiguration().getString("template.default.extension");
   
  -        if (defaultTemplate.indexOf('/') == -1) 
  +        //
  +        // insist that the default template starts with a '/'
  +        //
  +        int idx = defaultTemplate.indexOf('/');
  +        if (idx == -1 || idx > 0)
           {
               defaultTemplate = "/" + defaultTemplate;
           }
  +
  +        //
  +        // set up the cache
  +        //
  +        String value;
  +        value = Turbine.getConfiguration().getString(RESOLVER_TEMPLATE_CACHE,
  +                                                     "false");
  +        doTemplateCache = Boolean.valueOf(value).booleanValue();
  +
  +        value = Turbine.getConfiguration().getString(RESOLVER_MODULE_CACHE,
  +                                                     "false");
  +        doModuleCache = Boolean.valueOf(value).booleanValue();
  +
  +        Log.debug("DefaultResolver: default template " + defaultTemplate);
  +        Log.debug("DefaultResolver: cache templates " + doTemplateCache);
  +        Log.debug("DefaultResolver: cache modules " + doModuleCache);
  +
  +        if (doTemplateCache)
  +        {
  +            templateCache = new DoubleKeyCache();
  +        }
   
  -        Log.debug("DefaultResolver: default template is " + defaultTemplate);
  +        if (doModuleCache) 
  +        {
  +            moduleCache = new DoubleKeyCache();
  +        }
       }
   
       /**
  +     * Get the qualified template name
        * Used for resolving top level rendering process.
  +     * For turbine classic edition, 
  +     * moduleType is one of (layouts, screens, navigations)
        */
       public String getTemplate(String moduleType, String targetTemplate)
           throws Exception
       {
  +        if (! doTemplateCache) 
  +        {
  +            return findTemplate(moduleType, targetTemplate);
  +        }
  +
           //
  -        // TODO: add caching here
  +        //  caching is on 
           //
  +        String template = (String)templateCache.get(moduleType, targetTemplate);
  +        if (template == null) 
  +        {
  +           template = findTemplate(moduleType, targetTemplate);
  +           templateCache.put(moduleType, targetTemplate, template);
  +        }
  +        return template;
  +    }
   
  +    /**
  +     * Get an instance of a module
  +     *
  +     * For turbine classic edition, type is one of (actions, screens) 
  +     * @param String name
  +     * @param String type
  +     * @return Module
  +     */
  +    public Module getModule(String type, String name)
  +        throws Exception
  +    {
  +        if (! doModuleCache) 
  +        {
  +            return findModule(type, name);
  +        }
  +
  +        //
  +        //  caching is on 
  +        //
  +        Module module = (Module)moduleCache.get(type, name);
  +        if (module == null) 
  +        {
  +           module = findModule(type, name);
  +           moduleCache.put(type, name, module);
  +        }
  +        return module;
  +    }
  +
  +
  +    protected String findTemplate(String moduleType, String targetTemplate)
  +        throws Exception
  +    {
  +
           StringBuffer sb = new StringBuffer();
           int i = PipelineUtil.parseTemplatePath(targetTemplate, sb);
           Iterator j = getPossibleTemplates(sb.toString());
  @@ -234,20 +330,13 @@
           return packages.iterator();
       }
   
  -    /**
  -     * Get an instance of a module
  -     *
  -     * @param String name
  -     * @param String type
  -     * @return Module
  -     */
  -    public Module getModule(String type, String name)
  +    protected Module findModule(String type, String name)
           throws Exception
       {
  +        //
           // just use the module loader's default algorithm
  -        // for this pass
  -        // eventually that functionality should be put
  -        // in the resolver.
  +        // perhaps that code should be put here 
  +        //
           return Turbine.getModuleLoader().getModule(type,name);
       }
   }
  
  
  
  1.1                  
jakarta-turbine-3/src/java/org/apache/turbine/util/DoubleKeyCache.java
  
  Index: DoubleKeyCache.java
  ===================================================================
  package org.apache.turbine.util;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Turbine" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Turbine", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import java.util.Map;
  import org.apache.commons.collections.FastHashMap;
  
  
  /**
   * This class is used to handle template and module caching--
   * each requires a double hash to get the value
   * e.g  map.get(moduleType, templateName)
   *
   * we can add more methods as they are needed
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Mike Haberman</a>
   * @version $Id: DoubleKeyCache.java,v 1.1 2001/11/15 15:58:33 mikeh Exp $
   */
  
  
  public class DoubleKeyCache
  {
      Map map = null; 
  
      public DoubleKeyCache()
      {
          // map must be a synchronized data structure
          map = new FastHashMap();
      }
  
      public Object put(Object key1, Object key2, Object value)
      {
          Map table = (Map) map.get(key1);
          if (table == null) 
          {
             table = new FastHashMap();
             map.put(key1, table);
          }
          return table.put(key2, value);
      }
  
      public Object get(Object key1, Object key2)
      {
          Map table = (Map) map.get(key1);
          if (table == null)
          {
             return null;
          }
          return table.get(key2);
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to