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;
025
026import javax.xml.bind.annotation.XmlAccessType;
027import javax.xml.bind.annotation.XmlAccessorType;
028import javax.xml.bind.annotation.XmlElement;
029import javax.xml.bind.annotation.XmlRootElement;
030
031import org.apache.camel.CamelContext;
032import org.apache.camel.spi.Metadata;
033import org.apache.camel.util.ObjectHelper;
034
035@Metadata(label = "routing,cloud,service-discovery")
036@XmlRootElement(name = "staticServiceDiscovery")
037@XmlAccessorType(XmlAccessType.FIELD)
038public class StaticServiceCallServiceDiscoveryConfiguration extends ServiceCallServiceDiscoveryConfiguration {
039    @XmlElement
040    private List<String> servers;
041
042    public StaticServiceCallServiceDiscoveryConfiguration() {
043        this(null);
044    }
045
046    public StaticServiceCallServiceDiscoveryConfiguration(ServiceCallDefinition parent) {
047        super(parent, "static-service-discovery");
048    }
049
050    // *************************************************************************
051    // Properties
052    // *************************************************************************
053
054    public List<String> getServers() {
055        return servers;
056    }
057
058    /**
059     * Sets the server list.
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 list.
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 StaticServiceCallServiceDiscoveryConfiguration servers(List<String> servers) {
085        setServers(servers);
086        return this;
087    }
088
089    /**
090     * Sets the server list.
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 StaticServiceCallServiceDiscoveryConfiguration 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}