|
| 1 | +/* |
| 2 | + * Copyright 2012-2014 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.autoconfigure.jdbc; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import javax.annotation.PostConstruct; |
| 24 | +import javax.sql.DataSource; |
| 25 | + |
| 26 | +import org.apache.commons.logging.Log; |
| 27 | +import org.apache.commons.logging.LogFactory; |
| 28 | +import org.springframework.beans.factory.annotation.Autowired; |
| 29 | +import org.springframework.context.ApplicationContext; |
| 30 | +import org.springframework.context.ApplicationListener; |
| 31 | +import org.springframework.core.io.Resource; |
| 32 | +import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils; |
| 33 | +import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; |
| 34 | +import org.springframework.util.StringUtils; |
| 35 | + |
| 36 | +/** |
| 37 | + * Bean to handle {@link DataSource} initialization by running {@literal schema-*.sql} on |
| 38 | + * {@link PostConstruct} and and {@literal data-*.sql} SQL scripts on a |
| 39 | + * {@link DataSourceInitializedEvent}. |
| 40 | + * |
| 41 | + * @author Dave Syer |
| 42 | + * @author Phillip Webb |
| 43 | + * @since 1.1.0 |
| 44 | + * @see DataSourceAutoConfiguration |
| 45 | + */ |
| 46 | +class DataSourceInitializer implements ApplicationListener<DataSourceInitializedEvent> { |
| 47 | + |
| 48 | + private static Log logger = LogFactory.getLog(DataSourceInitializer.class); |
| 49 | + |
| 50 | + @Autowired |
| 51 | + private ApplicationContext applicationContext; |
| 52 | + |
| 53 | + @Autowired(required = false) |
| 54 | + private DataSource dataSource; |
| 55 | + |
| 56 | + @Autowired |
| 57 | + private DataSourceProperties properties; |
| 58 | + |
| 59 | + private boolean initialized = false; |
| 60 | + |
| 61 | + @PostConstruct |
| 62 | + protected void initialize() { |
| 63 | + if (!this.properties.isInitialize()) { |
| 64 | + logger.debug("Initialization disabled (not running DDL scripts)"); |
| 65 | + return; |
| 66 | + } |
| 67 | + if (this.dataSource == null) { |
| 68 | + logger.debug("No DataSource found so not initializing"); |
| 69 | + return; |
| 70 | + } |
| 71 | + runSchemaScripts(); |
| 72 | + } |
| 73 | + |
| 74 | + private void runSchemaScripts() { |
| 75 | + List<Resource> scripts = getScripts(this.properties.getSchema(), "schema"); |
| 76 | + if (!scripts.isEmpty()) { |
| 77 | + runScripts(scripts); |
| 78 | + this.applicationContext.publishEvent(new DataSourceInitializedEvent( |
| 79 | + this.dataSource)); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void onApplicationEvent(DataSourceInitializedEvent event) { |
| 85 | + // NOTE the even can happen more than once and |
| 86 | + // the event datasource if not used here |
| 87 | + if (!this.initialized) { |
| 88 | + runDataScripts(); |
| 89 | + this.initialized = true; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private void runDataScripts() { |
| 94 | + List<Resource> scripts = getScripts(this.properties.getData(), "data"); |
| 95 | + runScripts(scripts); |
| 96 | + } |
| 97 | + |
| 98 | + private List<Resource> getScripts(String locations, String fallback) { |
| 99 | + if (locations == null) { |
| 100 | + String platform = this.properties.getPlatform(); |
| 101 | + locations = "classpath*:" + fallback + "-" + platform + ".sql,"; |
| 102 | + locations += "classpath*:" + fallback + ".sql"; |
| 103 | + } |
| 104 | + return getResources(locations); |
| 105 | + } |
| 106 | + |
| 107 | + private List<Resource> getResources(String locations) { |
| 108 | + List<Resource> resources = new ArrayList<Resource>(); |
| 109 | + for (String location : StringUtils.commaDelimitedListToStringArray(locations)) { |
| 110 | + try { |
| 111 | + for (Resource resource : this.applicationContext.getResources(location)) { |
| 112 | + if (resource.exists()) { |
| 113 | + resources.add(resource); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + catch (IOException ex) { |
| 118 | + throw new IllegalStateException("Unable to load resource from " |
| 119 | + + location, ex); |
| 120 | + } |
| 121 | + } |
| 122 | + return resources; |
| 123 | + } |
| 124 | + |
| 125 | + private void runScripts(List<Resource> resources) { |
| 126 | + if (resources.isEmpty()) { |
| 127 | + return; |
| 128 | + } |
| 129 | + ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); |
| 130 | + populator.setContinueOnError(this.properties.isContinueOnError()); |
| 131 | + populator.setSeparator(this.properties.getSeparator()); |
| 132 | + for (Resource resource : resources) { |
| 133 | + populator.addScript(resource); |
| 134 | + } |
| 135 | + DatabasePopulatorUtils.execute(populator, this.dataSource); |
| 136 | + } |
| 137 | + |
| 138 | +} |
0 commit comments