001    // Copyright 2008, 2009 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.tapestry5.ioc.internal.services;
016    
017    import org.apache.tapestry5.ioc.MethodAdvice;
018    import org.apache.tapestry5.ioc.internal.util.Defense;
019    import org.apache.tapestry5.ioc.services.AspectDecorator;
020    import org.apache.tapestry5.ioc.services.AspectInterceptorBuilder;
021    import org.apache.tapestry5.ioc.services.Builtin;
022    import org.apache.tapestry5.ioc.services.ClassFactory;
023    
024    import java.lang.reflect.Method;
025    
026    public class AspectDecoratorImpl implements AspectDecorator
027    {
028        private final ClassFactory classFactory;
029    
030        public AspectDecoratorImpl(@Builtin ClassFactory classFactory)
031        {
032            this.classFactory = classFactory;
033        }
034    
035        public <T> T build(Class<T> serviceInterface, T delegate, MethodAdvice advice, String description)
036        {
037            Defense.notNull(advice, "advice");
038    
039            AspectInterceptorBuilder<T> builder = createBuilder(serviceInterface, delegate, description);
040    
041            builder.adviseAllMethods(advice);
042    
043            return builder.build();
044        }
045    
046        public <T> AspectInterceptorBuilder<T> createBuilder(final Class<T> serviceInterface, final T delegate,
047                                                             final String description)
048        {
049            Defense.notNull(serviceInterface, "serviceInterface");
050            Defense.notNull(delegate, "delegate");
051            Defense.notBlank(description, "description");
052    
053            // Defer creating the real builder until a method gets advised.  If no method is advised then
054            // the delegate can be used unchanged.
055    
056            return new AspectInterceptorBuilder<T>()
057            {
058                private AspectInterceptorBuilder<T> builder;
059    
060                public void adviseMethod(Method method, MethodAdvice advice)
061                {
062                    getBuilder().adviseMethod(method, advice);
063                }
064    
065                public void adviseAllMethods(MethodAdvice advice)
066                {
067                    getBuilder().adviseAllMethods(advice);
068                }
069    
070                public Class getInterface()
071                {
072                    return serviceInterface;
073                }
074    
075                public T build()
076                {
077                    return builder == null ? delegate : builder.build();
078                }
079    
080                private AspectInterceptorBuilder<T> getBuilder()
081                {
082                    if (builder == null)
083                        builder = new AspectInterceptorBuilderImpl<T>(classFactory, serviceInterface, delegate,
084                                                                      description);
085    
086                    return builder;
087                }
088            };
089        }
090    }