package org.dromara.config;

import cn.hutool.core.io.file.FileReader;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.dsl.xml.jaxb.CamelJaxbXmlRoutesBuilderLoader;
import org.apache.camel.spi.Resource;
import org.apache.camel.support.ResourceHelper;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.apache.camel.*;

@Component
@Order(2)
public class CamelRouterConfig implements InitializingBean {

    @Value("${camelxmlpath}")
    private String filepath;

    @Autowired
    private CamelContext camelContext;

    @Override
    public void afterPropertiesSet() throws Exception {
        try {
            CamelContext camelContext = this.camelContext;
            FileReader routexml = new FileReader(filepath);
            Resource resource = ResourceHelper.fromString("a.xml", routexml.readString());
            CamelJaxbXmlRoutesBuilderLoader xmlRoutesBuilderLoader = new CamelJaxbXmlRoutesBuilderLoader();

            RouteBuilder routeBuilder = xmlRoutesBuilderLoader.doLoadRouteBuilder(resource);
            camelContext.addRoutes(routeBuilder);

            camelContext.getRouteController().startAllRoutes();

        } catch (Exception e) {
            System.out.println("------error-----");
        }
    }
}
