    001// Copyright 2007, 2008 The Apache Software Foundation
    002//
    003// Licensed under the Apache License, Version 2.0 (the "License");
    004// you may not use this file except in compliance with the License.
    005// You may obtain a copy of the License at
    006//
    007//     http://www.apache.org/licenses/LICENSE-2.0
    008//
    009// Unless required by applicable law or agreed to in writing, software
    010// distributed under the License is distributed on an "AS IS" BASIS,
    011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    012// See the License for the specific language governing permissions and
    013// limitations under the License.
    014
    015package org.apache.tapestry5.internal.services;
    016
    017import org.apache.tapestry5.Asset;
    018import org.apache.tapestry5.annotations.Path;
    019import org.apache.tapestry5.ioc.AnnotationProvider;
    020import org.apache.tapestry5.ioc.ObjectLocator;
    021import org.apache.tapestry5.ioc.ObjectProvider;
    022import org.apache.tapestry5.ioc.services.Builtin;
    023import org.apache.tapestry5.ioc.services.SymbolSource;
    024import org.apache.tapestry5.ioc.services.TypeCoercer;
    025import org.apache.tapestry5.services.AssetSource;
    026import org.apache.tapestry5.services.Core;
    027
    028/**
    029 * Exposes assets (in the current locale). The Inject annotation must be supplemented by a {@link Path} annotation, to
    030 * identify what asset to be injected.
    031 */
    032public class AssetObjectProvider implements ObjectProvider
    033{
    034    private final AssetSource source;
    035
    036    private final TypeCoercer typeCoercer;
    037
    038    private final SymbolSource symbolSource;
    039
    040    public AssetObjectProvider(@Core AssetSource source,
    041
    042                               @Builtin TypeCoercer typeCoercer,
    043
    044                               @Builtin SymbolSource symbolSource)
    045    {
    046        this.source = source;
    047        this.typeCoercer = typeCoercer;
    048        this.symbolSource = symbolSource;
    049    }
    050
    051    /**
    052     * Provides the asset. If the expression does not identify an asset domain, with a prefix, it is assumed to be a
    053     * path on the classpath, relative to the root of the classpath.
    054     *
    055     * @param objectType the type of object (which must be Object or Asset)
    056     * @param locator    not used
    057     */
    058    public <T> T provide(Class<T> objectType, AnnotationProvider annotationProvider, ObjectLocator locator)
    059    {
    060        Path path = annotationProvider.getAnnotation(Path.class);
    061
    062        if (path == null) return null;
    063
    064        String expanded = symbolSource.expandSymbols(path.value());
    065
    066        Asset asset = source.getAsset(null, expanded, null);
    067
    068        return typeCoercer.coerce(asset, objectType);
    069    }
    070}