001    // Copyright 2008, 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.tynamo.jpa.internal;
016    
017    import org.apache.tapestry5.ioc.internal.util.Defense;
018    import org.apache.tapestry5.ioc.services.AspectDecorator;
019    import org.apache.tapestry5.ioc.services.AspectInterceptorBuilder;
020    
021    import org.tynamo.jpa.JPATransactionAdvisor;
022    import org.tynamo.jpa.JPATransactionDecorator;
023    
024    public class JPATransactionDecoratorImpl implements JPATransactionDecorator
025    {
026            private final AspectDecorator aspectDecorator;
027    
028            private final JPATransactionAdvisor advisor;
029    
030            public JPATransactionDecoratorImpl(AspectDecorator aspectDecorator, JPATransactionAdvisor advisor)
031            {
032                    this.aspectDecorator = aspectDecorator;
033                    this.advisor = advisor;
034            }
035    
036            public <T> T build(Class<T> serviceInterface, T delegate, String serviceId)
037            {
038                    Defense.notNull(serviceInterface, "serviceInterface");
039                    Defense.notNull(delegate, "delegate");
040                    Defense.notBlank(serviceId, "serviceId");
041    
042                    String description = String.format("<JPA Transaction interceptor for %s(%s)>", serviceId, serviceInterface
043                            .getName());
044    
045                    AspectInterceptorBuilder<T> builder = aspectDecorator.createBuilder(serviceInterface, delegate, description);
046    
047                    advisor.addTransactionCommitAdvice(builder);
048    
049                    return builder.build();
050            }
051    }