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 */
017 package org.apache.camel.model.loadbalancer;
018
019 import java.util.ArrayList;
020 import java.util.List;
021 import javax.xml.bind.annotation.XmlAccessType;
022 import javax.xml.bind.annotation.XmlAccessorType;
023 import javax.xml.bind.annotation.XmlAttribute;
024 import javax.xml.bind.annotation.XmlElement;
025 import javax.xml.bind.annotation.XmlRootElement;
026
027 import org.apache.camel.model.LoadBalancerDefinition;
028 import org.apache.camel.processor.loadbalancer.FailOverLoadBalancer;
029 import org.apache.camel.processor.loadbalancer.LoadBalancer;
030 import org.apache.camel.spi.RouteContext;
031
032 /**
033 * Represents an XML <failover/> element
034 */
035 @XmlRootElement(name = "failover")
036 @XmlAccessorType(XmlAccessType.FIELD)
037 public class FailoverLoadBalancerDefinition extends LoadBalancerDefinition {
038 @XmlElement(name = "exception")
039 private List<String> exceptions = new ArrayList<String>();
040 @XmlAttribute
041 private Boolean roundRobin;
042 @XmlAttribute
043 private Integer maximumFailoverAttempts;
044
045 public FailoverLoadBalancerDefinition() {
046 }
047
048 @Override
049 protected LoadBalancer createLoadBalancer(RouteContext routeContext) {
050 FailOverLoadBalancer answer;
051
052 if (!exceptions.isEmpty()) {
053 List<Class<?>> classes = new ArrayList<Class<?>>();
054 for (String name : exceptions) {
055 Class<?> type = routeContext.getCamelContext().getClassResolver().resolveClass(name);
056 if (type == null) {
057 throw new IllegalArgumentException("Cannot find class: " + name + " in the classpath");
058 }
059 classes.add(type);
060 }
061 answer = new FailOverLoadBalancer(classes);
062 } else {
063 answer = new FailOverLoadBalancer();
064 }
065
066 if (getMaximumFailoverAttempts() != null) {
067 answer.setMaximumFailoverAttempts(getMaximumFailoverAttempts());
068 }
069 if (roundRobin != null) {
070 answer.setRoundRobin(roundRobin);
071 }
072
073 return answer;
074 }
075
076 public List<String> getExceptions() {
077 return exceptions;
078 }
079
080 public void setExceptions(List<String> exceptions) {
081 this.exceptions = exceptions;
082 }
083
084 public boolean isRoundRobin() {
085 return roundRobin != null && roundRobin;
086 }
087
088 public Boolean getRoundRobin() {
089 return roundRobin;
090 }
091
092 public void setRoundRobin(Boolean roundRobin) {
093 this.roundRobin = roundRobin;
094 }
095
096 public Integer getMaximumFailoverAttempts() {
097 return maximumFailoverAttempts;
098 }
099
100 public void setMaximumFailoverAttempts(Integer maximumFailoverAttempts) {
101 this.maximumFailoverAttempts = maximumFailoverAttempts;
102 }
103
104 @Override
105 public String toString() {
106 return "FailoverLoadBalancer";
107 }
108 }