<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:spring="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:file="http://www.mulesoft.org/schema/mule/file"
       xmlns:jms="http://www.mulesoft.org/schema/mule/jms"
       xmlns:smtp="http://www.mulesoft.org/schema/mule/smtp"
       xmlns:email="http://www.mulesoft.org/schema/mule/email"
       xmlns:xm="http://www.mulesoft.org/schema/mule/xml"
    xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-current.xsd
       http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
       http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
       http://www.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd
       http://www.mulesoft.org/schema/mule/smtp http://www.mulesoft.org/schema/mule/smtp/current/mule-smtp.xsd
       http://www.mulesoft.org/schema/mule/email http://www.mulesoft.org/schema/mule/email/current/mule-email.xsd
       http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd">

    <description>
        This example demonstrates how to publish events to multiple outbound endpoints. 
        The example consists of two components: ExceptionManager and BusinessErrorManager.
    </description>
    
    <spring:bean id="errorManager" class="org.mule.example.errorhandler.ErrorManager">
        <spring:property name="handlers">
            <spring:list>
                <spring:ref local="fatalHandler"/>
                <spring:ref local="defaultHandler"/>
                <spring:ref local="businessHandler"/>
            </spring:list>
        </spring:property>
    </spring:bean>
    
    <spring:bean id="defaultHandler" class="org.mule.example.errorhandler.handlers.DefaultHandler">
        <spring:property name="endpointName" value="ExceptionFileWriter"/>
        <spring:property name="errorManager">
            <spring:ref local="errorManager"/>
        </spring:property>
    </spring:bean>

    <spring:bean id="fatalHandler" class="org.mule.example.errorhandler.handlers.FatalHandler">
        <spring:property name="endpointName" value="ExceptionEmailer"/>
        <spring:property name="errorManager">
            <spring:ref local="errorManager"/>
        </spring:property>
    </spring:bean>

    <spring:bean id="businessHandler" class="org.mule.example.errorhandler.handlers.BusinessHandler">
        <spring:property name="endpointName" value="ExceptionQueue"/>
        <spring:property name="errorManager">
            <spring:ref local="errorManager"/>
        </spring:property>
    </spring:bean>
    
    <context:property-placeholder location="email.properties"/>
    
    <jms:activemq-connector name="JMSConnector"/>
    
    <file:connector name="FileConnector" 
                    pollingFrequency="1000" 
                    moveToDirectory="${app.home}/test-data/out"/>

    <!-- 
        All transformers must be declared here to be referenced by their names in the 
        endpoint definitions 
    -->
    <xm:object-to-xml-transformer name="ExceptionBeanToXML" 
                                  returnClass="java.lang.String"/>

    <xm:xml-to-object-transformer name="XMLToExceptionBean"
                                  returnClass="org.mule.example.errorhandler.ExceptionBean"/>
                             
    <custom-transformer name="ErrorMessageToExceptionBean" 
                        class="org.mule.example.errorhandler.ErrorMessageToExceptionBean"
                        returnClass="org.mule.example.errorhandler.ExceptionBean"/>

    <custom-transformer name="ExceptionBeanToErrorMessage"
                        class="org.mule.example.errorhandler.ExceptionBeanToErrorMessage"
                        returnClass="org.mule.example.errorhandler.ErrorMessage"/>

    <flow name="errorhandler">
        <file:inbound-endpoint path="${app.home}/test-data/in">
            <!-- Ignore other files than our XML error input. When running this example from
                 a svn checkout the file connector would try to process subversion's meta
                 dirs which is not what we want.
            -->
            <file:filename-wildcard-filter pattern="*.xml"/>
            <transformer ref="XMLToExceptionBean"/>
            <transformer ref="ExceptionBeanToErrorMessage"/>
        </file:inbound-endpoint>
        <pooled-component>
            <prototype-object class="org.mule.example.errorhandler.ErrorManager">
                <properties>
                    <spring:entry key="handlers">
                        <spring:list>
                            <spring:ref local="fatalHandler"/>
                            <spring:ref local="defaultHandler"/>
                            <spring:ref local="businessHandler"/>
                        </spring:list>
                    </spring:entry>
                </properties>
             </prototype-object>
        </pooled-component>
        <choice>
            <when expression="payload.throwable instanceof org.mule.api.DefaultMuleException" evaluator="groovy">
                <file:outbound-endpoint path="${app.home}/test-data/exceptions" 
                                        outputPattern="Exception-[function:uuid].xml">
                    <transformer ref="ErrorMessageToExceptionBean" />
                    <transformer ref="ExceptionBeanToXML" />
                </file:outbound-endpoint>
            </when>
            
            <when expression="payload.throwable instanceof org.mule.api.lifecycle.FatalException" evaluator="groovy">
                <smtp:outbound-endpoint user="${smtp.username}" password="${smtp.password}"
                                        host="${smtp.host}" port="${smtp.port}"
                                        to="${email.toAddress}" from="${email.fromAddress}"
                                        subject="${email.subject}">
                    <transformer ref="ErrorMessageToExceptionBean" />
                    <transformer ref="ExceptionBeanToXML" />
                    <email:string-to-email-transformer/>
                 </smtp:outbound-endpoint>
            </when>
            
            <when expression="payload.throwable instanceof org.mule.example.errorhandler.exceptions.BusinessException" evaluator="groovy">
                <jms:outbound-endpoint queue="exception.queue" exchange-pattern="one-way">
                    <transformer ref="ErrorMessageToExceptionBean" />
                    <transformer ref="ExceptionBeanToXML" />
                </jms:outbound-endpoint>
            </when>
        </choice>
    </flow>
    
    <flow name="Business Error Manager">
        <jms:inbound-endpoint queue="exception.queue">
            <transformer ref="XMLToExceptionBean"/>
            <transformer ref="ExceptionBeanToErrorMessage"/>
        </jms:inbound-endpoint>
        <component class="org.mule.example.errorhandler.BusinessErrorManager"/>
    </flow>
</mule>
