// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package routing.demo;

import org.apache.activemq.ActiveMQConnectionFactory;

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.spi.Registry;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import routing.demo.supp.CSV2XML;
import routing.demo.supp.JSON2XML;
import routing.demo.supp.Truncater;
import routing.demo.supp.XML2CSV;
import routing.demo.supp.XML2JSON;

public class TestJavaConfig {
    @SuppressWarnings("serial")
    public static void main(String[] args) throws Exception {
        CamelContext ctx = new DefaultCamelContext();
        Registry reg = ctx.getRegistry();
        reg.bind("MyDecoder", new MyDecoder()); // decoder for binary/socket in channel
        reg.bind("MyAMQ", new ActiveMQConnectionFactory("tcp://localhost:61616")); // MQ connection factory
        reg.bind("MyDB", new MysqlDataSource() { { setServerName("localhost"); setDatabaseName("Test"); setUser("root"); setPassword(""); } } ); // DB data source
        reg.bind("MyBean", new MyBean()); // bean for Java code out channel
        ctx.addRoutes(new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                // **** in channels ****
                // binary/socket in channel:
                from("netty:tcp://0.0.0.0:9999?decoders=#MyDecoder") // listen port 9999, accept connection, read message, convert from binary to XML
                    .setHeader("channel", constant("socket")) // set channel header
                    .multicast().to("file:/work/camel/out?fileName=socket.log&fileExist=Append&appendChars=\r\n", // log to in channel log file
                                    "direct:some"); // route to some out channels
                // CSV/file in channel:
                from("file:/work/camel/in") // watch directory and read CSV files
                    .split().tokenize("\r\n", 1)  // split file into lines
                    .setHeader("channel", constant("file")) // set channel header
                    .multicast().to("file:/work/camel/out?fileName=file.log&fileExist=Append&appendChars=\r\n", // log to in channel log file
                                    "direct:csv2xml"); // route to CSV to XML converter (that will route to all out channels)
                // XML/MQ in channel:
                from("jms:queue:in_q?connectionFactory=MyAMQ") // receive from queue in_q
                    .setHeader("channel", constant("mq")) // set channel header
                    .multicast().to("file:/work/camel/out?fileName=mq.log&fileExist=Append&appendChars=\r\n", // log to in channel log file
                                    "direct:all"); // route to all out channels
                // JSON/HTTP in channel:
                from("jetty:http://0.0.0.0:8888/demo") // expose web service on port 8888 and receive requests
                    .convertBodyTo(String.class) // extract HTTP request body
                    .setHeader("channel", constant("http")) // set channel header
                    .multicast().to("file:/work/camel/out?fileName=http.log&fileExist=Append&appendChars=\r\n", // log to in channel log file
                                    "direct:json2xml"); // route to JSON to XML converter (that will route to all out channels)
                // **** routing ****
                from("direct:csv2xml")
                    .process(new CSV2XML("data", "ival,sval", ',', '"')) // convert CSV to XML
                    .to("direct:all"); // route to all out channels
                from("direct:json2xml")
                    .process(new JSON2XML("data")) // convert JSON to XML
                    .to("direct:all"); // route to all out channels
                from("direct:all")
                    .multicast().to("direct:db", // route to database out channel
                                    "direct:websvc", // route to web service out channel
                                    "direct:mq", // route to MQ out channel
                                    "direct:java", // route to Java code out channel
                                    "direct:log", // route to log out channel
                                    "direct:out"); // route to output out channel
                from("direct:some")
                    .multicast().to("direct:db", // route to database out channel
                                    "direct:log", // route to log out channel
                                    "direct:out"); // route to output out channel
                // **** out channels ****
                // database out channel:
                from("direct:db")
                    .process(new XML2CSV(',','"')).transform(simple("${headers.channel},${body}")) // convert from XML to CSV and add channel as first field
                    .to("sql:insert into data(channel,ival,sval) values (#,#,#)?dataSource=MyDB"); // insert into database using CSV for parameters
                // web service out channel:
                from("direct:websvc")
                    .process(new XML2JSON()) // convert from XML to JSON
                    .to("http://localhost/svclog.ashx?bridgeEndpoint=true"); // call web service
                // MQ out channel:
                from("direct:mq")
                    .to("jms:queue:out_q?connectionFactory=MyAMQ&exchangePattern=InOnly"); // send to queue out_q
                // Java code out channel:
                from("direct:java")
                    .to("bean:MyBean?method=something"); // call method something in bean class
                // log out channel:
                from("direct:log")
                    .to("file:/work/camel/out?fileName=router.log&fileExist=Append&appendChars=\r\n"); // log (routed XML)
                // output out channel:
                from("direct:out")
                    .process(new Truncater(40, "...")) // truncate to max. 40 characters
                    .to("stream:out"); // output
            }
        });
        ctx.start();
        System.out.print("Press enter to exit");
        System.in.read();
        ctx.stop();
        ctx.close();
    }
}
