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.reifier.loadbalancer;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import org.apache.camel.model.LoadBalancerDefinition;
023import org.apache.camel.model.loadbalancer.FailoverLoadBalancerDefinition;
024import org.apache.camel.processor.loadbalancer.FailOverLoadBalancer;
025import org.apache.camel.processor.loadbalancer.LoadBalancer;
026import org.apache.camel.spi.RouteContext;
027import org.apache.camel.util.ObjectHelper;
028
029public class FailoverLoadBalancerReifier extends LoadBalancerReifier<FailoverLoadBalancerDefinition> {
030
031    public FailoverLoadBalancerReifier(LoadBalancerDefinition definition) {
032        super((FailoverLoadBalancerDefinition)definition);
033    }
034
035    @Override
036    public LoadBalancer createLoadBalancer(RouteContext routeContext) {
037        FailOverLoadBalancer answer;
038
039        List<Class<?>> classes = new ArrayList<>();
040        if (!definition.getExceptionTypes().isEmpty()) {
041            classes.addAll(definition.getExceptionTypes());
042        } else if (!definition.getExceptions().isEmpty()) {
043            for (String name : definition.getExceptions()) {
044                Class<?> type = routeContext.getCamelContext().getClassResolver().resolveClass(name);
045                if (type == null) {
046                    throw new IllegalArgumentException("Cannot find class: " + name + " in the classpath");
047                }
048                if (!ObjectHelper.isAssignableFrom(Throwable.class, type)) {
049                    throw new IllegalArgumentException("Class is not an instance of Throwable: " + type);
050                }
051                classes.add(type);
052            }
053        }
054        if (classes.isEmpty()) {
055            answer = new FailOverLoadBalancer();
056        } else {
057            answer = new FailOverLoadBalancer(classes);
058        }
059
060        if (definition.getMaximumFailoverAttempts() != null) {
061            answer.setMaximumFailoverAttempts(definition.getMaximumFailoverAttempts());
062        }
063        if (definition.getRoundRobin() != null) {
064            answer.setRoundRobin(definition.getRoundRobin());
065        }
066        if (definition.getSticky() != null) {
067            answer.setSticky(definition.getSticky());
068        }
069
070        return answer;
071    }
072
073}