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 015 package org.apache.tapestry.annotations; 016 017 import java.lang.reflect.Method; 018 019 import org.apache.hivemind.ApplicationRuntimeException; 020 import org.apache.hivemind.ClassResolver; 021 import org.apache.hivemind.Location; 022 import org.apache.hivemind.Resource; 023 import org.apache.hivemind.util.ClasspathResource; 024 import org.apache.tapestry.enhance.EnhancementOperation; 025 import org.apache.tapestry.enhance.EnhancementWorker; 026 import org.apache.tapestry.enhance.InjectAssetWorker; 027 import org.apache.tapestry.spec.IAssetSpecification; 028 import org.apache.tapestry.spec.IComponentSpecification; 029 030 /** 031 * Injects an asset. 032 * 033 * @author Howard M. Lewis Ship 034 * @since 4.0 035 * @see org.apache.tapestry.annotations.InjectAsset 036 * @see org.apache.tapestry.enhance.InjectAssetWorker 037 */ 038 public class InjectAssetAnnotationWorker implements EnhancementWorker 039 { 040 InjectAssetWorker _delegate; 041 042 private ClassResolver _classResolver; 043 044 InjectAssetAnnotationWorker(InjectAssetWorker delegate) 045 { 046 _delegate = delegate; 047 } 048 049 public InjectAssetAnnotationWorker() 050 { 051 this(new InjectAssetWorker()); 052 } 053 054 public void performEnhancement(EnhancementOperation op, IComponentSpecification spec) 055 { 056 Class clazz = op.getBaseClass(); 057 058 Resource classResource = newClassResource(clazz); 059 060 for (Method m : clazz.getMethods()) 061 { 062 if (m.getAnnotation(InjectAsset.class) != null) { 063 064 performEnhancement(op, spec, m, 065 AnnotationUtils.buildLocationForAnnotation( 066 m, 067 m.getAnnotation(InjectAsset.class), 068 classResource)); 069 } 070 } 071 } 072 073 private ClasspathResource newClassResource(Class clazz) 074 { 075 return new ClasspathResource(_classResolver, clazz.getName().replace('.', '/')); 076 } 077 078 public void performEnhancement(EnhancementOperation op, IComponentSpecification spec, Method method, Location location) 079 { 080 InjectAsset as = method.getAnnotation(InjectAsset.class); 081 082 IAssetSpecification asset = spec.getAsset(as.value()); 083 if (asset == null) { 084 085 throw new ApplicationRuntimeException(AnnotationMessages.unknownAsset(as.value(), location)); 086 } 087 088 String propertyName = AnnotationUtils.getPropertyName(method); 089 090 _delegate.injectAsset(op, as.value(), propertyName, location); 091 } 092 093 public void setClassResolver(ClassResolver classResolver) 094 { 095 _classResolver = classResolver; 096 } 097 }