Polishing.
Reformat code. Lazily obtain the keyspace name from CqlSession in SimpleUserTypeResolver. See #380 Original pull request: #1485
This commit is contained in:
@@ -145,7 +145,7 @@ public class SessionFactoryFactoryBean extends AbstractFactoryBean<SessionFactor
|
||||
|
||||
super.afterPropertiesSet();
|
||||
|
||||
if(!shouldRunSchemaAction()) {
|
||||
if (!shouldRunSchemaAction()) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -173,7 +173,7 @@ public class SessionFactoryFactoryBean extends AbstractFactoryBean<SessionFactor
|
||||
@SuppressWarnings("all")
|
||||
public void destroy() throws Exception {
|
||||
|
||||
if(!shouldRunSchemaAction()) {
|
||||
if (!shouldRunSchemaAction()) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -126,7 +126,7 @@ public class MappingCassandraConverter extends AbstractCassandraConverter
|
||||
* Create a new {@link MappingCassandraConverter} with a {@link CassandraMappingContext}.
|
||||
*/
|
||||
public MappingCassandraConverter() {
|
||||
this(newDefaultMappingContext(new CassandraCustomConversions(Collections.emptyList())));
|
||||
this(newDefaultMappingContext());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -134,6 +134,7 @@ public class MappingCassandraConverter extends AbstractCassandraConverter
|
||||
*
|
||||
* @param mappingContext must not be {@literal null}.
|
||||
*/
|
||||
@SuppressWarnings({ "deprecation" })
|
||||
public MappingCassandraConverter(CassandraMappingContext mappingContext) {
|
||||
|
||||
super(newConversionService());
|
||||
@@ -171,16 +172,14 @@ public class MappingCassandraConverter extends AbstractCassandraConverter
|
||||
/**
|
||||
* Constructs a new instance of a {@link MappingContext} for Cassandra.
|
||||
*
|
||||
* @param conversions {@link CassandraCustomConversions} object encapsulating complex type conversion logic.
|
||||
* @return a new {@link CassandraMappingContext}.
|
||||
* @see org.springframework.data.cassandra.core.mapping.CassandraMappingContext
|
||||
* @see org.springframework.data.mapping.context.MappingContext
|
||||
*/
|
||||
private static CassandraMappingContext newDefaultMappingContext(CassandraCustomConversions conversions) {
|
||||
@SuppressWarnings({ "deprecation" })
|
||||
private static CassandraMappingContext newDefaultMappingContext() {
|
||||
|
||||
CassandraMappingContext mappingContext = new CassandraMappingContext();
|
||||
|
||||
mappingContext.setCustomConversions(conversions);
|
||||
mappingContext.setCustomConversions(new CassandraCustomConversions(Collections.emptyList()));
|
||||
mappingContext.afterPropertiesSet();
|
||||
|
||||
return mappingContext;
|
||||
@@ -263,7 +262,7 @@ public class MappingCassandraConverter extends AbstractCassandraConverter
|
||||
public CodecRegistry getCodecRegistry() {
|
||||
|
||||
CodecRegistry registry = this.codecRegistry != null ? this.codecRegistry.get() : null;
|
||||
return registry != null ? registry : getMappingContext().getCodecRegistry();
|
||||
return registry != null ? registry : getMappingContext().getCodecRegistry();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -17,6 +17,7 @@ package org.springframework.data.cassandra.core.mapping;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.springframework.data.util.Lazy;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
@@ -36,7 +37,7 @@ public class SimpleUserTypeResolver implements UserTypeResolver {
|
||||
|
||||
private final Supplier<Metadata> metadataSupplier;
|
||||
|
||||
private final CqlIdentifier keyspaceName;
|
||||
private final Supplier<CqlIdentifier> keyspaceName;
|
||||
|
||||
/**
|
||||
* Create a new {@link SimpleUserTypeResolver}.
|
||||
@@ -49,7 +50,7 @@ public class SimpleUserTypeResolver implements UserTypeResolver {
|
||||
Assert.notNull(session, "Session must not be null");
|
||||
|
||||
this.metadataSupplier = session::getMetadata;
|
||||
this.keyspaceName = session.getKeyspace().orElse(CqlIdentifier.fromCql("system"));
|
||||
this.keyspaceName = Lazy.of(() -> session.getKeyspace().orElse(CqlIdentifier.fromCql("system")));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -65,7 +66,7 @@ public class SimpleUserTypeResolver implements UserTypeResolver {
|
||||
Assert.notNull(keyspaceName, "Keyspace must not be null");
|
||||
|
||||
this.metadataSupplier = session::getMetadata;
|
||||
this.keyspaceName = keyspaceName;
|
||||
this.keyspaceName = Lazy.of(keyspaceName);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -81,13 +82,13 @@ public class SimpleUserTypeResolver implements UserTypeResolver {
|
||||
Assert.notNull(keyspaceName, "Keyspace must not be null");
|
||||
|
||||
this.metadataSupplier = metadataSupplier;
|
||||
this.keyspaceName = keyspaceName;
|
||||
this.keyspaceName = Lazy.of(keyspaceName);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public UserDefinedType resolveType(CqlIdentifier typeName) {
|
||||
return metadataSupplier.get().getKeyspace(keyspaceName) //
|
||||
return metadataSupplier.get().getKeyspace(keyspaceName.get()) //
|
||||
.flatMap(it -> it.getUserDefinedType(typeName)) //
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
@@ -16,19 +16,16 @@
|
||||
package org.springframework.data.cassandra.config;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.Collections;
|
||||
|
||||
import com.datastax.oss.driver.api.core.CqlIdentifier;
|
||||
import com.datastax.oss.driver.api.core.CqlSession;
|
||||
import com.datastax.oss.driver.internal.core.metadata.DefaultEndPoint;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.data.cassandra.SessionFactory;
|
||||
import org.springframework.data.cassandra.core.CassandraTemplate;
|
||||
@@ -39,7 +36,11 @@ import org.springframework.data.cassandra.core.convert.MappingCassandraConverter
|
||||
import org.springframework.data.cassandra.core.mapping.CassandraMappingContext;
|
||||
import org.springframework.data.cassandra.core.mapping.SimpleUserTypeResolver;
|
||||
|
||||
import com.datastax.oss.driver.api.core.CqlSession;
|
||||
|
||||
/**
|
||||
* Test for a lazily initialized session to assert no access to the Session.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
class LazyStartupConfigurationTest {
|
||||
@@ -57,18 +58,15 @@ class LazyStartupConfigurationTest {
|
||||
@Configuration
|
||||
static class LazyStartupConfig {
|
||||
|
||||
@Lazy
|
||||
@Bean
|
||||
CqlSession cqlSession() {
|
||||
|
||||
return CqlSession.builder()
|
||||
.addContactEndPoint(new DefaultEndPoint(InetSocketAddress.createUnresolved("127.0.0.2", 9042)))
|
||||
.withKeyspace("system")
|
||||
.build();
|
||||
return mock(CqlSession.class, invocation -> {
|
||||
throw new BeanCreationException("I am expected");
|
||||
});
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SessionFactoryFactoryBean cassandraSessionFactory(CassandraConverter converter, @Lazy CqlSession cqlSession) {
|
||||
public SessionFactoryFactoryBean cassandraSessionFactory(CassandraConverter converter, CqlSession cqlSession) {
|
||||
|
||||
SessionFactoryFactoryBean session = new SessionFactoryFactoryBean();
|
||||
session.setSession(cqlSession);
|
||||
@@ -85,14 +83,13 @@ class LazyStartupConfigurationTest {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CassandraConverter cassandraConverter(CassandraMappingContext mappingContext, @Lazy CqlSession cqlSession) {
|
||||
public CassandraConverter cassandraConverter(CassandraMappingContext mappingContext, CqlSession cqlSession) {
|
||||
|
||||
MappingCassandraConverter converter = new MappingCassandraConverter(mappingContext);
|
||||
converter.setCodecRegistry(() -> cqlSession.getContext().getCodecRegistry());
|
||||
converter.setCustomConversions(mappingContext.getCustomConversions());
|
||||
|
||||
CqlIdentifier keyspace = CqlIdentifier.fromCql("system");
|
||||
converter.setUserTypeResolver(new SimpleUserTypeResolver(cqlSession::getMetadata, keyspace));
|
||||
converter.setUserTypeResolver(new SimpleUserTypeResolver(cqlSession));
|
||||
return converter;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user