11/*
2- * Copyright 2002-2013 the original author or authors.
2+ * Copyright 2002-2014 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
2626import java .util .Locale ;
2727import java .util .Map ;
2828import java .util .Set ;
29+ import java .util .concurrent .atomic .AtomicBoolean ;
2930
3031import org .apache .commons .logging .Log ;
3132import org .apache .commons .logging .LogFactory ;
@@ -169,13 +170,10 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
169170 private long startupDate ;
170171
171172 /** Flag that indicates whether this context is currently active */
172- private boolean active = false ;
173+ private final AtomicBoolean active = new AtomicBoolean () ;
173174
174175 /** Flag that indicates whether this context has been closed already */
175- private boolean closed = false ;
176-
177- /** Synchronization monitor for the "active" flag */
178- private final Object activeMonitor = new Object ();
176+ private final AtomicBoolean closed = new AtomicBoolean ();
179177
180178 /** Synchronization monitor for the "refresh" and "destroy" */
181179 private final Object startupShutdownMonitor = new Object ();
@@ -504,10 +502,7 @@ public void refresh() throws BeansException, IllegalStateException {
504502 */
505503 protected void prepareRefresh () {
506504 this .startupDate = System .currentTimeMillis ();
507-
508- synchronized (this .activeMonitor ) {
509- this .active = true ;
510- }
505+ this .active .set (true );
511506
512507 if (logger .isInfoEnabled ()) {
513508 logger .info ("Refreshing " + this );
@@ -785,9 +780,7 @@ protected void finishRefresh() {
785780 * @param ex the exception that led to the cancellation
786781 */
787782 protected void cancelRefresh (BeansException ex ) {
788- synchronized (this .activeMonitor ) {
789- this .active = false ;
790- }
783+ this .active .set (false );
791784 }
792785
793786
@@ -862,13 +855,7 @@ public void close() {
862855 * @see #registerShutdownHook()
863856 */
864857 protected void doClose () {
865- boolean actuallyClose ;
866- synchronized (this .activeMonitor ) {
867- actuallyClose = this .active && !this .closed ;
868- this .closed = true ;
869- }
870-
871- if (actuallyClose ) {
858+ if (this .active .get () && this .closed .compareAndSet (false , true )) {
872859 if (logger .isInfoEnabled ()) {
873860 logger .info ("Closing " + this );
874861 }
@@ -900,9 +887,7 @@ protected void doClose() {
900887 // Let subclasses do some final clean-up if they wish...
901888 onClose ();
902889
903- synchronized (this .activeMonitor ) {
904- this .active = false ;
905- }
890+ this .active .set (false );
906891 }
907892 }
908893
@@ -935,9 +920,7 @@ protected void onClose() {
935920
936921 @ Override
937922 public boolean isActive () {
938- synchronized (this .activeMonitor ) {
939- return this .active ;
940- }
923+ return this .active .get ();
941924 }
942925
943926 /**
@@ -950,14 +933,12 @@ public boolean isActive() {
950933 * no-op if {@link #getBeanFactory()} itself throws an exception in such a case.
951934 */
952935 protected void assertBeanFactoryActive () {
953- synchronized (this .activeMonitor ) {
954- if (!this .active ) {
955- if (this .closed ) {
956- throw new IllegalStateException (getDisplayName () + " has been closed already" );
957- }
958- else {
959- throw new IllegalStateException (getDisplayName () + " has not been refreshed yet" );
960- }
936+ if (!this .active .get ()) {
937+ if (this .closed .get ()) {
938+ throw new IllegalStateException (getDisplayName () + " has been closed already" );
939+ }
940+ else {
941+ throw new IllegalStateException (getDisplayName () + " has not been refreshed yet" );
961942 }
962943 }
963944 }
0 commit comments