001// Copyright 2005 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.tapestry.annotations;
016
017import java.lang.reflect.Method;
018
019import org.apache.hivemind.Location;
020import org.apache.hivemind.Resource;
021import org.apache.tapestry.enhance.EnhancementOperation;
022import org.apache.tapestry.spec.AssetSpecification;
023import org.apache.tapestry.spec.IAssetSpecification;
024import org.apache.tapestry.spec.IComponentSpecification;
025import org.apache.tapestry.util.DescribedLocation;
026
027/**
028 * Uses the {@link org.apache.tapestry.annotations.Asset} annotation to create a new
029 * {@link org.apache.tapestry.spec.IAssetSpecification} which is then added to the
030 * {@link org.apache.tapestry.spec.IComponentSpecification}.
031 * 
032 * @author Howard Lewis Ship
033 * @since 4.0
034 */
035public class AssetAnnotationWorker implements MethodAnnotationEnhancementWorker
036{
037
038    public void performEnhancement(EnhancementOperation op, IComponentSpecification spec,
039            Method method, Location location)
040    {
041        Asset asset = method.getAnnotation(Asset.class);
042        String propertyName = AnnotationUtils.getPropertyName(method);
043
044        IAssetSpecification as = new AssetSpecification();
045        as.setPath(asset.value());
046        as.setPropertyName(propertyName);
047
048        // Very important for assets, as they need a location (really, the Resource
049        // of a location) to figure out what kind of asset they are.
050
051        Resource specResource = spec.getSpecificationLocation();
052        Location assetLocation = new DescribedLocation(specResource, location.toString());
053
054        as.setLocation(assetLocation);
055
056        spec.addAsset(propertyName, as);
057    }
058
059}