CVS: Tapestry/framework/src/net/sf/tapestry/engine Namespace.java,1.7.2.1,1.7.2.2 DefaultTemplateSource.java,1.9.2.3,1.9.2.4 DefaultSpecificationSource.java,1.15.2.2,1.15.2.3

Howard Lewis Ship <[email protected]>
Newsgroups gmane.comp.java.tapestry.cvs
Message-ID <[email protected]>
Update of /cvsroot/tapestry/Tapestry/framework/src/net/sf/tapestry/engine
In directory sc8-pr-cvs1:/tmp/cvs-serv15280/framework/src/net/sf/tapestry/engine

Modified Files:
      Tag: hship-2-3
	Namespace.java DefaultTemplateSource.java 
	DefaultSpecificationSource.java 
Log Message:
Check in support for templates in the application root.

Index: Namespace.java
===================================================================
RCS file: /cvsroot/tapestry/Tapestry/framework/src/net/sf/tapestry/engine/Namespace.java,v
retrieving revision 1.7.2.1
retrieving revision 1.7.2.2
diff -C2 -d -r1.7.2.1 -r1.7.2.2
*** Namespace.java	7 Dec 2002 13:26:10 -0000	1.7.2.1
--- Namespace.java	11 Dec 2002 14:02:26 -0000	1.7.2.2
***************
*** 1,4 ****
--- 1,5 ----
  package net.sf.tapestry.engine;
  
+ import java.util.Collections;
  import java.util.HashMap;
  import java.util.List;
***************
*** 68,75 ****
      /**
       *  Map of {@link ComponentSpecification} keyed on page name.
       * 
       **/
  
!     private Map _pages = new HashMap();
  
      /**
--- 69,79 ----
      /**
       *  Map of {@link ComponentSpecification} keyed on page name.
+      *  The map is synchronized because different threads may
+      *  try to update it simultaneously (due to dynamic page
+      *  discovery in the application namespace).
       * 
       **/
  
!     private Map _pages = Collections.synchronizedMap(new HashMap());
  
      /**
***************
*** 219,223 ****
       **/
  
!     private String getNamespaceId()
      {
          if (_frameworkNamespace)
--- 223,227 ----
       **/
  
!     public String getNamespaceId()
      {
          if (_frameworkNamespace)
***************
*** 230,233 ****
--- 234,245 ----
      }
  
+ 
+     /**
+      *  Gets the specification from the specification source.
+      * 
+      *  @throws ApplicationRuntimeException if the named page is not defined.
+      * 
+      **/
+ 
      private ComponentSpecification locatePageSpecification(String name)
      {
***************
*** 242,252 ****
      }
  
!     private ComponentSpecification locateComponentSpecification(String alias)
      {
!         String path = _specification.getComponentSpecificationPath(alias);
  
          if (path == null)
              throw new ApplicationRuntimeException(
!                 Tapestry.getString("Namespace.no-such-alias", alias, getNamespaceId()));
  
         IResourceLocation location = getSpecificationLocation().getRelativeLocation(path);
--- 254,264 ----
      }
  
!     private ComponentSpecification locateComponentSpecification(String type)
      {
!         String path = _specification.getComponentSpecificationPath(type);
  
          if (path == null)
              throw new ApplicationRuntimeException(
!                 Tapestry.getString("Namespace.no-such-alias", type, getNamespaceId()));
  
         IResourceLocation location = getSpecificationLocation().getRelativeLocation(path);
***************
*** 292,299 ****
      }
  
! 
      public IResourceLocation getSpecificationLocation()
      {
          return _specification.getSpecificationLocation();
      }
  
--- 304,326 ----
      }
  
!     /** @since 2.4 **/
!     
      public IResourceLocation getSpecificationLocation()
      {
          return _specification.getSpecificationLocation();
+     }
+ 
+     /** @since 2.4 **/
+     
+     public boolean isApplicationNamespace()
+     {
+         return _applicationNamespace;
+     }
+ 
+     /** @since 2.4 **/
+     
+     public void installPageSpecification(String pageName, ComponentSpecification specification)
+     {
+         _pages.put(pageName, specification);
      }
  

