001 package org.kuali.common.jdbc.service.spring;
002
003 import java.sql.Driver;
004
005 import org.kuali.common.jdbc.model.Credentials;
006 import org.kuali.common.jdbc.model.JdbcConnections;
007 import org.kuali.common.jdbc.model.JdbcKeys;
008 import org.kuali.common.jdbc.model.context.ConnectionContext;
009 import org.kuali.common.jdbc.vendor.model.DatabaseVendor;
010 import org.kuali.common.jdbc.vendor.spring.DatabaseVendorConfig;
011 import org.kuali.common.util.ReflectionUtils;
012 import org.kuali.common.util.spring.env.EnvironmentService;
013 import org.kuali.common.util.spring.service.SpringServiceConfig;
014 import org.slf4j.Logger;
015 import org.slf4j.LoggerFactory;
016 import org.springframework.beans.factory.annotation.Autowired;
017 import org.springframework.context.annotation.Bean;
018 import org.springframework.context.annotation.Configuration;
019 import org.springframework.context.annotation.Import;
020
021 @Configuration
022 @Import({ SpringServiceConfig.class, DatabaseVendorConfig.class })
023 public class JdbcConnectionsConfig {
024
025 private static final Logger logger = LoggerFactory.getLogger(JdbcConnectionsConfig.class);
026
027 @Autowired
028 DatabaseVendor vendor;
029
030 @Autowired
031 EnvironmentService env;
032
033 @Bean
034 public JdbcConnections jdbcConnections() {
035 Class<? extends Driver> driver = getDriver(env, vendor.getDriver());
036 return new JdbcConnections(getNormal(), getDba(), driver);
037 }
038
039 protected Class<? extends Driver> getDriver(EnvironmentService env, Class<? extends Driver> defaultValue) {
040 String driver = env.getString(JdbcKeys.DRIVER.getValue(), defaultValue.getName());
041 return ReflectionUtils.getTypedClass(driver);
042 }
043
044 protected ConnectionContext getNormal() {
045 String username = env.getString(JdbcKeys.USERNAME.getValue()); // No default value. They must supply jdbc.username
046 String password = env.getString(JdbcKeys.PASSWORD.getValue(), username);
047
048 String key = JdbcKeys.URL.getValue();
049 String defaultValue = vendor.getUrl();
050 boolean contains = env.containsProperty(key);
051 String url = env.getString(key, defaultValue);
052
053 Object[] args = { key, defaultValue, url, contains };
054 logger.debug("key={} dv:{} av:{} contains:{}", args);
055
056 return new ConnectionContext(url, username, password);
057 }
058
059 protected ConnectionContext getDba() {
060 Credentials auth = vendor.getDba().getCredentials();
061 String username = env.getString(JdbcKeys.DBA_USERNAME.getValue(), auth.getUsername());
062 String password = env.getString(JdbcKeys.DBA_PASSWORD.getValue(), auth.getPassword());
063 String url = env.getString(JdbcKeys.DBA_URL.getValue(), vendor.getDba().getUrl());
064 return new ConnectionContext(url, username, password);
065 }
066 }