[#ftl attributes={"generated.type":"blueprint"}]
<?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"
           xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 https://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">

    <reference-list id="outConnector"
                    interface="ru.entaxy.esb.system.profile.commons.connectors.out.OutConnector"
                    filter="(systemName=[=systemName])" availability="optional">
        <reference-listener ref="profileOutput"
                            bind-method="register" unbind-method="unregister"/>
    </reference-list>

    <reference-list id="inConnector"
                    interface="ru.entaxy.esb.system.profile.commons.connectors.in.InConnector"
                    filter="(systemName=[=systemName])" availability="optional">
        <reference-listener ref="inConnectorCollector"
                            bind-method="register" unbind-method="unregister"/>
    </reference-list>

    <bean id="inConnectorCollector" class="ru.entaxy.esb.system.profile.commons.InConnectorCollector"/>

    <bean id="system-profile" class="ru.entaxy.esb.system.registry.systems.profile.impl.defaults.DefaultSystemProfile"
          activation="eager">
        <property name="systemName" value="[=systemName]"/>
        <property name="profileOutput" ref="profileOutput"/>
        <property name="inConnectorCollector" ref="inConnectorCollector"/>
    </bean>

    <bean id="profileOutput" class="ru.entaxy.esb.system.profile.commons.profile_output.ProfileOutputImpl"
          activation="eager">
        <property name="systemName" value="[=systemName]"/>
    </bean>

    <service activation="eager" auto-export="interfaces" ref="system-profile"/>

    <reference id="pooledConnectionFactory" interface="javax.jms.ConnectionFactory"/>

    <bean id="jms" class="org.apache.camel.component.jms.JmsComponent">
        <property name="connectionFactory" ref="pooledConnectionFactory"/>
    </bean>

    <camelContext id="profile-[=systemName]" xmlns="http://camel.apache.org/schema/blueprint">

        <route>
            <from uri="direct-vm:profile-[=systemName]-exit-dispatcher"/>
            <log message="Message ${headers} send to profile dispatcher" loggingLevel="TRACE"/>
            <choice>
                <when>
                    <simple>${headers.NTX_NextPoint} != null</simple>
                    <setProperty name="NTX_Tmp">
                        <simple>${headers.NTX_NextPoint}</simple>
                    </setProperty>
                    <removeHeader headerName="NTX_NextPoint"/>
                    <toD uri="${exchangeProperty.NTX_Tmp}"/>
                </when>
                <otherwise>
                    <to uri="direct-vm:profile-[=systemName]-exit-point"/>
                </otherwise>
            </choice>
        </route>

        <route>
            <from uri="direct-vm:profile-[=systemName]-enter-point"/>
            <log message="Message ${headers} send to profile output" loggingLevel="TRACE"/>
            <choice>
                <when>
                    <simple>${headers.NTX_Origin} == null</simple>
                    <toD uri="permission:checkSystemAccessException?objectId=${headers.X-SystemId}&amp;subjectId=[=systemName]"/>
                </when>
            </choice>
            <to uri="bean:profileOutput?method=sendTo"/>
        </route>
    </camelContext>
</blueprint>
