Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  // Copyright 2006, 2007, 2008, 2009, 2010 The Apache Software Foundation
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  // http://www.apache.org/licenses/LICENSE-2.0
  //
  // Unless required by applicable law or agreed to in writing, software
 // distributed under the License is distributed on an "AS IS" BASIS,
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
 package org.apache.tapestry5.internal.services;
 
 
 import java.util.List;
 import java.util.Map;

Service implementation that manages a cache of parsed component templates.
 
         UpdateListener
 {
     private final TemplateParser parser;
 
     private final ComponentTemplateLocator locator;
 
     private final URLChangeTracker tracker;

    
Caches from a key (combining component name and locale) to a resource. Often, many different keys will point to the same resource (i.e., "foo:en_US", "foo:en_UK", and "foo:en" may all be parsed from the same "foo.tml" resource). The resource may end up being null, meaning the template does not exist in any locale.
 
     private final Map<MultiKeyResourcetemplateResources = CollectionFactory.newConcurrentMap();

    
Cache of parsed templates, keyed on resource.
 
     private final Map<ResourceComponentTemplatetemplates = CollectionFactory.newConcurrentMap();
 
     private final ComponentTemplate missingTemplate = new ComponentTemplate()
     {
         public Map<StringLocationgetComponentIds()
         {
             return Collections.emptyMap();
         }
 
         public Resource getResource()
         {
             return null;
         }
 
         public List<TemplateTokengetTokens()
         {
             return Collections.emptyList();
         }
 
         public boolean isMissing()
         {
             return true;
         }
 
         public List<TemplateTokengetExtensionPointTokens(String extensionPointId)
         {
             return null;
         }
 
         public boolean isExtension()
         {
             return false;
         }
     };
 
     ComponentTemplateLocator templateLocatorClasspathURLConverter classpathURLConverter)
     {
         this(parsertemplateLocatornew URLChangeTracker(classpathURLConverter));
     }
    {
        this. = parser;
        this. = locator;
        this. = tracker;
    }

    
Resolves the component name to a localized org.apache.tapestry5.ioc.Resource (using the org.apache.tapestry5.services.templates.ComponentTemplateLocator chain of command service). The localized resource is used as the key to a cache of org.apache.tapestry5.internal.parser.ComponentTemplates.

If a template doesn't exist, then the missing ComponentTemplate is returned.

    public ComponentTemplate getTemplate(ComponentModel componentModelLocale locale)
    {
        String componentName = componentModel.getComponentClassName();
        MultiKey key = new MultiKey(componentNamelocale);
        // First cache is key to resource.
        Resource resource = .get(key);
        if (resource == null)
        {
            resource = locateTemplateResource(componentModellocale);
            .put(keyresource);
        }
        // If we haven't yet parsed the template into the cache, do so now.
        ComponentTemplate result = .get(resource);
        if (result == null)
        {
            result = parseTemplate(resource);
            .put(resourceresult);
        }
        return result;
    }
    {
        // In a race condition, we may parse the same template more than once. This will likely add
        // the resource to the tracker multiple times. Not likely this will cause a big issue.
        if (!r.exists())
            return ;
        .add(r.toURL());
        return .parseTemplate(r);
    }
    private Resource locateTemplateResource(ComponentModel initialModelLocale locale)
    {
        ComponentModel model = initialModel;
        while (model != null)
        {
            Resource localized = .locateTemplate(modellocale);
            if (localized != null)
                return localized;
            // Otherwise, this component doesn't have its own template ... lets work up to its
            // base class and check there.
            model = model.getParentModel();
        }
        // This will be a Resource whose URL is null, which will be picked up later and force the
        // return of the empty template.
        return initialModel.getBaseResource().withExtension(.);
    }

    
Checks to see if any parsed resource has changed. If so, then all internal caches are cleared, and an invalidation event is fired. This is brute force ... a more targeted dependency management strategy may come later.
    public void checkForUpdates()
    {
        if (.containsChanges())
        {
            .clear();
            .clear();
            .clear();
            fireInvalidationEvent();
        }
    }
    {
        return this;
    }
New to GrepCode? Check out our FAQ X