Commits

Anonymous committed 3143623

Solved issue XW-572 - refactored ActionSupport to use injecting for TextProvider in TextProviderFactory

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@2078e221344d-f017-0410-9bd5-d282ab1896d7

Comments (0)

Files changed (3)

core/src/main/java/com/opensymphony/xwork2/ActionSupport.java

  */
 package com.opensymphony.xwork2;
 
+import com.opensymphony.xwork2.inject.Container;
+import com.opensymphony.xwork2.inject.Inject;
 import com.opensymphony.xwork2.util.ValueStack;
 import com.opensymphony.xwork2.util.logging.Logger;
 import com.opensymphony.xwork2.util.logging.LoggerFactory;
 
 import java.io.Serializable;
-import java.util.*;
+import java.util.Collection;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.ResourceBundle;
 
 
 /**
 
     protected static Logger LOG = LoggerFactory.getLogger(ActionSupport.class);
 
-    private final transient TextProvider textProvider = new TextProviderFactory().createInstance(getClass(), this);
     private final ValidationAwareSupport validationAware = new ValidationAwareSupport();
 
+    private transient TextProvider textProvider;
+    private Container container;
 
     public void setActionErrors(Collection<String> errorMessages) {
         validationAware.setActionErrors(errorMessages);
     /**
      * @deprecated Use {@link #getActionErrors()}.
      */
