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; 018 019import org.apache.camel.Channel; 020import org.apache.camel.Processor; 021import org.apache.camel.Route; 022import org.apache.camel.model.LoadBalanceDefinition; 023import org.apache.camel.model.ProcessorDefinition; 024import org.apache.camel.model.loadbalancer.FailoverLoadBalancerDefinition; 025import org.apache.camel.processor.loadbalancer.LoadBalancer; 026import org.apache.camel.reifier.loadbalancer.LoadBalancerReifier; 027 028public class LoadBalanceReifier extends ProcessorReifier<LoadBalanceDefinition> { 029 030 public LoadBalanceReifier(Route route, ProcessorDefinition<?> definition) { 031 super(route, (LoadBalanceDefinition)definition); 032 } 033 034 @Override 035 public Processor createProcessor() throws Exception { 036 LoadBalancer loadBalancer = LoadBalancerReifier.reifier(route, definition.getLoadBalancerType()).createLoadBalancer(); 037 038 // some load balancer can only support a fixed number of outputs 039 int max = definition.getLoadBalancerType().getMaximumNumberOfOutputs(); 040 int size = definition.getOutputs().size(); 041 if (size > max) { 042 throw new IllegalArgumentException("To many outputs configured on " + definition.getLoadBalancerType() + ": " + size + " > " + max); 043 } 044 045 for (ProcessorDefinition<?> processorType : definition.getOutputs()) { 046 // output must not be another load balancer 047 // check for instanceof as the code below as there is 048 // compilation errors on earlier versions of JDK6 049 // on Windows boxes or with IBM JDKs etc. 050 if (LoadBalanceDefinition.class.isInstance(processorType)) { 051 throw new IllegalArgumentException("Loadbalancer already configured to: " + definition.getLoadBalancerType() + ". Cannot set it to: " + processorType); 052 } 053 Processor processor = createProcessor(processorType); 054 Channel channel = wrapChannel(processor, processorType); 055 loadBalancer.addProcessor(channel); 056 } 057 058 Boolean inherit = definition.isInheritErrorHandler(); 059 if (definition.getLoadBalancerType() instanceof FailoverLoadBalancerDefinition) { 060 // special for failover load balancer where you can configure it to 061 // not inherit error handler for its children 062 // but the load balancer itself should inherit so Camels error 063 // handler can react afterwards 064 inherit = true; 065 } 066 Processor target = wrapChannel(loadBalancer, definition, inherit); 067 return target; 068 } 069 070}