struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lukaszlen...@apache.org
Subject [struts] 11/11: WW-5049 Supports bean-selection via Java config
Date Tue, 03 Dec 2019 08:33:01 GMT
This is an automated email from the ASF dual-hosted git repository.

lukaszlenart pushed a commit to branch WW-5049-velocity-plugin
in repository https://gitbox.apache.org/repos/asf/struts.git

commit 93b6efee3c699e8d1d496180ab130206d2b681ab
Author: Lukasz Lenart <lukaszlenart@apache.org>
AuthorDate: Wed Nov 27 23:56:49 2019 +0100

    WW-5049 Supports bean-selection via Java config
---
 .../config/providers/XmlConfigurationProvider.java |  2 +-
 .../struts2/config/StrutsJavaConfiguration.java    |  4 +++
 .../config/StrutsJavaConfigurationProvider.java    | 35 +++++++++++++++++-----
 .../BeanSelectionConfig.java}                      | 29 +++++++++++++-----
 core/src/main/resources/struts-2.6.dtd             |  6 ++--
 core/src/main/resources/xwork-2.6.dtd              |  6 ++--
 .../xwork2/util/location/LocationUtilsTest.java    |  6 ++++
 .../StrutsJavaConfigurationProviderTest.java       | 28 ++++++++++++++---
 .../struts2/config/TestBeanSelectionProvider.java} | 17 ++++++-----
 .../velocity/src/main/resources/struts-plugin.xml  |  2 +-
 10 files changed, 100 insertions(+), 35 deletions(-)

