001/*
002 * Copyright 2011 Atteo.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. 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 distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014package org.atteo.moonshine.jta;
015
016import javax.inject.Singleton;
017import javax.xml.bind.annotation.XmlIDREF;
018import javax.xml.bind.annotation.XmlRootElement;
019
020import org.atteo.moonshine.TopLevelService;
021import org.atteo.moonshine.services.ImportService;
022
023import com.google.inject.AbstractModule;
024import com.google.inject.Module;
025import com.google.inject.matcher.Matchers;
026
027/**
028 * Adds support for @Transactional annotation.
029 * <p>
030 * Provides support for &#064;{@link Transactional} annotation and {@link Transaction} helper.
031 * </p>
032 */
033@XmlRootElement(name = "transactional")
034@Singleton
035public class TransactionalService extends TopLevelService {
036    @XmlIDREF
037    @ImportService
038    private JtaService jtaService;
039
040    @Override
041    public Module configure() {
042        return new AbstractModule() {
043            @Override
044            protected void configure() {
045                requestStaticInjection(Transaction.class);
046                TransactionalInterceptor interceptor = new TransactionalInterceptor();
047                bindInterceptor(Matchers.any(), Matchers.annotatedWith(Transactional.class), interceptor);
048                bindInterceptor(Matchers.annotatedWith(Transactional.class), Matchers.any(), interceptor);
049            }
050        };
051    }
052}