Index: DefaultTemplateSource.java
===================================================================
RCS file: /cvsroot/tapestry/Tapestry/framework/src/net/sf/tapestry/engine/DefaultTemplateSource.java,v
retrieving revision 1.9.2.3
retrieving revision 1.9.2.4
diff -C2 -d -r1.9.2.3 -r1.9.2.4
*** DefaultTemplateSource.java	8 Dec 2002 15:04:47 -0000	1.9.2.3
--- DefaultTemplateSource.java	11 Dec 2002 14:02:26 -0000	1.9.2.4
***************
*** 6,9 ****
--- 6,10 ----
  import java.io.InputStreamReader;
  import java.net.URL;
+ import java.util.Collections;
  import java.util.HashMap;
  import java.util.Iterator;
***************
*** 11,14 ****
--- 12,16 ----
  import java.util.Map;
  
+ import org.apache.commons.lang.builder.ToStringBuilder;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
***************
*** 58,67 ****
      // is the ComponentTemplate.
  
!     private Map _cache = new HashMap();
  
!     // Previously read templates; key is the HTML resource path, value
      // is the ComponentTemplate.
  
!     private Map _templates = new HashMap();
  
      /**
--- 60,69 ----
      // is the ComponentTemplate.
  
!     private Map _cache = Collections.synchronizedMap(new HashMap());
  
!     // Previously read templates; key is the IResourceLocation, value
      // is the ComponentTemplate.
  
!     private Map _templates = Collections.synchronizedMap(new HashMap());
  
      /**
***************
*** 128,132 ****
      public void reset()
      {
!         _cache = null;
          _templates.clear();
  
--- 130,134 ----
      public void reset()
      {
!         _cache.clear();
          _templates.clear();
  
***************
*** 159,163 ****
          {
              String stringKey =
!                 (locale == null) ? "DefaultTemplateSource.no-template" : "DefaultTemplateSource.no-template-in-locale";
  
              throw new ApplicationRuntimeException(Tapestry.getString(stringKey, component.getExtendedId(), locale));
--- 161,167 ----
          {
              String stringKey =
!                 component.getSpecification().isPageSpecification()
!                     ? "DefaultTemplateSource.no-template-for-page"
!                     : "DefaultTemplateSource.no-template-for-component";
  
              throw new ApplicationRuntimeException(Tapestry.getString(stringKey, component.getExtendedId(), locale));
***************
*** 169,191 ****
      }
  
!     private synchronized ComponentTemplate searchCache(Object key)
      {
-         if (_cache == null)
-             return null;
- 
          return (ComponentTemplate) _cache.get(key);
- 
      }
  
!     private synchronized void saveToCache(Object key, ComponentTemplate template)
      {
-         if (_cache == null)
-             _cache = new HashMap();
- 
          _cache.put(key, template);
  
      }
  
!     private synchronized ComponentTemplate findTemplate(
          IRequestCycle cycle,
          IResourceLocation location,
--- 173,201 ----
      }
  
!     private ComponentTemplate searchCache(Object key)
      {
          return (ComponentTemplate) _cache.get(key);
      }
  
!     private void saveToCache(Object key, ComponentTemplate template)
      {
          _cache.put(key, template);
  
      }
  
!     /**
!      *  Finds the template for the given component, using the following rules:
!      *  <ul>
!      *  <li>If the component has a $template asset, use that
!      *  <li>Look for a template in the same folder as the component
!      *  <li>If a page in the application namespace, search in the application root
!      *  <li>Fail!
!      *  </ul>
!      * 
!      *  @returns the template, or null if not found
!      * 
!      **/
! 
!     private ComponentTemplate findTemplate(
          IRequestCycle cycle,
          IResourceLocation location,
***************
*** 198,202 ****
              return readTemplateFromAsset(cycle, component, templateAsset, locale);
  
!         return findStandardTemplate(cycle, location, component, locale);
      }
  
--- 208,246 ----
              return readTemplateFromAsset(cycle, component, templateAsset, locale);
  
