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