<?xml version="1.0" encoding="UTF-8"?>
<!--

    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.

-->
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="auditLog" class="sample.camel.AuditLog"/>
    <bean id="transactionManagerImple"
          class="com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionManagerImple"/>
    <bean id="userTransactionImple" class="com.arjuna.ats.internal.jta.transaction.arjunacore.UserTransactionImple"/>
    <bean id="recoveryManagerFactory" class="sample.camel.RMFactory"/>
    <bean id="recoveryManager" factory-bean="recoveryManagerFactory" factory-method="createInstance"/>
    <bean id="jtaTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
        <property name="transactionManager" ref="transactionManagerImple"/>
        <property name="userTransaction" ref="userTransactionImple"/>
    </bean>
    <bean id="PROPAGATION_REQUIRED" class="org.apache.camel.spring.spi.SpringTransactionPolicy">
        <property name="transactionManager" ref="jtaTransactionManager"/>
        <property name="propagationBehaviorName" value="PROPAGATION_REQUIRED"/>
    </bean>

    <bean id="jtaDataSource" class="com.mysql.cj.jdbc.MysqlXADataSource">
        <property name="url" value="${mysql.url}"/>
        <property name="user" value="${mysql.user}"/>
        <property name="password" value="${mysql.password}"/>
    </bean>

    <!-- Pooled JTA aware data source -->
    <bean id="pooledJtaDataSource" class="org.apache.commons.dbcp2.managed.BasicManagedDataSource">
        <property name="transactionManager" ref="transactionManagerImple"/>
        <property name="xaDataSourceInstance" ref="jtaDataSource"/>
        <property name="initialSize" value="5"/>
        <property name="maxIdle" value="5"/>
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="packagesToScan" value="sample.camel"/>
        <property name="jtaDataSource" ref="pooledJtaDataSource"/>
        <property name="persistenceUnitName" value="camel"/>
        <property name="jpaDialect">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
        </property>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
            </bean>
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">none</prop>
                <prop key="hibernate.id.new_generator_mappings">false</prop>
                <prop key="hibernate.transaction.coordinator_class">jta</prop>
                <prop key="hibernate.transaction.jta.platform">
                    org.hibernate.engine.transaction.jta.platform.internal.JBossStandAloneJtaPlatform
                </prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.format_sql">true"</prop>
            </props>
        </property>
    </bean>

    <bean id="jpa" class="org.apache.camel.component.jpa.JpaComponent">
        <property name="transactionManager" ref="jtaTransactionManager"/>
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
        <property name="joinTransaction" value="true"/>
        <property name="sharedEntityManager" value="false"/>
    </bean>

    <bean id="jtaConnectionFactory" class="org.apache.activemq.artemis.jms.client.ActiveMQXAConnectionFactory">
        <constructor-arg value="${broker.url}"/>
        <constructor-arg value="${broker.user}"/>
        <constructor-arg value="${broker.password}"/>
    </bean>

    <!-- Pooled JTA aware connection factory -->
    <bean id="pooledJtaConnectionFactory" class="org.messaginghub.pooled.jms.JmsPoolXAConnectionFactory">
        <property name="transactionManager" ref="transactionManagerImple"/>
        <property name="connectionFactory" ref="jtaConnectionFactory"/>
        <property name="maxConnections" value="1"/>
        <property name="maxSessionsPerConnection" value="100"/>
        <property name="useAnonymousProducers" value="false"/>
    </bean>

    <bean id="jms" class="org.apache.camel.component.jms.JmsComponent">
        <property name="configuration">
            <bean class="org.apache.camel.component.jms.JmsConfiguration">
                <property name="transactionManager" ref="jtaTransactionManager"/>
                <property name="connectionFactory" ref="pooledJtaConnectionFactory"/>
                <!-- disable local transactions as JTA TM will take care of enrolling -->
                <property name="transacted" value="false"/>
                <!-- caching does not work with distributed transactions -->
                <property name="cacheLevelName" value="CACHE_NONE"/>
                <property name="maxConcurrentConsumers" value="1"/>
                <property name="testConnectionOnStartup" value="true"/>
            </bean>
        </property>
    </bean>
</beans>
