package com.yihu.hos.broker.services.camel;

import com.yihu.hos.core.constants.CoreConstant;
import org.apache.camel.CamelContext;
import org.apache.camel.impl.DefaultCamelContext;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.SynchronousQueue;

/**
 * Created by lingfeng on 2016/8/9.
 */
public class SystemCamelContext {
    private static CamelContext context;
    private static SynchronousQueue<String> queue = new SynchronousQueue<>();

    private static Map<String, String> mapping = new HashMap<>();

    public static Map<String, String> getMapping() {
        return mapping;
    }

    public static void putClassMapping(String routeCode, String packageName, String type, String className) {
        mapping.put(routeCode + "|" + type + "|" + className, packageName + CoreConstant.DOT + className);
    }

    public static String getClassMapping(String routeCode, String className, String type) {
        return mapping.get(routeCode + "|" + type + "|" + className);
    }

    public static URL getResource(Object o) {
        return o.getClass().getProtectionDomain().getClassLoader().getResource("");
    }

    public static CamelContext getContext() {
        if (context == null) {
            context = new DefaultCamelContext();
        }
        return context;
    }

    public static SynchronousQueue<String> getQueue() {
        return queue;
    }

    public static void startRouter(String routeCode) throws Exception {
        getContext().startRoute(routeCode);
    }

    public static void stopRouter(String routeCode) throws Exception {
        getContext().stopRoute(routeCode);
    }

    public static boolean removeRouter(String routeCode) throws Exception {
        return getContext().removeRoute(routeCode);
    }
}
