Author: mrdon
Date: Thu May  1 19:29:02 2008
New Revision: 652734

URL: http://svn.apache.org/viewvc?rev=652734&view=rev
Log:
Fixing locale so it can be set in struts.xml
WW-2068

Added:
    
struts/struts2/trunk/core/src/test/java/org/apache/struts2/config/LegacyPropertiesConfigurationProviderTest.java
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
    
struts/struts2/trunk/core/src/test/java/org/apache/struts2/config/SettingsTest.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=652734&r1=652733&r2=652734&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  1 19:29:02 2008
@@ -23,6 +23,7 @@
 
 import java.util.Iterator;
 import java.util.Locale;
+import java.util.StringTokenizer;
 
 import com.opensymphony.xwork2.config.Configuration;
 import com.opensymphony.xwork2.config.ConfigurationException;
@@ -30,10 +31,19 @@
 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;
 
 public class LegacyPropertiesConfigurationProvider implements 
ConfigurationProvider {
 
+    /**
+     * The Logging instance for this class.
+     */
+    private static final Logger LOG = 
LoggerFactory.getLogger(LegacyPropertiesConfigurationProvider.class);
+
     public void destroy() {
         Settings.reset();
     }
@@ -58,10 +68,31 @@
         
         loadSettings(props, settings);
         
-        // Set default locale
-        final Locale locale = settings.getLocale();
+        // Set default locale by lazily resolving the locale property as 
needed into a Locale object
         builder.factory(Locale.class, new Factory() {
-            public Object create(Context context) throws Exception {
+            private Locale locale;
+
+            public synchronized Object create(Context context) throws 
Exception {
+                if (locale == null) {
+                    String loc = 
context.getContainer().getInstance(String.class, StrutsConstants.STRUTS_LOCALE);
+                    if (loc != null) {
+                        StringTokenizer localeTokens = new 
StringTokenizer(loc, "_");
+                        String lang = null;
+                        String country = null;
+
+                        if (localeTokens.hasMoreTokens()) {
+                            lang = localeTokens.nextToken();
+                        }
+
+                        if (localeTokens.hasMoreTokens()) {
+                            country = localeTokens.nextToken();
+                        }
+                        locale = new Locale(lang, country);
+                    } else {
+                        LOG.info("No locale define, substituting the default 
VM locale");
+                        locale = Locale.getDefault();
+                    }
+                }
                 return locale;
             }
         });

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=652734&r1=652733&r2=652734&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  1 19:29:02 2008
@@ -57,6 +57,7 @@
  * <li>[EMAIL PROTECTED] #listImpl()}</li>
  * <li>[EMAIL PROTECTED] #isSetImpl(String)}</li>
  * </ul>
+ * @deprecated Since Struts 2.1.2
  */
 class Settings {
 
@@ -124,27 +125,7 @@
      * @see java.util.Locale#getDefault()
      */
     public static Locale getLocale() {
-        if (locale == null) {
-            try {
-                StringTokenizer localeTokens = new 
StringTokenizer(get(StrutsConstants.STRUTS_LOCALE), "_");
-                String lang = null;
-                String country = null;
-
-                if (localeTokens.hasMoreTokens()) {
-                    lang = localeTokens.nextToken();
-                }
-
-                if (localeTokens.hasMoreTokens()) {
-                    country = localeTokens.nextToken();
-                }
-
-                locale = new Locale(lang, country);
-            } catch (Throwable t) {
-                // Default
-                LOG.warn("Settings: Could not parse struts.locale setting, 
substituting default VM locale");
-                locale = Locale.getDefault();
-            }
-        }
+        // Locale processing has been moved to the 
LegacyPropertiesConfigurationProvider
 
         return locale;
     }

Added: 
struts/struts2/trunk/core/src/test/java/org/apache/struts2/config/LegacyPropertiesConfigurationProviderTest.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/config/LegacyPropertiesConfigurationProviderTest.java?rev=652734&view=auto
==============================================================================
--- 
struts/struts2/trunk/core/src/test/java/org/apache/struts2/config/LegacyPropertiesConfigurationProviderTest.java
 (added)
+++ 
struts/struts2/trunk/core/src/test/java/org/apache/struts2/config/LegacyPropertiesConfigurationProviderTest.java
 Thu May  1 19:29:02 2008
@@ -0,0 +1,79 @@
+/*
+ * $Id: SettingsTest.java 651946 2008-04-27 13:41:38Z apetrelli $
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.struts2.config;
+
+import java.util.Iterator;
+import java.util.Locale;
+
+import org.apache.struts2.StrutsConstants;
+import org.apache.struts2.StrutsTestCase;
+
+import com.opensymphony.xwork2.util.LocalizedTextUtil;
+import com.opensymphony.xwork2.util.location.LocatableProperties;
+import com.opensymphony.xwork2.inject.ContainerBuilder;
+import com.opensymphony.xwork2.inject.Container;
+import junit.framework.TestCase;
+
+
+/**
+ * Unit test for [EMAIL PROTECTED] SettingsTest}.
+ *
+ */
+public class LegacyPropertiesConfigurationProviderTest extends TestCase {
+
+    public void testRegister_DifferentLocale() {
+
+        ContainerBuilder builder = new ContainerBuilder();
+        builder.constant("foo", "bar");
+        builder.constant("struts.locale", "DE_de");
+
+        LegacyPropertiesConfigurationProvider prov = new 
LegacyPropertiesConfigurationProvider();
+        prov.register(builder, new LocatableProperties());
+
+        Container container = builder.create(true);
+
+        Locale locale = container.getInstance(Locale.class);
+
+        assertNotNull(locale);
+        assertEquals("DE", locale.getCountry());
+        assertEquals("de", locale.getLanguage());
+
+    }
+
+    public void testRegister_NoLocale() {
+
+        ContainerBuilder builder = new ContainerBuilder();
+        builder.constant("foo", "bar");
+
+        LegacyPropertiesConfigurationProvider prov = new 
LegacyPropertiesConfigurationProvider();
+        prov.register(builder, new LocatableProperties());
+
+        Container container = builder.create(true);
+
+        Locale locale = container.getInstance(Locale.class);
+
+        assertNotNull(locale);
+        Locale vmLocale = Locale.getDefault();
+        assertEquals(locale, vmLocale);
+    }
+
+}

Modified: 
struts/struts2/trunk/core/src/test/java/org/apache/struts2/config/SettingsTest.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/config/SettingsTest.java?rev=652734&r1=652733&r2=652734&view=diff
==============================================================================
--- 
struts/struts2/trunk/core/src/test/java/org/apache/struts2/config/SettingsTest.java
 (original)
+++ 
struts/struts2/trunk/core/src/test/java/org/apache/struts2/config/SettingsTest.java
 Thu May  1 19:29:02 2008
@@ -44,9 +44,6 @@
         assertEquals("testvalue", Settings.get("testkey"));
         assertEquals("othertestvalue", Settings.get("othertestkey"));
 
-        Locale locale = Settings.getLocale();
-        assertEquals("de", locale.getLanguage());
-
         int count = getKeyCount();
         assertEquals(11, count);
     }


Reply via email to