Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moving JDBC Driver related info to a property file and removed references in java filles #8

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;

/**
* Base Spring Configuration with what's common to all Configuration subclasses.
Expand All @@ -41,6 +42,7 @@
@Configuration
@Import({ WebXmlConfiguration.class, WebXmlOauthConfiguration.class, WebFrontEndConfiguration.class })
@ImportResource({ "classpath*:META-INF/spring/appContext.xml" })
@PropertySource(value="classpath:META-INF/spring/jdbc.properties")
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class,
HibernateJpaAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.fineract.infrastructure.core.boot;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

@Service
public class JDBCDriverConfig {

private final static String DRIVER_CLASS_PROPERTYNAME = "DRIVERCLASS_NAME" ;
private final static String PROTOCOL_PROPERTYNAME = "PROTOCOL" ;
private final static String SUBPROTOCOL_PROPERTYNAME = "SUB_PROTOCOL" ;
private final static String PORT_PROPERTYNAME = "PORT" ;

private String driverClassName ;
private String protocol ;
private String subProtocol ;
private Integer port ;

@Autowired ApplicationContext context ;

@PostConstruct
protected void init() {
Environment environment = context.getEnvironment() ;
driverClassName = (String)environment.getProperty(DRIVER_CLASS_PROPERTYNAME) ;
protocol = (String) environment.getProperty(PROTOCOL_PROPERTYNAME) ;
subProtocol = (String) environment.getProperty(SUBPROTOCOL_PROPERTYNAME) ;
port = Integer.parseInt((String) environment.getProperty(PORT_PROPERTYNAME)) ;
}

public String getDriverClassName() {
return this.driverClassName ;
}

public String getProtocol() {
return this.protocol ;
}

public String getSubProtocol() {
return this.subProtocol ;
}

public Integer getPort() {
return this.port ;
}

public String constructProtocol(String schemaServer, String schemaServerPort, String schemaName) {
final String url = new StringBuilder(protocol).append(":").append(subProtocol).append("://").append(schemaServer).append(':').append(schemaServerPort)
.append('/').append(schemaName).toString();
return url;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@

import javax.sql.DataSource;

import org.apache.fineract.infrastructure.core.boot.JDBCDriverConfig;
import org.apache.tomcat.jdbc.pool.PoolConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

Expand All @@ -33,10 +35,12 @@
@Configuration
public class DataSourceConfiguration {
private static final Logger logger = LoggerFactory.getLogger(DataSourceConfiguration.class);


@Autowired JDBCDriverConfig config ;

@Bean
public DataSourceProperties dataSourceProperties() {
return new DataSourceProperties();
return new DataSourceProperties(config.getDriverClassName(), config.getProtocol(), config.getSubProtocol(), config.getPort());
}

@Bean
Expand All @@ -50,5 +54,4 @@ public DataSource tenantDataSourceJndi() {
protected DataSourceProperties getProperties() {
return dataSourceProperties();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import javax.validation.constraints.NotNull;

import org.apache.tomcat.jdbc.pool.PoolProperties;
import org.drizzle.jdbc.DrizzleDriver;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;

Expand Down Expand Up @@ -64,14 +63,12 @@ public class DataSourceProperties extends PoolProperties {
@Value("${" + SUBPROTOCOL + ":mysql:thin}")
private volatile @NotNull String jdbcSubprotocol;


public DataSourceProperties() {
public DataSourceProperties(String driverClassName, String protocol, String subProtocol, Integer port) {
super();

// default to save us from re-specifying this; note that it can still be
// overridden
setDriverClassName(DrizzleDriver.class.getName());

setDriverClassName(driverClassName);
this.jdbcProtocol = protocol ;
this.jdbcSubprotocol = subProtocol ;
this.port = port ;
setDefaults();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,13 @@ public FineractPlatformTenantConnection(final Long connectionId,final String sch
this.testOnBorrow=tesOnBorrow;
}

public String databaseURL() {
//The Connection Protocol should be built based on jdbc.properties. We can't hard code this here and also, constructing protocol is not this class
//responsibility
/*public String databaseURL() {
final String url = new StringBuilder("jdbc:mysql:thin://").append(this.schemaServer).append(':').append(this.schemaServerPort)
.append('/').append(this.schemaName).toString();
return url;
}
}*/

