001/*
002 * Copyright 2012-2016 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 *      http://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.boot.autoconfigure;
018
019import javax.annotation.PostConstruct;
020import javax.sql.DataSource;
021
022import org.springframework.boot.jdbc.DatabaseDriver;
023import org.springframework.core.io.ResourceLoader;
024import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
025import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
026import org.springframework.jdbc.support.JdbcUtils;
027import org.springframework.jdbc.support.MetaDataAccessException;
028import org.springframework.util.Assert;
029
030/**
031 * Base class used for database initialization.
032 *
033 * @author Vedran Pavic
034 * @author Stephane Nicoll
035 * @since 1.5.0
036 */
037public abstract class AbstractDatabaseInitializer {
038
039        private static final String PLATFORM_PLACEHOLDER = "@@platform@@";
040
041        private final DataSource dataSource;
042
043        private final ResourceLoader resourceLoader;
044
045        protected AbstractDatabaseInitializer(DataSource dataSource,
046                        ResourceLoader resourceLoader) {
047                Assert.notNull(dataSource, "DataSource must not be null");
048                Assert.notNull(resourceLoader, "ResourceLoader must not be null");
049                this.dataSource = dataSource;
050                this.resourceLoader = resourceLoader;
051        }
052
053        @PostConstruct
054        protected void initialize() {
055                if (!isEnabled()) {
056                        return;
057                }
058                ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
059                String schemaLocation = getSchemaLocation();
060                if (schemaLocation.contains(PLATFORM_PLACEHOLDER)) {
061                        String platform = getDatabaseName();
062                        schemaLocation = schemaLocation.replace(PLATFORM_PLACEHOLDER, platform);
063                }
064                populator.addScript(this.resourceLoader.getResource(schemaLocation));
065                populator.setContinueOnError(true);
066                DatabasePopulatorUtils.execute(populator, this.dataSource);
067        }
068
069        protected abstract boolean isEnabled();
070
071        protected abstract String getSchemaLocation();
072
073        protected String getDatabaseName() {
074                try {
075                        String productName = JdbcUtils.commonDatabaseName(JdbcUtils
076                                        .extractDatabaseMetaData(this.dataSource, "getDatabaseProductName")
077                                        .toString());
078                        DatabaseDriver databaseDriver = DatabaseDriver.fromProductName(productName);
079                        if (databaseDriver == DatabaseDriver.UNKNOWN) {
080                                throw new IllegalStateException("Unable to detect database type");
081                        }
082                        return databaseDriver.getId();
083                }
084                catch (MetaDataAccessException ex) {
085                        throw new IllegalStateException("Unable to detect database type", ex);
086                }
087        }
088
089}