Polish ParentContextCloserApplicationListener

Fix formatting for the recent ParentContextCloserApplicationListener
change, also some minor internal renames.
This commit is contained in:
Phillip Webb 2014-03-24 11:02:38 -07:00
parent 9d2983e994
commit 39cbb1d0ae

View File

@ -25,13 +25,15 @@ import org.springframework.context.event.ContextClosedEvent;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
/** /**
* Listener that closes the application context if its parent is closed. It listens for refresh events and grabs the * Listener that closes the application context if its parent is closed. It listens for
* current context from there, and then listens for closed events and propagates it down the hierarchy. * refresh events and grabs the current context from there, and then listens for closed
* events and propagates it down the hierarchy.
* *
* @author Dave Syer * @author Dave Syer
* @author Eric Bottard
*/ */
public class ParentContextCloserApplicationListener implements ApplicationListener<ParentContextAvailableEvent>, public class ParentContextCloserApplicationListener implements
Ordered { ApplicationListener<ParentContextAvailableEvent>, Ordered {
@Override @Override
public int getOrder() { public int getOrder() {
@ -39,38 +41,43 @@ public class ParentContextCloserApplicationListener implements ApplicationListen
} }
@Override @Override
public final void onApplicationEvent(ParentContextAvailableEvent event) { public void onApplicationEvent(ParentContextAvailableEvent event) {
maybeInstallListenerInParent(event.getApplicationContext()); maybeInstallListenerInParent(event.getApplicationContext());
} }
private void maybeInstallListenerInParent(ConfigurableApplicationContext child) { private void maybeInstallListenerInParent(ConfigurableApplicationContext child) {
if (child.getParent() instanceof ConfigurableApplicationContext) { if (child.getParent() instanceof ConfigurableApplicationContext) {
ConfigurableApplicationContext parent = (ConfigurableApplicationContext) child.getParent(); ConfigurableApplicationContext parent = (ConfigurableApplicationContext) child
.getParent();
parent.addApplicationListener(createContextCloserListener(child)); parent.addApplicationListener(createContextCloserListener(child));
} }
} }
/** /**
* Subclasses may override to create their own subclass of ContextCloserListener. This still enforces the use of a * Subclasses may override to create their own subclass of ContextCloserListener. This
* weak reference. * still enforces the use of a weak reference.
*/ */
protected ContextCloserListener createContextCloserListener(ConfigurableApplicationContext child) { protected ContextCloserListener createContextCloserListener(
ConfigurableApplicationContext child) {
return new ContextCloserListener(child); return new ContextCloserListener(child);
} }
protected static class ContextCloserListener implements ApplicationListener<ContextClosedEvent> { protected static class ContextCloserListener implements
ApplicationListener<ContextClosedEvent> {
private WeakReference<ConfigurableApplicationContext> contextRef; private WeakReference<ConfigurableApplicationContext> childContext;
public ContextCloserListener(ConfigurableApplicationContext context) {
this.contextRef = new WeakReference<ConfigurableApplicationContext>(context);
public ContextCloserListener(ConfigurableApplicationContext childContext) {
this.childContext = new WeakReference<ConfigurableApplicationContext>(
childContext);
} }
@Override @Override
public void onApplicationEvent(ContextClosedEvent event) { public void onApplicationEvent(ContextClosedEvent event) {
ConfigurableApplicationContext context = contextRef.get(); ConfigurableApplicationContext context = this.childContext.get();
if (context != null && event.getApplicationContext() == context.getParent() && context.isActive()) { if ((context != null)
&& (event.getApplicationContext() == context.getParent())
&& context.isActive()) {
context.close(); context.close();
} }
} }