|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2021 the original author or authors. |
| 2 | + * Copyright 2012-2022 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
19 | 19 | import java.io.IOException;
|
20 | 20 | import java.security.NoSuchAlgorithmException;
|
21 | 21 | import java.time.Duration;
|
| 22 | +import java.util.Collections; |
22 | 23 | import java.util.LinkedHashMap;
|
23 | 24 | import java.util.List;
|
24 | 25 | import java.util.Map;
|
|
63 | 64 | * @author Eddú Meléndez
|
64 | 65 | * @author Stephane Nicoll
|
65 | 66 | * @author Steffen F. Qvistgaard
|
| 67 | + * @author Ittay Stern |
66 | 68 | * @since 1.3.0
|
67 | 69 | */
|
68 | 70 | @Configuration(proxyBeanMethods = false)
|
69 | 71 | @ConditionalOnClass({ CqlSession.class })
|
70 | 72 | @EnableConfigurationProperties(CassandraProperties.class)
|
71 | 73 | public class CassandraAutoConfiguration {
|
72 | 74 |
|
| 75 | + private static final Config SPRING_BOOT_DEFAULTS; |
| 76 | + static { |
| 77 | + CassandraDriverOptions options = new CassandraDriverOptions(); |
| 78 | + options.add(DefaultDriverOption.CONTACT_POINTS, Collections.singletonList("127.0.0.1:9042")); |
| 79 | + options.add(DefaultDriverOption.PROTOCOL_COMPRESSION, "none"); |
| 80 | + options.add(DefaultDriverOption.CONTROL_CONNECTION_TIMEOUT, (int) Duration.ofSeconds(5).toMillis()); |
| 81 | + SPRING_BOOT_DEFAULTS = options.build(); |
| 82 | + } |
| 83 | + |
73 | 84 | @Bean
|
74 | 85 | @ConditionalOnMissingBean
|
75 | 86 | @Lazy
|
@@ -118,43 +129,41 @@ public DriverConfigLoader cassandraDriverConfigLoader(CassandraProperties proper
|
118 | 129 | }
|
119 | 130 |
|
120 | 131 | private Config cassandraConfiguration(CassandraProperties properties) {
|
121 |
| - Config config = mapConfig(properties); |
122 |
| - Resource configFile = properties.getConfig(); |
123 |
| - return (configFile != null) ? applyDefaultFallback(config.withFallback(loadConfig(configFile))) |
124 |
| - : applyDefaultFallback(config); |
125 |
| - } |
126 |
| - |
127 |
| - private Config applyDefaultFallback(Config config) { |
128 | 132 | ConfigFactory.invalidateCaches();
|
129 |
| - return ConfigFactory.defaultOverrides().withFallback(config) |
130 |
| - .withFallback(mapConfig(CassandraProperties.defaults())).withFallback(ConfigFactory.defaultReference()) |
131 |
| - .resolve(); |
| 133 | + Config config = ConfigFactory.defaultOverrides(); |
| 134 | + config = config.withFallback(mapConfig(properties)); |
| 135 | + if (properties.getConfig() != null) { |
| 136 | + config = config.withFallback(loadConfig(properties.getConfig())); |
| 137 | + } |
| 138 | + config = config.withFallback(SPRING_BOOT_DEFAULTS); |
| 139 | + config = config.withFallback(ConfigFactory.defaultReference()); |
| 140 | + return config.resolve(); |
132 | 141 | }
|
133 | 142 |
|
134 |
| - private Config loadConfig(Resource config) { |
| 143 | + private Config loadConfig(Resource resource) { |
135 | 144 | try {
|
136 |
| - return ConfigFactory.parseURL(config.getURL()); |
| 145 | + return ConfigFactory.parseURL(resource.getURL()); |
137 | 146 | }
|
138 | 147 | catch (IOException ex) {
|
139 |
| - throw new IllegalStateException("Failed to load cassandra configuration from " + config, ex); |
| 148 | + throw new IllegalStateException("Failed to load cassandra configuration from " + resource, ex); |
140 | 149 | }
|
141 | 150 | }
|
142 | 151 |
|
143 | 152 | private Config mapConfig(CassandraProperties properties) {
|
144 | 153 | CassandraDriverOptions options = new CassandraDriverOptions();
|
145 |
| - PropertyMapper map = PropertyMapper.get(); |
| 154 | + PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull(); |
146 | 155 | map.from(properties.getSessionName()).whenHasText()
|
147 | 156 | .to((sessionName) -> options.add(DefaultDriverOption.SESSION_NAME, sessionName));
|
148 |
| - map.from(properties::getUsername).whenNonNull() |
| 157 | + map.from(properties::getUsername) |
149 | 158 | .to((username) -> options.add(DefaultDriverOption.AUTH_PROVIDER_USER_NAME, username)
|
150 | 159 | .add(DefaultDriverOption.AUTH_PROVIDER_PASSWORD, properties.getPassword()));
|
151 |
| - map.from(properties::getCompression).whenNonNull() |
| 160 | + map.from(properties::getCompression) |
152 | 161 | .to((compression) -> options.add(DefaultDriverOption.PROTOCOL_COMPRESSION, compression));
|
153 | 162 | mapConnectionOptions(properties, options);
|
154 | 163 | mapPoolingOptions(properties, options);
|
155 | 164 | mapRequestOptions(properties, options);
|
156 | 165 | mapControlConnectionOptions(properties, options);
|
157 |
| - map.from(mapContactPoints(properties)).whenNonNull() |
| 166 | + map.from(mapContactPoints(properties)) |
158 | 167 | .to((contactPoints) -> options.add(DefaultDriverOption.CONTACT_POINTS, contactPoints));
|
159 | 168 | map.from(properties.getLocalDatacenter()).whenHasText().to(
|
160 | 169 | (localDatacenter) -> options.add(DefaultDriverOption.LOAD_BALANCING_LOCAL_DATACENTER, localDatacenter));
|
@@ -211,9 +220,12 @@ private void mapControlConnectionOptions(CassandraProperties properties, Cassand
|
211 | 220 | }
|
212 | 221 |
|
213 | 222 | private List<String> mapContactPoints(CassandraProperties properties) {
|
214 |
| - List<String> contactPoints = properties.getContactPoints(); |
215 |
| - return (contactPoints == null) ? null : contactPoints.stream() |
216 |
| - .map((candidate) -> formatContactPoint(candidate, properties.getPort())).collect(Collectors.toList()); |
| 223 | + if (properties.getContactPoints() != null) { |
| 224 | + return properties.getContactPoints().stream() |
| 225 | + .map((candidate) -> formatContactPoint(candidate, properties.getPort())) |
| 226 | + .collect(Collectors.toList()); |
| 227 | + } |
| 228 | + return null; |
217 | 229 | }
|
218 | 230 |
|
219 | 231 | private String formatContactPoint(String candidate, int port) {
|
|
0 commit comments