Search in sources :

Example 56 with Resource

use of org.apache.tapestry5.commons.Resource in project tapestry-5 by apache.

the class ComponentTemplateSourceImpl method getTemplate.

public ComponentTemplate getTemplate(ComponentModel componentModel, ComponentResourceSelector selector) {
    String componentName = componentModel.getComponentClassName();
    MultiKey key = new MultiKey(componentName, selector);
    // First cache is key to resource.
    Resource resource = templateResources.get(key);
    if (resource == null) {
        resource = locateTemplateResource(componentModel, selector);
        templateResources.put(key, resource);
    }
    // If we haven't yet parsed the template into the cache, do so now.
    ComponentTemplate result = templates.get(resource);
    if (result == null) {
        result = parseTemplate(resource);
        templates.put(resource, result);
    }
    return result;
}
Also used : MultiKey(org.apache.tapestry5.commons.util.MultiKey) Resource(org.apache.tapestry5.commons.Resource) ComponentTemplate(org.apache.tapestry5.internal.parser.ComponentTemplate)

Example 57 with Resource

use of org.apache.tapestry5.commons.Resource in project tapestry-5 by apache.

the class MessagesSourceImpl method findBundleProperties.

/**
 * Assembles a set of properties appropriate for the bundle in question, and the desired locale. The properties
 * reflect the properties of the bundles' parent (if any) for the locale, overalyed with any properties defined for
 * this bundle and its locale.
 */
private Map<String, String> findBundleProperties(MessagesBundle bundle, ComponentResourceSelector selector) {
    if (bundle == null)
        return emptyMap;
    MultiKey key = new MultiKey(bundle.getId(), selector);
    Map<String, String> existing = cookedProperties.get(key);
    if (existing != null)
        return existing;
    // What would be cool is if we could maintain a cache of bundle id + locale -->
    // Resource. That would optimize quite a bit of this; may need to use an alternative to
    // LocalizedNameGenerator.
    Resource propertiesResource = bundle.getBaseResource().withExtension("properties");
    List<Resource> localizations = resourceLocator.locateMessageCatalog(propertiesResource, selector);
    // Localizations are now in least-specific to most-specific order.
    Map<String, String> previous = findBundleProperties(bundle.getParent(), selector);
    for (Resource localization : F.flow(localizations).reverse()) {
        Map<String, String> rawProperties = getRawProperties(localization);
        // Woould be nice to write into the cookedProperties cache here,
        // but we can't because we don't know the selector part of the MultiKey.
        previous = extend(previous, rawProperties);
    }
    cookedProperties.put(key, previous);
    return previous;
}
Also used : MultiKey(org.apache.tapestry5.commons.util.MultiKey) Resource(org.apache.tapestry5.commons.Resource)

Example 58 with Resource

use of org.apache.tapestry5.commons.Resource in project tapestry-5 by apache.

the class ComponentMessagesSourceImplTest method no_app_catalog.

@Test
public void no_app_catalog() {
    ComponentModel model = mockComponentModel();
    ComponentModel parent = mockComponentModel();
    train_getComponentClassName(model, "org.apache.tapestry5.internal.services.SubclassComponent");
    train_getBaseResource(model, new ClasspathResource("org/apache/tapestry5/internal/services/SubclassComponent.class"));
    train_getParentModel(model, parent);
    train_getComponentClassName(parent, SIMPLE_COMPONENT_CLASS_NAME);
    train_getBaseResource(parent, simpleComponentResource);
    train_getParentModel(parent, null);
    replay();
    forceCacheClear();
    Resource resource = simpleComponentResource.forFile("NoSuchAppCatalog.properties");
    List<Resource> resources = Arrays.asList(resource);
    ComponentMessagesSource source = new ComponentMessagesSourceImpl(true, resources, new PropertiesFileParserImpl(), resourceLocator, converter, componentRequestSelectorAnalyzer, threadLocale);
    Messages messages = source.getMessages(model, Locale.ENGLISH);
    assertEquals(messages.get("color"), "color");
    assertEquals(messages.get("app-catalog-source"), "[[missing key: app-catalog-source]]");
    assertEquals(messages.get("app-catalog-overridden"), "Overridden by Component");
    verify();
}
Also used : ClasspathResource(org.apache.tapestry5.ioc.internal.util.ClasspathResource) PropertiesFileParserImpl(org.apache.tapestry5.internal.services.messages.PropertiesFileParserImpl) Messages(org.apache.tapestry5.commons.Messages) ComponentModel(org.apache.tapestry5.model.ComponentModel) Resource(org.apache.tapestry5.commons.Resource) ClasspathResource(org.apache.tapestry5.ioc.internal.util.ClasspathResource) ComponentMessagesSource(org.apache.tapestry5.services.messages.ComponentMessagesSource) Test(org.testng.annotations.Test)

Example 59 with Resource

use of org.apache.tapestry5.commons.Resource in project tapestry-5 by apache.

the class ComponentTemplateSourceImplTest method localization_to_same.

/**
 * Checks that localization to the same resource works (w/ caching).
 */
