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