Search in sources :

Example 6 with AssetFactory

use of org.apache.tapestry5.services.AssetFactory in project tapestry-5 by apache.

the class AssetSourceImpl method createAssetFromResource.

private Asset createAssetFromResource(Resource resource) {
    try {
        upgradeReadLockToWriteLock();
        // Check for competing thread beat us to it (not very likely!):
        Asset result = TapestryInternalUtils.getAndDeref(cache, resource);
        if (result != null) {
            return result;
        }
        Class resourceClass = resource.getClass();
        AssetFactory factory = registry.get(resourceClass);
        return factory.createAsset(resource);
    } finally {
        downgradeWriteLockToReadLock();
    }
}
Also used : Asset(org.apache.tapestry5.Asset) AssetFactory(org.apache.tapestry5.services.AssetFactory)

Example 7 with AssetFactory

use of org.apache.tapestry5.services.AssetFactory in project tapestry-5 by apache.

the class AssetSourceImplTest method absolute_asset_with_known_prefix.

@Test
public void absolute_asset_with_known_prefix() {
    AssetFactory factory = mockAssetFactory();
    ThreadLocale threadLocale = mockThreadLocale();
    Asset asset = mockAsset();
    Resource expectedResource = rootResource.forFile("org/apache/tapestry5/internal/services/SimpleComponent_en_GB.properties");
    train_getRootResource(factory, rootResource);
    train_createAsset(factory, expectedResource, asset);
    Map<String, AssetFactory> configuration = Collections.singletonMap("classpath", factory);
    replay();
    AssetSource source = new AssetSourceImpl(threadLocale, configuration, null, null, tracker);
    assertSame(source.getAsset(baseResource, "classpath:org/apache/tapestry5/internal/services/SimpleComponent.properties", Locale.UK), asset);
    // Check that a leading slash is not a problem:
    assertSame(source.getAsset(baseResource, "classpath:/org/apache/tapestry5/internal/services/SimpleComponent.properties", Locale.UK), asset);
    verify();
}
Also used : AssetSource(org.apache.tapestry5.services.AssetSource) ThreadLocale(org.apache.tapestry5.ioc.services.ThreadLocale) Resource(org.apache.tapestry5.commons.Resource) ClasspathResource(org.apache.tapestry5.ioc.internal.util.ClasspathResource) Asset(org.apache.tapestry5.Asset) AssetFactory(org.apache.tapestry5.services.AssetFactory) Test(org.testng.annotations.Test)

Example 8 with AssetFactory

use of org.apache.tapestry5.services.AssetFactory in project tapestry-5 by apache.

the class AssetSourceImplTest method get_classpath_asset_for_unspecified_locale.

@Test
public void get_classpath_asset_for_unspecified_locale() {
    AssetFactory factory = mockAssetFactory();
    ThreadLocale threadLocale = mockThreadLocale();
    Asset asset = mockAsset();
    Locale locale = Locale.UK;
    Resource expectedResource = baseResource.forFile("SimpleComponent_en_GB.properties");
    train_getRootResource(factory, rootResource);
    train_createAsset(factory, expectedResource, asset);
    Map<String, AssetFactory> configuration = Collections.singletonMap("classpath", factory);
    train_getLocale(threadLocale, locale);
    replay();
    AssetSource source = new AssetSourceImpl(threadLocale, configuration, null, null, tracker);
    assertSame(source.getClasspathAsset("org/apache/tapestry5/internal/services/SimpleComponent.properties"), asset);
    verify();
}
Also used : ThreadLocale(org.apache.tapestry5.ioc.services.ThreadLocale) Locale(java.util.Locale) AssetSource(org.apache.tapestry5.services.AssetSource) ThreadLocale(org.apache.tapestry5.ioc.services.ThreadLocale) Resource(org.apache.tapestry5.commons.Resource) ClasspathResource(org.apache.tapestry5.ioc.internal.util.ClasspathResource) Asset(org.apache.tapestry5.Asset) AssetFactory(org.apache.tapestry5.services.AssetFactory) Test(org.testng.annotations.Test)

Example 9 with AssetFactory

use of org.apache.tapestry5.services.AssetFactory in project tapestry-5 by apache.

the class AssetSourceImplTest method relative_asset.

