|
| 1 | +/* |
| 2 | + * Copyright 2023-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.pulsar.autoconfigure; |
| 18 | + |
| 19 | +import java.net.InetSocketAddress; |
| 20 | +import java.net.URI; |
| 21 | +import java.time.Duration; |
| 22 | +import java.util.List; |
| 23 | +import java.util.concurrent.TimeUnit; |
| 24 | + |
| 25 | +import org.apache.pulsar.client.api.ClientBuilder; |
| 26 | +import org.apache.pulsar.client.api.PulsarClientException; |
| 27 | +import org.apache.pulsar.client.api.SizeUnit; |
| 28 | + |
| 29 | +import org.springframework.boot.context.properties.PropertyMapper; |
| 30 | +import org.springframework.boot.util.LambdaSafe; |
| 31 | +import org.springframework.pulsar.autoconfigure.PulsarProperties.Client; |
| 32 | +import org.springframework.pulsar.core.PulsarClientBuilderCustomizer; |
| 33 | +import org.springframework.util.Assert; |
| 34 | +import org.springframework.util.CollectionUtils; |
| 35 | +import org.springframework.util.StringUtils; |
| 36 | +import org.springframework.util.unit.DataSize; |
| 37 | + |
| 38 | +/** |
| 39 | + * Configure Pulsar {@link ClientBuilder} with sensible defaults and apply a list of |
| 40 | + * optional {@link PulsarClientBuilderCustomizer customizers}. |
| 41 | + * |
| 42 | + * @author Chris Bono |
| 43 | + */ |
| 44 | +public class PulsarClientBuilderConfigurer { |
| 45 | + |
| 46 | + private final PulsarProperties properties; |
| 47 | + |
| 48 | + private final List<PulsarClientBuilderCustomizer> customizers; |
| 49 | + |
| 50 | + /** |
| 51 | + * Creates a new configurer that will use the given properties for configuration. |
| 52 | + * @param properties properties to use |
| 53 | + * @param customizers list of customizers to apply or empty list if no customizers |
| 54 | + */ |
| 55 | + public PulsarClientBuilderConfigurer(PulsarProperties properties, List<PulsarClientBuilderCustomizer> customizers) { |
| 56 | + Assert.notNull(properties, "properties must not be null"); |
| 57 | + Assert.notNull(customizers, "customizers must not be null"); |
| 58 | + this.properties = properties; |
| 59 | + this.customizers = customizers; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Configure the specified {@link ClientBuilder}. The builder can be further tuned and |
| 64 | + * default settings can be overridden. |
| 65 | + * @param clientBuilder the {@link ClientBuilder} instance to configure |
| 66 | + */ |
| 67 | + public void configure(ClientBuilder clientBuilder) { |
| 68 | + applyProperties(this.properties, clientBuilder); |
| 69 | + applyCustomizers(this.customizers, clientBuilder); |
| 70 | + } |
| 71 | + |
| 72 | + @SuppressWarnings("deprecation") |
| 73 | + protected void applyProperties(PulsarProperties pulsarProperties, ClientBuilder clientBuilder) { |
| 74 | + var map = PropertyMapper.get().alwaysApplyingWhenNonNull(); |
| 75 | + var clientProperties = pulsarProperties.getClient(); |
| 76 | + map.from(clientProperties::getServiceUrl).to(clientBuilder::serviceUrl); |
| 77 | + map.from(clientProperties::getListenerName).to(clientBuilder::listenerName); |
| 78 | + map.from(clientProperties::getNumIoThreads).to(clientBuilder::ioThreads); |
| 79 | + map.from(clientProperties::getNumListenerThreads).to(clientBuilder::listenerThreads); |
| 80 | + map.from(clientProperties::getNumConnectionsPerBroker).to(clientBuilder::connectionsPerBroker); |
| 81 | + map.from(clientProperties::getMaxConcurrentLookupRequest).to(clientBuilder::maxConcurrentLookupRequests); |
| 82 | + map.from(clientProperties::getMaxLookupRequest).to(clientBuilder::maxLookupRequests); |
| 83 | + map.from(clientProperties::getMaxLookupRedirects).to(clientBuilder::maxLookupRedirects); |
| 84 | + map.from(clientProperties::getMaxNumberOfRejectedRequestPerConnection) |
| 85 | + .to(clientBuilder::maxNumberOfRejectedRequestPerConnection); |
| 86 | + map.from(clientProperties::getUseTcpNoDelay).to(clientBuilder::enableTcpNoDelay); |
| 87 | + |
| 88 | + // Authentication properties |
| 89 | + applyAuthentication(clientProperties, clientBuilder); |
| 90 | + |
| 91 | + // TLS properties |
| 92 | + map.from(clientProperties::getUseTls).to(clientBuilder::enableTls); |
| 93 | + map.from(clientProperties::getTlsHostnameVerificationEnable).to(clientBuilder::enableTlsHostnameVerification); |
| 94 | + map.from(clientProperties::getTlsTrustCertsFilePath).to(clientBuilder::tlsTrustCertsFilePath); |
| 95 | + map.from(clientProperties::getTlsCertificateFilePath).to(clientBuilder::tlsCertificateFilePath); |
| 96 | + map.from(clientProperties::getTlsKeyFilePath).to(clientBuilder::tlsKeyFilePath); |
| 97 | + map.from(clientProperties::getTlsAllowInsecureConnection).to(clientBuilder::allowTlsInsecureConnection); |
| 98 | + map.from(clientProperties::getUseKeyStoreTls).to(clientBuilder::useKeyStoreTls); |
| 99 | + map.from(clientProperties::getSslProvider).to(clientBuilder::sslProvider); |
| 100 | + map.from(clientProperties::getTlsTrustStoreType).to(clientBuilder::tlsTrustStoreType); |
| 101 | + map.from(clientProperties::getTlsTrustStorePath).to(clientBuilder::tlsTrustStorePath); |
| 102 | + map.from(clientProperties::getTlsTrustStorePassword).to(clientBuilder::tlsTrustStorePassword); |
| 103 | + map.from(clientProperties::getTlsCiphers).to(clientBuilder::tlsCiphers); |
| 104 | + map.from(clientProperties::getTlsProtocols).to(clientBuilder::tlsProtocols); |
| 105 | + |
| 106 | + map.from(clientProperties::getStatsInterval).as(Duration::toSeconds).to(clientBuilder, |
| 107 | + (cb, val) -> cb.statsInterval(val, TimeUnit.SECONDS)); |
| 108 | + map.from(clientProperties::getKeepAliveInterval).asInt(Duration::toMillis).to(clientBuilder, |
| 109 | + (cb, val) -> cb.keepAliveInterval(val, TimeUnit.MILLISECONDS)); |
| 110 | + map.from(clientProperties::getConnectionTimeout).asInt(Duration::toMillis).to(clientBuilder, |
| 111 | + (cb, val) -> cb.connectionTimeout(val, TimeUnit.MILLISECONDS)); |
| 112 | + map.from(clientProperties::getOperationTimeout).asInt(Duration::toMillis).to(clientBuilder, |
| 113 | + (cb, val) -> cb.operationTimeout(val, TimeUnit.MILLISECONDS)); |
| 114 | + map.from(clientProperties::getLookupTimeout).asInt(Duration::toMillis).to(clientBuilder, |
| 115 | + (cb, val) -> cb.lookupTimeout(val, TimeUnit.MILLISECONDS)); |
| 116 | + map.from(clientProperties::getInitialBackoffInterval).as(Duration::toMillis).to(clientBuilder, |
| 117 | + (cb, val) -> cb.startingBackoffInterval(val, TimeUnit.MILLISECONDS)); |
| 118 | + map.from(clientProperties::getMaxBackoffInterval).as(Duration::toMillis).to(clientBuilder, |
| 119 | + (cb, val) -> cb.maxBackoffInterval(val, TimeUnit.MILLISECONDS)); |
| 120 | + map.from(clientProperties::getEnableBusyWait).to(clientBuilder::enableBusyWait); |
| 121 | + map.from(clientProperties::getMemoryLimit).as(DataSize::toBytes).to(clientBuilder, |
| 122 | + (cb, val) -> cb.memoryLimit(val, SizeUnit.BYTES)); |
| 123 | + map.from(clientProperties::getEnableTransaction).to(clientBuilder::enableTransaction); |
| 124 | + map.from(clientProperties::getProxyServiceUrl) |
| 125 | + .to((proxyUrl) -> clientBuilder.proxyServiceUrl(proxyUrl, clientProperties.getProxyProtocol())); |
| 126 | + map.from(clientProperties::getDnsLookupBindAddress) |
| 127 | + .to((bindAddr) -> clientBuilder.dnsLookupBind(bindAddr, clientProperties.getDnsLookupBindPort())); |
| 128 | + map.from(clientProperties::getSocks5ProxyAddress).as(this::parseSocketAddress) |
| 129 | + .to(clientBuilder::socks5ProxyAddress); |
| 130 | + map.from(clientProperties::getSocks5ProxyUsername).to(clientBuilder::socks5ProxyUsername); |
| 131 | + map.from(clientProperties::getSocks5ProxyPassword).to(clientBuilder::socks5ProxyPassword); |
| 132 | + } |
| 133 | + |
| 134 | + private void applyAuthentication(Client clientProperties, ClientBuilder clientBuilder) { |
| 135 | + if (StringUtils.hasText(clientProperties.getAuthParams()) |
| 136 | + && !CollectionUtils.isEmpty(clientProperties.getAuthentication())) { |
| 137 | + throw new IllegalArgumentException( |
| 138 | + "Cannot set both spring.pulsar.client.authParams and spring.pulsar.client.authentication.*"); |
| 139 | + } |
| 140 | + var authPluginClass = clientProperties.getAuthPluginClassName(); |
| 141 | + if (StringUtils.hasText(authPluginClass)) { |
| 142 | + var authParams = clientProperties.getAuthParams(); |
| 143 | + if (clientProperties.getAuthentication() != null) { |
| 144 | + authParams = AuthParameterUtils.maybeConvertToEncodedParamString(clientProperties.getAuthentication()); |
| 145 | + } |
| 146 | + try { |
| 147 | + clientBuilder.authentication(authPluginClass, authParams); |
| 148 | + } |
| 149 | + catch (PulsarClientException.UnsupportedAuthenticationException ex) { |
| 150 | + throw new IllegalArgumentException("Unable to configure authentication: " + ex.getMessage(), ex); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + private InetSocketAddress parseSocketAddress(String address) { |
| 156 | + try { |
| 157 | + var uri = URI.create(address); |
| 158 | + return new InetSocketAddress(uri.getHost(), uri.getPort()); |
| 159 | + } |
| 160 | + catch (Exception e) { |
| 161 | + throw new IllegalArgumentException("Invalid address: " + e.getMessage(), e); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + protected void applyCustomizers(List<PulsarClientBuilderCustomizer> clientBuilderCustomizers, |
| 166 | + ClientBuilder clientBuilder) { |
| 167 | + LambdaSafe.callbacks(PulsarClientBuilderCustomizer.class, clientBuilderCustomizers, clientBuilder) |
| 168 | + .withLogger(PulsarClientBuilderConfigurer.class) |
| 169 | + .invoke((customizer) -> customizer.customize(clientBuilder)); |
| 170 | + } |
| 171 | + |
| 172 | +} |
0 commit comments