/*
 * 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.boot.autoconfigure.orm.jpa;

import java.util.List;

import javax.persistence.EntityManager;
import javax.sql.DataSource;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.autoconfigure.AutoConfigurationUtils;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter;
import org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.util.Assert;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * Base {@link EnableAutoConfiguration Auto-configuration} for JPA.
 * 
 * @author Phillip Webb
 * @author Dave Syer
 */
@ConditionalOnClass({ LocalContainerEntityManagerFactoryBean.class,
		EnableTransactionManagement.class, EntityManager.class })
@ConditionalOnBean(DataSource.class)
public abstract class JpaBaseConfiguration implements BeanFactoryAware {

	private ConfigurableListableBeanFactory beanFactory;

	@Bean
	public PlatformTransactionManager transactionManager() {
		return new JpaTransactionManager(entityManagerFactory().getObject());
	}

	@Bean
	public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
		LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
		entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter());
		entityManagerFactoryBean.setDataSource(getDataSource());
		entityManagerFactoryBean.setPackagesToScan(getPackagesToScan());
		configure(entityManagerFactoryBean);
		return entityManagerFactoryBean;
	}

	@Configuration
	@ConditionalOnWebApplication
	@ConditionalOnMissingBean({ OpenEntityManagerInViewInterceptor.class,
			OpenEntityManagerInViewFilter.class })
	@ConditionalOnExpression("${spring.jpa.openInView:${spring.jpa.open_in_view:true}}")
	protected static class JpaWebConfiguration extends WebMvcConfigurerAdapter {

		@Override
		public void addInterceptors(InterceptorRegistry registry) {
			registry.addWebRequestInterceptor(openEntityManagerInViewInterceptor());
		}

		@Bean
		public OpenEntityManagerInViewInterceptor openEntityManagerInViewInterceptor() {
			return new OpenEntityManagerInViewInterceptor();
		}

	}

	/**
	 * Determines if the {@code dataSource} being used by Spring was created from
	 * {@link EmbeddedDatabaseConfiguration}.
	 * @return true if the data source was auto-configured.
	 */
	protected boolean isAutoConfiguredDataSource() {
		try {
			BeanDefinition beanDefinition = this.beanFactory
					.getBeanDefinition("dataSource");
			return EmbeddedDatabaseConfiguration.class.getName().equals(
					beanDefinition.getFactoryBeanName());
		}
		catch (NoSuchBeanDefinitionException ex) {
			return false;
		}
	}

	@Bean
	public abstract JpaVendorAdapter jpaVendorAdapter();

	protected DataSource getDataSource() {
		try {
			return this.beanFactory.getBean("dataSource", DataSource.class);
		}
		catch (RuntimeException ex) {
			return this.beanFactory.getBean(DataSource.class);
		}
	}

	protected String[] getPackagesToScan() {
		List<String> basePackages = AutoConfigurationUtils
				.getBasePackages(this.beanFactory);
		Assert.notEmpty(basePackages,
				"Unable to find JPA packages to scan, please define "
						+ "a @ComponentScan annotation or disable JpaAutoConfiguration");
		return basePackages.toArray(new String[basePackages.size()]);
	}

	protected void configure(
			LocalContainerEntityManagerFactoryBean entityManagerFactoryBean) {
	}

	@Override
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
	}
}

@@ -3595,639 +3595,8 @@
 %09%7D%0A%0A
-%09@Configuration%0A%09@ConditionalOnWebApplication%0A%09@ConditionalOnMissingBean(%7B OpenEntityManagerInViewInterceptor.class,%0A%09%09%09OpenEntityManagerInViewFilter.class %7D)%0A%09@ConditionalOnExpression(%22$%7Bspring.jpa.openInView:$%7Bspring.jpa.open_in_view:true%7D%7D%22)%0A%09protected static class JpaWebConfiguration extends WebMvcConfigurerAdapter %7B%0A%0A%09%09@Override%0A%09%09public void addInterceptors(InterceptorRegistry registry) %7B%0A%09%09%09registry.addWebRequestInterceptor(openEntityManagerInViewInterceptor());%0A%09%09%7D%0A%0A%09%09@Bean%0A%09%09public OpenEntityManagerInViewInterceptor openEntityManagerInViewInterceptor() %7B%0A%09%09%09return new OpenEntityManagerInViewInterceptor();%0A%09%09%7D%0A%0A%09%7D%0A%0A
 %09/**
@@ -5010,11 +5010,642 @@
 tory;%0A%09%7D
+%0A%0A%09@Configuration%0A%09@ConditionalOnWebApplication%0A%09@ConditionalOnMissingBean(%7B OpenEntityManagerInViewInterceptor.class,%0A%09%09%09OpenEntityManagerInViewFilter.class %7D)%0A%09@ConditionalOnExpression(%22$%7Bspring.jpa.openInView:$%7Bspring.jpa.open_in_view:true%7D%7D%22)%0A%09protected static class JpaWebConfiguration extends WebMvcConfigurerAdapter %7B%0A%0A%09%09@Override%0A%09%09public void addInterceptors(InterceptorRegistry registry) %7B%0A%09%09%09registry.addWebRequestInterceptor(openEntityManagerInViewInterceptor());%0A%09%09%7D%0A%0A%09%09@Bean%0A%09%09public OpenEntityManagerInViewInterceptor openEntityManagerInViewInterceptor() %7B%0A%09%09%09return new OpenEntityManagerInViewInterceptor();%0A%09%09%7D%0A%0A%09%7D
 %0A%7D%0A

Move inner class to end of file for consistency