!         String name = location.getName();
!         int dotx = name.lastIndexOf('.');
!         String templateBaseName = name.substring(0, dotx) + ".html";
! 
!         ComponentTemplate result = findStandardTemplate(cycle, location, component, templateBaseName, locale);
! 
!         if (result == null
!             && component.getSpecification().isPageSpecification()
!             && component.getNamespace().isApplicationNamespace())
!             result = findPageTemplateInApplicationRoot(cycle, component, templateBaseName, locale);
! 
!         return result;
!     }
! 
!     private IResourceLocation _applicationRootLocation;
! 
!     private ComponentTemplate findPageTemplateInApplicationRoot(
!         IRequestCycle cycle,
!         IComponent component,
!         String templateBaseName,
!         Locale locale)
!     {
!         if (LOG.isDebugEnabled())
!             LOG.debug("Checking for " + templateBaseName + " in application root");
! 
!         if (_applicationRootLocation == null)
!             _applicationRootLocation = Tapestry.getApplicationRootLocation(cycle);
! 
!         IResourceLocation baseLocation = _applicationRootLocation.getRelativeLocation(templateBaseName);
!         IResourceLocation localizedLocation = baseLocation.getLocalization(locale);
! 
!         if (localizedLocation == null)
!             return null;
! 
!         return getOrParseTemplate(cycle, localizedLocation, component);
      }
  
***************
*** 206,210 ****
       **/
  
!     private synchronized ComponentTemplate readTemplateFromAsset(
          IRequestCycle cycle,
          IComponent component,
--- 250,254 ----
       **/
  
!     private ComponentTemplate readTemplateFromAsset(
          IRequestCycle cycle,
          IComponent component,
***************
*** 229,233 ****
          }
  
!         return constructTokens(cycle, templateData, asset.toString(), component);
      }
  
--- 273,277 ----
          }
  
!         return constructTemplateInstance(cycle, templateData, asset.toString(), component);
      }
  
***************
*** 237,246 ****
       *  parsing the template.
       *
       **/
  
!     private synchronized ComponentTemplate findStandardTemplate(
          IRequestCycle cycle,
          IResourceLocation location,
          IComponent component,
          Locale locale)
      {
--- 281,293 ----
       *  parsing the template.
       *
+      *  @returns the template, or null if not found.
+      * 
       **/
  
!     private ComponentTemplate findStandardTemplate(
          IRequestCycle cycle,
          IResourceLocation location,
          IComponent component,
+         String templateBaseName,
          Locale locale)
      {
***************
*** 252,268 ****
                      + locale.getDisplayName());
  
!         String name = location.getName();
  
!         int dotx = name.lastIndexOf('.');
  
!         String templateBaseName = name.substring(0, dotx) + ".html";
  
!         IResourceLocation baseTemplateLocation = location.getRelativeLocation(templateBaseName);
  
!         IResourceLocation localizedTemplateLocation = baseTemplateLocation.getLocalization(locale);
  
!         // See if it's been parsed before
  
!         ComponentTemplate result = (ComponentTemplate) _templates.get(localizedTemplateLocation);
          if (result != null)
              return result;
--- 299,324 ----
                      + locale.getDisplayName());
  
!         IResourceLocation baseTemplateLocation = location.getRelativeLocation(templateBaseName);
  
!         IResourceLocation localizedTemplateLocation = baseTemplateLocation.getLocalization(locale);
  
!         if (localizedTemplateLocation == null)
!             return null;
  
!         return getOrParseTemplate(cycle, localizedTemplateLocation, component);
  
!     }
  
!     /**
!      *  Returns a previously parsed template at the specified location (which must already
!      *  be localized).  If not already in the template Map, then the
!      *  location is parsed and stored into the templates Map, then returned.
!      * 
!      **/
  
