Skip to content

Commit

Permalink
WW-3714 Add factory support for new Interceptor, Result interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
kusalk committed Oct 17, 2024
1 parent e3fbe88 commit b3d72a2
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,18 @@ public Interceptor buildInterceptor(InterceptorConfig interceptorConfig, Map<Str
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 + "].";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

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;
Expand Down Expand Up @@ -51,7 +52,16 @@ public Result buildResult(ResultConfig resultConfig, Map<String, Object> extraCo
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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,35 @@ default String intercept(org.apache.struts2.ActionInvocation invocation) throws
}

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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@

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;

Expand All @@ -53,7 +54,15 @@ public Result buildResult(ResultConfig resultConfig, Map<String, Object> extraCo
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);
Expand Down

0 comments on commit b3d72a2

Please sign in to comment.