@Test
public void localization_to_same() {
    Resource resource = mockResource();
    TemplateParser parser = mockTemplateParser();
    ComponentTemplate template = mockComponentTemplate();
    ComponentModel model = mockComponentModel();
    ComponentResourceLocator locator = newMock(ComponentResourceLocator.class);
    train_getComponentClassName(model, PACKAGE + ".Fred");
    expect(locator.locateTemplate(model, english)).andReturn(resource).once();
    expect(resource.exists()).andReturn(true).anyTimes();
    expect(resource.toURL()).andReturn(null).anyTimes();
    expect(locator.locateTemplate(model, french)).andReturn(resource).once();
    train_parseTemplate(parser, resource, template);
    replay();
    ComponentTemplateSourceImpl source = new ComponentTemplateSourceImpl(true, parser, locator, converter, componentRequestSelectorAnalyzer, threadLocale);
    assertSame(source.getTemplate(model, Locale.ENGLISH), template);
    // A second pass finds the same resource, but using a different
    // path/locale combination.
    assertSame(source.getTemplate(model, Locale.FRENCH), template);
    // A third pass should further demonstrate the caching.
    assertSame(source.getTemplate(model, Locale.FRENCH), template);
    verify();
}
Also used : Resource(org.apache.tapestry5.commons.Resource) ClasspathResource(org.apache.tapestry5.ioc.internal.util.ClasspathResource) ComponentModel(org.apache.tapestry5.model.ComponentModel) ComponentResourceLocator(org.apache.tapestry5.services.pageload.ComponentResourceLocator) ComponentTemplate(org.apache.tapestry5.internal.parser.ComponentTemplate) Test(org.testng.annotations.Test)

Example 60 with Resource

use of org.apache.tapestry5.commons.Resource in project tapestry-5 by apache.

the class ComponentTemplateSourceImplTest method invalidation.

/**
 * Tests resource invalidation.
 */
@Test
public void invalidation() throws Exception {
    File rootDir = createClasspathRoot();
    URLClassLoader loader = newLoaderWithClasspathRoot(rootDir);
    ComponentModel model = mockComponentModel();
    File packageDir = new File(rootDir, "baz");
    packageDir.mkdirs();
    File f = new File(packageDir, "Biff.tml");
    f.createNewFile();
    Resource baseResource = new ClasspathResource(loader, "baz/Biff.class");
    Resource localized = baseResource.withExtension(TapestryConstants.TEMPLATE_EXTENSION);
    TemplateParser parser = mockTemplateParser();
    ComponentTemplate template = mockComponentTemplate();
    InvalidationListener listener = mockInvalidationListener();
    train_getComponentClassName(model, "baz.Biff");
    ComponentResourceLocator locator = mockLocator(model, english, localized);
    train_parseTemplate(parser, localized, template);
    replay();
    ComponentTemplateSourceImpl source = new ComponentTemplateSourceImpl(false, parser, locator, converter, componentRequestSelectorAnalyzer, threadLocale);
    source.addInvalidationListener(listener);
    assertSame(source.getTemplate(model, Locale.ENGLISH), template);
    // Check for updates (which won't be found).
    source.checkForUpdates();
    // A second pass will test the caching (the
    // parser is not invoked).
    assertSame(source.getTemplate(model, Locale.ENGLISH), template);
    verify();
    // Now, change the file and processInbound an UpdateEvent.
    touch(f);
    listener.objectWasInvalidated();
    replay();
    // Check for updates (which will be found).
    source.checkForUpdates();
    verify();
    // Check that the cache really is cleared.
    train_getComponentClassName(model, "baz.Biff");
    expect(locator.locateTemplate(model, english)).andReturn(localized);
    train_parseTemplate(parser, localized, template);
    replay();
    assertSame(source.getTemplate(model, Locale.ENGLISH), template);
    verify();
}
Also used : ClasspathResource(org.apache.tapestry5.ioc.internal.util.ClasspathResource) URLClassLoader(java.net.URLClassLoader) ComponentModel(org.apache.tapestry5.model.ComponentModel) Resource(org.apache.tapestry5.commons.Resource) ClasspathResource(org.apache.tapestry5.ioc.internal.util.ClasspathResource) ComponentResourceLocator(org.apache.tapestry5.services.pageload.ComponentResourceLocator) ComponentTemplate(org.apache.tapestry5.internal.parser.ComponentTemplate) InvalidationListener(org.apache.tapestry5.commons.services.InvalidationListener) File(java.io.File) Test(org.testng.annotations.Test)

Aggregations

Resource (org.apache.tapestry5.commons.Resource)78 Test (org.testng.annotations.Test)62 MutableComponentModel (org.apache.tapestry5.model.MutableComponentModel)38 Logger (org.slf4j.Logger)38 ClasspathResource (org.apache.tapestry5.ioc.internal.util.ClasspathResource)16 Asset (org.apache.tapestry5.Asset)14 ComponentModel (org.apache.tapestry5.model.ComponentModel)10 MutableEmbeddedComponentModel (org.apache.tapestry5.model.MutableEmbeddedComponentModel)10 ComponentTemplate (org.apache.tapestry5.internal.parser.ComponentTemplate)6 AssetFactory (org.apache.tapestry5.services.AssetFactory)6 IOException (java.io.IOException)5 AssetSource (org.apache.tapestry5.services.AssetSource)5 ComponentResourceLocator (org.apache.tapestry5.services.pageload.ComponentResourceLocator)5 Context (org.apache.tapestry5.http.services.Context)4 BeginRender (org.apache.tapestry5.annotations.BeginRender)3 Location (org.apache.tapestry5.commons.Location)3 AbstractResource (org.apache.tapestry5.ioc.internal.util.AbstractResource)3 ThreadLocale (org.apache.tapestry5.ioc.services.ThreadLocale)3 ClasspathAssetAliasManager (org.apache.tapestry5.services.ClasspathAssetAliasManager)3 StreamableResource (org.apache.tapestry5.services.assets.StreamableResource)3