diff --git a/core/src/main/java/org/apache/struts2/config/LegacyPropertiesConfigurationProvider.java b/core/src/main/java/org/apache/struts2/config/LegacyPropertiesConfigurationProvider.java index 79262f039..f82e36fe3 100644 --- a/core/src/main/java/org/apache/struts2/config/LegacyPropertiesConfigurationProvider.java +++ b/core/src/main/java/org/apache/struts2/config/LegacyPropertiesConfigurationProvider.java @@ -21,22 +21,21 @@ package org.apache.struts2.config; -import java.util.Iterator; -import java.util.Locale; -import java.util.StringTokenizer; - import com.opensymphony.xwork2.config.Configuration; import com.opensymphony.xwork2.config.ConfigurationException; import com.opensymphony.xwork2.config.ConfigurationProvider; import com.opensymphony.xwork2.inject.ContainerBuilder; import com.opensymphony.xwork2.inject.Context; import com.opensymphony.xwork2.inject.Factory; -import com.opensymphony.xwork2.inject.Inject; import com.opensymphony.xwork2.util.location.LocatableProperties; import com.opensymphony.xwork2.util.logging.Logger; import com.opensymphony.xwork2.util.logging.LoggerFactory; import org.apache.struts2.StrutsConstants; +import java.util.Iterator; +import java.util.Locale; +import java.util.StringTokenizer; + public class LegacyPropertiesConfigurationProvider implements ConfigurationProvider { /** @@ -69,10 +68,10 @@ public class LegacyPropertiesConfigurationProvider implements ConfigurationProvi loadSettings(props, settings); // Set default locale by lazily resolving the locale property as needed into a Locale object - builder.factory(Locale.class, new Factory() { + builder.factory(Locale.class, new Factory() { private Locale locale; - public synchronized Object create(Context context) throws Exception { + public synchronized Locale create(Context context) throws Exception { if (locale == null) { String loc = context.getContainer().getInstance(String.class, StrutsConstants.STRUTS_LOCALE); if (loc != null) { diff --git a/core/src/main/java/org/apache/struts2/config/Settings.java b/core/src/main/java/org/apache/struts2/config/Settings.java index a7d1765be..a654bd1ce 100644 --- a/core/src/main/java/org/apache/struts2/config/Settings.java +++ b/core/src/main/java/org/apache/struts2/config/Settings.java @@ -21,16 +21,14 @@ package org.apache.struts2.config; -import java.util.Iterator; -import java.util.Locale; -import java.util.StringTokenizer; - -import org.apache.struts2.StrutsConstants; - import com.opensymphony.xwork2.ObjectFactory; import com.opensymphony.xwork2.util.location.Location; import com.opensymphony.xwork2.util.logging.Logger; import com.opensymphony.xwork2.util.logging.LoggerFactory; +import org.apache.struts2.StrutsConstants; + +import java.util.Iterator; +import java.util.Locale; /** @@ -74,6 +72,11 @@ class Settings { */ static Settings defaultImpl; + /** + * Guard used to protect the defaultImpl initialisation. + */ + private static final Object DEFAULT_LOCK = new Object(); + /** * An instance of the default locale as specified by the struts.locale setting. * @@ -267,23 +270,27 @@ class Settings { */ private static Settings getDefaultInstance() { if (defaultImpl == null) { - // Create bootstrap implementation - defaultImpl = new DefaultSettings(); + synchronized (DEFAULT_LOCK) { + if (defaultImpl == null) { + // Create bootstrap implementation + defaultImpl = new DefaultSettings(); - // Create default implementation - try { - String className = get(StrutsConstants.STRUTS_CONFIGURATION); - - if (!className.equals(defaultImpl.getClass().getName())) { + // Create default implementation try { - // singleton instances shouldn't be built accessing request or session-specific context data - defaultImpl = (Settings) ObjectFactory.getObjectFactory().buildBean(Thread.currentThread().getContextClassLoader().loadClass(className), null); - } catch (Exception e) { - LOG.error("Settings: Could not instantiate the struts.configuration object, substituting the default implementation.", e); + String className = get(StrutsConstants.STRUTS_CONFIGURATION); + + if (!className.equals(defaultImpl.getClass().getName())) { + try { + // singleton instances shouldn't be built accessing request or session-specific context data + defaultImpl = (Settings) ObjectFactory.getObjectFactory().buildBean(Thread.currentThread().getContextClassLoader().loadClass(className), null); + } catch (Exception e) { + LOG.error("Settings: Could not instantiate the struts.configuration object, substituting the default implementation.", e); + } + } + } catch (IllegalArgumentException ex) { + // ignore } } - } catch (IllegalArgumentException ex) { - // ignore } } @@ -294,7 +301,10 @@ class Settings { * Resets the default and any plugin Setting instance to null. */ public static void reset() { - defaultImpl = null; + synchronized (DEFAULT_LOCK) { + defaultImpl = null; + } settingsImpl = null; } + }