/**
 * Copyright © 2018 Mercateo AG (http://www.mercateo.com)
 *
 * 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.factcast.store.pgsql.rds;

import javax.annotation.Resource;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.cloud.aws.jdbc.datasource.TomcatJdbcDataSourceFactory;
import org.springframework.cloud.aws.jdbc.rds.AmazonRdsDataSourceFactoryBean;
import org.springframework.core.env.Environment;

/**
 * exchange the given TomcatJdbcDataSourceFactory with a customized factory so
 * we can configure the datasource connection pool
 * 
 *
 */

public class RdsDataSourceFactorBeanPostProcessor implements BeanPostProcessor {

    @Resource
    private Environment env;

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName)
            throws BeansException {

        if (bean instanceof AmazonRdsDataSourceFactoryBean) {
            ((AmazonRdsDataSourceFactoryBean) bean).setDataSourceFactory(
                    tomcatJdbcDataSourceFactory());
        }

        return bean;
    }

    // TODO this was reset to 0.0.6 state. Will be discussed with the original
    // author of that change
    TomcatJdbcDataSourceFactory tomcatJdbcDataSourceFactory() {

        TomcatJdbcDataSourceFactory fac = new TomcatJdbcDataSourceFactory();

        fac.setRemoveAbandonedTimeout(360000);
        fac.setMaxWait(20000);

        return fac;

    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName)
            throws BeansException {
        return bean;
    }
}

@@ -1651,116 +1651,8 @@
  %7D%0A%0A
-    // TODO this was reset to 0.0.6 state. Will be discussed with the original%0A    // author of that change%0A
     
@@ -1809,72 +1809,288 @@
 .set
-RemoveAbandonedTimeout(360000);%0A        fac.setMaxWait(20000
+TestOnBorrow(env.getProperty(%22spring.datasource.tomcat.testOnBorrow%22, Boolean.class, true));%0A        fac.setConnectionProperties(env.getProperty(%22spring.datasource.tomcat.connectionProperties%22, String.class,%0A                %22socketTimeout=20;connectTimeout=10;loginTimeout=10%22)
 );%0A
-%0A
     
@@ -2105,17 +2105,16 @@
 rn fac;%0A
-%0A
     %7D%0A%0A 

add default values for tomcat properties (#144)