This is an automated email from the ASF dual-hosted git repository.

kusal pushed a commit to branch kusal-depr-apis-3.5
in repository https://gitbox.apache.org/repos/asf/struts.git

commit b3d72a24162ed23010965961bf311107f90ab445
Author: Kusal Kithul-Godage <g...@kusal.io>
AuthorDate: Thu Oct 17 20:05:59 2024 +1100

    WW-3714 Add factory support for new Interceptor, Result interfaces
---
 .../xwork2/factory/DefaultInterceptorFactory.java  | 13 ++++++---
 .../xwork2/factory/DefaultResultFactory.java       | 12 ++++++++-
 .../xwork2/interceptor/Interceptor.java            | 31 ++++++++++++++++++++++
 .../struts2/factory/StrutsResultFactory.java       | 13 +++++++--
 4 files changed, 62 insertions(+), 7 deletions(-)

diff --git 
a/core/src/main/java/com/opensymphony/xwork2/factory/DefaultInterceptorFactory.java
 
b/core/src/main/java/com/opensymphony/xwork2/factory/DefaultInterceptorFactory.java
index a1d9ee2ce..580464130 100644
--- 
a/core/src/main/java/com/opensymphony/xwork2/factory/DefaultInterceptorFactory.java
+++ 
b/core/src/main/java/com/opensymphony/xwork2/factory/DefaultInterceptorFactory.java
@@ -70,13 +70,18 @@ public class DefaultInterceptorFactory implements 
InterceptorFactory {
                 reflectionProvider.setProperties(params, o);
             }
 
+            Interceptor interceptor = null;
             if (o instanceof Interceptor) {
-                Interceptor interceptor = (Interceptor) o;
-                interceptor.init();
-                return interceptor;
+                interceptor = (Interceptor) o;
+            } else if (o instanceof 
org.apache.struts2.interceptor.Interceptor) {
+                interceptor = 
Interceptor.adapt((org.apache.struts2.interceptor.Interceptor) o);
             }
 
