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.cloud;
018
019import java.util.ArrayList;
020import java.util.Arrays;
021import java.util.List;
022import java.util.ListIterator;
023import java.util.Map;
024
025import javax.xml.bind.annotation.XmlAccessType;
026import javax.xml.bind.annotation.XmlAccessorType;
027import javax.xml.bind.annotation.XmlElement;
028import javax.xml.bind.annotation.XmlRootElement;
029
030import org.apache.camel.CamelContext;
031import org.apache.camel.spi.Metadata;
032import org.apache.camel.util.ObjectHelper;
033
034@Metadata(label = "routing,cloud,service-filter")
035@XmlRootElement(name = "blacklistServiceFilter")
036@XmlAccessorType(XmlAccessType.FIELD)
037public class BlacklistServiceCallServiceFilterConfiguration extends ServiceCallServiceFilterConfiguration {
038    @XmlElement
039    private List<String> servers;
040
041    public BlacklistServiceCallServiceFilterConfiguration() {
042        this(null);
043    }
044
045    public BlacklistServiceCallServiceFilterConfiguration(ServiceCallDefinition parent) {
046        super(parent, "blacklist-service-filter");
047    }
048
049    // *************************************************************************
050    // Properties
051    // *************************************************************************
052
053    public List<String> getServers() {
054        return servers;
055    }
056
057    /**
058     * Sets the server blacklist.
059     * 
060     * Each entry can be a list of servers separated by comma in the format:
061     *
062     *   [service@]host:port,[service@]host2:port,[service@]host3:port
063     *
064     * @param servers a list of servers.
065     * @return this instance
066     */
067    public void setServers(List<String> servers) {
068        this.servers = servers;
069    }
070
071    // *************************************************************************
072    // Fluent API
073    // *************************************************************************
074
075    /**
076     * Sets the server blacklist.
077     *
078     * Each entry can be a list of servers separated by comma in the format:
079     *
080     *   [service@]host:port,[service@]host2:port,[service@]host3:port
081     *
082     * @param servers a list of servers.
083     * @return this instance
084     */
085    public BlacklistServiceCallServiceFilterConfiguration servers(List<String> servers) {
086        setServers(servers);
087        return this;
088    }
089
090    /**
091     * Sets the server blacklist.
092     *
093     * @param servers a list of servers separated by comma in the format: [service@]host:port,[service@]host2:port,[service@]host3:port
094     * @return this instance
095     */
096    public BlacklistServiceCallServiceFilterConfiguration servers(String servers) {
097        if (ObjectHelper.isNotEmpty(servers)) {
098            String[] parts = servers.split(",");
099
100            if (this.servers == null) {
101                this.servers = new ArrayList<>();
102            }
103
104            this.servers.addAll(Arrays.asList(parts));
105        }
106
107        return this;
108    }
109
110    // *************************************************************************
111    // Utilities
112    // *************************************************************************
113
114    protected void postProcessFactoryParameters(CamelContext camelContext, Map<String, Object> parameters) throws Exception  {
115        List<String> servers = List.class.cast(parameters.get("servers"));
116
117        if (ObjectHelper.isNotEmpty(servers)) {
118            final ListIterator<String> it = servers.listIterator();
119            while (it.hasNext()) {
120                it.set(camelContext.resolvePropertyPlaceholders(it.next()));
121            }
122
123            parameters.put("servers", servers);
124        }
125    }
126}