diff --git a/core/src/main/java/com/opensymphony/xwork2/config/providers/XmlConfigurationProvider.java
b/core/src/main/java/com/opensymphony/xwork2/config/providers/XmlConfigurationProvider.java
index 9b2a7ad..5fb5df6 100644
--- a/core/src/main/java/com/opensymphony/xwork2/config/providers/XmlConfigurationProvider.java
+++ b/core/src/main/java/com/opensymphony/xwork2/config/providers/XmlConfigurationProvider.java
@@ -221,7 +221,7 @@ public class XmlConfigurationProvider implements ConfigurationProvider
{
 
                     final String nodeName = child.getNodeName();
 
-                    if ("bean-provider".equals(nodeName)) {
+                    if ("bean-selection".equals(nodeName)) {
                         String name = child.getAttribute("name");
                         String impl = child.getAttribute("class");
                         try {
diff --git a/core/src/main/java/org/apache/struts2/config/StrutsJavaConfiguration.java b/core/src/main/java/org/apache/struts2/config/StrutsJavaConfiguration.java
index da84d5e..1129304 100644
--- a/core/src/main/java/org/apache/struts2/config/StrutsJavaConfiguration.java
+++ b/core/src/main/java/org/apache/struts2/config/StrutsJavaConfiguration.java
@@ -19,8 +19,10 @@
 package org.apache.struts2.config;
 
 import java.util.List;
+import java.util.Optional;
 
 import org.apache.struts2.config.entities.BeanConfig;
+import org.apache.struts2.config.entities.BeanSelectionConfig;
 import org.apache.struts2.config.entities.ConstantConfig;
 
 public interface StrutsJavaConfiguration {
@@ -28,5 +30,7 @@ public interface StrutsJavaConfiguration {
 
     List<ConstantConfig> constants();
 
+    default Optional<BeanSelectionConfig> beanSelection() { return Optional.empty();}
+
     List<String> unknownHandlerStack();
 }
diff --git a/core/src/main/java/org/apache/struts2/config/StrutsJavaConfigurationProvider.java
b/core/src/main/java/org/apache/struts2/config/StrutsJavaConfigurationProvider.java
index 5aaaf57..7538ea3 100644
--- a/core/src/main/java/org/apache/struts2/config/StrutsJavaConfigurationProvider.java
+++ b/core/src/main/java/org/apache/struts2/config/StrutsJavaConfigurationProvider.java
@@ -24,9 +24,12 @@ import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 
+import com.opensymphony.xwork2.config.BeanSelectionProvider;
+import com.opensymphony.xwork2.util.ClassLoaderUtil;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import org.apache.struts2.config.entities.BeanConfig;
+import org.apache.struts2.config.entities.BeanSelectionConfig;
 import org.apache.struts2.config.entities.ConstantConfig;
 
 import com.opensymphony.xwork2.config.Configuration;
@@ -91,6 +94,20 @@ public class StrutsJavaConfigurationProvider implements ConfigurationProvider
{
             }
         }
 
+        // bean-selection
+        javaConfig.beanSelection().ifPresent(beanSelectionConfig -> {
+            try {
+                LOG.debug("Registering bean selection provider {} of type {}",
+                    beanSelectionConfig.getName(), beanSelectionConfig.getClazz().getName());
+
+                BeanSelectionProvider provider = beanSelectionConfig.getClazz().newInstance();
+                provider.register(builder, props);
+            } catch (IllegalAccessException | InstantiationException e) {
+                throw new ConfigurationException("Unable to load : name:" + beanSelectionConfig.getName()
+                    + " class:" + beanSelectionConfig.getClazz().getName());
+            }
+        });
+
         // unknown-handler-stack
         List<String> unknownHandlers = javaConfig.unknownHandlerStack();
         if (unknownHandlers != null) {
@@ -125,10 +142,10 @@ public class StrutsJavaConfigurationProvider implements ConfigurationProvider
{
             } else {
                 if (containerBuilder.contains(beanConf.getType(), beanConf.getName())) {
                     Location loc = LocationUtils
-                            .getLocation(loadedBeans.get(beanConf.getType().getName() + beanConf.getName()));
+                        .getLocation(loadedBeans.get(beanConf.getType().getName() + beanConf.getName()));
                     if (throwExceptionOnDuplicateBeans) {
                         throw new ConfigurationException("Bean type " + beanConf.getType()
+ " with the name "
-                                + beanConf.getName() + " has already been loaded by " + loc,
javaConfig);
+                            + beanConf.getName() + " has already been loaded by " + loc,
javaConfig);
                     }
                 }
 
@@ -137,17 +154,21 @@ public class StrutsJavaConfigurationProvider implements ConfigurationProvider
{
                 beanConf.getClazz().getDeclaredConstructors();
 
                 LOG.debug("Loaded type: {} name: {} clazz: {}", beanConf.getType(), beanConf.getName(),
-                        beanConf.getClazz());
+                    beanConf.getClazz());
+
                 containerBuilder.factory(
-                        beanConf.getType(), beanConf.getName(), new LocatableFactory(beanConf.getName(),
-                                beanConf.getType(), beanConf.getClazz(), beanConf.getScope(),
javaConfig),
-                        beanConf.getScope());
+                    beanConf.getType(),
+                    beanConf.getName(),
+                    new LocatableFactory(
+                        beanConf.getName(), beanConf.getType(), beanConf.getClazz(), beanConf.getScope(),
javaConfig
+                    ),
+                    beanConf.getScope());
             }
             loadedBeans.put(beanConf.getType().getName() + beanConf.getName(), javaConfig);
         } catch (Throwable ex) {
             if (!beanConf.isOptional()) {
                 throw new ConfigurationException(
-                        "Unable to load bean: type:" + beanConf.getType() + " class:" + beanConf.getClazz(),
ex);
+                    "Unable to load bean: type:" + beanConf.getType() + " class:" + beanConf.getClazz(),
ex);
             } else {
                 LOG.debug("Unable to load optional class: {}", beanConf.getClazz());
             }
diff --git a/core/src/main/java/org/apache/struts2/config/StrutsJavaConfiguration.java b/core/src/main/java/org/apache/struts2/config/entities/BeanSelectionConfig.java
similarity index 53%
copy from core/src/main/java/org/apache/struts2/config/StrutsJavaConfiguration.java
copy to core/src/main/java/org/apache/struts2/config/entities/BeanSelectionConfig.java
index da84d5e..dfa7697 100644
--- a/core/src/main/java/org/apache/struts2/config/StrutsJavaConfiguration.java
+++ b/core/src/main/java/org/apache/struts2/config/entities/BeanSelectionConfig.java
@@ -16,17 +16,30 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.struts2.config;
+package org.apache.struts2.config.entities;
 
-import java.util.List;
+import com.opensymphony.xwork2.config.BeanSelectionProvider;
+import com.opensymphony.xwork2.inject.Container;
 
-import org.apache.struts2.config.entities.BeanConfig;
-import org.apache.struts2.config.entities.ConstantConfig;
+public class BeanSelectionConfig {
 
-public interface StrutsJavaConfiguration {
-    List<BeanConfig> beans();
+    private final Class<? extends BeanSelectionProvider> clazz;
+    private final String name;
 
-    List<ConstantConfig> constants();
+    public BeanSelectionConfig(Class<? extends BeanSelectionProvider> clazz) {
+        this(clazz, Container.DEFAULT_NAME);
+    }
 
-    List<String> unknownHandlerStack();
+    public BeanSelectionConfig(Class<? extends BeanSelectionProvider> clazz, String
name) {
+        this.clazz = clazz;
+        this.name = name;
+    }
+
+    public Class<? extends BeanSelectionProvider> getClazz() {
+        return clazz;
+    }
+
+    public String getName() {
+        return name;
+    }
 }
diff --git a/core/src/main/resources/struts-2.6.dtd b/core/src/main/resources/struts-2.6.dtd
index 84714cc..964fd69 100644
--- a/core/src/main/resources/struts-2.6.dtd
+++ b/core/src/main/resources/struts-2.6.dtd
@@ -30,7 +30,7 @@
 	"http://struts.apache.org/dtds/struts-2.6.dtd">
 -->
 
-<!ELEMENT struts ((package|include|bean|constant)*,bean-provider?, unknown-handler-stack?)>
+<!ELEMENT struts ((package|include|bean|constant)*,bean-selection?, unknown-handler-stack?)>
 <!ATTLIST struts
     order CDATA #IMPLIED
 >
@@ -135,8 +135,8 @@
     optional CDATA #IMPLIED
 >
 
-<!ELEMENT bean-provider (#PCDATA)>
-<!ATTLIST bean-provider
+<!ELEMENT bean-selection (#PCDATA)>
+<!ATTLIST bean-selection
     name CDATA #IMPLIED
     class CDATA #IMPLIED
 >
diff --git a/core/src/main/resources/xwork-2.6.dtd b/core/src/main/resources/xwork-2.6.dtd
index 8c70840..476b71b 100644
--- a/core/src/main/resources/xwork-2.6.dtd
+++ b/core/src/main/resources/xwork-2.6.dtd
@@ -30,7 +30,7 @@
 	"http://struts.apache.org/dtds/xwork-2.6.dtd">
 -->
 
-<!ELEMENT xwork ((package|include|bean|constant)*, bean-provider?, unknown-handler-stack?)>
+    <!ELEMENT xwork ((package|include|bean|constant)*, bean-selection?, unknown-handler-stack?)>
 <!ATTLIST xwork
     order CDATA #IMPLIED
 >
@@ -135,8 +135,8 @@
     optional CDATA #IMPLIED
 >
 
-<!ELEMENT bean-provider (#PCDATA)>
-<!ATTLIST bean-provider
+<!ELEMENT bean-selection (#PCDATA)>
+<!ATTLIST bean-selection
     name CDATA #IMPLIED
     class CDATA #IMPLIED
 >
diff --git a/core/src/test/java/com/opensymphony/xwork2/util/location/LocationUtilsTest.java
b/core/src/test/java/com/opensymphony/xwork2/util/location/LocationUtilsTest.java
index a47d718..f64ffef 100644
--- a/core/src/test/java/com/opensymphony/xwork2/util/location/LocationUtilsTest.java
+++ b/core/src/test/java/com/opensymphony/xwork2/util/location/LocationUtilsTest.java
@@ -19,9 +19,11 @@
 package com.opensymphony.xwork2.util.location;
 
 import java.util.List;
+import java.util.Optional;
 
 import org.apache.struts2.config.StrutsJavaConfiguration;
 import org.apache.struts2.config.entities.BeanConfig;
+import org.apache.struts2.config.entities.BeanSelectionConfig;
 import org.apache.struts2.config.entities.ConstantConfig;
 
 import junit.framework.TestCase;
@@ -74,6 +76,10 @@ public class LocationUtilsTest extends TestCase {
             public List<BeanConfig> beans() {
                 return null;
             }
+            @Override
+            public Optional<BeanSelectionConfig> beanSelection() {
+                return Optional.empty();
+            }
         };
         Location loc = LocationUtils.getLocation(conf, null);
 
diff --git a/core/src/test/java/org/apache/struts2/config/StrutsJavaConfigurationProviderTest.java
b/core/src/test/java/org/apache/struts2/config/StrutsJavaConfigurationProviderTest.java
index 933de81..5e08f98 100644
--- a/core/src/test/java/org/apache/struts2/config/StrutsJavaConfigurationProviderTest.java
+++ b/core/src/test/java/org/apache/struts2/config/StrutsJavaConfigurationProviderTest.java
@@ -19,10 +19,14 @@
 package org.apache.struts2.config;
 
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.List;
+import java.util.Optional;
+import java.util.Set;
 
 import org.apache.struts2.StrutsConstants;
 import org.apache.struts2.config.entities.BeanConfig;
+import org.apache.struts2.config.entities.BeanSelectionConfig;
 import org.apache.struts2.config.entities.ConstantConfig;
 import org.junit.Assert;
 import org.junit.Test;
@@ -46,18 +50,26 @@ public class StrutsJavaConfigurationProviderTest {
         StrutsJavaConfiguration javaConfig = new StrutsJavaConfiguration() {
             @Override
             public List<String> unknownHandlerStack() {
-                return Arrays.asList(expectedUnknownHandler);
+                return Collections.singletonList(expectedUnknownHandler);
             }
 
             @Override
             public List<ConstantConfig> constants() {
-                return Arrays.asList(constantConfig);
+                return Collections.singletonList(constantConfig);
             }
 
             @Override
             public List<BeanConfig> beans() {
-                return Arrays.asList(new BeanConfig(TestBean.class),
-                        new BeanConfig(TestBean.class, "testBean", TestBean.class, Scope.PROTOTYPE,
true, true));
+                return Arrays.asList(
+                    new BeanConfig(TestBean.class, "struts"),
+                    new BeanConfig(TestBean.class, "struts.static", TestBean.class, Scope.PROTOTYPE,
true, true),
+                    new BeanConfig(TestBean.class, "struts.test.bean", TestBean.class)
+                );
+            }
+
+            @Override
+            public Optional<BeanSelectionConfig> beanSelection() {
+                return Optional.of(new BeanSelectionConfig(TestBeanSelectionProvider.class,
"testBeans"));
             }
         };
         StrutsJavaConfigurationProvider provider = new StrutsJavaConfigurationProvider(javaConfig);
@@ -83,5 +95,13 @@ public class StrutsJavaConfigurationProviderTest {
         Container container = builder.create(true);
         TestBean testBean = container.getInstance(TestBean.class);
         Assert.assertNotNull(testBean);
+
+        testBean = container.getInstance(TestBean.class, "struts");
+        Assert.assertNotNull(testBean);
+
+        // bean selection
+        Set<String> names = container.getInstanceNames(TestBean.class);
+        Assert.assertTrue(names.contains("struts"));
+        Assert.assertTrue(names.contains("struts.test.bean"));
     }
 }
diff --git a/core/src/main/java/org/apache/struts2/config/StrutsJavaConfiguration.java b/core/src/test/java/org/apache/struts2/config/TestBeanSelectionProvider.java
similarity index 62%
copy from core/src/main/java/org/apache/struts2/config/StrutsJavaConfiguration.java
copy to core/src/test/java/org/apache/struts2/config/TestBeanSelectionProvider.java
index da84d5e..79d6dd2 100644
--- a/core/src/main/java/org/apache/struts2/config/StrutsJavaConfiguration.java
+++ b/core/src/test/java/org/apache/struts2/config/TestBeanSelectionProvider.java
@@ -18,15 +18,16 @@
  */
 package org.apache.struts2.config;
 
-import java.util.List;
+import com.opensymphony.xwork2.TestBean;
+import com.opensymphony.xwork2.config.ConfigurationException;
+import com.opensymphony.xwork2.inject.ContainerBuilder;
+import com.opensymphony.xwork2.util.location.LocatableProperties;
 
-import org.apache.struts2.config.entities.BeanConfig;
-import org.apache.struts2.config.entities.ConstantConfig;
+public class TestBeanSelectionProvider extends AbstractBeanSelectionProvider {
 
-public interface StrutsJavaConfiguration {
-    List<BeanConfig> beans();
+    @Override
+    public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException
{
+        alias(TestBean.class, "struts.test.bean", builder, props);
+    }
 
-    List<ConstantConfig> constants();
-
-    List<String> unknownHandlerStack();
 }
diff --git a/plugins/velocity/src/main/resources/struts-plugin.xml b/plugins/velocity/src/main/resources/struts-plugin.xml
index b4fabf5..39112ba 100644
--- a/plugins/velocity/src/main/resources/struts-plugin.xml
+++ b/plugins/velocity/src/main/resources/struts-plugin.xml
@@ -39,6 +39,6 @@
         </result-types>
     </package>
 
-    <bean-provider name="velocityBeans" class="org.apache.struts2.views.velocity.VelocityBeanSelectionProvider"/>
+    <bean-selection name="velocityBeans" class="org.apache.struts2.views.velocity.VelocityBeanSelectionProvider"/>
 
 </struts>


Mime
View raw message