Skip to content

Commit

Permalink
Merge pull request #563 from groldan/simplejndi_refactoring
Browse files Browse the repository at this point in the history
Workaround an issue where the JNDIInitializer would be called several times
  • Loading branch information
groldan authored Oct 24, 2024
2 parents 00a0597 + cee9348 commit e3ac725
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import org.geoserver.cloud.autoconfigure.geotools.GeoToolsHttpClientAutoConfiguration;
import org.geoserver.cloud.config.catalog.backend.core.CoreBackendConfiguration;
import org.geoserver.cloud.config.jndidatasource.JNDIDataSourceAutoConfiguration;
import org.geoserver.cloud.config.jndi.JNDIDataSourceConfiguration;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Import;
Expand All @@ -23,6 +23,6 @@
* @see CoreBackendConfiguration
*/
@AutoConfiguration(
after = {GeoToolsHttpClientAutoConfiguration.class, JNDIDataSourceAutoConfiguration.class})
after = {GeoToolsHttpClientAutoConfiguration.class, JNDIDataSourceConfiguration.class})
@Import(CoreBackendConfiguration.class)
public class GeoServerBackendAutoConfiguration {}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
package org.geoserver.cloud.autoconfigure.catalog.backend.pgconfig;

import org.geoserver.cloud.config.catalog.backend.pgconfig.PconfigDataSourceConfiguration;
import org.geoserver.cloud.config.jndidatasource.JNDIDataSourceAutoConfiguration;
import org.geoserver.cloud.config.jndi.JNDIDataSourceConfiguration;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Import;

