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.Map; 024import java.util.stream.Collectors; 025 026import org.apache.camel.cloud.ServiceDefinition; 027import org.apache.camel.cloud.ServiceFilter; 028import org.apache.camel.util.StringHelper; 029 030public class BlacklistServiceFilter implements ServiceFilter { 031 private List<ServiceDefinition> services; 032 033 public BlacklistServiceFilter() { 034 this.services = new ArrayList<>(); 035 } 036 037 public BlacklistServiceFilter(List<ServiceDefinition> blacklist) { 038 this.services = new ArrayList<>(blacklist); 039 } 040 041 /** 042 * Set the servers. 043 * 044 * @param servers server in the format: [service@]host:port. 045 */ 046 public void setServers(List<String> servers) { 047 this.services.clear(); 048 servers.forEach(this::addServer); 049 } 050 051 /** 052 * Set the servers. 053 * 054 * @param servers servers separated by comma in the format: [service@]host:port,[service@]host2:port,[service@]host3:port and so on. 055 */ 056 public void setServers(String servers) { 057 this.services.clear(); 058 addServer(servers); 059 } 060 061 /** 062 * Add a server to the known list of servers. 063 */ 064 public void addServer(ServiceDefinition server) { 065 services.add(server); 066 } 067 068 /** 069 * Add a server to the known list of servers. 070 * @param serverString servers separated by comma in the format: [service@]host:port,[service@]host2:port,[service@]host3:port and so on. 071 */ 072 public void addServer(String serverString) { 073 String[] parts = serverString.split(","); 074 for (String part : parts) { 075 String service = StringHelper.before(part, "@"); 076 if (service != null) { 077 part = StringHelper.after(part, "@"); 078 } 079 String host = StringHelper.before(part, ":"); 080 String port = StringHelper.after(part, ":"); 081 082 addServer(service, host, Integer.valueOf(port)); 083 } 084 } 085 086 /** 087 * Add a server to the known list of servers. 088 */ 089 public void addServer(String host, int port) { 090 addServer(null, host, port, null); 091 } 092 093 /** 094 * Add a server to the known list of servers. 095 */ 096 public void addServer(String name, String host, int port) { 097 addServer(name, host, port, null); 098 } 099 100 /** 101 * Add a server to the known list of servers. 102 */ 103 public void addServer(String name, String host, int port, Map<String, String> meta) { 104 services.add(new DefaultServiceDefinition(name, host, port, meta)); 105 } 106 107 @Override 108 public List<ServiceDefinition> apply(List<ServiceDefinition> services) { 109 return services.stream().filter(s -> !this.services.contains(s)).collect(Collectors.toList()); 110 } 111 112 List<ServiceDefinition> getBlacklistedServices() { 113 return Collections.unmodifiableList(this.services); 114 } 115 116 // ************************************************************************* 117 // Helpers 118 // ************************************************************************* 119 120 public static BlacklistServiceFilter forServices(Collection<ServiceDefinition> definitions) { 121 BlacklistServiceFilter filter = new BlacklistServiceFilter(); 122 for (ServiceDefinition definition: definitions) { 123 filter.addServer(definition); 124 } 125 126 return filter; 127 } 128 129 public static BlacklistServiceFilter forServices(ServiceDefinition... definitions) { 130 BlacklistServiceFilter filter = new BlacklistServiceFilter(); 131 for (ServiceDefinition definition: definitions) { 132 filter.addServer(definition); 133 } 134 135 return filter; 136 } 137}