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.init; 018 019import javax.sql.DataSource; 020 021import org.springframework.beans.factory.DisposableBean; 022import org.springframework.beans.factory.InitializingBean; 023import org.springframework.util.Assert; 024 025/** 026 * Used to {@linkplain #setDatabasePopulator set up} a database during 027 * initialization and {@link #setDatabaseCleaner clean up} a database during 028 * destruction. 029 * 030 * @author Dave Syer 031 * @author Sam Brannen 032 * @since 3.0 033 * @see DatabasePopulator 034 */ 035public class DataSourceInitializer implements InitializingBean, DisposableBean { 036 037 private DataSource dataSource; 038 039 private DatabasePopulator databasePopulator; 040 041 private DatabasePopulator databaseCleaner; 042 043 private boolean enabled = true; 044 045 046 /** 047 * The {@link DataSource} for the database to populate when this component 048 * is initialized and to clean up when this component is shut down. 049 * <p>This property is mandatory with no default provided. 050 * @param dataSource the DataSource 051 */ 052 public void setDataSource(DataSource dataSource) { 053 this.dataSource = dataSource; 054 } 055 056 /** 057 * Set the {@link DatabasePopulator} to execute during the bean initialization phase. 058 * @param databasePopulator the {@code DatabasePopulator} to use during initialization 059 * @see #setDatabaseCleaner 060 */ 061 public void setDatabasePopulator(DatabasePopulator databasePopulator) { 062 this.databasePopulator = databasePopulator; 063 } 064 065 /** 066 * Set the {@link DatabasePopulator} to execute during the bean destruction 067 * phase, cleaning up the database and leaving it in a known state for others. 068 * @param databaseCleaner the {@code DatabasePopulator} to use during destruction 069 * @see #setDatabasePopulator 070 */ 071 public void setDatabaseCleaner(DatabasePopulator databaseCleaner) { 072 this.databaseCleaner = databaseCleaner; 073 } 074 075 /** 076 * Flag to explicitly enable or disable the {@linkplain #setDatabasePopulator 077 * database populator} and {@linkplain #setDatabaseCleaner database cleaner}. 078 * @param enabled {@code true} if the database populator and database cleaner 079 * should be called on startup and shutdown, respectively 080 */ 081 public void setEnabled(boolean enabled) { 082 this.enabled = enabled; 083 } 084 085 086 /** 087 * Use the {@linkplain #setDatabasePopulator database populator} to set up 088 * the database. 089 */ 090 @Override 091 public void afterPropertiesSet() { 092 execute(this.databasePopulator); 093 } 094 095 /** 096 * Use the {@linkplain #setDatabaseCleaner database cleaner} to clean up the 097 * database. 098 */ 099 @Override 100 public void destroy() { 101 execute(this.databaseCleaner); 102 } 103 104 private void execute(DatabasePopulator populator) { 105 Assert.state(this.dataSource != null, "DataSource must be set"); 106 if (this.enabled && populator != null) { 107 DatabasePopulatorUtils.execute(populator, this.dataSource); 108 } 109 } 110 111}