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