Skip to content

Commit

Permalink
Simplify composite (#73)
Browse files Browse the repository at this point in the history
* Simplify composite

* Extract common code to method

---------

Co-authored-by: Tim Yates <[email protected]>
  • Loading branch information
sdelamo and timyates authored Mar 3, 2023
1 parent 61f2e5e commit 085fafe
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,15 @@
*/
package io.micronaut.crac.resources;

import com.zaxxer.hikari.HikariDataSource;
import io.micronaut.context.annotation.EachBean;
import io.micronaut.context.annotation.Requires;
import io.micronaut.core.annotation.Experimental;
import io.micronaut.crac.CracConfiguration;
import io.micronaut.crac.CracEventPublisher;
import io.micronaut.crac.CracResourceRegistrar;
import io.micronaut.crac.OrderedResource;
import io.micronaut.crac.resources.datasources.HikariDataSourceResource;
import io.micronaut.crac.resources.datasources.UnknownDataSourceResource;
import io.micronaut.crac.resources.datasources.resolver.DataSourceResolver;
import io.micronaut.crac.resources.datasources.resolver.DataSourceResourceResolver;
import jakarta.inject.Inject;
import org.crac.Context;
import org.crac.Resource;
Expand All @@ -51,10 +49,10 @@ public class DataSourceResource implements OrderedResource {

private final CracEventPublisher eventPublisher;
private final Resource handler;
private final DataSourceResolver dataSourceResolver;
private final DataSourceResourceResolver dataSourceResolver;

/**
* @deprecated Use {@link #DataSourceResource(CracConfiguration, CracEventPublisher, DataSource, DataSourceResolver)} instead
* @deprecated Use {@link #DataSourceResource(CracConfiguration, CracEventPublisher, DataSource, DataSourceResourceResolver)} instead
*
* @param configuration
* @param eventPublisher
Expand All @@ -66,34 +64,28 @@ public DataSourceResource(
CracEventPublisher eventPublisher,
DataSource dataSource
) {
this(configuration, eventPublisher, dataSource, dataSource1 -> Optional.empty());
this(configuration, eventPublisher, dataSource, (ds, conf) -> Optional.empty());
}

@Inject
public DataSourceResource(
CracConfiguration configuration,
CracEventPublisher eventPublisher,
DataSource dataSource,
DataSourceResolver dataSourceResolver
DataSourceResourceResolver dataSourceResolver
) {
this.eventPublisher = eventPublisher;
this.dataSourceResolver = dataSourceResolver;
this.handler = getHandler(dataSource, configuration);
}

private Resource getHandler(DataSource dataSource, CracConfiguration configuration) {
Optional<DataSource> resolvedDataSourceOptional = dataSourceResolver.resolve(dataSource);
if (resolvedDataSourceOptional.isPresent()) {
if (LOG.isDebugEnabled()) {
LOG.debug("HikariDataSource detected, using HikariDataSourceResource");
return dataSourceResolver.resolve(dataSource, configuration).orElseGet(() -> {
if (LOG.isWarnEnabled()) {
LOG.warn("DataSource {} is not currently supported by CRaC", dataSource.getClass().getName());
}
return new HikariDataSourceResource((HikariDataSource) resolvedDataSourceOptional.get(), configuration);
}

if (LOG.isWarnEnabled()) {
LOG.warn("DataSource {} is not currently supported by CRaC", dataSource.getClass().getName());
}
return new UnknownDataSourceResource(dataSource);
return new UnknownDataSourceResource(dataSource);
});
}

@Override
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import io.micronaut.context.annotation.Primary;
import io.micronaut.core.annotation.Experimental;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.crac.CracConfiguration;
import jakarta.inject.Singleton;
import org.crac.Resource;

import javax.sql.DataSource;
import java.util.List;
Expand All @@ -33,21 +35,21 @@
@Primary
@Singleton
@Experimental
public class CompositeDataSourceResolver implements DataSourceResolver {
public class CompositeDataSourceResolver implements DataSourceResourceResolver {

private final List<DataSourceResolver> resolvers;
private final List<DataSourceResourceResolver> resolvers;

public CompositeDataSourceResolver(List<DataSourceResolver> resolvers) {
public CompositeDataSourceResolver(List<DataSourceResourceResolver> resolvers) {
this.resolvers = resolvers;
}

@Override
@NonNull
public Optional<DataSource> resolve(@NonNull DataSource dataSource) {
return resolvers.stream().reduce(
Optional.of(dataSource),
(ds, r) -> ds.flatMap(r::resolve),
(ds1, ds2) -> ds1
);
public Optional<Resource> resolve(@NonNull DataSource dataSource, @NonNull CracConfiguration configuration) {
return resolvers.stream()
.map(resolver -> resolver.resolve(dataSource, configuration))
.filter(Optional::isPresent)
.map(Optional::get)
.findFirst();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.order.Ordered;
import io.micronaut.crac.CracConfiguration;
import org.crac.Resource;

import javax.sql.DataSource;
import java.util.Optional;
Expand All @@ -28,8 +30,8 @@
* @since 1.2.0
*/
@FunctionalInterface
public interface DataSourceResolver extends Ordered {
public interface DataSourceResourceResolver extends Ordered {

@NonNull
Optional<DataSource> resolve(@NonNull DataSource dataSource);
Optional<Resource> resolve(@NonNull DataSource dataSource, @NonNull CracConfiguration configuration);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,52 @@

import com.zaxxer.hikari.HikariDataSource;
import io.micronaut.context.annotation.Requires;
import io.micronaut.core.annotation.Experimental;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.crac.CracConfiguration;
import io.micronaut.crac.resources.datasources.HikariDataSourceResource;
import io.micronaut.transaction.jdbc.DelegatingDataSource;
import jakarta.inject.Singleton;
import org.crac.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.sql.DataSource;
import java.util.Optional;

/**
* Filter the DataSource if it is not a Hikari DataSource.
* If the data Source is of type {@link DelegatingDataSource}, extract the underlying data source.
*
* @author Tim Yates
* @author Sergio del Amo
* @since 1.2.0
*/
@Singleton
@Requires(classes = HikariDataSource.class)
public class HikariDataSourceResolver implements DataSourceResolver {
@Experimental
@Requires(classes = HikariDataSource.class, missing = { DelegatingDataSource.class })
public class HikariDataSourceResolver implements DataSourceResourceResolver {

static final int ORDER = 1;

private static final Logger LOG = LoggerFactory.getLogger(HikariDataSourceResolver.class);

@Override
@NonNull
public Optional<DataSource> resolve(@NonNull DataSource dataSource) {
if (dataSource instanceof HikariDataSource) {
return Optional.of(dataSource);
}
return Optional.empty();
public Optional<Resource> resolve(@NonNull DataSource dataSource, @NonNull CracConfiguration configuration) {
return resourceForNonDelegatingDataSource(dataSource, configuration);
}

static Optional<Resource> resourceForNonDelegatingDataSource(DataSource dataSource, CracConfiguration configuration) {
return dataSource instanceof HikariDataSource ?
Optional.of(dataSource).map(ds -> {
if (LOG.isDebugEnabled()) {
LOG.debug("HikariDataSource detected, using HikariDataSourceResource");
}
return new HikariDataSourceResource((HikariDataSource) ds, configuration);
}) : Optional.empty();
}

@Override
public int getOrder() {
return DelegatingDataSourceResolver.ORDER + 1;
return ORDER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@
*/
package io.micronaut.crac.resources.datasources.resolver;

import com.zaxxer.hikari.HikariDataSource;
import io.micronaut.context.annotation.Requires;
import io.micronaut.core.annotation.Experimental;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.crac.CracConfiguration;
import io.micronaut.transaction.jdbc.DelegatingDataSource;
import jakarta.inject.Singleton;
import org.crac.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -33,24 +37,25 @@
*/
@Singleton
@Experimental
@Requires(classes = DelegatingDataSource.class)
public class DelegatingDataSourceResolver implements DataSourceResolver {
@Requires(classes = { DelegatingDataSource.class, HikariDataSource.class })
public class HikariDelegatingDataSourceResolver implements DataSourceResourceResolver {

static final int ORDER = 1;

private static final Logger LOG = LoggerFactory.getLogger(DelegatingDataSourceResolver.class);
private static final Logger LOG = LoggerFactory.getLogger(HikariDelegatingDataSourceResolver.class);

@Override
public Optional<DataSource> resolve(DataSource dataSource) {
@NonNull
public Optional<Resource> resolve(@NonNull DataSource dataSource, @NonNull CracConfiguration configuration) {
if (dataSource instanceof DelegatingDataSource) {
if (LOG.isDebugEnabled()) {
LOG.debug("DelegatingDataSource detected, unwrapping");
}
DataSource result = DelegatingDataSource.unwrapDataSource(dataSource);
DataSource result = DelegatingDataSource.unwrapDataSource(dataSource);
if (LOG.isDebugEnabled()) {
LOG.debug("Unwrapped DataSource is {}", result.getClass().getName());
}
return Optional.of(result);
return HikariDataSourceResolver.resourceForNonDelegatingDataSource(result, configuration);
}
return Optional.empty();
}
Expand Down

0 comments on commit 085fafe

Please sign in to comment.