/**
* @return the schemaServer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import javax.annotation.PostConstruct;
import javax.sql.DataSource;

import org.apache.fineract.infrastructure.core.boot.JDBCDriverConfig;
import org.apache.fineract.infrastructure.core.boot.db.TenantDataSourcePortFixService;
import org.apache.fineract.infrastructure.core.domain.FineractPlatformTenant;
import org.apache.fineract.infrastructure.core.domain.FineractPlatformTenantConnection;
Expand All @@ -42,11 +43,12 @@
@Service
public class TenantDatabaseUpgradeService {

private final String DRIVER_CLASS = "org.drizzle.jdbc.DrizzleDriver" ;
private final TenantDetailsService tenantDetailsService;
protected final DataSource tenantDataSource;
protected final TenantDataSourcePortFixService tenantDataSourcePortFixService;


@Autowired private JDBCDriverConfig driverConfig ;

@Autowired
public TenantDatabaseUpgradeService(final TenantDetailsService detailsService,
@Qualifier("tenantDataSourceJndi") final DataSource dataSource, TenantDataSourcePortFixService tenantDataSourcePortFixService) {
Expand All @@ -63,14 +65,15 @@ public void upgradeAllTenants() {
final FineractPlatformTenantConnection connection = tenant.getConnection();
if (connection.isAutoUpdateEnabled()) {
final Flyway flyway = new Flyway();
DriverDataSource source = new DriverDataSource(DRIVER_CLASS, connection.databaseURL(), connection.getSchemaUsername(), connection.getSchemaPassword()) ;
String connectionProtocol = driverConfig.constructProtocol(connection.getSchemaServer(), connection.getSchemaServerPort(), connection.getSchemaName()) ;
DriverDataSource source = new DriverDataSource(driverConfig.getDriverClassName(), connectionProtocol, connection.getSchemaUsername(), connection.getSchemaPassword()) ;
flyway.setDataSource(source);
flyway.setLocations("sql/migrations/core_db");
flyway.setOutOfOrder(true);
try {
flyway.migrate();
} catch (FlywayException e) {
String betterMessage = e.getMessage() + "; for Tenant DB URL: " + connection.databaseURL() + ", username: "
String betterMessage = e.getMessage() + "; for Tenant DB URL: " + connectionProtocol + ", username: "
+ connection.getSchemaPassword();
throw new FlywayException(betterMessage, e.getCause());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import javax.sql.DataSource;

import org.apache.fineract.infrastructure.core.boot.JDBCDriverConfig;
import org.apache.fineract.infrastructure.core.domain.FineractPlatformTenant;
import org.apache.fineract.infrastructure.core.domain.FineractPlatformTenantConnection;
import org.apache.tomcat.jdbc.pool.PoolConfiguration;
Expand All @@ -45,6 +46,9 @@ public class TomcatJdbcDataSourcePerTenantService implements RoutingDataSourceSe
private final Map<Long, DataSource> tenantToDataSourceMap = new HashMap<>(1);
private final DataSource tenantDataSource;

@Autowired
private JDBCDriverConfig driverConfig ;

@Autowired
public TomcatJdbcDataSourcePerTenantService(final @Qualifier("tenantDataSourceJndi") DataSource tenantDataSource) {
this.tenantDataSource = tenantDataSource;
Expand Down Expand Up @@ -83,10 +87,10 @@ private DataSource createNewDataSourceFor(final FineractPlatformTenantConnection
// http://www.tomcatexpert.com/blog/2010/04/01/configuring-jdbc-pool-high-concurrency

// see also org.apache.fineract.DataSourceProperties.setDefaults()

final String jdbcUrl = tenantConnectionObj.databaseURL();
String jdbcUrl = this.driverConfig.constructProtocol(tenantConnectionObj.getSchemaServer(), tenantConnectionObj.getSchemaServerPort(), tenantConnectionObj.getSchemaName()) ;
//final String jdbcUrl = tenantConnectionObj.databaseURL();
final PoolConfiguration poolConfiguration = new PoolProperties();
poolConfiguration.setDriverClassName("org.drizzle.jdbc.DrizzleDriver");
poolConfiguration.setDriverClassName(this.driverConfig.getDriverClassName());
poolConfiguration.setName(tenantConnectionObj.getSchemaName() + "_pool");
poolConfiguration.setUrl(jdbcUrl);
poolConfiguration.setUsername(tenantConnectionObj.getSchemaUsername());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
#

DRIVERCLASS_NAME:org.drizzle.jdbc.DrizzleDriver
PROTOCOL:jdbc
SUB_PROTOCOL:mysql:thin
PORT:3306