001// Copyright 2008, 2009, 2010 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
015package org.apache.tapestry5.internal.hibernate;
016
017import org.apache.tapestry5.hibernate.HibernateTransactionAdvisor;
018import org.apache.tapestry5.hibernate.HibernateTransactionDecorator;
019import org.apache.tapestry5.ioc.internal.util.InternalUtils;
020import org.apache.tapestry5.ioc.services.AspectDecorator;
021import org.apache.tapestry5.ioc.services.AspectInterceptorBuilder;
022
023public class HibernateTransactionDecoratorImpl implements HibernateTransactionDecorator
024{
025    private final AspectDecorator aspectDecorator;
026
027    private final HibernateTransactionAdvisor advisor;
028
029    public HibernateTransactionDecoratorImpl(AspectDecorator aspectDecorator, HibernateTransactionAdvisor advisor)
030    {
031        this.aspectDecorator = aspectDecorator;
032        this.advisor = advisor;
033    }
034
035    @Override
036    public <T> T build(Class<T> serviceInterface, T delegate, String serviceId)
037    {
038        assert serviceInterface != null;
039        assert delegate != null;
040        assert InternalUtils.isNonBlank(serviceId);
041        String description = String.format("<Hibernate Transaction interceptor for %s(%s)>",
042                                           serviceId,
043                                           serviceInterface.getName());
044
045        AspectInterceptorBuilder<T> builder = aspectDecorator.createBuilder(serviceInterface, delegate, description);
046
047        advisor.addTransactionCommitAdvice(builder);
048
049        return builder.build();
050    }
051}