    001    // Copyright 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
    015    package org.apache.tapestry5.internal.transform;
    016
    017    import org.apache.tapestry5.annotations.HeartbeatDeferred;
    018    import org.apache.tapestry5.model.MutableComponentModel;
    019    import org.apache.tapestry5.plastic.MethodAdvice;
    020    import org.apache.tapestry5.plastic.MethodInvocation;
    021    import org.apache.tapestry5.plastic.PlasticClass;
    022    import org.apache.tapestry5.plastic.PlasticMethod;
    023    import org.apache.tapestry5.services.Heartbeat;
    024    import org.apache.tapestry5.services.transform.ComponentClassTransformWorker2;
    025    import org.apache.tapestry5.services.transform.TransformationSupport;
    026
    027    public class HeartbeatDeferredWorker implements ComponentClassTransformWorker2
    028    {
    029        private final Heartbeat heartbeat;
    030
    031        private final MethodAdvice deferredAdvice = new MethodAdvice()
    032        {
    033            public void advise(final MethodInvocation invocation)
    034            {
    035                heartbeat.defer(new Runnable()
    036                {
    037                    public void run()
    038                    {
    039                        invocation.proceed();
    040                    }
    041                });
    042            }
    043        };
    044
    045        public HeartbeatDeferredWorker(Heartbeat heartbeat)
    046        {
    047            this.heartbeat = heartbeat;
    048        }
    049
    050        public void transform(PlasticClass plasticClass, TransformationSupport support, MutableComponentModel model)
    051        {
    052            for (PlasticMethod method : plasticClass.getMethodsWithAnnotation(HeartbeatDeferred.class))
    053            {
    054                deferMethodInvocations(method);
    055            }
    056        }
    057
    058        void deferMethodInvocations(PlasticMethod method)
    059        {
    060            validateVoid(method);
    061
    062            validateNoCheckedExceptions(method);
    063
    064            method.addAdvice(deferredAdvice);
    065
    066        }
    067
    068        private void validateNoCheckedExceptions(PlasticMethod method)
    069        {
    070            if (method.getDescription().checkedExceptionTypes.length > 0)
    071                throw new RuntimeException(
    072                        String.format(
    073                                "Method %s is not compatible with the @HeartbeatDeferred annotation, as it throws checked exceptions.",
    074                                method.getMethodIdentifier()));
    075        }
    076
    077        private void validateVoid(PlasticMethod method)
    078        {
    079            if (!method.isVoid())
    080                throw new RuntimeException(String.format(
    081                        "Method %s is not compatible with the @HeartbeatDeferred annotation, as it is not a void method.",
    082                        method.getMethodIdentifier()));
    083        }
    084    }