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