<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:camel="http://camel.apache.org/schema/spring"
        xmlns:util="http://www.springframework.org/schema/util"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd  
                            http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
                            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
    <bean id="CSV2XML" class="routing.demo.supp.CSV2XML"> <!-- processor to convert from CSV to XML -->
        <constructor-arg index="0" value="data"/>
        <constructor-arg index="1" value="ival,sval"/>
        <constructor-arg index="2" value=","/>
        <constructor-arg index="3" value='"'/>
    </bean>
    <bean id="JSON2XML" class="routing.demo.supp.JSON2XML"> <!-- processor to convert from JSON to XML -->
        <constructor-arg index="0" value="data"/>
    </bean>
    <bean id="XML2CSV" class="routing.demo.supp.XML2CSV"> <!-- processor to convert from XML to CSV -->
        <constructor-arg index="0" value=","/>
        <constructor-arg index="1" value='"'/>
    </bean>
    <bean id="XML2JSON" class="routing.demo.supp.XML2JSON"/> <!-- processor to convert from XML to JSON -->
    <bean id="Truncater" class="routing.demo.supp.Truncater"> <!-- processor to truncate text -->
        <constructor-arg index="0" value="40"/>
        <constructor-arg index="1" value="..."/>
    </bean>
    <bean id="MyDecoder" class="routing.demo.MyDecoder"/> <!-- decoder for binary/socket in channel -->
    <bean id="MyAMQ" class="org.apache.activemq.ActiveMQConnectionFactory"> <!-- MQ connection factory -->
        <constructor-arg index="0" value="tcp://localhost:61616"/>
    </bean>
    <bean id="MyDB" class="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"> <!-- DB data source -->
        <property name="serverName" value="localhost"/>
        <property name="databaseName" value="Test"/>
        <property name="user" value="root"/>
        <property name="password" value=""/>
    </bean>
    <bean id="MyBean" class="routing.demo.MyBean"/> <!-- bean for Java code out channel -->
    <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
        <!-- **** in channels **** -->
        <!-- binary/socket in channel: -->
        <route>
            <from uri="netty:tcp://0.0.0.0:9999?decoders=#MyDecoder"/> <!-- listen port 9999, accept connection, read message, convert from binary to XML -->
            <setHeader name="channel"><constant>socket</constant></setHeader> <!-- set channel header -->
            <multicast>
                <to uri="file:/work/camel/out?fileName=socket.log&amp;fileExist=Append&amp;appendChars=\r\n"/> <!-- log to in channel log file -->
                <to uri="direct:some"/> <!-- route to some out channels -->
            </multicast>
        </route>
        <!-- CSV/file in channel: -->
        <route>
            <from uri="file:/work/camel/in"/> <!-- watch directory and read CSV files -->
            <split>
                <tokenize token="\r\n"/> <!-- split file into lines -->
                <setHeader name="channel"><constant>file</constant></setHeader> <!-- set channel header -->
                <multicast>
                    <to uri="file:/work/camel/out?fileName=file.log&amp;fileExist=Append&amp;appendChars=\r\n"/> <!-- log to in channel log file -->
                    <to uri="direct:csv2xml"/> <!-- route to CSV to XML converter (that will route to all out channels) -->
                </multicast>
            </split>
        </route>
        <!-- XML/MQ in channel: -->
        <route>
		    <from uri="jms:queue:in_q?connectionFactory=MyAMQ"/> <!-- receive from queue in_q -->
		    <setHeader name="channel"><constant>mq</constant></setHeader> <!-- set channel header -->
		    <multicast>
		        <to uri="file:/work/camel/out?fileName=mq.log&amp;fileExist=Append&amp;appendChars=\r\n"/> <!-- log to in channel log file -->
				<to uri="direct:all"/> <!-- route to all out channels -->
			</multicast>
        </route>
        <!-- JSON/HTTP in channel: -->
        <route>
		    <from uri="jetty:http://0.0.0.0:8888/demo"/> <!-- expose web service on port 8888 and receive requests -->
		    <convertBodyTo type="String"/> <!-- extract HTTP request body -->
		    <setHeader name="channel"><constant>http</constant></setHeader> <!-- set channel header -->
		    <multicast>
		        <to uri="file:/work/camel/out?fileName=http.log&amp;fileExist=Append&amp;appendChars=\r\n"/> <!-- log to in channel log file -->
				<to uri="direct:json2xml"/> <!-- route to JSON to XML converter (that will route to all out channels) -->
			</multicast>
		</route>
        <!-- **** routing **** -->
        <route>
            <from uri="direct:csv2xml"/>
            <process ref="CSV2XML"/> <!-- convert CSV to XML -->
            <to uri="direct:all"/> <!-- route to all out channels -->
        </route>
        <route>
		    <from uri="direct:json2xml"/>
		    <process ref="JSON2XML"/> <!-- convert JSON to XML -->
		    <to uri="direct:all"/> <!-- route to all out channels -->
		</route>
		<route>
			<from uri="direct:all"/>
			<multicast>
			    <to uri="direct:db"/> <!-- route to database out channel -->
				<to uri="direct:websvc"/> <!-- route to web service out channel -->
				<to uri="direct:mq"/> <!-- route to MQ out channel -->
				<to uri="direct:java"/> <!-- route to Java code out channel -->
				<to uri="direct:log"/> <!-- route to log out channel -->
				<to uri="direct:out"/> <!-- route to output out channel -->
		   </multicast>
		</route>
		<route>
		    <from uri="direct:some"/>
		    <multicast>
		        <to uri="direct:db"/> <!-- route to database out channel -->
				<to uri="direct:log"/> <!-- route to log out channel -->
				<to uri="direct:out"/> <!-- route to output out channel -->
		   </multicast>
		</route>
		<!-- **** out channels **** -->
		<!-- database out channel: -->
		<route>
		    <from uri="direct:db"/>
		    <process ref="XML2CSV"/><transform><simple>${headers.channel},${body}</simple></transform> <!-- convert from XML to CSV and add channel as first field -->
		    <to uri="sql:insert into data(channel,ival,sval) values (#,#,#)?dataSource=MyDB"/> <!-- insert into database using CSV for parameters -->
		</route>
		<!-- web service out channel: -->
		<route>
			<from uri="direct:websvc"/>
			<process ref="XML2JSON"/> <!-- convert from XML to JSON -->
			<to uri="http://localhost/svclog.ashx?bridgeEndpoint=true"/> <!-- call web service -->
		</route>
		<!-- MQ out channel: -->
		<route>
		    <from uri="direct:mq"/>
			<to uri="jms:queue:out_q?connectionFactory=MyAMQ&amp;exchangePattern=InOnly"/> <!-- send to queue out_q -->
		</route>
		<!-- Java code out channel: -->
		<route>
		    <from uri="direct:java"/>
			<to uri="bean:MyBean?method=something"/> <!-- call method something in bean class -->
		</route>
		<!-- log out channel: -->
		<route>
		    <from uri="direct:log"/>
			<to uri="file:/work/camel/out?fileName=router.log&amp;fileExist=Append&amp;appendChars=\r\n"/> <!-- log (routed XML) -->
		</route>
		<!-- output out channel: -->
		<route>
		    <from uri="direct:out"/>
			<process ref="Truncater"/> <!-- truncate to max. 40 characters -->
			<to uri="stream:out"/> <!-- output -->
		</route>
    </camelContext>
</beans>