001// Copyright 2007, 2008, 2010, 2011 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.ComponentResources; 019import org.apache.tapestry5.annotations.Path; 020import org.apache.tapestry5.ioc.ObjectLocator; 021import org.apache.tapestry5.model.MutableComponentModel; 022import org.apache.tapestry5.plastic.ComputedValue; 023import org.apache.tapestry5.plastic.InstanceContext; 024import org.apache.tapestry5.plastic.PlasticField; 025import org.apache.tapestry5.services.AssetSource; 026import org.apache.tapestry5.services.transform.InjectionProvider2; 027 028/** 029 * Performs injection of assets, based on the presence of the {@link Path} annotation. This is more 030 * useful than the 031 * general {@link AssetObjectProvider}, because relative assets are supported. 032 */ 033public class AssetInjectionProvider implements InjectionProvider2 034{ 035 private final AssetSource assetSource; 036 037 public AssetInjectionProvider(AssetSource assetSource) 038 { 039 this.assetSource = assetSource; 040 } 041 042 public boolean provideInjection(PlasticField field, ObjectLocator locator, MutableComponentModel componentModel) 043 { 044 Path path = field.getAnnotation(Path.class); 045 046 if (path == null) 047 { 048 return false; 049 } 050 051 final String assetPath = path.value(); 052 053 ComputedValue<Asset> computedAsset = new ComputedValue<Asset>() 054 { 055 public Asset get(InstanceContext context) 056 { 057 ComponentResources resources = context.get(ComponentResources.class); 058 059 return assetSource.getComponentAsset(resources, assetPath); 060 } 061 }; 062 063 field.injectComputed(computedAsset); 064 065 return true; 066 } 067 068}