    001    // Copyright 2008, 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
    015    package org.apache.tapestry5.internal.transform;
    016
    017    import org.apache.tapestry5.annotations.Log;
    018    import org.apache.tapestry5.ioc.internal.services.LoggingAdvice;
    019    import org.apache.tapestry5.ioc.services.ExceptionTracker;
    020    import org.apache.tapestry5.model.MutableComponentModel;
    021    import org.apache.tapestry5.plastic.MethodAdvice;
    022    import org.apache.tapestry5.plastic.PlasticClass;
    023    import org.apache.tapestry5.plastic.PlasticMethod;
    024    import org.apache.tapestry5.services.transform.ComponentClassTransformWorker2;
    025    import org.apache.tapestry5.services.transform.TransformationSupport;
    026
    027    import java.util.List;
    028
    029    /**
    030     * Looks for the {@link org.apache.tapestry5.annotations.Log} marker annotation and adds method advice to perform the
    031     * logging. This is similar to what the {@link org.apache.tapestry5.ioc.services.LoggingDecorator} does for service
    032     * interface methods.
    033     */
    034    public class LogWorker implements ComponentClassTransformWorker2
    035    {
    036        private final ExceptionTracker exceptionTracker;
    037
    038        public LogWorker(ExceptionTracker exceptionTracker)
    039        {
    040            this.exceptionTracker = exceptionTracker;
    041        }
    042
    043        public void transform(PlasticClass plasticClass, TransformationSupport support, MutableComponentModel model)
    044        {
    045            List<PlasticMethod> methods = plasticClass.getMethodsWithAnnotation(Log.class);
    046
    047            if (methods.isEmpty())
    048            {
    049                return;
    050            }
    051
    052            final MethodAdvice loggingAdvice = new LoggingAdvice(model.getLogger(), exceptionTracker);
    053
    054            for (PlasticMethod method : methods)
    055            {
    056                method.addAdvice(loggingAdvice);
    057            }
    058        }
    059    }