Author: lukaszlenart Date: Thu May 23 05:34:19 2013 New Revision: 1485576 URL: http://svn.apache.org/r1485576 Log: WW-3689 Adds synchronized block with global lock to avoid NPE
Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/config/LegacyPropertiesConfigurationProvider.java struts/struts2/trunk/core/src/main/java/org/apache/struts2/config/Settings.java Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/config/LegacyPropertiesConfigurationProvider.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/config/LegacyPropertiesConfigurationProvider.java?rev=1485576&r1=1485575&r2=1485576&view=diff ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/config/LegacyPropertiesConfigurationProvider.java (original) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/config/LegacyPropertiesConfigurationProvider.java Thu May 23 05:34:19 2013 @@ -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 LegacyPropertiesConfigurati 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<Locale>() { 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) { Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/config/Settings.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/config/Settings.java?rev=1485576&r1=1485575&r2=1485576&view=diff ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/config/Settings.java (original) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/config/Settings.java Thu May 23 05:34:19 2013 @@ -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; /** @@ -75,6 +73,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 <code>struts.locale</code> setting. * * @see #getLocale @@ -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; } + }