/*
 *    Copyright 2013-2021 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.mybatis.cdi;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.UndeclaredThrowableException;

import javax.inject.Inject;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.NotSupportedException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;

import org.apache.ibatis.session.SqlSessionManager;

/**
 * Best-effort interceptor for local transactions. It locates all the instances of {@code SqlSssionManager} and starts
 * transactions on all them. It cannot guarantee atomiticy if there is more than one {@code SqlSssionManager}. Use XA
 * drivers, a JTA container and the {@link JtaTransactionInterceptor} in that case.
 *
 * @see JtaTransactionInterceptor
 *
 * @author Frank David Martínez
 */
@Transactional
@Interceptor
public class LocalTransactionInterceptor implements Serializable {

  private static final long serialVersionUID = 1L;

  @Inject
  private transient SqlSessionManagerRegistry registry;

  /**
   * Invoke.
   *
   * @param ctx
   *          the ctx
   * @return the object
   * @throws Exception
   *           the exception
   */
  @AroundInvoke
  public Object invoke(InvocationContext ctx) throws Exception {
    Transactional transactional = getTransactionalAnnotation(ctx);
    boolean isInitiator = start(transactional);
    boolean isExternalJta = isTransactionActive();
    if (isInitiator && !isExternalJta) {
      beginJta();
    }
    boolean needsRollback = transactional.rollbackOnly();
    Object result;
    try {
      result = ctx.proceed();
    } catch (Exception ex) {
      Exception unwrapped = unwrapException(ex);
      needsRollback = needsRollback || needsRollback(transactional, unwrapped);
      throw unwrapped;
    } finally {
      if (isInitiator) {
        try {
          if (needsRollback) {
            rollback(transactional);
          } else {
            commit(transactional);
          }
        } finally {
          close();
          endJta(isExternalJta, needsRollback);
        }
      }
    }
    return result;
  }

  protected boolean isTransactionActive() throws SystemException {
    return false;
  }

  protected void beginJta() throws NotSupportedException, SystemException {
    // nothing to do
  }

  protected void endJta(boolean isExternaTransaction, boolean commit)
      throws SystemException, RollbackException, HeuristicMixedException, HeuristicRollbackException {
    // nothing to do
  }

  private boolean needsRollback(Transactional transactional, Throwable throwable) {
    if (RuntimeException.class.isAssignableFrom(throwable.getClass())) {
      return true;
    }
    for (Class<? extends Throwable> exceptionClass : transactional.rollbackFor()) {
      if (exceptionClass.isAssignableFrom(throwable.getClass())) {
        return true;
      }
    }
    return false;
  }

  protected Transactional getTransactionalAnnotation(InvocationContext ctx) {
    Transactional t = ctx.getMethod().getAnnotation(Transactional.class);
    if (t == null) {
      t = ctx.getMethod().getDeclaringClass().getAnnotation(Transactional.class);
    }
    return t;
  }

  private boolean start(Transactional transactional) {
    boolean started = false;
    for (SqlSessionManager manager : this.registry.getManagers()) {
      if (!manager.isManagedSessionStarted()) {
        manager.startManagedSession(transactional.executorType(),
            transactional.isolation().getTransactionIsolationLevel());
        started = true;
      }
    }
    return started;
  }

  private void commit(Transactional transactional) {
    for (SqlSessionManager manager : this.registry.getManagers()) {
      manager.commit(transactional.force());
    }
  }

  private void rollback(Transactional transactional) {
    for (SqlSessionManager manager : this.registry.getManagers()) {
      manager.rollback(transactional.force());
    }
  }

  private void close() {
    for (SqlSessionManager manager : this.registry.getManagers()) {
      manager.close();
    }
  }

  private Exception unwrapException(Exception wrapped) {
    Throwable unwrapped = wrapped;
    while (true) {
      if (unwrapped instanceof InvocationTargetException) {
        unwrapped = ((InvocationTargetException) unwrapped).getTargetException();
      } else if (unwrapped instanceof UndeclaredThrowableException) {
        unwrapped = ((UndeclaredThrowableException) unwrapped).getUndeclaredThrowable();
      } else if (!(unwrapped instanceof Exception)) {
        return new RuntimeException(unwrapped);
      } else {
        return (Exception) unwrapped;
      }
    }
  }

}

@@ -2945,197 +2945,1051 @@
 %0A%0A  
-protected boolean isTransactionActive() throws SystemException %7B%0A    return false;%0A  %7D%0A%0A  protected void beginJta() throws NotSupportedException, SystemException %7B%0A    // nothing to do%0A  %7D%0A
+/**%0A   * Checks if is transaction active.%0A   *%0A   * @return true, if is transaction active%0A   * @throws SystemException%0A   *           used by jtaTransactionInterceptor%0A   */%0A  protected boolean isTransactionActive() throws SystemException %7B%0A    return false;%0A  %7D%0A%0A  /**%0A   * Begin jta.%0A   *%0A   * @throws NotSupportedException%0A   *           used by jtaTransactionInterceptor%0A   * @throws SystemException%0A   *           used by jtaTransactionInterceptor%0A   */%0A  protected void beginJta() throws NotSupportedException, SystemException %7B%0A    // nothing to do%0A  %7D%0A%0A  /**%0A   * End jta.%0A   *%0A   * @param isExternaTransaction%0A   *          the is externa transaction%0A   * @param commit%0A   *          the commit%0A   * @throws SystemException%0A   *           used by jtaTransactionInterceptor%0A   * @throws RollbackException%0A   *           used by jtaTransactionInterceptor%0A   * @throws HeuristicMixedException%0A   *           used by jtaTransactionInterceptor%0A   * @throws HeuristicRollbackException%0A   *           used by jtaTransactionInterceptor%0A   */
 %0A  p

Add javadoc to define why unused throws exist (used by extension module)