Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Reformat code. Lazily obtain the keyspace name from CqlSession in SimpleUserTypeResolver.

See #380
Original pull request: #1485
  • Loading branch information
mp911de committed Mar 18, 2024
1 parent f31c877 commit 2c6b029
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public void afterPropertiesSet() throws Exception {

super.afterPropertiesSet();

if(!shouldRunSchemaAction()) {
if (!shouldRunSchemaAction()) {
return;
}

Expand Down Expand Up @@ -173,7 +173,7 @@ protected SessionFactory createInstance() {
@SuppressWarnings("all")
public void destroy() throws Exception {

if(!shouldRunSchemaAction()) {
if (!shouldRunSchemaAction()) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,15 @@ public class MappingCassandraConverter extends AbstractCassandraConverter
* Create a new {@link MappingCassandraConverter} with a {@link CassandraMappingContext}.
*/
public MappingCassandraConverter() {
this(newDefaultMappingContext(new CassandraCustomConversions(Collections.emptyList())));
this(newDefaultMappingContext());
}

/**
* Create a new {@link MappingCassandraConverter} with the given {@link CassandraMappingContext}.
*
* @param mappingContext must not be {@literal null}.
*/
@SuppressWarnings({ "deprecation" })
public MappingCassandraConverter(CassandraMappingContext mappingContext) {

super(newConversionService());
Expand Down Expand Up @@ -171,16 +172,14 @@ private static ConversionService newConversionService() {
/**
* 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;
Expand Down Expand Up @@ -263,7 +262,7 @@ public void setCodecRegistry(Supplier<CodecRegistry> codecRegistry) {
public CodecRegistry getCodecRegistry() {

CodecRegistry registry = this.codecRegistry != null ? this.codecRegistry.get() : null;
return registry != null ? registry : getMappingContext().getCodecRegistry();
return registry != null ? registry : getMappingContext().getCodecRegistry();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.function.Supplier;

import org.springframework.data.util.Lazy;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

Expand All @@ -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}.
Expand All @@ -49,7 +50,7 @@ public SimpleUserTypeResolver(CqlSession session) {
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")));
}

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

this.metadataSupplier = session::getMetadata;
this.keyspaceName = keyspaceName;
this.keyspaceName = Lazy.of(keyspaceName);
}

/**
Expand All @@ -81,13 +82,13 @@ public SimpleUserTypeResolver(Supplier<Metadata> metadataSupplier, CqlIdentifier
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,7 +36,11 @@
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 {
Expand All @@ -57,18 +58,15 @@ void shouldDelayCqlSessionBeanInitializationTillFirstUsage() {
@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);
Expand All @@ -85,14 +83,13 @@ public CassandraMappingContext cassandraMappingContext() {
}

@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;
}

Expand Down

0 comments on commit 2c6b029

Please sign in to comment.