001 // Copyright 2008 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.tynamo.jpa.internal;
016
017 import org.apache.tapestry5.model.MutableComponentModel;
018 import org.apache.tapestry5.services.ClassTransformation;
019 import org.apache.tapestry5.services.ComponentClassTransformWorker;
020 import org.apache.tapestry5.services.ComponentMethodAdvice;
021 import org.apache.tapestry5.services.ComponentMethodInvocation;
022 import org.apache.tapestry5.services.TransformMethodSignature;
023
024 import org.tynamo.jpa.JPATransactionManager;
025 import org.tynamo.jpa.annotations.CommitAfter;
026
027 /**
028 * Searches for methods that have the {@link CommitAfter} annotation and adds logic around the
029 * method to commit or abort the transaction.
030 */
031 public class CommitAfterWorker implements ComponentClassTransformWorker
032 {
033 private final JPATransactionManager manager;
034
035 private final ComponentMethodAdvice advice = new ComponentMethodAdvice()
036 {
037 public void advise(ComponentMethodInvocation invocation)
038 {
039 try
040 {
041 invocation.proceed();
042
043 // Success or checked exception:
044
045 manager.commit();
046 }
047 catch (RuntimeException ex)
048 {
049 manager.abort();
050
051 throw ex;
052 }
053 }
054 };
055
056 public CommitAfterWorker(JPATransactionManager manager)
057 {
058 this.manager = manager;
059 }
060
061 public void transform(ClassTransformation transformation, MutableComponentModel model)
062 {
063 for (TransformMethodSignature sig : transformation.findMethodsWithAnnotation(CommitAfter.class))
064 {
065 transformation.advise(sig, advice);
066 }
067 }
068 }