/*
 * 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.jdbi.v3.spring4;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;

import org.jdbi.v3.core.Jdbi;
import org.jdbi.v3.core.spi.JdbiPlugin;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceUtils;

/**
 * Utility class which constructs an {@link Jdbi} instance which can conveniently participate
 * in Spring's transaction management system.
 */
public class JdbiFactoryBean implements FactoryBean<Jdbi> {
    private DataSource dataSource;
    private final Map<String, Object> globalDefines = new HashMap<>();

    private boolean autoInstallPlugins = false;
    private Collection<JdbiPlugin> plugins = Collections.emptyList();

    public JdbiFactoryBean() {}

    public JdbiFactoryBean(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    /**
     * See {@link org.springframework.beans.factory.FactoryBean#getObject}
     */
    @Override
    public Jdbi getObject() throws Exception {
        final Jdbi jdbi = Jdbi.create(() -> DataSourceUtils.getConnection(dataSource));

        if (autoInstallPlugins) {
            jdbi.installPlugins();
        }

        plugins.forEach(jdbi::installPlugin);

        globalDefines.forEach(jdbi::define);

        return jdbi;
    }

    /**
     * See {@link org.springframework.beans.factory.FactoryBean#getObjectType}
     */
    @Override
    public Class<Jdbi> getObjectType() {
        return Jdbi.class;
    }

    /**
     * See {@link org.springframework.beans.factory.FactoryBean#isSingleton}
     *
     * @return false
     */
    @Override
    public boolean isSingleton() {
        return true;
    }

    /**
     * The datasource, which should be managed by spring's transaction system, from which
     * the {@link Jdbi} will obtain connections
     *
     * @param dataSource the data source.
     * @return this
     */
    public JdbiFactoryBean setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
        return this;
    }

    /**
     * Installs the given plugins which will be installed into the {@link Jdbi}.
     * @param plugins collection of Jdbi plugins to install.
     * @return this
     */
    @Autowired(required = false)
    public JdbiFactoryBean setPlugins(Collection<JdbiPlugin> plugins) {
        this.plugins = new ArrayList<>(plugins);
        return this;
    }

    /**
     * Sets whether to install plugins automatically from the classpath, using
     * {@link java.util.ServiceLoader} manifests.
     *
     * @param autoInstallPlugins whether to install plugins automatically from
     *                           the classpath.
     * @return this
     * @see Jdbi#installPlugins() for detail
     */
    public JdbiFactoryBean setAutoInstallPlugins(boolean autoInstallPlugins) {
        this.autoInstallPlugins = autoInstallPlugins;
        return this;
    }

    public void setGlobalDefines(Map<String, Object> defines) {
        globalDefines.putAll(defines);
    }

    /**
     * Verifies that a dataSource has been set
     */
    @PostConstruct
    @SuppressWarnings("unused")
    private void afterPropertiesSet() {
        if (dataSource == null) {
            throw new IllegalStateException("'dataSource' property must be set");
        }
    }
}

@@ -721,47 +721,8 @@
 p;%0A%0A
-import javax.annotation.PostConstruct;%0A
 impo
@@ -821,62 +821,8 @@
 in;%0A
-import org.springframework.beans.factory.FactoryBean;%0A
 impo
@@ -880,16 +880,85 @@
 owired;%0A
+import org.springframework.beans.factory.config.AbstractFactoryBean;%0A
 import o
@@ -1095,16 +1095,19 @@
 eniently
+%0A *
  partici
@@ -1110,19 +1110,16 @@
 ticipate
-%0A *
  in Spri
@@ -1191,19 +1191,24 @@
 ean 
-implement
+extend
 s 
+Abstract
 Fact
@@ -1587,134 +1587,51 @@
     
-/**%0A     * See %7B@link org.springframework.beans.factory.FactoryBean#getObject%7D%0A     */%0A    @Override%0A    public Jdbi getObject
+@Override%0A    protected Jdbi createInstance
 () t
@@ -2126,206 +2126,8 @@
  %7D%0A%0A
-    /**%0A     * See %7B@link org.springframework.beans.factory.FactoryBean#isSingleton%7D%0A     *%0A     * @return false%0A     */%0A    @Override%0A    public boolean isSingleton() %7B%0A        return true;%0A    %7D%0A%0A
     
@@ -3519,27 +3519,8 @@
  */%0A
-    @PostConstruct%0A
     
@@ -3552,22 +3552,21 @@
 %22)%0A    p
-rivate
+ublic
  void af
@@ -3583,16 +3583,33 @@
 iesSet()
+ throws Exception
  %7B%0A     
@@ -3729,16 +3729,52 @@
       %7D%0A
+        super.afterPropertiesSet();%0A
     %7D%0A%7D%0A

Improve JdbiFactoryBean