Skip to content

Commit

Permalink
Remove ApplicationInfo
Browse files Browse the repository at this point in the history
Rework commit 4a69755b to remove the need for the ApplicationInfo class.
The updated code now uses the auto-configuration class to compute a
default persistence unit root location

Closes gh-6635
  • Loading branch information
philwebb authored and snicoll committed Aug 31, 2016
1 parent b488a3d commit 2a22a7a
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,21 @@

package org.springframework.boot.autoconfigure.orm.jpa;

import java.net.URL;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.ApplicationInfo;
import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
Expand All @@ -50,6 +53,7 @@
import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.jta.JtaTransactionManager;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

Expand All @@ -65,6 +69,8 @@
@Import(DataSourceInitializedPublisher.Registrar.class)
public abstract class JpaBaseConfiguration implements BeanFactoryAware {

private final Log logger = LogFactory.getLog(getClass());

private final DataSource dataSource;

private final JpaProperties properties;
Expand Down Expand Up @@ -104,11 +110,9 @@ public EntityManagerFactoryBuilder entityManagerFactoryBuilder(
ObjectProvider<PersistenceUnitManager> persistenceUnitManagerProvider) {
EntityManagerFactoryBuilder builder = new EntityManagerFactoryBuilder(
jpaVendorAdapter, this.properties.getProperties(),
persistenceUnitManagerProvider.getIfAvailable());
persistenceUnitManagerProvider.getIfAvailable(),
determinePersistenceUnitRootLocation());
builder.setCallback(getVendorCallback());
if (this.beanFactory.containsBean("springApplicationInfo")) {
builder.setApplicationInfo(this.beanFactory.getBean(ApplicationInfo.class));
}
return builder;
}

Expand Down Expand Up @@ -186,6 +190,19 @@ public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
}

private URL determinePersistenceUnitRootLocation() {
Class<?> source = getClass();
try {
URL url = source.getProtectionDomain().getCodeSource().getLocation();
return ResourceUtils.extractJarFileURL(url);
}
catch (Exception ex) {
logger.info("Could not determine persistence "
+ "unit root location from " + source + " : " + ex);
}
return null;
}

@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass(WebMvcConfigurerAdapter.class)
Expand Down
4 changes: 2 additions & 2 deletions spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ You can also use the `spring.main.banner-mode` property to determine if the bann
to be printed on `System.out` (`console`), using the configured logger (`log`) or not
at all (`off`).

The printed banner will be available via an `ApplicationInfo` registered as a singleton
bean under the name `springApplicationInfo`.
The printed banner will be registered as a singleton bean under the name
`springBootBanner`.

