001 // Copyright 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.internal.hibernate; 016 017 import org.apache.tapestry5.hibernate.HibernateSessionManager; 018 import org.apache.tapestry5.hibernate.HibernateTransactionAdvisor; 019 import org.apache.tapestry5.hibernate.annotations.CommitAfter; 020 import org.apache.tapestry5.ioc.Invocation; 021 import org.apache.tapestry5.ioc.MethodAdvice; 022 import org.apache.tapestry5.ioc.MethodAdviceReceiver; 023 024 import java.lang.reflect.Method; 025 026 public class HibernateTransactionAdvisorImpl implements HibernateTransactionAdvisor 027 { 028 private final HibernateSessionManager manager; 029 030 /** 031 * The rules for advice are the same for any method: commit on success or checked exception, abort on thrown 032 * exception ... so we can use a single shared advice object. 033 */ 034 private final MethodAdvice advice = new MethodAdvice() 035 { 036 public void advise(Invocation invocation) 037 { 038 try 039 { 040 invocation.proceed(); 041 } 042 catch (RuntimeException ex) 043 { 044 manager.abort(); 045 046 throw ex; 047 } 048 049 // For success or checked exception, commit the transaction. 050 051 manager.commit(); 052 } 053 }; 054 055 public HibernateTransactionAdvisorImpl(HibernateSessionManager manager) 056 { 057 this.manager = manager; 058 } 059 060 public void addTransactionCommitAdvice(MethodAdviceReceiver receiver) 061 { 062 for (Method m : receiver.getInterface().getMethods()) 063 { 064 if (m.getAnnotation(CommitAfter.class) != null) 065 { 066 receiver.adviseMethod(m, advice); 067 } 068 } 069 } 070 }