<commit_before>/*
 * Copyright 2015 Kantega AS
 *
 * Licensed 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.kantega.respiro.jdbc;

import org.apache.tomcat.jdbc.pool.PoolProperties;
import org.kantega.respiro.api.DataSourceBuilder;

import javax.sql.DataSource;
import java.util.Collection;

public class DefaultDataSourceBuilder implements DataSourceBuilder {
    private final Collection<DataSourceCustomizer> dataSourceCustomizers;

    public DefaultDataSourceBuilder(Collection<DataSourceCustomizer> dataSourceCustomizers) {

        this.dataSourceCustomizers = dataSourceCustomizers;
    }

    @Override
    public Build datasource(String url) {
        return new DefaultBuild(url);
    }

    private class DefaultBuild implements Build {


        private final String url;
        private String username;
        private String password;
        private String driverClassname;
        private Long maxAge;

        public DefaultBuild(String url) {
            this.url = url;

        }

        @Override
        public Build username(String username) {
            this.username = username;
            return this;
        }

        @Override
        public Build password(String password) {
            this.password = password;
            return this;
        }

        @Override
        public Build driverClassname(String driver) {
            this.driverClassname = driver;
            return this;
        }

        @Override
        public Build maxAge(long maxAge) {
            this.maxAge = maxAge;
            return this;
        }

        @Override
        public DataSource build() {
            PoolProperties poolProperties = new PoolProperties();
            poolProperties.setDriverClassName(driverClassname);
            poolProperties.setUrl(url);
            poolProperties.setUsername(username);
            poolProperties.setPassword(password);

            if( maxAge != null)
                poolProperties.setMaxAge(maxAge);

            DataSource dataSource = new org.apache.tomcat.jdbc.pool.DataSource(poolProperties);
            for (DataSourceCustomizer customizer : dataSourceCustomizers) {
                dataSource = customizer.wrapDataSource(dataSource);
            }
            return dataSource;<commit_msg>Set default value to 20 seconds maxAge<commit_after>/*
 * Copyright 2015 Kantega AS
 *
 * Licensed 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.kantega.respiro.jdbc;

import org.apache.tomcat.jdbc.pool.PoolProperties;
import org.kantega.respiro.api.DataSourceBuilder;

import javax.sql.DataSource;
import java.util.Collection;

public class DefaultDataSourceBuilder implements DataSourceBuilder {
    private final Collection<DataSourceCustomizer> dataSourceCustomizers;

    public DefaultDataSourceBuilder(Collection<DataSourceCustomizer> dataSourceCustomizers) {

        this.dataSourceCustomizers = dataSourceCustomizers;
    }

    @Override
    public Build datasource(String url) {
        return new DefaultBuild(url);
    }

    private class DefaultBuild implements Build {


        private final String url;
        private String username;
        private String password;
        private String driverClassname;
        private long maxAge = 20_000L;

        public DefaultBuild(String url) {
            this.url = url;

        }

        @Override
        public Build username(String username) {
            this.username = username;
            return this;
        }

        @Override
        public Build password(String password) {
            this.password = password;
            return this;
        }

        @Override
        public Build driverClassname(String driver) {
            this.driverClassname = driver;
            return this;
        }

        @Override
        public Build maxAge(long maxAge) {
            this.maxAge = maxAge;
            return this;
        }

        @Override
        public DataSource build() {
            PoolProperties poolProperties = new PoolProperties();
            poolProperties.setDriverClassName(driverClassname);
            poolProperties.setUrl(url);
            poolProperties.setUsername(username);
            poolProperties.setPassword(password);

            poolProperties.setMaxAge(maxAge);

            DataSource dataSource = new org.apache.tomcat.jdbc.pool.DataSource(poolProperties);
            for (DataSourceCustomizer customizer : dataSourceCustomizers) {
                dataSource = customizer.wrapDataSource(dataSource);
            }
            return dataSource;
{'fraction_non_alphanumeric': 0.06664271600502963, 'fraction_numerical': 0.004131489132387282, 'mean_word_length': 2.4909090909090907, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1223798', 'n_tokens_mistral': 1373, 'n_tokens_neox': 1294, 'n_words': 413}