001    package org.kuali.common.jdbc.service.spring;
002    
003    import java.sql.Driver;
004    import java.util.Properties;
005    
006    import javax.sql.DataSource;
007    
008    import org.apache.commons.lang3.StringUtils;
009    import org.kuali.common.jdbc.model.Credentials;
010    import org.kuali.common.jdbc.model.context.ConnectionContext;
011    import org.kuali.common.jdbc.model.context.DatabaseProcessContext;
012    import org.kuali.common.util.Assert;
013    import org.kuali.common.util.property.ImmutableProperties;
014    import org.springframework.beans.BeanUtils;
015    import org.springframework.beans.factory.annotation.Autowired;
016    import org.springframework.context.annotation.Bean;
017    import org.springframework.context.annotation.Configuration;
018    import org.springframework.context.annotation.Import;
019    import org.springframework.jdbc.datasource.SimpleDriverDataSource;
020    
021    @Configuration
022    @Import({ DatabaseProcessContextConfig.class })
023    public class DataSourceConfig {
024    
025            private static final String USERNAME_KEY = "user";
026            private static final String PASSWORD_KEY = "password";
027    
028            @Autowired
029            DatabaseProcessContext context;
030    
031            @Bean
032            public DataSource dataSource() {
033                    ConnectionContext normal = context.getConnections().getNormal();
034                    return getDataSource(normal, jdbcDriver());
035            }
036    
037            @Bean
038            public DataSource dbaDataSource() {
039                    ConnectionContext dba = context.getConnections().getDba();
040                    return getDataSource(dba, jdbcDriver());
041            }
042    
043            @Bean
044            public Class<? extends Driver> jdbcDriverClass() {
045                    return context.getConnections().getDriver();
046            }
047    
048            @Bean
049            public Driver jdbcDriver() {
050                    return BeanUtils.instantiateClass(jdbcDriverClass());
051            }
052    
053            protected DataSource getDataSource(ConnectionContext context, Driver driver) {
054                    Properties properties = getProperties(context);
055                    return new SimpleDriverDataSource(driver, context.getUrl(), properties);
056            }
057    
058            protected Properties getProperties(ConnectionContext context) {
059                    Credentials creds = context.getCredentials();
060                    String username = toNull(creds.getUsername(), Credentials.NO_USERNAME);
061                    String password = toNull(creds.getPassword(), Credentials.NO_PASSWORD);
062                    Properties properties = new Properties(context.getProperties());
063                    if (username != null) {
064                            properties.setProperty(USERNAME_KEY, username);
065                    }
066                    if (password != null) {
067                            properties.setProperty(PASSWORD_KEY, password);
068                    }
069                    return new ImmutableProperties(properties);
070            }
071    
072            protected String toNull(String token, String nullToken) {
073                    Assert.notNull(nullToken);
074                    return StringUtils.equals(token, nullToken) ? null : token;
075            }
076    }