Skip to content

Commit

Permalink
Add ability to customize name of dataSource bean and databaseUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
mfvanek committed Dec 3, 2024
1 parent 395626b commit bfdd30d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import io.github.mfvanek.pg.connection.PgConnection;
import io.github.mfvanek.pg.connection.PgConnectionImpl;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
Expand All @@ -24,6 +24,7 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;

import javax.annotation.Nonnull;
import javax.sql.DataSource;

/**
Expand All @@ -42,15 +43,17 @@ public class DatabaseStructureHealthAutoConfiguration {
/**
* {@link PgConnection} bean.
*
* @param dataSource {@link DataSource} instance
* @param beanFactory {@link BeanFactory} instance
* @param databaseUrl connection string to database
* @return {@link PgConnection} instance
*/
@Bean
@ConditionalOnBean(name = "dataSource")
@ConditionalOnBean(DataSource.class)
@ConditionalOnMissingBean
public PgConnection pgConnection(@Qualifier("dataSource") final DataSource dataSource,
public PgConnection pgConnection(@Nonnull final BeanFactory beanFactory,
@Value("${spring.datasource.url:#{null}}") final String databaseUrl) {
// TODO
final DataSource dataSource = beanFactory.getBean("dataSource", DataSource.class);
return PgConnectionImpl.ofUrl(dataSource, databaseUrl);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/
public class DatabaseStructureHealthCondition extends SpringBootCondition {

// TODO use value from properties
private static final String PROPERTY_NAME = "spring.datasource.url";

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@

package io.github.mfvanek.pg.spring;

import io.github.mfvanek.pg.model.validation.Validators;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConstructorBinding;
import org.springframework.boot.context.properties.bind.DefaultValue;

import javax.annotation.concurrent.Immutable;

/**
* Represents properties for managing pg-index-health-test-starter configuration.
*
Expand All @@ -22,38 +25,86 @@
*/
@ConstructorBinding
@ConfigurationProperties(prefix = "pg.index.health.test")
@Immutable
public class DatabaseStructureHealthProperties {

/**
* Allows to manually disable starter even it presents on classpath.
* Indicates whether the starter is enabled, even if it is present on the classpath.
* This allows for manual control over autoconfiguration.
* <p>
* Default value: {@code true}.
* </p>
*/
private final boolean enabled;

/**
* Constructs a {@code DatabaseStructureHealthProperties} instance.
* The name of the datasource bean to use for health checks.
* <p>
* Default value: {@code "dataSource"}.
* </p>
*/
private final String datasourceBeanName;

/**
* The name of the datasource URL property used in the configuration.
* <p>
* Default value: {@code "spring.datasource.url"}.
* </p>
*/
private final String datasourceUrlPropertyName;

/**
* Constructs a new {@code DatabaseStructureHealthProperties} instance with the specified values.
*
* @param enabled enabled or disabled autoconfiguration
* @param enabled whether the autoconfiguration is enabled (default: {@code true})
* @param datasourceBeanName the name of the datasource bean (default: {@code "dataSource"}, must not be blank)
* @param datasourceUrlPropertyName the name of the datasource URL property (default: {@code "spring.datasource.url"}, must not be blank)
* @throws IllegalArgumentException if {@code datasourceBeanName} or {@code datasourceUrlPropertyName} is blank
*/
public DatabaseStructureHealthProperties(@DefaultValue("true") final boolean enabled) {
public DatabaseStructureHealthProperties(@DefaultValue("true") final boolean enabled,
@DefaultValue("dataSource") final String datasourceBeanName,
@DefaultValue("spring.datasource.url") final String datasourceUrlPropertyName) {
this.enabled = enabled;
this.datasourceBeanName = Validators.notBlank(datasourceBeanName, "datasourceBeanName");
this.datasourceUrlPropertyName = Validators.notBlank(datasourceUrlPropertyName, "datasourceUrlPropertyName");
}

/**
* Retrieves the state of autoconfiguration: enabled or disabled.
* Checks if the autoconfiguration is enabled.
*
* @return true if starter enabled otherwise false
* @return {@code true} if the starter is enabled; {@code false} otherwise
*/
public boolean isEnabled() {
return enabled;
}

/**
* Retrieves the name of the datasource bean to be used.
*
* @return the name of the datasource bean (default: {@code "dataSource"})
*/
public String getDatasourceBeanName() {
return datasourceBeanName;
}

/**
* Retrieves the name of the datasource URL property.
*
* @return the name of the datasource URL property (default: {@code "spring.datasource.url"})
*/
public String getDatasourceUrlPropertyName() {
return datasourceUrlPropertyName;
}

/**
* {@inheritDoc}
*/
@Override
public String toString() {
return DatabaseStructureHealthProperties.class.getSimpleName() + '{' +
"enabled=" + enabled +
", datasourceBeanName='" + datasourceBeanName + '\'' +
", datasourceUrlPropertyName='" + datasourceUrlPropertyName + '\'' +
'}';
}
}

0 comments on commit bfdd30d

Please sign in to comment.