136 lines
5.0 KiB
Java
136 lines
5.0 KiB
Java
/**
|
|
*
|
|
* Project: Eight Track, File: EightTrackDataSource.java
|
|
*
|
|
* Copyright 2019-2019 Locusworks LLC.
|
|
* All rights reserved. Federal copyright law prohibits unauthorized reproduction by
|
|
* any means and imposes fines up to $25,000 for violation. No part of this material
|
|
* may be reproduced, transmitted, transcribed, stored in a retrieval system, copied,
|
|
* modified, duplicated, adapted or translated into another program language in any
|
|
* form or by any means, electronic, mechanical, photocopying, recording, or
|
|
* otherwise, without the prior written permission from Locusworks. Locusworks
|
|
* affirms that Eight-Track(R) software and data is subject to United States
|
|
* Government Purpose Rights. Contact Locusworks, 1313 Lawnview Drive
|
|
* Forney TX 75126, (802) 488-0438, for commercial licensing opportunities.
|
|
*
|
|
* IN NO EVENT SHALL LOCUSWORKS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
|
|
* INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT
|
|
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF LOCUSWORKS HAS BEEN
|
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. NO RESPONSIBILITY IS ASSUMED BY
|
|
* LOCUSWORKS FOR ITS USE, OR FOR ANY INFRINGEMENTS OF PATENTS OR OTHER RIGHTS OF
|
|
* THIRD PARTIES RESULTING FROM ITS USE. LOCUSWORKS SPECIFICALLY DISCLAIMS ANY
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE AND
|
|
* ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS
|
|
* IS". LOCUSWORKS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
|
|
* ENHANCEMENTS, OR MODIFICATIONS.
|
|
*/
|
|
package net.locusworks.discord.eighttrack.database.config;
|
|
|
|
import java.util.LinkedHashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Properties;
|
|
import java.util.TimeZone;
|
|
import java.util.stream.Collectors;
|
|
|
|
import javax.sql.DataSource;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.mariadb.jdbc.MariaDbDataSource;
|
|
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.Primary;
|
|
import org.springframework.stereotype.Component;
|
|
import com.mchange.v2.c3p0.ComboPooledDataSource;
|
|
import net.locusworks.discord.eighttrack.services.ConfigurationService;
|
|
|
|
@Primary
|
|
@Component
|
|
public class EightTrackDataSource {
|
|
|
|
private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
|
|
|
|
private static final String JNDI_STRING = "jdbc:mysql://%s:%d%s";
|
|
|
|
private static Map<String, String> mysqlProperties;
|
|
static {
|
|
mysqlProperties = new LinkedHashMap<>();
|
|
mysqlProperties.put("rewriteBatchedStatements", "true");
|
|
mysqlProperties.put("zeroDateTimeBehavior", "CONVERT_TO_NULL");
|
|
mysqlProperties.put("useSSL", "false");
|
|
mysqlProperties.put("serverTimezone", TimeZone.getDefault().getDisplayName(false, TimeZone.SHORT));
|
|
}
|
|
|
|
private Logger logger = LoggerFactory.getLogger(EightTrackDataSource.class);
|
|
|
|
@Autowired
|
|
private ConfigurationService confService;
|
|
|
|
/**
|
|
* Create the data source bean
|
|
* @return the data source bean
|
|
* @throws Exception
|
|
*/
|
|
@Bean
|
|
public DataSource dataSource() throws Exception {
|
|
return getDataSource(false);
|
|
}
|
|
|
|
@Bean
|
|
public DataSource flywayDataSource() throws Exception {
|
|
logger.debug("Logging in with flyway for migrations");
|
|
|
|
String user = confService.getDatabaseRootUsername();
|
|
String passwd = confService.getDatabaseRootPassword();
|
|
|
|
String url = String.format("jdbc:mariadb://%s:%d?user=%s&password=%s",
|
|
confService.getDatabaseHost(), confService.getDatabasePort(),
|
|
user, passwd);
|
|
|
|
MariaDbDataSource mariadbDS = new MariaDbDataSource(url);
|
|
|
|
return mariadbDS;
|
|
}
|
|
|
|
private DataSource getDataSource(Boolean isFlyway) throws Exception {
|
|
logger.debug("Getting datasource");
|
|
|
|
String user = confService.getDatabaseUsername();
|
|
String passwd = confService.getDatabasePassword();
|
|
|
|
Properties props = new Properties();
|
|
props.setProperty("user", user);
|
|
props.setProperty("password", passwd);
|
|
|
|
List<String> params = mysqlProperties
|
|
.entrySet()
|
|
.stream()
|
|
.map(prop -> String.format("%s=%s", prop.getKey(), prop.getValue()))
|
|
.collect(Collectors.toList());
|
|
|
|
String host = confService.getDatabaseHost();
|
|
int port = confService.getDatabasePort();
|
|
|
|
String url = String.format(JNDI_STRING, host, port, "/eighttrack");
|
|
|
|
url = String.format("%s?%s", url, StringUtils.join(params, "&"));
|
|
|
|
logger.debug("Database connection string: %s", url);
|
|
|
|
ComboPooledDataSource cpds = new ComboPooledDataSource();
|
|
cpds.setDriverClass(DRIVER);
|
|
cpds.setJdbcUrl(url);
|
|
cpds.setUser(user);
|
|
cpds.setPassword(passwd);
|
|
cpds.setMinPoolSize(5);
|
|
cpds.setMaxPoolSize(100);
|
|
cpds.setAcquireIncrement(10);
|
|
|
|
return cpds;
|
|
}
|
|
|
|
}
|