[#ftl attributes={"generated.type":"blueprint"}]
[#--

    ~~~~~~licensing~~~~~~
    artemis-adapter
    ==========
    Copyright (C) 2020 - 2021 EmDev LLC
    ==========
    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.
    ~~~~~~/licensing~~~~~~

--]
[#assign factoryProperties = ["url", "username", "password", "maxConnections", "maxSessionsPerConnection"]]
[#function exceptFactoryProperties(propertyName)]
  [#return !factoryProperties?seq_contains(propertyName)]
[/#function]
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"
           xsi:schemaLocation="
               http://www.osgi.org/xmlns/blueprint/v1.0.0 https://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"
           >

    <cm:property-placeholder persistent-id="[=connection.configurationPid]"
                             update-strategy="reload">
        [#if connection.properties??]
        <cm:default-properties>
        [#list connection.properties as key, value]
            <cm:property name="[=connection.configurationPid].[=key]" value="[=value]"/>
        [/#list]
        </cm:default-properties>
        [/#if]
    </cm:property-placeholder>

    <service interface="org.apache.camel.Component" ref="[=connection.name]">
        <service-properties>
            <entry key="connection.name" value="[=connection.name]"/>
        </service-properties>
    </service>

    <bean id="[=connection.name]" class="org.apache.camel.component.jms.JmsComponent">
        [#-- //TODO change template for property reference usage instead of text value --]
        <property name="connectionFactory" ref="pooledConnectionFactory"/>
        [#if connection.properties??]
        [#list connection.properties?keys?filter(exceptFactoryProperties) as key]
        <property name="[=key]" value="[='$']{[=connection.configurationPid].[=key]}"/>
        [/#list]
        [/#if]
    </bean>

    <bean id="pooledConnectionFactory" class="org.messaginghub.pooled.jms.JmsPoolConnectionFactory"
          init-method="start" destroy-method="stop">
        <property name="maxConnections" value="[='$']{[=connection.configurationPid].maxConnections}"/>
        <property name="maxSessionsPerConnection"
                  value="[='$']{[=connection.configurationPid].maxSessionsPerConnection}"/>
        <property name="connectionFactory" ref="artemisConnectionFactory"/>
    </bean>

    <bean id="artemisConnectionFactory" class="org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory">
        <argument index="0" value="[='$']{[=connection.configurationPid].url}"/>
        <argument index="1" value="[='$']{[=connection.configurationPid].username}"/>
        <argument index="2" value="[='$']{[=connection.configurationPid].password}"/>
    </bean>

</blueprint>
