001    // Copyright 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.jpa;
016    
017    import org.apache.tapestry5.ioc.MethodAdviceReceiver;
018    import org.apache.tapestry5.jpa.EntityManagerManager;
019    import org.apache.tapestry5.jpa.JpaTransactionAdvisor;
020    import org.apache.tapestry5.jpa.annotations.CommitAfter;
021    import org.apache.tapestry5.plastic.MethodAdvice;
022    
023    import javax.persistence.PersistenceContext;
024    import java.lang.reflect.Method;
025    
026    public class JpaTransactionAdvisorImpl implements JpaTransactionAdvisor
027    {
028        private final EntityManagerManager manager;
029    
030        private final MethodAdvice shared;
031    
032    
033        public JpaTransactionAdvisorImpl(EntityManagerManager manager)
034        {
035            this.manager = manager;
036    
037            shared = new CommitAfterMethodAdvice(manager, null);
038        }
039    
040        public void addTransactionCommitAdvice(final MethodAdviceReceiver receiver)
041        {
042            for (final Method m : receiver.getInterface().getMethods())
043            {
044                if (m.getAnnotation(CommitAfter.class) != null)
045                {
046                    PersistenceContext annotation = receiver.getMethodAnnotation(m, PersistenceContext.class);
047    
048                    MethodAdvice advice =
049                            annotation == null ? shared : new CommitAfterMethodAdvice(manager, annotation);
050    
051                    receiver.adviseMethod(m, advice);
052                }
053            }
054    
055        }
056    
057    }