!     private ComponentTemplate getOrParseTemplate(IRequestCycle cycle, IResourceLocation location, IComponent component)
!     {
! 
!         ComponentTemplate result = (ComponentTemplate) _templates.get(location);
          if (result != null)
              return result;
***************
*** 270,277 ****
          // Ok, see if it exists.
  
!         result = parseTemplate(cycle, localizedTemplateLocation, component);
  
          if (result != null)
!             _templates.put(localizedTemplateLocation, result);
  
          return result;
--- 326,333 ----
          // Ok, see if it exists.
  
!         result = parseTemplate(cycle, location, component);
  
          if (result != null)
!             _templates.put(location, result);
  
          return result;
***************
*** 292,299 ****
              return null;
  
!         return constructTokens(cycle, templateData, location.toString(), component);
      }
  
!     private ComponentTemplate constructTokens(
          IRequestCycle cycle,
          char[] templateData,
--- 348,363 ----
              return null;
  
!         return constructTemplateInstance(cycle, templateData, location.toString(), component);
      }
  
!     /** 
!      *  This method is currently synchronized, because
!      *  {@link TemplateParser} is not threadsafe.  Another good candidate
!      *  for a pooling mechanism, especially because parsing a template
!      *  may take a while.
!      * 
!      **/
! 
!     private synchronized ComponentTemplate constructTemplateInstance(
          IRequestCycle cycle,
          char[] templateData,
***************
*** 338,348 ****
      private char[] readTemplate(IResourceLocation location)
      {
          URL url = location.getResourceURL();
  
          if (url == null)
              return null;
  
          if (LOG.isDebugEnabled())
!             LOG.debug("Reading template " + location + " from " + url);
  
          InputStream stream = null;
--- 402,420 ----
      private char[] readTemplate(IResourceLocation location)
      {
+         if (LOG.isDebugEnabled())
+             LOG.debug("Reading template " + location);
+ 
          URL url = location.getResourceURL();
  
          if (url == null)
+         {
+             if (LOG.isDebugEnabled())
+                 LOG.debug("Template does not exist.");
+ 
              return null;
+         }
  
          if (LOG.isDebugEnabled())
!             LOG.debug("Reading template from URL " + url);
  
          InputStream stream = null;
***************
*** 362,374 ****
          finally
          {
!             try
!             {
!                 if (stream != null)
!                     stream.close();
!             }
!             catch (IOException e)
!             {
!                 // Ignore it!
!             }
          }
  
--- 434,438 ----
          finally
          {
!             Tapestry.close(stream);
          }
  
***************
*** 419,442 ****
      }
  
!     public synchronized String toString()
      {
!         StringBuffer buffer = new StringBuffer("DefaultTemplateSource@");
!         buffer.append(Integer.toHexString(hashCode()));
! 
!         buffer.append('[');
! 
!         if (_cache != null)
!             buffer.append(_cache.keySet());
  
!         if (_tokenCount > 0)
!         {
!             buffer.append(", ");
!             buffer.append(_tokenCount);
!             buffer.append(" tokens");
!         }
  
!         buffer.append(']');
  
!         return buffer.toString();
      }
  
--- 483,495 ----
      }
  
!     public String toString()
      {
!         ToStringBuilder builder = new ToStringBuilder(this);
  
!         builder.append("tokenCount", _tokenCount);
  
!         builder.append("templates", _templates.keySet());
  
!         return builder.toString();
      }
  

Index: DefaultSpecificationSource.java
===================================================================
RCS file: /cvsroot/tapestry/Tapestry/framework/src/net/sf/tapestry/engine/DefaultSpecificationSource.java,v
retrieving revision 1.15.2.2
retrieving revision 1.15.2.3
diff -C2 -d -r1.15.2.2 -r1.15.2.3
*** DefaultSpecificationSource.java	7 Dec 2002 13:26:10 -0000	1.15.2.2
--- DefaultSpecificationSource.java	11 Dec 2002 14:02:26 -0000	1.15.2.3
***************
*** 321,324 ****
--- 321,327 ----
      private SpecificationParser getParser()
      {
+         // It would be good if this could get resused.  SpecificationParser
+         // is not threadsafe, so it would have to be pooled.
+         
          return new SpecificationParser();
      }



-------------------------------------------------------
This sf.net email is sponsored by:
With Great Power, Comes Great Responsibility 
Learn to use your power at OSDN's High Performance Computing Channel
http://hpc.devchannel.org/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.