moving driver info to a property file

This commit is contained in:
unknown
2016-02-16 18:41:33 +05:30
parent 9c27413cf1
commit 1c0e5502e0
10 changed files with 164 additions and 20 deletions
@@ -1,2 +1,20 @@
#
# 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.
#
spring.profiles.default=basicauth
@@ -1,3 +1,21 @@
#
# 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.
#
spring.profiles.default=basicauth
spring.profiles.active=oauth
@@ -26,6 +26,7 @@ import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfigurat
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.
@@ -41,6 +42,7 @@ import org.springframework.context.annotation.ImportResource;
@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,
@@ -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;
}
}
@@ -20,9 +20,11 @@ package org.apache.fineract.infrastructure.core.boot.db;
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;
@@ -33,10 +35,12 @@ import org.springframework.context.annotation.Configuration;
@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
@@ -50,5 +54,4 @@ public class DataSourceConfiguration {
protected DataSourceProperties getProperties() {
return dataSourceProperties();
}
}
@@ -21,7 +21,6 @@ package org.apache.fineract.infrastructure.core.boot.db;
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;
@@ -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();
}
@@ -77,11 +77,13 @@ public class FineractPlatformTenantConnection {
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
@@ -23,6 +23,7 @@ import java.util.List;
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;
@@ -42,11 +43,12 @@ import com.googlecode.flyway.core.util.jdbc.DriverDataSource;
@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) {
@@ -63,14 +65,15 @@ public class TenantDatabaseUpgradeService {
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());
}
@@ -23,6 +23,7 @@ import java.util.Map;
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;
@@ -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;
@@ -83,10 +87,10 @@ public class TomcatJdbcDataSourcePerTenantService implements RoutingDataSourceSe
// 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());
@@ -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