Skip to content
Snippets Groups Projects
Commit 5a035645 authored by Deepa Kumari's avatar Deepa Kumari
Browse files

moved configuration to osm

parent 93c5cb3a
No related branches found
No related tags found
1 merge request!452Introduce Partition-Core-Plus
Checking pipeline status
......@@ -35,7 +35,7 @@
<dependency>
<groupId>org.opengroup.osdu</groupId>
<artifactId>os-osm.postgres</artifactId>
<version>0.24.0-rc6</version>
<version>0.24.0-rc7</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
......
/*
* Copyright 2020-2021 Google LLC
* Copyright 2020-2021 EPAM Systems, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.opengroup.osdu.partition.coreplus.config;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
@Primary
@Configuration
@ConditionalOnProperty(name = "osmDriver", havingValue = "postgres")
@ConfigurationProperties(prefix = "osm.postgres")
@Getter
@Setter
public class PostgresOsmConfiguration {
private String url;
private String username;
private String password;
private Integer maximumPoolSize = 40;
private Integer minimumIdle = 0;
private Integer idleTimeout = 30000;
private Integer maxLifetime = 1800000;
private Integer connectionTimeout = 30000;
}
/*
* Copyright 2020-2021 Google LLC
* Copyright 2020-2021 EPAM Systems, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.opengroup.osdu.partition.coreplus.osm.config.resolver;
import com.zaxxer.hikari.HikariDataSource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.opengroup.osdu.core.osm.core.model.Destination;
import org.opengroup.osdu.core.osm.postgresql.PgDestinationResolution;
import org.opengroup.osdu.core.osm.postgresql.PgDestinationResolver;
import org.opengroup.osdu.partition.coreplus.config.PostgresOsmConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import javax.annotation.PreDestroy;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_SINGLETON;
@Primary
@Component
@Scope(SCOPE_SINGLETON)
@ConditionalOnProperty(name = "osmDriver", havingValue = "postgres")
@RequiredArgsConstructor
@Slf4j
public class OsmPostgresDestinationResolver implements PgDestinationResolver {
private final PostgresOsmConfiguration properties;
private static final String DRIVER_CLASS_NAME = "org.postgresql.Driver";
private static DataSource dataSource;
@Override
public PgDestinationResolution resolve(Destination destination) {
if (dataSource == null || (dataSource instanceof HikariDataSource && ((HikariDataSource) dataSource).isClosed())) {
dataSource = buildDataSourceFromProperties();
}
return PgDestinationResolution.builder()
.datasource(dataSource)
.build();
}
private DataSource buildDataSourceFromProperties() {
DataSource dataSource;
dataSource = DataSourceBuilder.create()
.driverClassName(DRIVER_CLASS_NAME)
.url(properties.getUrl())
.username(properties.getUsername())
.password(properties.getPassword())
.build();
HikariDataSource hikariDataSource = (HikariDataSource) dataSource;
hikariDataSource.setMaximumPoolSize(properties.getMaximumPoolSize());
hikariDataSource.setMinimumIdle(properties.getMinimumIdle());
hikariDataSource.setIdleTimeout(properties.getIdleTimeout());
hikariDataSource.setMaxLifetime(properties.getMaxLifetime());
hikariDataSource.setConnectionTimeout(properties.getConnectionTimeout());
return dataSource;
}
@PreDestroy
@Override
public void close() {
log.info("On pre-destroy. {} shutting down the datasource");
if (dataSource instanceof HikariDataSource && !((HikariDataSource) dataSource).isClosed()) {
((HikariDataSource) dataSource).close();
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment