From 1f1a7e0ed3a125f6fbd6d5401a85083ebf33c598 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 24 Jun 2014 11:46:04 -0700 Subject: [PATCH] Polish --- ...arentContextCloserApplicationListener.java | 32 ++++++++----------- .../event/EventPublishingRunListener.java | 7 ++-- .../boot/SpringApplicationTests.java | 1 + 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/builder/ParentContextCloserApplicationListener.java b/spring-boot/src/main/java/org/springframework/boot/builder/ParentContextCloserApplicationListener.java index 271943e1bf..9f5418330b 100644 --- a/spring-boot/src/main/java/org/springframework/boot/builder/ParentContextCloserApplicationListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/builder/ParentContextCloserApplicationListener.java @@ -26,6 +26,7 @@ import org.springframework.context.ApplicationListener; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.event.ContextClosedEvent; import org.springframework.core.Ordered; +import org.springframework.util.ObjectUtils; /** * Listener that closes the application context if its parent is closed. It listens for @@ -77,6 +78,9 @@ public class ParentContextCloserApplicationListener implements return new ContextCloserListener(child); } + /** + * {@link ApplicationListener} to close the context. + */ protected static class ContextCloserListener implements ApplicationListener { @@ -99,31 +103,23 @@ public class ParentContextCloserApplicationListener implements @Override public int hashCode() { - final int prime = 31; - int result = 1; - result = prime - * result - + ((this.childContext.get() == null) ? 0 : this.childContext.get() - .hashCode()); - return result; + return ObjectUtils.nullSafeHashCode(this.childContext.get()); } @Override public boolean equals(Object obj) { - if (this == obj) + if (this == obj) { return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - ContextCloserListener other = (ContextCloserListener) obj; - if (this.childContext.get() == null) { - if (other.childContext.get() != null) - return false; } - else if (!this.childContext.get().equals(other.childContext.get())) + if (obj == null) { return false; - return true; + } + if (obj instanceof ContextCloserListener) { + ContextCloserListener other = (ContextCloserListener) obj; + return ObjectUtils.nullSafeEquals(this.childContext.get(), + other.childContext.get()); + } + return super.equals(obj); } } diff --git a/spring-boot/src/main/java/org/springframework/boot/context/event/EventPublishingRunListener.java b/spring-boot/src/main/java/org/springframework/boot/context/event/EventPublishingRunListener.java index fc06444715..5245619745 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/event/EventPublishingRunListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/event/EventPublishingRunListener.java @@ -90,10 +90,9 @@ public class EventPublishingRunListener implements SpringApplicationRunListener @Override public void finished(ConfigurableApplicationContext context, Throwable exception) { if (exception != null) { - publishEvent(new ApplicationFailedEvent(this.application, this.args, context, - exception)); - } - else { + ApplicationFailedEvent event = new ApplicationFailedEvent(this.application, + this.args, context, exception); + publishEvent(event); } } diff --git a/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java b/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java index 578997c6f6..23ab071b89 100644 --- a/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java @@ -80,6 +80,7 @@ import static org.mockito.Mockito.verify; * Tests for {@link SpringApplication}. * * @author Phillip Webb + * @author Dave Syer */ public class SpringApplicationTests {