/**
* @since 1.4
*/
@AutoConfiguration(after = JNDIDataSourceAutoConfiguration.class)
@AutoConfiguration(after = JNDIDataSourceConfiguration.class)
@ConditionalOnPgconfigBackendEnabled
@Import(PconfigDataSourceConfiguration.class)
public class PgconfigDataSourceAutoConfiguration {}
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ DataSource pgconfigDataSource(PgconfigBackendProperties configprops) {
Object jndiInitializerFallback() {
log.warn(
"""
jndiInitializer is not provided, beware a JNDI datasource \
definition for the pgconfig catalog backend won't work.
""");
jndiInitializer is not provided, beware a JNDI datasource \
definition for the pgconfig catalog backend won't work.
""");
return new Object();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import lombok.Getter;
import lombok.SneakyThrows;

import org.geoserver.cloud.autoconfigure.jndi.SimpleJNDIStaticContextInitializer;
import org.geoserver.cloud.config.catalog.backend.pgconfig.PgconfigDatabaseMigrations;
import org.geoserver.cloud.config.jndi.SimpleJNDIStaticContextInitializer;
import org.geoserver.cloud.config.jndidatasource.JNDIDataSourceAutoConfiguration;
import org.geoserver.cloud.config.jndi.JNDIDataSourceConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.AbstractApplicationContextRunner;
import org.springframework.jdbc.core.JdbcTemplate;
Expand Down Expand Up @@ -94,8 +94,7 @@ public void tearDown() {
runner
// enable simplejndi
.withInitializer(new SimpleJNDIStaticContextInitializer())
.withConfiguration(
AutoConfigurations.of(JNDIDataSourceAutoConfiguration.class))
.withConfiguration(AutoConfigurations.of(JNDIDataSourceConfiguration.class))
.withPropertyValues(
"geoserver.backend.pgconfig.enabled: true", //
// java:comp/env/jdbc/testdb config properties
Expand Down
5 changes: 5 additions & 0 deletions src/library/spring-boot-simplejndi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,10 @@
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* (c) 2022 Open Source Geospatial Foundation - all rights reserved This code is licensed under the
* GPL 2.0 license, available at the root application directory.
*/
package org.geoserver.cloud.autoconfigure.jndi;

import org.geoserver.cloud.config.jndi.JNDIDataSourceConfiguration;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.context.annotation.Import;
import org.springframework.core.Ordered;

/**
* @since 1.0
*/
@AutoConfiguration
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@Import(JNDIDataSourceConfiguration.class)
public class JNDIDataSourceAutoConfiguration {}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* (c) 2022 Open Source Geospatial Foundation - all rights reserved This code is licensed under the
* GPL 2.0 license, available at the root application directory.
*/
package org.geoserver.cloud.config.jndi;
package org.geoserver.cloud.autoconfigure.jndi;

import lombok.extern.slf4j.Slf4j;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
* (c) 2022 Open Source Geospatial Foundation - all rights reserved This code is licensed under the
* GPL 2.0 license, available at the root application directory.
*/
package org.geoserver.cloud.config.jndidatasource;
package org.geoserver.cloud.config.jndi;

import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* @since 1.0
*/
@AutoConfiguration
@Configuration
@EnableConfigurationProperties(JNDIDataSourcesConfigurationProperties.class)
public class JNDIDataSourceAutoConfiguration {
public class JNDIDataSourceConfiguration {
@Bean
JNDIInitializer jndiInitializer(JNDIDataSourcesConfigurationProperties config) {
return new JNDIInitializer(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* (c) 2022 Open Source Geospatial Foundation - all rights reserved This code is licensed under the
* GPL 2.0 license, available at the root application directory.
*/
package org.geoserver.cloud.config.jndidatasource;
package org.geoserver.cloud.config.jndi;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* (c) 2022 Open Source Geospatial Foundation - all rights reserved This code is licensed under the
* GPL 2.0 license, available at the root application directory.
*/
package org.geoserver.cloud.config.jndidatasource;
package org.geoserver.cloud.config.jndi;

import lombok.Data;
import lombok.EqualsAndHashCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* (c) 2022 Open Source Geospatial Foundation - all rights reserved This code is licensed under the
* GPL 2.0 license, available at the root application directory.
*/
package org.geoserver.cloud.config.jndidatasource;
package org.geoserver.cloud.config.jndi;

import com.zaxxer.hikari.HikariDataSource;

Expand All @@ -28,12 +28,17 @@ public class JNDIInitializer implements InitializingBean {

private JNDIDataSourcesConfigurationProperties config;

static boolean initialized = false;

JNDIInitializer(JNDIDataSourcesConfigurationProperties config) {
this.config = config;
}

@Override
public void afterPropertiesSet() throws Exception {
if (initialized)
throw new IllegalStateException("JNDI already initialized or failed. Giving up.");
initialized = true;

Map<String, JNDIDatasourceConfig> configs = config.getDatasources();

Expand All @@ -52,8 +57,12 @@ public void afterPropertiesSet() throws Exception {
props.setName(name);
}
});
configs.entrySet()
.forEach(e -> setUpDataSource(toJndiDatasourceName(e.getKey()), e.getValue()));
try {
configs.entrySet()
.forEach(e -> setUpDataSource(toJndiDatasourceName(e.getKey()), e.getValue()));
} catch (Exception e) {
throw new IllegalStateException(e);
}
}

String toJndiDatasourceName(String dsname) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Initializers
org.springframework.context.ApplicationContextInitializer=\
org.geoserver.cloud.config.jndi.SimpleJNDIStaticContextInitializer
org.geoserver.cloud.autoconfigure.jndi.SimpleJNDIStaticContextInitializer

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.geoserver.cloud.config.jndidatasource.JNDIDataSourceAutoConfiguration
org.geoserver.cloud.autoconfigure.jndi.JNDIDataSourceAutoConfiguration
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
* (c) 2022 Open Source Geospatial Foundation - all rights reserved This code is licensed under the
* GPL 2.0 license, available at the root application directory.
*/
package org.geoserver.cloud.config.jndidatasource;
package org.geoserver.cloud.autoconfigure.jndi;

import static org.assertj.core.api.Assertions.assertThat;

import com.zaxxer.hikari.HikariDataSource;

import org.geoserver.cloud.config.jndi.SimpleJNDIStaticContextInitializer;
import org.geoserver.cloud.config.jndi.JNDIDataSourceConfiguration;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
Expand All @@ -24,11 +24,10 @@ class JNDIDataSourceAutoConfigurationTest {
private ApplicationContextRunner runner =
new ApplicationContextRunner()
.withInitializer(new SimpleJNDIStaticContextInitializer())
.withConfiguration(
AutoConfigurations.of(JNDIDataSourceAutoConfiguration.class));
.withConfiguration(AutoConfigurations.of(JNDIDataSourceConfiguration.class));

@Test
void test() {
void testInitialContextLookup() {

runner.withPropertyValues( //
"jndi.datasources.ds1.url: jdbc:h2:mem:ds1", //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import static org.assertj.core.api.Assertions.assertThat;

import org.geoserver.cloud.autoconfigure.jndi.SimpleJNDIStaticContextInitializer;
import org.geoserver.cloud.jndi.SimpleNamingContext;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
Expand Down

0 comments on commit e3ac725

Please sign in to comment.