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.cluster; 018 019import org.apache.camel.CamelContext; 020import org.apache.camel.NamedNode; 021import org.apache.camel.RuntimeCamelException; 022import org.apache.camel.cluster.CamelClusterService; 023import org.apache.camel.spi.RoutePolicy; 024import org.apache.camel.spi.RoutePolicyFactory; 025import org.apache.camel.support.cluster.ClusterServiceSelectors; 026import org.apache.camel.util.ObjectHelper; 027 028public class ClusteredRoutePolicyFactory implements RoutePolicyFactory { 029 private final String namespace; 030 private final CamelClusterService clusterService; 031 private final CamelClusterService.Selector clusterServiceSelector; 032 033 public ClusteredRoutePolicyFactory(String namespace) { 034 ObjectHelper.notNull(namespace, "Cluster View Namespace"); 035 036 this.namespace = namespace; 037 this.clusterService = null; 038 this.clusterServiceSelector = ClusterServiceSelectors.DEFAULT_SELECTOR; 039 } 040 041 public ClusteredRoutePolicyFactory(CamelClusterService.Selector selector, String namespace) { 042 ObjectHelper.notNull(namespace, "Cluster View Namespace"); 043 ObjectHelper.notNull(selector, "Cluster Service Selector"); 044 045 this.namespace = namespace; 046 this.clusterService = null; 047 this.clusterServiceSelector = selector; 048 } 049 050 public ClusteredRoutePolicyFactory(CamelClusterService clusterService, String viewName) { 051 ObjectHelper.notNull(clusterService, "Cluster Service"); 052 ObjectHelper.notNull(viewName, "Cluster View Namespace"); 053 054 this.clusterService = clusterService; 055 this.namespace = viewName; 056 this.clusterServiceSelector = null; 057 } 058 059 @Override 060 public RoutePolicy createRoutePolicy(CamelContext camelContext, String routeId, NamedNode route) { 061 try { 062 return clusterService != null 063 ? ClusteredRoutePolicy.forNamespace(clusterService, namespace) : 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}