-    @Deprecated public Collection<String> getErrorMessages() {
+    @Deprecated
+    public Collection<String> getErrorMessages() {
         return getActionErrors();
     }
 
     /**
      * @deprecated Use {@link #getFieldErrors()}.
      */
-    @Deprecated public Map<String, List<String>> getErrors() {
+    @Deprecated
+    public Map<String, List<String>> getErrors() {
         return getFieldErrors();
     }
 
     }
 
     public boolean hasKey(String key) {
-        return textProvider.hasKey(key);
+        return getTextProvider().hasKey(key);
     }
 
     public String getText(String aTextName) {
-        return textProvider.getText(aTextName);
+        return getTextProvider().getText(aTextName);
     }
 
     public String getText(String aTextName, String defaultValue) {
-        return textProvider.getText(aTextName, defaultValue);
+        return getTextProvider().getText(aTextName, defaultValue);
     }
 
     public String getText(String aTextName, String defaultValue, String obj) {
-        return textProvider.getText(aTextName, defaultValue, obj);
+        return getTextProvider().getText(aTextName, defaultValue, obj);
     }
 
     public String getText(String aTextName, List<Object> args) {
-        return textProvider.getText(aTextName, args);
+        return getTextProvider().getText(aTextName, args);
     }
 
     public String getText(String key, String[] args) {
-        return textProvider.getText(key, args);
+        return getTextProvider().getText(key, args);
     }
 
     public String getText(String aTextName, String defaultValue, List<Object> args) {
-        return textProvider.getText(aTextName, defaultValue, args);
+        return getTextProvider().getText(aTextName, defaultValue, args);
     }
 
     public String getText(String key, String defaultValue, String[] args) {
-        return textProvider.getText(key, defaultValue, args);
+        return getTextProvider().getText(key, defaultValue, args);
     }
 
     public String getText(String key, String defaultValue, List<Object> args, ValueStack stack) {
-        return textProvider.getText(key, defaultValue, args, stack);
+        return getTextProvider().getText(key, defaultValue, args, stack);
     }
 
     public String getText(String key, String defaultValue, String[] args, ValueStack stack) {
-        return textProvider.getText(key, defaultValue, args, stack);
+        return getTextProvider().getText(key, defaultValue, args, stack);
     }
 
     public ResourceBundle getTexts() {
-        return textProvider.getTexts();
+        return getTextProvider().getTexts();
     }
 
     public ResourceBundle getTexts(String aBundleName) {
-        return textProvider.getTexts(aBundleName);
+        return getTextProvider().getTexts(aBundleName);
     }
 
     public void addActionError(String anErrorMessage) {
     public void validate() {
     }
 
-    @Override public Object clone() throws CloneNotSupportedException {
+    @Override
+    public Object clone() throws CloneNotSupportedException {
         return super.clone();
     }
 
     public void pause(String result) {
     }
 
+    /**
+     * If called first time it will create {@link com.opensymphony.xwork2.TextProviderFactory},
+     * inject dependency (if {@link com.opensymphony.xwork2.inject.Container} is accesible) into in,
+     * then will create new {@link com.opensymphony.xwork2.TextProvider} and store it in a field
+     * for future references and at the returns reference to that field
+     *
+     * @return reference to field with TextProvider
+     */
+    private TextProvider getTextProvider() {
+        if (textProvider == null) {
+            TextProviderFactory tpf = new TextProviderFactory();
+            if (container != null) {
+                container.inject(tpf);
+            }
+            textProvider = tpf.createInstance(getClass(), this);
+        }
+        return textProvider;
+    }
+
+    @Inject
+    public void setContainer(Container container) {
+        this.container = container;
+    }
+
 }

core/src/main/java/com/opensymphony/xwork2/TextProvider.java

  * You can override {@link LocaleProvider#getLocale()} to change the behaviour of how
  * to choose locale for the bundles that are returned. Typically you would
  * use the {@link LocaleProvider} interface to get the users configured locale.
+ * <p/>
+ * When you want to use your own implementation for Struts 2 project you have to define following
+ * bean and constant in struts.xml:
+ * &lt;bean class=&quot;org.demo.MyTextProvider&quot; name=&quot;myTextProvider&quot; type=&quot;com.opensymphony.xwork2.TextProvider&quot; /&gt;
+ * &lt;constant name=&quot;struts.xworkTextProvider&quot; value=&quot;myTextProvider&quot; /&gt;
+ * <p/>
+ * if you want to also use your implemntation for framework's messages define another constant (remember to put
+ * into it all framework messages)
+ * &lt;constant name=&quot;system&quot; value=&quot;myTextProvider&quot; /&gt;
+ * <p/>
+ * Take a look on {@link com.opensymphony.xwork2.ActionSupport} for example TextProvider implemntation.
  *
  * @author Jason Carreira
  * @author Rainer Hermanns
     /**
      * Checks if a message key exists.
      *
-     * @param key
+     * @param key message key to check for
      * @return boolean true if key exists, false otherwise.
      */
     boolean hasKey(String key);

core/src/test/java/com/opensymphony/xwork2/ActionSupportTest.java

 
 import com.opensymphony.xwork2.util.ValueStack;
 
-import java.util.*;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.ResourceBundle;
 
 /**
  * Unit test for {@link ActionSupport}.
 
     private ActionSupport as;
 
-    @Override protected void setUp() throws Exception {
+    @Override
+    protected void setUp() throws Exception {
         super.setUp();
         as = new ActionSupport();
     }
 
-    @Override protected void tearDown() throws Exception {
+    @Override
+    protected void tearDown() throws Exception {
         super.tearDown();
         as = null;
     }
 
-    public void testNothingDoneOnActionSupport() throws Exception{
+    public void testNothingDoneOnActionSupport() throws Exception {
         assertEquals(false, as.hasErrors());
 
         assertNotNull(as.getActionErrors());
         assertNotNull(as.getFieldErrors());
         assertEquals(0, as.getFieldErrors().size());
         assertEquals(false, as.hasFieldErrors());
-        
+
         assertNull(as.getText(null));
-        
+
         try {
-        	as.pause(null);
-		} catch (Exception e) {
-        	fail("Should not fail");
-		}
-        
+            as.pause(null);
+        } catch (Exception e) {
+            fail("Should not fail");
+        }
+
         assertEquals(Action.INPUT, as.input());
         assertEquals(Action.SUCCESS, as.doDefault());
         assertEquals(Action.SUCCESS, as.execute());
-        try{
-        	as.clone();
-        	fail("Failure expected for clone()");
-        }catch(CloneNotSupportedException e){
-        	// success!
+        try {
+            as.clone();
+            fail("Failure expected for clone()");
+        } catch (CloneNotSupportedException e) {
+            // success!
         }
-        
-        
-        assertNull(as.getText(null, (List)null));
-        assertNull(as.getText(null, (String)null));
-        assertNull(as.getText(null, (String[])null));
-        
-        assertNull(as.getText(null, (String)null, (List)null));
-        assertNull(as.getText(null, (String)null, (String)null));
-        assertNull(as.getText(null, (String)null, (String[])null));
-
-        assertNull(as.getText(null, (String)null, (List)null, (ValueStack)null));
-        assertNull(as.getText(null, (String)null, (String[])null, (ValueStack)null));
-        
+
+
+        assertNull(as.getText(null, (List) null));
+        assertNull(as.getText(null, (String) null));
+        assertNull(as.getText(null, (String[]) null));
+
+        assertNull(as.getText(null, (String) null, (List) null));
+        assertNull(as.getText(null, (String) null, (String) null));
+        assertNull(as.getText(null, (String) null, (String[]) null));
+
+        assertNull(as.getText(null, (String) null, (List) null, (ValueStack) null));
+        assertNull(as.getText(null, (String) null, (String[]) null, (ValueStack) null));
+
         assertNotNull(as.getLocale());
         assertEquals(ActionContext.getContext().getLocale(), as.getLocale());
 
         ActionContext.getContext().setLocale(new Locale("da"));
         MyActionSupport mas = new MyActionSupport();
 
+        checkGetTexts(mas);
+    }
+
+    public void testSimpleGetTextsWithInjectedTextProvider() throws Exception {
+        ActionContext.getContext().setLocale(new Locale("da"));
+        MyActionSupport mas = new MyActionSupport();
+
+        TextProvider textProvider = container.getInstance(TextProvider.class, "system");
+
+        assertNotNull(textProvider);
+
+        container.inject(mas);
+
+        checkGetTexts(mas);
+    }
+
+    private void checkGetTexts(MyActionSupport mas) {
         assertEquals("Hello World", mas.getText("hello"));
         assertEquals("not.in.bundle", mas.getText("not.in.bundle"));
 
         ActionContext.getContext().setLocale(new Locale("da"));
         MyActionSupport mas = new MyActionSupport();
 
-        String[] args = { "Santa", "loud" };
+        String[] args = {"Santa", "loud"};
         assertEquals("Hello World", mas.getText("hello", "this is default", args)); // no args in bundle
         assertEquals("Hello World Santa", mas.getText("hello.0", "this is default", args)); // only 1 arg in bundle
         assertEquals("Hello World. This is Santa speaking loud", mas.getText("hello.1", "this is default", args));
 
         ValueStack stack = ActionContext.getContext().getValueStack();
 
-        String[] args = { "Santa", "loud" };
+        String[] args = {"Santa", "loud"};
         assertEquals("Hello World", mas.getText("hello", "this is default", args, stack)); // no args in bundle
         assertEquals("Hello World Santa", mas.getText("hello.0", "this is default", args, stack)); // only 1 arg in bundle
         assertEquals("Hello World. This is Santa speaking loud", mas.getText("hello.1", "this is default", args, stack));
 
     private class MyActionSupport extends ActionSupport {
 
-        @Override public String doDefault() throws Exception {
+        @Override
+        public String doDefault() throws Exception {
             return "santa";
         }
 
-        @Override public void validate() {
+        @Override
+        public void validate() {
             super.validate(); // to have code coverage
             addActionMessage("validation was called");
         }