001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.impl.ha;
018
019import org.apache.camel.CamelContext;
020import org.apache.camel.RuntimeCamelException;
021import org.apache.camel.ha.CamelClusterService;
022import org.apache.camel.model.RouteDefinition;
023import org.apache.camel.spi.RoutePolicy;
024import org.apache.camel.spi.RoutePolicyFactory;
025import org.apache.camel.util.ObjectHelper;
026
027public class ClusteredRoutePolicyFactory implements RoutePolicyFactory {
028    private final String namespace;
029    private final CamelClusterService clusterService;
030    private final CamelClusterService.Selector clusterServiceSelector;
031
032    public ClusteredRoutePolicyFactory(String namespace) {
033        ObjectHelper.notNull(namespace, "Cluster View Namespace");
034
035        this.namespace = namespace;
036        this.clusterService = null;
037        this.clusterServiceSelector = ClusterServiceSelectors.DEFAULT_SELECTOR;
038    }
039
040    public ClusteredRoutePolicyFactory(CamelClusterService.Selector selector, String namespace) {
041        ObjectHelper.notNull(namespace, "Cluster View Namespace");
042        ObjectHelper.notNull(selector, "Cluster Service Selector");
043
044        this.namespace = namespace;
045        this.clusterService = null;
046        this.clusterServiceSelector = selector;
047    }
048
049    public ClusteredRoutePolicyFactory(CamelClusterService clusterService, String viewName) {
050        ObjectHelper.notNull(clusterService, "Cluster Service");
051        ObjectHelper.notNull(viewName, "Cluster View Namespace");
052
053        this.clusterService = clusterService;
054        this.namespace = viewName;
055        this.clusterServiceSelector = null;
056    }
057
058    @Override
059    public RoutePolicy createRoutePolicy(CamelContext camelContext, String routeId, RouteDefinition route) {
060        try {
061            return clusterService != null
062                ? ClusteredRoutePolicy.forNamespace(clusterService, namespace)
063                : ClusteredRoutePolicy.forNamespace(camelContext, clusterServiceSelector, namespace);
064        } catch (Exception e) {
065            throw new RuntimeCamelException(e);
066        }
067    }
068
069    // ****************************************************
070    // Static helpers
071    // ****************************************************
072
073    public static ClusteredRoutePolicyFactory forNamespace(String namespace) {
074        return new ClusteredRoutePolicyFactory(namespace);
075    }
076
077    public static ClusteredRoutePolicyFactory forNamespace(CamelClusterService.Selector selector, String namespace) {
078        return new ClusteredRoutePolicyFactory(selector, namespace);
079    }
080
081    public static ClusteredRoutePolicyFactory forNamespace(CamelClusterService clusterService, String namespace) {
082        return new ClusteredRoutePolicyFactory(clusterService, namespace);
083    }
084}