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.model;
018
019import java.util.List;
020import java.util.stream.Collectors;
021
022import javax.xml.bind.annotation.XmlAccessType;
023import javax.xml.bind.annotation.XmlAccessorType;
024import javax.xml.bind.annotation.XmlAttribute;
025import javax.xml.bind.annotation.XmlElementRef;
026import javax.xml.bind.annotation.XmlRootElement;
027
028import org.apache.camel.spi.Metadata;
029
030/**
031 * Route to be executed when Hystrix EIP executes fallback
032 */
033@Metadata(label = "eip,routing,circuitbreaker")
034@XmlRootElement(name = "onFallback")
035@XmlAccessorType(XmlAccessType.FIELD)
036public class OnFallbackDefinition extends OutputDefinition<OnFallbackDefinition> {
037
038    @XmlAttribute
039    @Metadata(label = "command", defaultValue = "false")
040    private String fallbackViaNetwork;
041
042    public OnFallbackDefinition() {
043    }
044
045    @Override
046    public List<ProcessorDefinition<?>> getOutputs() {
047        return outputs;
048    }
049
050    @XmlElementRef
051    @Override
052    public void setOutputs(List<ProcessorDefinition<?>> outputs) {
053        super.setOutputs(outputs);
054    }
055
056    @Override
057    public String toString() {
058        if (Boolean.toString(true).equals(fallbackViaNetwork)) {
059            return "OnFallbackViaNetwork[" + getOutputs() + "]";
060        } else if (fallbackViaNetwork == null || Boolean.toString(false).equals(fallbackViaNetwork)) {
061            return "OnFallback["  + getOutputs() + "]";
062        } else {
063            return "OnFallback[viaNetwork=" + fallbackViaNetwork + "," + getOutputs() + "]";
064        }
065    }
066
067    @Override
068    public String getShortName() {
069        return "onFallback";
070    }
071
072    @Override
073    public String getLabel() {
074        String name;
075        if (Boolean.toString(true).equals(fallbackViaNetwork)) {
076            name = "OnFallbackViaNetwork";
077        } else if (fallbackViaNetwork == null || Boolean.toString(false).equals(fallbackViaNetwork)) {
078            name = "onFallback";
079        } else {
080            name = "onFallback(viaNetwork=" + fallbackViaNetwork + ")";
081        }
082        return getOutputs().stream().map(ProcessorDefinition::getLabel)
083                .collect(Collectors.joining(",", name + "[", "]"));
084    }
085
086    public String getFallbackViaNetwork() {
087        return fallbackViaNetwork;
088    }
089
090    /**
091     * Whether the fallback goes over the network.
092     * <p/>
093     * If the fallback will go over the network it is another possible point of
094     * failure and so it also needs to be wrapped by a HystrixCommand. It is
095     * important to execute the fallback command on a separate thread-pool,
096     * otherwise if the main command were to become latent and fill the
097     * thread-pool this would prevent the fallback from running if the two
098     * commands share the same pool.
099     */
100    public void setFallbackViaNetwork(String fallbackViaNetwork) {
101        this.fallbackViaNetwork = fallbackViaNetwork;
102    }
103
104}