001/*
002 * Copyright 2002-2017 the original author or authors.
003 *
004 * Licensed under the Apache 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 *      https://www.apache.org/licenses/LICENSE-2.0
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
017package org.springframework.jdbc.datasource.embedded;
018
019import javax.sql.DataSource;
020
021import org.springframework.beans.factory.DisposableBean;
022import org.springframework.beans.factory.FactoryBean;
023import org.springframework.beans.factory.InitializingBean;
024import org.springframework.jdbc.datasource.init.DatabasePopulator;
025import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
026import org.springframework.lang.Nullable;
027
028/**
029 * A subclass of {@link EmbeddedDatabaseFactory} that implements {@link FactoryBean}
030 * for registration as a Spring bean. Returns the actual {@link DataSource} that
031 * provides connectivity to the embedded database to Spring.
032 *
033 * <p>The target {@link DataSource} is returned instead of an {@link EmbeddedDatabase}
034 * proxy since the {@link FactoryBean} will manage the initialization and destruction
035 * lifecycle of the embedded database instance.
036 *
037 * <p>Implements {@link DisposableBean} to shutdown the embedded database when the
038 * managing Spring container is being closed.
039 *
040 * @author Keith Donald
041 * @author Juergen Hoeller
042 * @since 3.0
043 */
044public class EmbeddedDatabaseFactoryBean extends EmbeddedDatabaseFactory
045                implements FactoryBean<DataSource>, InitializingBean, DisposableBean {
046
047        @Nullable
048        private DatabasePopulator databaseCleaner;
049
050
051        /**
052         * Set a script execution to be run in the bean destruction callback,
053         * cleaning up the database and leaving it in a known state for others.
054         * @param databaseCleaner the database script executor to run on destroy
055         * @see #setDatabasePopulator
056         * @see org.springframework.jdbc.datasource.init.DataSourceInitializer#setDatabaseCleaner
057         */
058        public void setDatabaseCleaner(DatabasePopulator databaseCleaner) {
059                this.databaseCleaner = databaseCleaner;
060        }
061
062        @Override
063        public void afterPropertiesSet() {
064                initDatabase();
065        }
066
067
068        @Override
069        @Nullable
070        public DataSource getObject() {
071                return getDataSource();
072        }
073
074        @Override
075        public Class<? extends DataSource> getObjectType() {
076                return DataSource.class;
077        }
078
079        @Override
080        public boolean isSingleton() {
081                return true;
082        }
083
084
085        @Override
086        public void destroy() {
087                if (this.databaseCleaner != null && getDataSource() != null) {
088                        DatabasePopulatorUtils.execute(this.databaseCleaner, getDataSource());
089                }
090                shutdownDatabase();
091        }
092
093}