001/* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*- 002 * 003 * Copyright © 2019 microBean. 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 014 * implied. See the License for the specific language governing 015 * permissions and limitations under the License. 016 */ 017package org.microbean.narayana.jta.cdi; 018 019import javax.enterprise.context.ApplicationScoped; 020 021import javax.enterprise.event.Event; 022 023import javax.inject.Inject; 024 025import javax.transaction.NotSupportedException; 026import javax.transaction.SystemException; 027import javax.transaction.Transaction; 028import javax.transaction.TransactionManager; // for javadoc only 029 030/** 031 * A {@link DelegatingTransactionManager} in {@linkplain 032 * ApplicationScoped application scope} that uses the return value 033 * that results from invoking the {@link 034 * com.arjuna.ats.jta.TransactionManager#transactionManager()} method 035 * as its backing implementation. 036 * 037 * @author <a href="https://about.me/lairdnelson" 038 * target="_parent">Laird Nelson</a> 039 * 040 * @see com.arjuna.ats.jta.TransactionManager#transactionManager() 041 */ 042@ApplicationScoped 043public class NarayanaTransactionManager extends DelegatingTransactionManager { 044 045 private final Event<Transaction> broadcaster; 046 047 /** 048 * Creates a new {@link NarayanaTransactionManager}. 049 * 050 * @param broadcaster an {@link Event} capable of {@linkplain 051 * Event#fire(Object) firing} {@link Transaction} instances; may be 052 * {@code null} 053 * 054 * @see #begin() 055 */ 056 @Inject 057 public NarayanaTransactionManager(final Event<Transaction> broadcaster) { 058 super(com.arjuna.ats.jta.TransactionManager.transactionManager()); 059 this.broadcaster = broadcaster; 060 } 061 062 /** 063 * Overrides the {@link DelegatingTransactionManager#begin()} method 064 * to additionally {@linkplain Event#fire(Object) fire} the return 065 * value of the {@link #getTransaction()} method immediately after a 066 * transaction is begun. 067 * 068 * @exception NotSupportedException if the thread is already 069 * associated with a transaction and this {@link TransactionManager} 070 * implementation does not support nested transactions 071 * 072 * @exception SystemException if this {@link TransactionManager} 073 * encounters an unexpected error condition 074 * 075 * @see DelegatingTransactionManager#begin() 076 * 077 * @see Transaction 078 */ 079 @Override 080 public void begin() throws NotSupportedException, SystemException { 081 super.begin(); 082 if (this.broadcaster != null) { 083 final Transaction transaction = this.getTransaction(); 084 assert transaction != null; 085 this.broadcaster.fire(transaction); 086 } 087 } 088 089}