Skip to content

Commit 2c6b029

Browse files
committed
Polishing.
Reformat code. Lazily obtain the keyspace name from CqlSession in SimpleUserTypeResolver. See #380 Original pull request: #1485
1 parent f31c877 commit 2c6b029

File tree

4 files changed

+26
-29
lines changed

4 files changed

+26
-29
lines changed

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/SessionFactoryFactoryBean.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public void afterPropertiesSet() throws Exception {
145145

146146
super.afterPropertiesSet();
147147

148-
if(!shouldRunSchemaAction()) {
148+
if (!shouldRunSchemaAction()) {
149149
return;
150150
}
151151

@@ -173,7 +173,7 @@ protected SessionFactory createInstance() {
173173
@SuppressWarnings("all")
174174
public void destroy() throws Exception {
175175

176-
if(!shouldRunSchemaAction()) {
176+
if (!shouldRunSchemaAction()) {
177177
return;
178178
}
179179

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/convert/MappingCassandraConverter.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,15 @@ public class MappingCassandraConverter extends AbstractCassandraConverter
126126
* Create a new {@link MappingCassandraConverter} with a {@link CassandraMappingContext}.
127127
*/
128128
public MappingCassandraConverter() {
129-
this(newDefaultMappingContext(new CassandraCustomConversions(Collections.emptyList())));
129+
this(newDefaultMappingContext());
130130
}
131131

132132
/**
133133
* Create a new {@link MappingCassandraConverter} with the given {@link CassandraMappingContext}.
134134
*
135135
* @param mappingContext must not be {@literal null}.
136136
*/
137+
@SuppressWarnings({ "deprecation" })
137138
public MappingCassandraConverter(CassandraMappingContext mappingContext) {
138139

139140
super(newConversionService());
@@ -171,16 +172,14 @@ private static ConversionService newConversionService() {
171172
/**
172173
* Constructs a new instance of a {@link MappingContext} for Cassandra.
173174
*
174-
* @param conversions {@link CassandraCustomConversions} object encapsulating complex type conversion logic.
175175
* @return a new {@link CassandraMappingContext}.
176-
* @see org.springframework.data.cassandra.core.mapping.CassandraMappingContext
177-
* @see org.springframework.data.mapping.context.MappingContext
178176
*/
179-
private static CassandraMappingContext newDefaultMappingContext(CassandraCustomConversions conversions) {
177+
@SuppressWarnings({ "deprecation" })
178+
private static CassandraMappingContext newDefaultMappingContext() {
180179

181180
CassandraMappingContext mappingContext = new CassandraMappingContext();
182181

183-
mappingContext.setCustomConversions(conversions);
182+
mappingContext.setCustomConversions(new CassandraCustomConversions(Collections.emptyList()));
184183
mappingContext.afterPropertiesSet();
185184

186185
return mappingContext;
@@ -263,7 +262,7 @@ public void setCodecRegistry(Supplier<CodecRegistry> codecRegistry) {
263262
public CodecRegistry getCodecRegistry() {
264263

265264
CodecRegistry registry = this.codecRegistry != null ? this.codecRegistry.get() : null;
266-
return registry != null ? registry : getMappingContext().getCodecRegistry();
265+
return registry != null ? registry : getMappingContext().getCodecRegistry();
267266
}
268267

269268
/**

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/mapping/SimpleUserTypeResolver.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.util.function.Supplier;
1919

20+
import org.springframework.data.util.Lazy;
2021
import org.springframework.lang.Nullable;
2122
import org.springframework.util.Assert;
2223

@@ -36,7 +37,7 @@ public class SimpleUserTypeResolver implements UserTypeResolver {
3637

3738
private final Supplier<Metadata> metadataSupplier;
3839

39-
private final CqlIdentifier keyspaceName;
40+
private final Supplier<CqlIdentifier> keyspaceName;
4041

4142
/**
4243
* Create a new {@link SimpleUserTypeResolver}.
@@ -49,7 +50,7 @@ public SimpleUserTypeResolver(CqlSession session) {
4950
Assert.notNull(session, "Session must not be null");
5051

5152
this.metadataSupplier = session::getMetadata;
52-
this.keyspaceName = session.getKeyspace().orElse(CqlIdentifier.fromCql("system"));
53+
this.keyspaceName = Lazy.of(() -> session.getKeyspace().orElse(CqlIdentifier.fromCql("system")));
5354
}
5455

5556
/**
@@ -65,7 +66,7 @@ public SimpleUserTypeResolver(CqlSession session, CqlIdentifier keyspaceName) {
6566
Assert.notNull(keyspaceName, "Keyspace must not be null");
6667

6768
this.metadataSupplier = session::getMetadata;
68-
this.keyspaceName = keyspaceName;
69+
this.keyspaceName = Lazy.of(keyspaceName);
6970
}
7071

7172
/**
@@ -81,13 +82,13 @@ public SimpleUserTypeResolver(Supplier<Metadata> metadataSupplier, CqlIdentifier
8182
Assert.notNull(keyspaceName, "Keyspace must not be null");
8283

8384
this.metadataSupplier = metadataSupplier;
84-
this.keyspaceName = keyspaceName;
85+
this.keyspaceName = Lazy.of(keyspaceName);
8586
}
8687

8788
@Nullable
8889
@Override
8990
public UserDefinedType resolveType(CqlIdentifier typeName) {
90-
return metadataSupplier.get().getKeyspace(keyspaceName) //
91+
return metadataSupplier.get().getKeyspace(keyspaceName.get()) //
9192
.flatMap(it -> it.getUserDefinedType(typeName)) //
9293
.orElse(null);
9394
}

spring-data-cassandra/src/test/java/org/springframework/data/cassandra/config/LazyStartupConfigurationTest.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,16 @@
1616
package org.springframework.data.cassandra.config;
1717

1818
import static org.assertj.core.api.Assertions.*;
19+
import static org.mockito.Mockito.*;
1920

20-
import java.net.InetSocketAddress;
2121
import java.util.Collections;
2222

23-
import com.datastax.oss.driver.api.core.CqlIdentifier;
24-
import com.datastax.oss.driver.api.core.CqlSession;
25-
import com.datastax.oss.driver.internal.core.metadata.DefaultEndPoint;
2623
import org.junit.jupiter.api.Test;
24+
2725
import org.springframework.beans.factory.BeanCreationException;
2826
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2927
import org.springframework.context.annotation.Bean;
3028
import org.springframework.context.annotation.Configuration;
31-
import org.springframework.context.annotation.Lazy;
3229
import org.springframework.context.support.GenericApplicationContext;
3330
import org.springframework.data.cassandra.SessionFactory;
3431
import org.springframework.data.cassandra.core.CassandraTemplate;
@@ -39,7 +36,11 @@
3936
import org.springframework.data.cassandra.core.mapping.CassandraMappingContext;
4037
import org.springframework.data.cassandra.core.mapping.SimpleUserTypeResolver;
4138

39+
import com.datastax.oss.driver.api.core.CqlSession;
40+
4241
/**
42+
* Test for a lazily initialized session to assert no access to the Session.
43+
*
4344
* @author Christoph Strobl
4445
*/
4546
class LazyStartupConfigurationTest {
@@ -57,18 +58,15 @@ void shouldDelayCqlSessionBeanInitializationTillFirstUsage() {
5758
@Configuration
5859
static class LazyStartupConfig {
5960

60-
@Lazy
6161
@Bean
6262
CqlSession cqlSession() {
63-
64-
return CqlSession.builder()
65-
.addContactEndPoint(new DefaultEndPoint(InetSocketAddress.createUnresolved("127.0.0.2", 9042)))
66-
.withKeyspace("system")
67-
.build();
63+
return mock(CqlSession.class, invocation -> {
64+
throw new BeanCreationException("I am expected");
65+
});
6866
}
6967

7068
@Bean
71-
public SessionFactoryFactoryBean cassandraSessionFactory(CassandraConverter converter, @Lazy CqlSession cqlSession) {
69+
public SessionFactoryFactoryBean cassandraSessionFactory(CassandraConverter converter, CqlSession cqlSession) {
7270

7371
SessionFactoryFactoryBean session = new SessionFactoryFactoryBean();
7472
session.setSession(cqlSession);
@@ -85,14 +83,13 @@ public CassandraMappingContext cassandraMappingContext() {
8583
}
8684

8785
@Bean
88-
public CassandraConverter cassandraConverter(CassandraMappingContext mappingContext, @Lazy CqlSession cqlSession) {
86+
public CassandraConverter cassandraConverter(CassandraMappingContext mappingContext, CqlSession cqlSession) {
8987

9088
MappingCassandraConverter converter = new MappingCassandraConverter(mappingContext);
9189
converter.setCodecRegistry(() -> cqlSession.getContext().getCodecRegistry());
9290
converter.setCustomConversions(mappingContext.getCustomConversions());
9391

94-
CqlIdentifier keyspace = CqlIdentifier.fromCql("system");
95-
converter.setUserTypeResolver(new SimpleUserTypeResolver(cqlSession::getMetadata, keyspace));
92+
converter.setUserTypeResolver(new SimpleUserTypeResolver(cqlSession));
9693
return converter;
9794
}
9895

0 commit comments

Comments
 (0)