@Test
public void relative_asset() {
    AssetFactory factory = mockAssetFactory();
    ThreadLocale threadLocale = mockThreadLocale();
    Asset asset = mockAsset();
    Resource expectedResource = baseResource.forFile("SimpleComponent_en_GB.properties");
    train_getRootResource(factory, rootResource);
    train_createAsset(factory, expectedResource, asset);
    Map<String, AssetFactory> configuration = Collections.singletonMap("classpath", factory);
    replay();
    AssetSource source = new AssetSourceImpl(threadLocale, configuration, null, null, tracker);
    // First try creates it:
    assertSame(source.getAsset(baseResource, "SimpleComponent.properties", Locale.UK), asset);
    // Second try shows that it is cached
    assertSame(source.getAsset(baseResource, "SimpleComponent.properties", Locale.UK), asset);
    verify();
}
Also used : AssetSource(org.apache.tapestry5.services.AssetSource) ThreadLocale(org.apache.tapestry5.ioc.services.ThreadLocale) Resource(org.apache.tapestry5.commons.Resource) ClasspathResource(org.apache.tapestry5.ioc.internal.util.ClasspathResource) Asset(org.apache.tapestry5.Asset) AssetFactory(org.apache.tapestry5.services.AssetFactory) Test(org.testng.annotations.Test)

Example 10 with AssetFactory

use of org.apache.tapestry5.services.AssetFactory in project tapestry-5 by apache.

the class AssetsModule method provideBuiltinAssetDispatchers.

/**
 * Contributes an handler for each mapped classpath alias, as well handlers for context assets
 * and stack assets (combined {@link org.apache.tapestry5.services.javascript.JavaScriptStack} files).
 */
@Contribute(Dispatcher.class)
@AssetRequestDispatcher
public static void provideBuiltinAssetDispatchers(MappedConfiguration<String, AssetRequestHandler> configuration, @ContextProvider AssetFactory contextAssetFactory, @Autobuild StackAssetRequestHandler stackAssetRequestHandler, ClasspathAssetAliasManager classpathAssetAliasManager, ResourceStreamer streamer, AssetSource assetSource, ClasspathAssetProtectionRule classpathAssetProtectionRule) {
    Map<String, String> mappings = classpathAssetAliasManager.getMappings();
    for (String folder : mappings.keySet()) {
        String path = mappings.get(folder);
        configuration.add(folder, new ClasspathAssetRequestHandler(streamer, assetSource, path, classpathAssetProtectionRule));
    }
    configuration.add(RequestConstants.CONTEXT_FOLDER, new ContextAssetRequestHandler(streamer, contextAssetFactory.getRootResource()));
    configuration.add(RequestConstants.STACK_FOLDER, stackAssetRequestHandler);
}
Also used : ContextAssetRequestHandler(org.apache.tapestry5.internal.services.assets.ContextAssetRequestHandler) ClasspathAssetRequestHandler(org.apache.tapestry5.internal.services.assets.ClasspathAssetRequestHandler) Contribute(org.apache.tapestry5.ioc.annotations.Contribute) AssetRequestDispatcher(org.apache.tapestry5.services.AssetRequestDispatcher)

Aggregations

AssetFactory (org.apache.tapestry5.services.AssetFactory)8 AssetSource (org.apache.tapestry5.services.AssetSource)7 Test (org.testng.annotations.Test)7 Asset (org.apache.tapestry5.Asset)6 ThreadLocale (org.apache.tapestry5.ioc.services.ThreadLocale)6 Resource (org.apache.tapestry5.commons.Resource)5 ClasspathResource (org.apache.tapestry5.ioc.internal.util.ClasspathResource)5 Contribute (org.apache.tapestry5.ioc.annotations.Contribute)2 Locale (java.util.Locale)1 ExternalUrlAssetFactory (org.apache.tapestry5.internal.services.ExternalUrlAssetFactory)1 ClasspathAssetRequestHandler (org.apache.tapestry5.internal.services.assets.ClasspathAssetRequestHandler)1 ContextAssetRequestHandler (org.apache.tapestry5.internal.services.assets.ContextAssetRequestHandler)1 DefaultTemplateLocator (org.apache.tapestry5.internal.services.templates.DefaultTemplateLocator)1 PageTemplateLocator (org.apache.tapestry5.internal.services.templates.PageTemplateLocator)1 SymbolSource (org.apache.tapestry5.ioc.services.SymbolSource)1 AssetRequestDispatcher (org.apache.tapestry5.services.AssetRequestDispatcher)1