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.impl.cloud;
018
019import java.util.ArrayList;
020import java.util.Collection;
021import java.util.Collections;
022import java.util.List;
023import java.util.Objects;
024import java.util.function.Predicate;
025import java.util.stream.Collectors;
026
027import org.apache.camel.cloud.ServiceDefinition;
028import org.apache.camel.util.ObjectHelper;
029import org.apache.camel.util.StringHelper;
030
031/**
032 * A static list of known servers Camel Service Call EIP.
033 */
034public class StaticServiceDiscovery extends DefaultServiceDiscovery {
035    private final List<ServiceDefinition> services;
036
037    public StaticServiceDiscovery() {
038        this.services = new ArrayList<>();
039    }
040
041    public StaticServiceDiscovery(List<ServiceDefinition> servers) {
042        this.services = new ArrayList<>(servers);
043    }
044
045    /**
046     * Set the servers.
047     *
048     * @param servers server in the format: [service@]host:port.
049     */
050    public void setServers(List<String> servers) {
051        this.services.clear();
052        servers.forEach(this::addServer);
053    }
054
055    public void addServers(String serviceName, List<String> servers) {
056        for (String server : servers) {
057            String host = StringHelper.before(server, ":");
058            String port = StringHelper.after(server, ":");
059
060            if (ObjectHelper.isNotEmpty(host) && ObjectHelper.isNotEmpty(port)) {
061                addServer(
062                    DefaultServiceDefinition.builder()
063                        .withName(serviceName)
064                        .withHost(host)
065                        .withPort(Integer.parseInt(port))
066                        .build()
067                );
068            }
069        }
070    }
071
072    /**
073     * Set the servers.
074     *
075     * @param servers servers separated by comma in the format: [service@]host:port,[service@]host2:port,[service@]host3:port and so on.
076     */
077    public void setServers(String servers) {
078        this.services.clear();
079        addServer(servers);
080    }
081
082    /**
083     * Add a server to the known list of servers.
084     */
085    public void addServer(ServiceDefinition server) {
086        services.add(server);
087    }
088
089    /**
090     * Add a server to the known list of servers.
091     * @param serverString servers separated by comma in the format: [service@]host:port,[service@]host2:port,[service@]host3:port and so on.
092     */
093    public void addServer(String serverString) {
094        DefaultServiceDefinition.parse(serverString).forEach(this::addServer);
095    }
096
097    /**
098     * Remove an existing server from the list of known servers.
099     */
100    public void removeServer(Predicate<ServiceDefinition> condition) {
101        services.removeIf(condition);
102    }
103
104    @Override
105    public List<ServiceDefinition> getServices(String name) {
106        return Collections.unmodifiableList(
107            services.stream()
108                .filter(s -> Objects.isNull(s.getName()) || Objects.equals(name, s.getName()))
109                .collect(Collectors.toList())
110        );
111    }
112
113    // *************************************************************************
114    // Helpers
115    // *************************************************************************
116
117    public static StaticServiceDiscovery forServices(Collection<ServiceDefinition> definitions) {
118        StaticServiceDiscovery discovery = new StaticServiceDiscovery();
119        for (ServiceDefinition definition: definitions) {
120            discovery.addServer(definition);
121        }
122
123        return discovery;
124    }
125
126    public static StaticServiceDiscovery forServices(ServiceDefinition... definitions) {
127        StaticServiceDiscovery discovery = new StaticServiceDiscovery();
128        for (ServiceDefinition definition: definitions) {
129            discovery.addServer(definition);
130        }
131
132        return discovery;
133    }
134}