001    /**
002     * Copyright 2010-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.common.jdbc.spring;
017    
018    import java.util.Arrays;
019    
020    import javax.sql.DataSource;
021    
022    import org.kuali.common.jdbc.ShowConfigExecutable;
023    import org.kuali.common.jdbc.ShowDbaConfigExecutable;
024    import org.kuali.common.jdbc.context.DatabaseProcessContext;
025    import org.kuali.common.util.CollectionUtils;
026    import org.kuali.common.util.execute.Executable;
027    import org.kuali.common.util.nullify.BeanNullifier;
028    import org.kuali.common.util.nullify.NullUtils;
029    import org.kuali.common.util.spring.SpringUtils;
030    import org.springframework.beans.factory.annotation.Autowired;
031    import org.springframework.context.annotation.Bean;
032    import org.springframework.context.annotation.Configuration;
033    import org.springframework.context.annotation.Import;
034    import org.springframework.core.env.Environment;
035    import org.springframework.jdbc.datasource.DriverManagerDataSource;
036    
037    /**
038     * @deprecated
039     */
040    @Deprecated
041    @Configuration
042    @Import(JdbcCommonConfig.class)
043    public class JdbcDataSourceConfig {
044    
045            // Property keys
046            protected static final String VENDOR_KEY = "db.vendor";
047            protected static final String DRIVER_KEY = "jdbc.driver";
048            protected static final String URL_KEY = "jdbc.url";
049            protected static final String USERNAME_KEY = "jdbc.username";
050            protected static final String PASSWORD_KEY = "jdbc.password";
051            protected static final String DBA_URL_KEY = "jdbc.dba.url";
052            protected static final String DBA_USERNAME_KEY = "jdbc.dba.username";
053            protected static final String DBA_PASSWORD_KEY = "jdbc.dba.password";
054            protected static final String ENCODING_KEY = "sql.encoding";
055            protected static final String SCHEMA_KEY = "sql.schema";
056            protected static final String SHOW_CONFIG_SKIP_KEY = "jdbc.showconfig.skip";
057    
058            // Default values
059            protected static final boolean DEFAULT_SHOW_CONFIG_SKIP = false;
060            protected static final String NULLIFIED_CONTEXT_PROPERTIES_CSV = "username,password,dbaUsername,dbaPassword";
061    
062            @Autowired
063            Environment env;
064    
065            @Autowired
066            JdbcCommonConfig commonConfig;
067    
068            @Bean
069            public DatabaseProcessContext jdbcDatabaseProcessContext() {
070                    DatabaseProcessContext ctx = new DatabaseProcessContext();
071                    ctx.setVendor(SpringUtils.getProperty(env, VENDOR_KEY));
072                    ctx.setDriver(SpringUtils.getProperty(env, DRIVER_KEY));
073                    ctx.setUrl(SpringUtils.getProperty(env, URL_KEY));
074                    ctx.setUsername(SpringUtils.getProperty(env, USERNAME_KEY));
075                    ctx.setPassword(SpringUtils.getProperty(env, PASSWORD_KEY));
076                    ctx.setDbaUrl(SpringUtils.getProperty(env, DBA_URL_KEY));
077                    ctx.setDbaUsername(SpringUtils.getProperty(env, DBA_USERNAME_KEY));
078                    ctx.setDbaPassword(SpringUtils.getProperty(env, DBA_PASSWORD_KEY));
079                    ctx.setEncoding(SpringUtils.getProperty(env, ENCODING_KEY));
080                    ctx.setSchema(SpringUtils.getProperty(env, SCHEMA_KEY));
081    
082                    BeanNullifier nullifier = new BeanNullifier();
083                    nullifier.setBean(ctx);
084                    nullifier.setNullTokens(Arrays.asList(NullUtils.NONE, NullUtils.NULL));
085                    nullifier.setProperties(CollectionUtils.getTrimmedListFromCSV(NULLIFIED_CONTEXT_PROPERTIES_CSV));
086    
087                    // Null out usernames/passwords that are set to NONE or NULL
088                    nullifier.nullify();
089    
090                    return ctx;
091            }
092    
093            @Bean
094            public DataSource jdbcDataSource() {
095                    DatabaseProcessContext ctx = jdbcDatabaseProcessContext();
096                    DriverManagerDataSource dmds = new DriverManagerDataSource();
097                    dmds.setDriverClassName(ctx.getDriver());
098                    dmds.setUrl(ctx.getUrl());
099                    dmds.setUsername(ctx.getUsername());
100                    dmds.setPassword(ctx.getPassword());
101                    return dmds;
102            }
103    
104            @Bean
105            public DataSource jdbcDbaDataSource() {
106                    DatabaseProcessContext ctx = jdbcDatabaseProcessContext();
107                    DriverManagerDataSource dmds = new DriverManagerDataSource();
108                    dmds.setDriverClassName(ctx.getDriver());
109                    dmds.setUrl(ctx.getDbaUrl());
110                    dmds.setUsername(ctx.getDbaUsername());
111                    dmds.setPassword(ctx.getDbaPassword());
112                    return dmds;
113            }
114    
115            /**
116             * This bean requires DBA credentials
117             */
118            @Bean
119            public Executable jdbcShowDbaConfigExecutable() {
120                    ShowDbaConfigExecutable exec = new ShowDbaConfigExecutable();
121                    exec.setService(commonConfig.jdbcService());
122                    exec.setContext(jdbcDatabaseProcessContext());
123                    exec.setDataSource(jdbcDbaDataSource());
124                    exec.setSkip(SpringUtils.getBoolean(env, SHOW_CONFIG_SKIP_KEY, DEFAULT_SHOW_CONFIG_SKIP));
125                    return exec;
126            }
127    
128            @Bean
129            public Executable jdbcShowConfigExecutable() {
130                    ShowConfigExecutable exec = new ShowConfigExecutable();
131                    exec.setService(commonConfig.jdbcService());
132                    exec.setContext(jdbcDatabaseProcessContext());
133                    exec.setDataSource(jdbcDataSource());
134                    exec.setSkip(SpringUtils.getBoolean(env, SHOW_CONFIG_SKIP_KEY, DEFAULT_SHOW_CONFIG_SKIP));
135                    return exec;
136            }
137    }