/*
 * Copyright 2012-2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.bootstrap.autoconfigure.jdbc;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.bootstrap.context.annotation.ConditionalOnMissingBean;
import org.springframework.bootstrap.context.annotation.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.task.AsyncUtils;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.util.ClassUtils;

/**
 * {@link EnableAutoConfiguration Auto-configuration} for embedded databases.
 * 
 * @author Phillip Webb
 */
@Configuration
@Conditional(EmbeddedDatabaseAutoConfiguration.EmbeddedDatabaseCondition.class)
@ConditionalOnMissingBean(DataSource.class)
public class EmbeddedDatabaseAutoConfiguration {

	private static final Map<EmbeddedDatabaseType, String> EMBEDDED_DATABASE_TYPE_CLASSES;
	static {
		EMBEDDED_DATABASE_TYPE_CLASSES = new LinkedHashMap<EmbeddedDatabaseType, String>();
		EMBEDDED_DATABASE_TYPE_CLASSES.put(EmbeddedDatabaseType.HSQL,
				"org.hsqldb.Database");
	}

	private ExecutorService executorService;

	@Autowired
	private ResourceLoader resourceLoader = new DefaultResourceLoader();

	@Value("${spring.jdbc.schema:classpath:schema.sql}")
	private String schemaLocation = "classpath:schema.sql"; // FIXME: DB platform

	@PostConstruct
	protected void initialize() throws Exception {
		Resource resource = this.resourceLoader.getResource(this.schemaLocation);
		if (resource.exists()) {
			ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
			populator.addScript(resource);
			populator.setContinueOnError(true);
			DatabasePopulatorUtils.execute(populator, dataSource());
		}
	}

	@Bean
	public DataSource dataSource() {
		EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder()
				.setType(getEmbeddedDatabaseType());
		return AsyncUtils.submitVia(this.executorService, builder).build();
	}

	public static EmbeddedDatabaseType getEmbeddedDatabaseType() {
		for (Map.Entry<EmbeddedDatabaseType, String> entry : EMBEDDED_DATABASE_TYPE_CLASSES
				.entrySet()) {
			if (ClassUtils.isPresent(entry.getValue(),
					EmbeddedDatabaseAutoConfiguration.class.getClassLoader())) {
				return entry.getKey();
			}
		}
		return null;
	}

	static class EmbeddedDatabaseCondition implements Condition {

		@Override
		public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
			if (!ClassUtils.isPresent(
					"org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType",
					context.getClassLoader())) {
				return false;
			}
			return getEmbeddedDatabaseType() != null;
		}
	}
}

@@ -847,71 +847,8 @@
 e;%0A%0A
-import org.springframework.beans.factory.annotation.Autowired;%0A
 impo
@@ -1393,31 +1393,24 @@
 core.io.
-Default
 Resource
 Loader;%0A
@@ -1405,110 +1405,8 @@
 urce
-Loader;%0Aimport org.springframework.core.io.Resource;%0Aimport org.springframework.core.io.ResourceLoader
 ;%0Aim
@@ -2486,89 +2486,29 @@
 ;%0A%0A%09
-@Autowired%0A%09private ResourceLoader resourceLoader = new DefaultResourceLoader();%0A
+// FIXME: DB platform
 %0A%09@V
@@ -2535,32 +2535,33 @@
 schema:classpath
+*
 :schema.sql%7D%22)%0A%09
@@ -2568,22 +2568,26 @@
 private 
-String
+Resource%5B%5D
  schemaL
@@ -2597,56 +2597,28 @@
 tion
+s
  = 
-%22classpath:schema.sql%22; // FIXME: DB platform
+new Resource%5B0%5D;
 %0A%0A%09@
@@ -2693,52 +2693,124 @@
 urce
- resource = this.resourceLoader.getR
+DatabasePopulator populator = new ResourceDatabasePopulator();%0A%09%09boolean exists = false;%0A%09%09for (Resource r
 esource
-(
+ : 
 this
@@ -2824,19 +2824,22 @@
 Location
-);%0A
+s) %7B%0A%09
 %09%09if (re
@@ -2864,82 +2864,28 @@
 %0A%09%09%09
-ResourceDatabasePopulator populator = new ResourceDatabasePopulator()
+%09exists = true
 ;%0A%09%09%09
+%09
 popu
@@ -2911,16 +2911,17 @@
 ource);%0A
+%09
 %09%09%09popul
@@ -2951,16 +2951,41 @@
 (true);%0A
+%09%09%09%7D%0A%09%09%7D%0A%09%09if (exists) %7B%0A
 %09%09%09Datab

Use Resource[] for database initialization