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.function.Predicate; 024import java.util.stream.Collectors; 025 026import org.apache.camel.cloud.ServiceDefinition; 027import org.apache.camel.cloud.ServiceFilter; 028 029public class BlacklistServiceFilter implements ServiceFilter { 030 private List<ServiceDefinition> services; 031 032 public BlacklistServiceFilter() { 033 this.services = new ArrayList<>(); 034 } 035 036 public BlacklistServiceFilter(List<ServiceDefinition> blacklist) { 037 this.services = new ArrayList<>(blacklist); 038 } 039 040 /** 041 * Set the servers to blacklist. 042 * 043 * @param servers server in the format: [service@]host:port. 044 */ 045 public void setServers(List<String> servers) { 046 this.services.clear(); 047 servers.forEach(this::addServer); 048 } 049 050 /** 051 * Set the servers to blacklist. 052 * 053 * @param servers servers separated by comma in the format: [service@]host:port,[service@]host2:port,[service@]host3:port and so on. 054 */ 055 public void setServers(String servers) { 056 this.services.clear(); 057 addServer(servers); 058 } 059 060 /** 061 * Add a server to the known list of servers. 062 */ 063 public void addServer(ServiceDefinition server) { 064 services.add(server); 065 } 066 067 /** 068 * Add a server to the known list of servers to blacklist. 069 * @param serverString servers separated by comma in the format: [service@]host:port,[service@]host2:port,[service@]host3:port and so on. 070 */ 071 public void addServer(String serverString) { 072 DefaultServiceDefinition.parse(serverString).forEach(this::addServer); 073 } 074 075 /** 076 * Remove an existing server from the list of known servers. 077 */ 078 public void removeServer(Predicate<ServiceDefinition> condition) { 079 services.removeIf(condition); 080 } 081 082 @Override 083 public List<ServiceDefinition> apply(List<ServiceDefinition> services) { 084 return services.stream().filter( 085 s -> this.services.stream().noneMatch(b -> b.matches(s)) 086 ).collect( 087 Collectors.toList() 088 ); 089 } 090 091 List<ServiceDefinition> getBlacklistedServices() { 092 return Collections.unmodifiableList(this.services); 093 } 094 095 // ************************************************************************* 096 // Helpers 097 // ************************************************************************* 098 099 public static BlacklistServiceFilter forServices(Collection<ServiceDefinition> definitions) { 100 BlacklistServiceFilter filter = new BlacklistServiceFilter(); 101 for (ServiceDefinition definition: definitions) { 102 filter.addServer(definition); 103 } 104 105 return filter; 106 } 107 108 public static BlacklistServiceFilter forServices(ServiceDefinition... definitions) { 109 BlacklistServiceFilter filter = new BlacklistServiceFilter(); 110 for (ServiceDefinition definition: definitions) { 111 filter.addServer(definition); 112 } 113 114 return filter; 115 } 116}