Skip to content

Commit

Permalink
Merge pull request #1081 from apache/kusal-depr-apis-3
Browse files Browse the repository at this point in the history
WW-3714 Deprecate and repackage common APIs part 2
  • Loading branch information
kusalk authored Oct 22, 2024
2 parents 144010c + e3fbe88 commit b488c80
Show file tree
Hide file tree
Showing 13 changed files with 394 additions and 170 deletions.
10 changes: 7 additions & 3 deletions core/src/main/java/com/opensymphony/xwork2/ActionContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private ActionContext(org.apache.struts2.ActionContext actualContext) {
super(actualContext.getContextMap());
}

private static ActionContext adapt(org.apache.struts2.ActionContext actualContext) {
static ActionContext adapt(org.apache.struts2.ActionContext actualContext) {
return actualContext != null ? new ActionContext(actualContext) : null;
}

Expand Down Expand Up @@ -77,15 +77,19 @@ public ActionContext bind() {
return this;
}

@Override
public ActionContext withActionInvocation(ActionInvocation actionInvocation) {
return withActionInvocation((org.apache.struts2.ActionInvocation) actionInvocation);
}

@Override
public ActionContext withActionInvocation(org.apache.struts2.ActionInvocation actionInvocation) {
super.withActionInvocation(actionInvocation);
return this;
}

@Override
public ActionInvocation getActionInvocation() {
return super.getActionInvocation();
return ActionInvocation.adapt(super.getActionInvocation());
}

@Override
Expand Down
234 changes: 89 additions & 145 deletions core/src/main/java/com/opensymphony/xwork2/ActionInvocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,158 +22,102 @@
import com.opensymphony.xwork2.util.ValueStack;

/**
* An {@link ActionInvocation} represents the execution state of an {@link Action}. It holds the Interceptors and the Action instance.
* By repeated re-entrant execution of the <code>invoke()</code> method, initially by the {@link ActionProxy}, then by the Interceptors, the
* Interceptors are all executed, and then the {@link Action} and the {@link Result}.
* {@inheritDoc}
*
* @author Jason Carreira
* @see com.opensymphony.xwork2.ActionProxy
* @deprecated since 6.7.0, use {@link org.apache.struts2.ActionInvocation} instead.
*/
public interface ActionInvocation {

/**
* Get the Action associated with this ActionInvocation.
*
* @return the Action
*/
Object getAction();

/**
* Gets whether this ActionInvocation has executed before.
* This will be set after the Action and the Result have executed.
*
* @return <tt>true</tt> if this ActionInvocation has executed before.
*/
boolean isExecuted();

/**
* Gets the ActionContext associated with this ActionInvocation. The ActionProxy is
* responsible for setting this ActionContext onto the ThreadLocal before invoking
* the ActionInvocation and resetting the old ActionContext afterwards.
*
* @return the ActionContext.
*/
@Deprecated
public interface ActionInvocation extends org.apache.struts2.ActionInvocation {

@Override
ActionContext getInvocationContext();

/**
* Get the ActionProxy holding this ActionInvocation.
*
* @return the ActionProxy.
*/
ActionProxy getProxy();

/**
* If the ActionInvocation has been executed before and the Result is an instance of {@link ActionChainResult}, this method
* will walk down the chain of <code>ActionChainResult</code>s until it finds a non-chain result, which will be returned. If the
* ActionInvocation's result has not been executed before, the Result instance will be created and populated with
* the result params.
*
* @return the result.
* @throws Exception can be thrown.
*/
@Override
Result getResult() throws Exception;

/**
* Gets the result code returned from this ActionInvocation.
*
* @return the result code
*/
String getResultCode();

/**
* Sets the result code, possibly overriding the one returned by the
* action.
*
* <p>
* The "intended" purpose of this method is to allow PreResultListeners to
* override the result code returned by the Action.
* </p>
*
* <p>
* If this method is used before the Action executes, the Action's returned
* result code will override what was set. However the Action could (if
* specifically coded to do so) inspect the ActionInvocation to see that
* someone "upstream" (e.g. an Interceptor) had suggested a value as the
* result, and it could therefore return the same value itself.
* </p>
*
* <p>
* If this method is called between the Action execution and the Result
* execution, then the value set here will override the result code the
* action had returned. Creating an Interceptor that implements
* {@link PreResultListener} will give you this opportunity.
* </p>
*
* <p>
* If this method is called after the Result has been executed, it will
* have the effect of raising an IllegalStateException.
* </p>
*
* @param resultCode the result code.
* @throws IllegalStateException if called after the Result has been executed.
* @see #isExecuted()
*/
void setResultCode(String resultCode);

/**
* Gets the ValueStack associated with this ActionInvocation.
*
* @return the ValueStack
*/
ValueStack getStack();

/**
* Register a {@link PreResultListener} to be notified after the Action is executed and
* before the Result is executed.
*
* <p>
* The ActionInvocation implementation must guarantee that listeners will be called in
* the order in which they are registered.
* </p>
*
* <p>
* Listener registration and execution does not need to be thread-safe.
* </p>
*
* @param listener the listener to add.
*/
@Override
default void addPreResultListener(org.apache.struts2.interceptor.PreResultListener listener) {
addPreResultListener(PreResultListener.adapt(listener));
}

void addPreResultListener(PreResultListener listener);

/**
* Invokes the next step in processing this ActionInvocation.
*
* <p>
* If there are more Interceptors, this will call the next one. If Interceptors choose not to short-circuit
* ActionInvocation processing and return their own return code, they will call invoke() to allow the next Interceptor
* to execute. If there are no more Interceptors to be applied, the Action is executed.
* If the {@link ActionProxy#getExecuteResult()} method returns <tt>true</tt>, the Result is also executed.
* </p>
*
* @throws Exception can be thrown.
* @return the return code.
*/
String invoke() throws Exception;

/**
* Invokes only the Action (not Interceptors or Results).
*
* <p>
* This is useful in rare situations where advanced usage with the interceptor/action/result workflow is
* being manipulated for certain functionality.
* </p>
*
* @return the return code.
* @throws Exception can be thrown.
*/
String invokeActionOnly() throws Exception;

/**
* Sets the action event listener to respond to key action events.
*
* @param listener the listener.
*/
void setActionEventListener(ActionEventListener listener);

void init(ActionProxy proxy) ;
static ActionInvocation adapt(org.apache.struts2.ActionInvocation actualInvocation) {
return actualInvocation != null ? new LegacyAdapter(actualInvocation) : null;
}

class LegacyAdapter implements ActionInvocation {

private final org.apache.struts2.ActionInvocation adaptee;

private LegacyAdapter(org.apache.struts2.ActionInvocation adaptee) {
this.adaptee = adaptee;
}

@Override
public Object getAction() {
return adaptee.getAction();
}

@Override
public boolean isExecuted() {
return adaptee.isExecuted();
}

@Override
public ActionContext getInvocationContext() {
return ActionContext.adapt(adaptee.getInvocationContext());
}

@Override
public ActionProxy getProxy() {
return adaptee.getProxy();
}

@Override
public Result getResult() throws Exception {
return Result.adapt(adaptee.getResult());
}

@Override
public String getResultCode() {
return adaptee.getResultCode();
}

@Override
public void setResultCode(String resultCode) {
adaptee.setResultCode(resultCode);
}

@Override
public ValueStack getStack() {
return adaptee.getStack();
}

@Override
public void addPreResultListener(PreResultListener listener) {
adaptee.addPreResultListener(listener);
}

@Override
public String invoke() throws Exception {
return adaptee.invoke();
}

@Override
public String invokeActionOnly() throws Exception {
return adaptee.invokeActionOnly();
}

@Override
public void setActionEventListener(ActionEventListener listener) {
adaptee.setActionEventListener(listener);
}

@Override
public void init(ActionProxy proxy) {
adaptee.init(proxy);
}
}

}
25 changes: 25 additions & 0 deletions core/src/main/java/com/opensymphony/xwork2/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,29 @@
*/
@Deprecated
public interface Result extends org.apache.struts2.Result {

@Override
default void execute(org.apache.struts2.ActionInvocation invocation) throws Exception {
execute(ActionInvocation.adapt(invocation));
}

void execute(ActionInvocation invocation) throws Exception;

static Result adapt(org.apache.struts2.Result actualResult) {
return actualResult != null ? new LegacyAdapter(actualResult) : null;
}

class LegacyAdapter implements Result {

private final org.apache.struts2.Result adaptee;

private LegacyAdapter(org.apache.struts2.Result adaptee) {
this.adaptee = adaptee;
}

@Override
public void execute(ActionInvocation invocation) throws Exception {
adaptee.execute(ActionInvocation.adapt(invocation));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,19 @@
*/
package com.opensymphony.xwork2.interceptor;

import com.opensymphony.xwork2.ActionInvocation;

/**
* {@inheritDoc}
*
* @deprecated since 6.7.0, use {@link org.apache.struts2.interceptor.Interceptor} instead.
*/
@Deprecated
public interface ConditionalInterceptor extends org.apache.struts2.interceptor.ConditionalInterceptor, Interceptor {

default boolean shouldIntercept(org.apache.struts2.ActionInvocation invocation) {
return shouldIntercept(ActionInvocation.adapt(invocation));
}

boolean shouldIntercept(ActionInvocation invocation);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,20 @@
*/
package com.opensymphony.xwork2.interceptor;

import com.opensymphony.xwork2.ActionInvocation;

/**
* {@inheritDoc}
*
* @deprecated since 6.7.0, use {@link org.apache.struts2.interceptor.Interceptor} instead.
*/
@Deprecated
public interface Interceptor extends org.apache.struts2.interceptor.Interceptor {

@Override
default String intercept(org.apache.struts2.ActionInvocation invocation) throws Exception {
return intercept(ActionInvocation.adapt(invocation));
}

String intercept(ActionInvocation invocation) throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,35 @@
import com.opensymphony.xwork2.ActionInvocation;

/**
* PreResultListeners may be registered with an {@link ActionInvocation} to get a callback after the
* {@link com.opensymphony.xwork2.Action} has been executed but before the {@link com.opensymphony.xwork2.Result}
* is executed.
* {@inheritDoc}
*
* @author Jason Carreira
* @deprecated since 6.7.0, use {@link org.apache.struts2.interceptor.PreResultListener} instead.
*/
public interface PreResultListener {

/**
* This callback method will be called after the {@link com.opensymphony.xwork2.Action} execution and
* before the {@link com.opensymphony.xwork2.Result} execution.
*
* @param invocation the action invocation
* @param resultCode the result code returned by the action (eg. <code>success</code>).
*/
@Deprecated
public interface PreResultListener extends org.apache.struts2.interceptor.PreResultListener {

@Override
default void beforeResult(org.apache.struts2.ActionInvocation invocation, String resultCode) {
beforeResult(ActionInvocation.adapt(invocation), resultCode);
}

void beforeResult(ActionInvocation invocation, String resultCode);

static PreResultListener adapt(org.apache.struts2.interceptor.PreResultListener actualListener) {
return actualListener != null ? new LegacyAdapter(actualListener) : null;
}

class LegacyAdapter implements PreResultListener {

private final org.apache.struts2.interceptor.PreResultListener adaptee;

private LegacyAdapter(org.apache.struts2.interceptor.PreResultListener adaptee) {
this.adaptee = adaptee;
}

@Override
public void beforeResult(ActionInvocation invocation, String resultCode) {
adaptee.beforeResult(invocation, resultCode);
}
}
}
Loading

0 comments on commit b488c80

Please sign in to comment.