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 */
017
018package org.apache.camel.model.cloud;
019
020import java.util.ArrayList;
021import java.util.Arrays;
022import java.util.List;
023import java.util.ListIterator;
024import java.util.Map;
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-discovery")
035@XmlRootElement(name = "staticServiceDiscovery")
036@XmlAccessorType(XmlAccessType.FIELD)
037public class StaticServiceCallServiceDiscoveryConfiguration extends ServiceCallServiceDiscoveryConfiguration {
038    @XmlElement
039    private List<String> servers;
040
041    public StaticServiceCallServiceDiscoveryConfiguration() {
042        this(null);
043    }
044
045    public StaticServiceCallServiceDiscoveryConfiguration(ServiceCallDefinition parent) {
046        super(parent, "static-service-discovery");
047    }
048
049    // *************************************************************************
050    // Properties
051    // *************************************************************************
052
053    public List<String> getServers() {
054        return servers;
055    }
056
057    /**
058     * Sets the server list.
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 list.
076     * Each entry can be a list of servers separated by comma in the format:
077     *
078     *   [service@]host:port,[service@]host2:port,[service@]host3:port
079     *
080     * @param servers a list of servers.
081     * @return this instance
082     */
083    public StaticServiceCallServiceDiscoveryConfiguration servers(List<String> servers) {
084        setServers(servers);
085        return this;
086    }
087
088    /**
089     * Sets the server list.
090     *
091     * @param servers a list of servers separated by comma in the format: [service@]host:port,[service@]host2:port,[service@]host3:port
092     * @return this instance
093     */
094    public StaticServiceCallServiceDiscoveryConfiguration servers(String servers) {
095        if (ObjectHelper.isNotEmpty(servers)) {
096            String[] parts = servers.split(",");
097
098            if (this.servers == null) {
099                this.servers = new ArrayList<>();
100            }
101
102            this.servers.addAll(Arrays.asList(parts));
103        }
104
105        return this;
106    }
107
108    // *************************************************************************
109    // Utilities
110    // *************************************************************************
111
112    protected void postProcessFactoryParameters(CamelContext camelContext, Map<String, Object> parameters) throws Exception  {
113        List<String> servers = List.class.cast(parameters.get("servers"));
114
115        if (ObjectHelper.isNotEmpty(servers)) {
116            final ListIterator<String> it = servers.listIterator();
117            while (it.hasNext()) {
118                it.set(camelContext.resolvePropertyPlaceholders(it.next()));
119            }
120
121            parameters.put("servers", servers);
122        }
123    }
124}