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;
}
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;
}
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();
}
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();
}
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();
}
Aggregations