1. opensymphony
  2. xwork

Commits

rgielen  committed b228683

XW-487 / XW-476:
Removing singleton pattern from TextProviderFatory.

See also:
https://issues.apache.org/struts/browse/WW-1856

git-svn-id: http://svn.opensymphony.com/svn/xwork/branches/2.0@1394e221344d-f017-0410-9bd5-d282ab1896d7

  • Participants
  • Parent commits 90408fa
  • Branches 2.0, xwork_2_0_7

Comments (0)

Files changed (5)

File src/java/com/opensymphony/xwork2/ActionSupport.java

View file
  • Ignore whitespace
 
     protected static Log LOG = LogFactory.getLog(ActionSupport.class);
 
-    private final transient TextProvider textProvider = TextProviderFactory.getInstance(getClass(), this);
+    private final transient TextProvider textProvider = new TextProviderFactory().createInstance(getClass(), this);
     private final ValidationAwareSupport validationAware = new ValidationAwareSupport();
 
 

File src/java/com/opensymphony/xwork2/ResourceBundleTextProvider.java

View file
  • Ignore whitespace
+package com.opensymphony.xwork2;
+
+import java.util.ResourceBundle;
+
+/**
+ * Extension Interface for TextProvider to help supporting ResourceBundles.
+ *
+ * @author Rene Gielen
+ */
+
+public interface ResourceBundleTextProvider extends TextProvider {
+
+    /**
+     * Set the resource bundle to use.
+     *
+     * @param bundle the bundle to use.
+     */
+    void setBundle(ResourceBundle bundle);
+
+    /**
+     * Set the class to use for reading the resource bundle.
+     *
+     * @param clazz the class to use for loading.
+     */
+    void setClazz(Class clazz);
+
+    /**
+     * Set the LocaleProvider to use.
+     *
+     * @param localeProvider the LocaleProvider to use.
+     */
+    void setLocaleProvider(LocaleProvider localeProvider);
+
+}

File src/java/com/opensymphony/xwork2/TextProviderFactory.java

View file
  • Ignore whitespace
  */
 package com.opensymphony.xwork2;
 
+import com.opensymphony.xwork2.inject.Inject;
+
 import java.util.ResourceBundle;
 
 /**
- * <code>TextProviderFactory</code>
+ * This factory enables users to provide and correctly initialize a custom TextProvider.
  *
  * @author Oleg Gorobets
+ * @author Rene Gielen
  */
 public class TextProviderFactory {
 
-    private static TextProvider instance = new TextProviderSupport();
+    private TextProvider textProvider;
 
-    /**
-     * @param instance Text provider
-     */
-    public static void setInstance(TextProvider instance) {
-        TextProviderFactory.instance = instance;
+    @Inject
+    public void setTextProvider(TextProvider textProvider) {
+        this.textProvider = textProvider;
     }
 
-    public static TextProvider getInstance() {
-        return instance;
+    protected TextProvider getTextProvider() {
+        if (this.textProvider == null) {
+            return new TextProviderSupport();
+        } else {
+            return textProvider;
+        }
     }
 
-    public static TextProvider getInstance(Class clazz, LocaleProvider provider) {
-        if (instance instanceof TextProviderSupport) {
-            ((TextProviderSupport) instance).setClazz(clazz);
-            ((TextProviderSupport) instance).setLocaleProvider(provider);
+    public TextProvider createInstance(Class clazz, LocaleProvider provider) {
+        TextProvider instance = getTextProvider();
+        if (instance instanceof ResourceBundleTextProvider) {
+            ((ResourceBundleTextProvider) instance).setClazz(clazz);
+            ((ResourceBundleTextProvider) instance).setLocaleProvider(provider);
         }
         return instance;
     }
 
-    public static TextProvider getInstance(ResourceBundle bundle, LocaleProvider provider) {
-        if (instance instanceof TextProviderSupport) {
-            ((TextProviderSupport) instance).setBundle(bundle);
-            ((TextProviderSupport) instance).setLocaleProvider(provider);
+    public TextProvider createInstance(ResourceBundle bundle, LocaleProvider provider) {
+        TextProvider instance = getTextProvider();
+        if (instance instanceof ResourceBundleTextProvider) {
+            ((ResourceBundleTextProvider) instance).setBundle(bundle);
+            ((ResourceBundleTextProvider) instance).setLocaleProvider(provider);
         }
         return instance;
     }

File src/java/com/opensymphony/xwork2/TextProviderSupport.java

View file
  • Ignore whitespace
  * @author Jason Carreira
  * @author Rainer Hermanns
  */
-public class TextProviderSupport implements TextProvider {
+public class TextProviderSupport implements ResourceBundleTextProvider {
 
     private Class clazz;
     private LocaleProvider localeProvider;

File src/java/com/opensymphony/xwork2/validator/DelegatingValidatorContext.java

View file
  • Ignore whitespace
      */
     public DelegatingValidatorContext(Class clazz) {
         localeProvider = new ActionContextLocaleProvider();
-        textProvider = TextProviderFactory.getInstance(clazz, localeProvider);
+        textProvider = new TextProviderFactory().createInstance(clazz, localeProvider);
         validationAware = new LoggingValidationAware(clazz);
     }
 
         if (object instanceof TextProvider) {
             return (TextProvider) object;
         } else {
-            return TextProviderFactory.getInstance(object.getClass(), localeProvider);
+            return new TextProviderFactory().createInstance(object.getClass(), localeProvider);
         }
     }