001// Copyright 2008, 2009, 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.ioc.internal.services; 016 017import org.apache.tapestry5.ioc.AnnotationAccess; 018import org.apache.tapestry5.ioc.annotations.PreventServiceDecoration; 019import org.apache.tapestry5.ioc.internal.AnnotationAccessImpl; 020import org.apache.tapestry5.ioc.internal.util.InternalUtils; 021import org.apache.tapestry5.ioc.services.AspectDecorator; 022import org.apache.tapestry5.ioc.services.AspectInterceptorBuilder; 023import org.apache.tapestry5.ioc.services.Builtin; 024import org.apache.tapestry5.ioc.services.PlasticProxyFactory; 025 026import java.lang.reflect.Method; 027 028@PreventServiceDecoration 029public class AspectDecoratorImpl implements AspectDecorator 030{ 031 private final PlasticProxyFactory proxyFactory; 032 033 public AspectDecoratorImpl(@Builtin 034 PlasticProxyFactory proxyFactory) 035 { 036 this.proxyFactory = proxyFactory; 037 } 038 039 public <T> AspectInterceptorBuilder<T> createBuilder(Class<T> serviceInterface, final T delegate, String description) 040 { 041 return createBuilder(serviceInterface, delegate, new AnnotationAccessImpl(delegate.getClass()), description); 042 } 043 044 public <T> AspectInterceptorBuilder<T> createBuilder(final Class<T> serviceInterface, final T delegate, 045 AnnotationAccess annotationAccess, final String description) 046 { 047 assert serviceInterface != null; 048 assert delegate != null; 049 assert InternalUtils.isNonBlank(description); 050 051 // The inner class here prevents the needless creation of the AspectInterceptorBuilderImpl, 052 // and the various Plastic related overhead, until there's some actual advice. 053 054 return new AbtractAspectInterceptorBuilder<T>(annotationAccess) 055 { 056 private AspectInterceptorBuilder<T> builder; 057 058 public void adviseMethod(Method method, org.apache.tapestry5.plastic.MethodAdvice advice) 059 { 060 getBuilder().adviseMethod(method, advice); 061 } 062 063 public void adviseAllMethods(org.apache.tapestry5.plastic.MethodAdvice advice) 064 { 065 getBuilder().adviseAllMethods(advice); 066 } 067 068 public Class getInterface() 069 { 070 return serviceInterface; 071 } 072 073 public T build() 074 { 075 return builder == null ? delegate : builder.build(); 076 } 077 078 private AspectInterceptorBuilder<T> getBuilder() 079 { 080 if (builder == null) 081 builder = new AspectInterceptorBuilderImpl<T>(annotationAccess, proxyFactory, serviceInterface, 082 delegate, description); 083 084 return builder; 085 } 086 }; 087 } 088}