-            throw new ConfigurationException("Class [" + interceptorClassName 
+ "] does not implement Interceptor", interceptorConfig);
+            if (interceptor == null) {
+                throw new ConfigurationException("Class [" + 
interceptorClassName + "] does not implement Interceptor", interceptorConfig);
+            }
+            interceptor.init();
+            return interceptor;
         } catch (InstantiationException e) {
             cause = e;
             message = "Unable to instantiate an instance of Interceptor class 
[" + interceptorClassName + "].";
diff --git 
a/core/src/main/java/com/opensymphony/xwork2/factory/DefaultResultFactory.java 
b/core/src/main/java/com/opensymphony/xwork2/factory/DefaultResultFactory.java
index e5fe5f8d5..5466a52ea 100644
--- 
a/core/src/main/java/com/opensymphony/xwork2/factory/DefaultResultFactory.java
+++ 
b/core/src/main/java/com/opensymphony/xwork2/factory/DefaultResultFactory.java
@@ -20,6 +20,7 @@ package com.opensymphony.xwork2.factory;
 
 import com.opensymphony.xwork2.ObjectFactory;
 import com.opensymphony.xwork2.Result;
+import com.opensymphony.xwork2.config.ConfigurationException;
 import com.opensymphony.xwork2.config.entities.ResultConfig;
 import com.opensymphony.xwork2.inject.Inject;
 import com.opensymphony.xwork2.util.reflection.ReflectionException;
@@ -51,7 +52,16 @@ public class DefaultResultFactory implements ResultFactory {
         Result result = null;
 
         if (resultClassName != null) {
-            result = (Result) objectFactory.buildBean(resultClassName, 
extraContext);
+            Object o = objectFactory.buildBean(resultClassName, extraContext);
+            if (o instanceof Result) {
+                result = (Result) o;
+            } else if (o instanceof org.apache.struts2.Result) {
+                result = Result.adapt((org.apache.struts2.Result) o);
+            }
+            if (result == null) {
+                throw new ConfigurationException("Class [" + resultClassName + 
"] does not implement Result", resultConfig);
+            }
+
             Map<String, String> params = resultConfig.getParams();
             if (params != null) {
                 for (Map.Entry<String, String> paramEntry : params.entrySet()) 
{
diff --git 
a/core/src/main/java/com/opensymphony/xwork2/interceptor/Interceptor.java 
b/core/src/main/java/com/opensymphony/xwork2/interceptor/Interceptor.java
index 628dda6f5..fef4a6666 100644
--- a/core/src/main/java/com/opensymphony/xwork2/interceptor/Interceptor.java
+++ b/core/src/main/java/com/opensymphony/xwork2/interceptor/Interceptor.java
@@ -34,4 +34,35 @@ public interface Interceptor extends 
org.apache.struts2.interceptor.Interceptor
     }
 
     String intercept(ActionInvocation invocation) throws Exception;
+
+    static Interceptor adapt(org.apache.struts2.interceptor.Interceptor 
actualInterceptor) {
+        if (actualInterceptor instanceof ActionInvocation) {
+            return (Interceptor) actualInterceptor;
+        }
+        return actualInterceptor != null ? new 
LegacyAdapter(actualInterceptor) : null;
+    }
+
+    class LegacyAdapter implements Interceptor {
+
+        private final org.apache.struts2.interceptor.Interceptor adaptee;
+
+        private LegacyAdapter(org.apache.struts2.interceptor.Interceptor 
adaptee) {
+            this.adaptee = adaptee;
+        }
+
+        @Override
+        public String intercept(ActionInvocation invocation) throws Exception {
+            return adaptee.intercept(invocation);
+        }
+
+        @Override
+        public void destroy() {
+            adaptee.destroy();
+        }
+
+        @Override
+        public void init() {
+            adaptee.init();
+        }
+    }
 }
diff --git 
a/core/src/main/java/org/apache/struts2/factory/StrutsResultFactory.java 
b/core/src/main/java/org/apache/struts2/factory/StrutsResultFactory.java
index 0a758f213..2818f33dd 100644
--- a/core/src/main/java/org/apache/struts2/factory/StrutsResultFactory.java
+++ b/core/src/main/java/org/apache/struts2/factory/StrutsResultFactory.java
@@ -20,13 +20,14 @@ package org.apache.struts2.factory;
 
 import com.opensymphony.xwork2.ObjectFactory;
 import com.opensymphony.xwork2.Result;
+import com.opensymphony.xwork2.config.ConfigurationException;
 import com.opensymphony.xwork2.config.entities.ResultConfig;
 import com.opensymphony.xwork2.factory.ResultFactory;
 import com.opensymphony.xwork2.inject.Inject;
+import com.opensymphony.xwork2.result.ParamNameAwareResult;
 import com.opensymphony.xwork2.util.reflection.ReflectionException;
 import com.opensymphony.xwork2.util.reflection.ReflectionExceptionHandler;
 import com.opensymphony.xwork2.util.reflection.ReflectionProvider;
-import com.opensymphony.xwork2.result.ParamNameAwareResult;
 
 import java.util.Map;
 
@@ -53,7 +54,15 @@ public class StrutsResultFactory implements ResultFactory {
         Result result = null;
 
         if (resultClassName != null) {
-            result = (Result) objectFactory.buildBean(resultClassName, 
extraContext);
+            Object o = objectFactory.buildBean(resultClassName, extraContext);
+            if (o instanceof Result) {
+                result = (Result) o;
+            } else if (o instanceof org.apache.struts2.Result) {
+                result = Result.adapt((org.apache.struts2.Result) o);
+            }
+            if (result == null) {
+                throw new ConfigurationException("Class [" + resultClassName + 
"] does not implement Result", resultConfig);
+            }
             Map<String, String> params = resultConfig.getParams();
             if (params != null) {
                 setParameters(extraContext, result, params);

Reply via email to