[NOTE]
====
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ welcome = { ->
// Try to print using the banner interface
if (beanFactory != null) {
try {
def appInfo = beanFactory.getBean("springApplicationInfo")
def banner = beanFactory.getBean("springBootBanner")
def out = new java.io.ByteArrayOutputStream()
appInfo.banner.printBanner(environment, null, new java.io.PrintStream(out))
banner.printBanner(environment, null, new java.io.PrintStream(out))
return out.toString()
} catch (Exception ex) {
// Ignore
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -359,10 +359,6 @@ private void prepareContext(ConfigurableApplicationContext context,
if (printedBanner != null) {
context.getBeanFactory().registerSingleton("springBootBanner", printedBanner);
}
ApplicationInfo applicationInfo = new ApplicationInfo(this, applicationArguments,
printedBanner);
context.getBeanFactory().registerSingleton("springApplicationInfo",
applicationInfo);

// Load the sources
Set<Object> sources = getSources();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.boot.ApplicationInfo;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager;
import org.springframework.util.ClassUtils;
import org.springframework.util.ResourceUtils;

/**
* Convenient builder for JPA EntityManagerFactory instances. Collects common
Expand All @@ -53,15 +51,15 @@ public class EntityManagerFactoryBuilder {
private static final Log logger = LogFactory
.getLog(EntityManagerFactoryBuilder.class);

private JpaVendorAdapter jpaVendorAdapter;
private final JpaVendorAdapter jpaVendorAdapter;

private PersistenceUnitManager persistenceUnitManager;
private final PersistenceUnitManager persistenceUnitManager;

private Map<String, Object> jpaProperties;
private final Map<String, Object> jpaProperties;

private EntityManagerFactoryBeanCallback callback;
private final URL persistenceUnitRootLocation;

private Class<?> applicationClass;
private EntityManagerFactoryBeanCallback callback;

/**
* Create a new instance passing in the common pieces that will be shared if multiple
Expand All @@ -73,9 +71,27 @@ public class EntityManagerFactoryBuilder {
*/
public EntityManagerFactoryBuilder(JpaVendorAdapter jpaVendorAdapter,
Map<String, ?> jpaProperties, PersistenceUnitManager persistenceUnitManager) {
this(jpaVendorAdapter, jpaProperties, persistenceUnitManager, null);
}

/**
* Create a new instance passing in the common pieces that will be shared if multiple
* EntityManagerFactory instances are created.
* @param jpaVendorAdapter a vendor adapter
* @param jpaProperties JPA properties to be passed to the persistence provider.
* @param persistenceUnitManager optional source of persistence unit information (can
* be null)
* @param persistenceUnitRootLocation the persistence unit root location to use as a
* fallback (can be null)
* @since 1.4.1
*/
public EntityManagerFactoryBuilder(JpaVendorAdapter jpaVendorAdapter,
Map<String, ?> jpaProperties, PersistenceUnitManager persistenceUnitManager,
URL persistenceUnitRootLocation) {
this.jpaVendorAdapter = jpaVendorAdapter;
this.persistenceUnitManager = persistenceUnitManager;
this.jpaProperties = new LinkedHashMap<String, Object>(jpaProperties);
this.persistenceUnitRootLocation = persistenceUnitRootLocation;
}

public Builder dataSource(DataSource dataSource) {
Expand All @@ -90,33 +106,6 @@ public void setCallback(EntityManagerFactoryBeanCallback callback) {
this.callback = callback;
}

/**
* An optional {@link ApplicationInfo} used to further tune the entity manager.
* @param applicationInfo the application info
*/
public void setApplicationInfo(ApplicationInfo applicationInfo) {
this.applicationClass = applicationInfo.getMainApplicationClass();
}

/**
* Determine a persistence unit root location to use if no {@code persistence.xml} or
* {@code orm.xml} are present in the project.
* @return the persistence unit root location or {@code null}
*/
protected String determinePersistenceUnitRootLocation() {
if (this.applicationClass != null) {
try {
URL mainLocation = this.applicationClass.getProtectionDomain()
.getCodeSource().getLocation();
return ResourceUtils.extractJarFileURL(mainLocation).toString();
}
catch (Exception ex) {
logger.info("Could not determine persistence unit root location: " + ex);
}
}
return null;
}

/**
* A fluent builder for a LocalContainerEntityManagerFactoryBean.
*/
Expand Down Expand Up @@ -220,9 +209,9 @@ public LocalContainerEntityManagerFactoryBean build() {
entityManagerFactoryBean.getJpaPropertyMap()
.putAll(EntityManagerFactoryBuilder.this.jpaProperties);
entityManagerFactoryBean.getJpaPropertyMap().putAll(this.properties);
String rootLocation = determinePersistenceUnitRootLocation();
URL rootLocation = EntityManagerFactoryBuilder.this.persistenceUnitRootLocation;
if (rootLocation != null) {
entityManagerFactoryBean.setPersistenceUnitRootLocation(rootLocation);
entityManagerFactoryBean.setPersistenceUnitRootLocation(rootLocation.toString());
}
if (EntityManagerFactoryBuilder.this.callback != null) {
EntityManagerFactoryBuilder.this.callback
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,6 @@ public void testBannerInContext() throws Exception {
application.setWebEnvironment(false);
this.context = application.run();
assertThat(this.context.containsBean("springBootBanner")).isTrue();
assertThat(this.context.containsBean("springApplicationInfo")).isTrue();
assertThat(this.context.getBean("springApplicationInfo", ApplicationInfo.class)
.getBanner()).isNotNull();
}

@Test
Expand All @@ -112,9 +109,7 @@ public void testCustomBannerInContext() throws Exception {
Banner banner = mock(Banner.class);
application.setBanner(banner);
this.context = application.run();
ApplicationInfo applicationInfo = this.context.getBean("springApplicationInfo",
ApplicationInfo.class);
Banner printedBanner = applicationInfo.getBanner();
Banner printedBanner = (Banner) this.context.getBean("springBootBanner");
assertThat(ReflectionTestUtils.getField(printedBanner, "banner"))
.isEqualTo(banner);
verify(banner).printBanner(any(Environment.class),
Expand All @@ -132,8 +127,6 @@ public void testDisableBannerInContext() throws Exception {
application.setWebEnvironment(false);
this.context = application.run();
assertThat(this.context.containsBean("springBootBanner")).isFalse();
assertThat(this.context.getBean("springApplicationInfo", ApplicationInfo.class)
.getBanner()).isNull();
}

@Test
Expand All @@ -150,8 +143,6 @@ protected void printBanner(Environment environment) {
this.context = application.run();
assertThat(this.out.toString()).contains("I printed a deprecated banner");
assertThat(this.context.containsBean("springBootBanner")).isFalse();
assertThat(this.context.getBean("springApplicationInfo", ApplicationInfo.class)
.getBanner()).isNull();
}

static class DummyBanner implements Banner {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -778,21 +778,6 @@ public void headlessSystemPropertyTakesPrecedence() throws Exception {
assertThat(System.getProperty("java.awt.headless")).isEqualTo("false");
}

@Test
public void getApplicationInfo() {
TestSpringApplication application = new TestSpringApplication(
ExampleConfig.class);
application.setWebEnvironment(false);
this.context = application.run("foo");
ApplicationInfo applicationInfo = this.context.getBean(ApplicationInfo.class);
assertThat(application.getMainApplicationClass())
.isEqualTo(application.getMainApplicationClass());
assertThat(applicationInfo.getApplicationArguments()).isNotNull();
assertThat(applicationInfo.getApplicationArguments().getNonOptionArgs())
.containsExactly("foo");
assertThat(applicationInfo.getBanner()).isNotNull();
}

@Test
public void getApplicationArgumentsBean() throws Exception {
TestSpringApplication application = new TestSpringApplication(
Expand Down

0 comments on commit 2a22a7a

Please sign in to comment.