/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.apache.camel.cdi.transaction;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.RollbackException;
import javax.transaction.Status;
import javax.transaction.SystemException;
import javax.transaction.Transaction;
import javax.transaction.TransactionManager;

import org.apache.camel.CamelException;
import org.apache.camel.jta.JtaTransactionPolicy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Helper methods for transaction handling
 *
 * This class requires the resource {@link TransactionManager} to be available through JNDI url
 * &quot;java:/TransactionManager&quot;
 */
public abstract class TransactionalJtaTransactionPolicy extends JtaTransactionPolicy {

    private static final Logger LOG = LoggerFactory.getLogger(TransactionalJtaTransactionPolicy.class);
    private static final String TRANSACTION_MANAGER_JNDI_NAME = "java:/TransactionManager";

    protected TransactionManager transactionManager;

    public TransactionalJtaTransactionPolicy() {
        try {
            transactionManager = InitialContext.doLookup(TRANSACTION_MANAGER_JNDI_NAME);
        } catch (NamingException e) {
            LOG.warn("Could not find the transaction manager through " + TRANSACTION_MANAGER_JNDI_NAME);
        }
    }

    protected void runWithTransaction(final Runnable runnable, final boolean isNew) throws Throwable {
        if (isNew) {
            begin();
        }
        try {
            runnable.run();
        } catch (Throwable e) {
            rollback(isNew);
            throw e;
        }
        if (isNew) {
            commit();
        }
    }

    private void begin() throws Exception {
        transactionManager.begin();
    }

    private void commit() throws Exception {
        try {
            transactionManager.commit();
        } catch (HeuristicMixedException | HeuristicRollbackException | RollbackException | SystemException e) {
            throw new CamelException("Unable to commit transaction", e);
        } catch (Exception | Error e) {
            rollback(true);
            throw e;
        }
    }

    protected void rollback(boolean isNew) throws Exception {
        try {
            if (isNew) {
                transactionManager.rollback();
            } else {
                transactionManager.setRollbackOnly();
            }
        } catch (Throwable e) {
            LOG.warn("Could not rollback transaction!", e);
        }
    }

    protected Transaction suspendTransaction() throws Exception {
        return transactionManager.suspend();
    }

    protected void resumeTransaction(final Transaction suspendedTransaction) {
        if (suspendedTransaction == null) {
            return;
        }

        try {
            transactionManager.resume(suspendedTransaction);
        } catch (Throwable e) {
            LOG.warn("Could not resume transaction!", e);
        }
    }

    protected boolean hasActiveTransaction() throws Exception {
        return transactionManager.getStatus() != Status.STATUS_MARKED_ROLLBACK
                && transactionManager.getStatus() != Status.STATUS_NO_TRANSACTION;
    }
}

@@ -1507,19 +1507,165 @@
 ugh 
-JNDI url%0A *
+any of JNDI locations:%0A * &quot;java:comp/TransactionManager&quot;, &quot;java:appserver/TransactionManager&quot;,%0A * &quot;java:pm/TransactionManager&quot;,
  &qu
@@ -1925,16 +1925,18 @@
 l String
+%5B%5D
  TRANSAC
@@ -1961,17 +1961,49 @@
 NAME
+S
  =
+ new String%5B%5D %7B%0A           
  %22java:
+comp
 /Tra
@@ -2018,16 +2018,153 @@
 Manager%22
+,%0A            %22java:appserver/TransactionManager%22,%0A            %22java:pm/TransactionManager%22,%0A            %22java:/TransactionManager%22%0A    %7D
 ;%0A%0A    p
@@ -2261,34 +2261,16 @@
 icy() %7B%0A
-        try %7B%0A    
         
@@ -2294,144 +2294,15 @@
 r = 
-InitialContext.doLookup(TRANSACTION_MANAGER_JNDI_NAME);%0A        %7D catch (NamingException e) %7B%0A            LOG.warn(%22Could not find the t
+lookupT
 rans
@@ -2311,70 +2311,18 @@
 tion
- m
+M
 anager
- through %22 + TRANSACTION_MANAGER_JNDI_NAME);%0A        %7D
+();
 %0A   
@@ -2666,32 +2666,878 @@
        %7D%0A    %7D%0A%0A
+    private TransactionManager lookupTransactionManager() %7B%0A        TransactionManager tm;%0A        for (String jndiName : TRANSACTION_MANAGER_JNDI_NAMES) %7B%0A            try %7B%0A                tm = InitialContext.doLookup(jndiName);%0A                if (LOG.isDebugEnabled()) %7B%0A                    LOG.debug(%22JTA TransactionManager found at JNDI location %5B%22 + jndiName + %22%5D%22);%0A                %7D%0A                return tm;%0A            %7D catch (NamingException ex) %7B%0A                if (LOG.isDebugEnabled()) %7B%0A                    LOG.debug(%22No JTA TransactionManager found at JNDI location %5B%22 + jndiName + %22%5D%22, ex);%0A                %7D%0A            %7D%0A        %7D%0A        LOG.warn(%22Could not find the transaction manager through any of following locations: %7B%7D%22,%0A                String.join(%22,%22, TRANSACTION_MANAGER_JNDI_NAMES));%0A        return null;%0A    %7D%0A%0A
     private void

Support alternative JNDI locations for TransactionManager (#4208)