WW-4963 Implements new ServletContextAware interface and fixes actions

that uses withServletContext instead of setServletContext
This commit is contained in:
Lukasz Lenart
2018-09-25 10:02:58 +02:00
parent 8cf787b39c
commit b608549933
6 changed files with 64 additions and 4 deletions
@@ -23,7 +23,7 @@ package org.apache.struts2.showcase.source;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ClassLoaderUtil;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.util.ServletContextAware;
import org.apache.struts2.action.ServletContextAware;
import javax.servlet.ServletContext;
import java.io.BufferedReader;
@@ -227,7 +227,7 @@ public class ViewSourceAction extends ActionSupport implements ServletContextAwa
return snippet;
}
public void setServletContext(ServletContext arg0) {
public void withServletContext(ServletContext arg0) {
this.servletContext = arg0;
}
@@ -28,7 +28,7 @@ import com.opensymphony.xwork2.inject.Inject;
import org.apache.struts2.osgi.BundleAccessor;
import org.apache.struts2.osgi.host.OsgiHost;
import org.apache.struts2.osgi.StrutsOsgiListener;
import org.apache.struts2.util.ServletContextAware;
import org.apache.struts2.action.ServletContextAware;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
@@ -197,7 +197,7 @@ public class BundlesAction extends ActionSupport implements ServletContextAware
this.bundleAccessor = bundleAccessor;
}
public void setServletContext(ServletContext servletContext) {
public void withServletContext(ServletContext servletContext) {
osgiHost = (OsgiHost) servletContext.getAttribute(StrutsOsgiListener.OSGI_HOST);
}
}
@@ -0,0 +1,31 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.struts2.action;
import javax.servlet.ServletContext;
/**
* For components that have a dependence on the Servlet context.
*
* @since 2.6
*/
public interface ServletContextAware {
void withServletContext(ServletContext context);
}
@@ -196,6 +196,12 @@ public class ServletConfigInterceptor extends AbstractInterceptor implements Str
ServletContext servletContext = (ServletContext) context.get(SERVLET_CONTEXT);
((ServletContextAware) action).setServletContext(servletContext);
}
if (action instanceof org.apache.struts2.action.ServletContextAware) {
ServletContext servletContext = (ServletContext) context.get(SERVLET_CONTEXT);
((org.apache.struts2.action.ServletContextAware) action).withServletContext(servletContext);
}
return invocation.invoke();
}
}
@@ -22,8 +22,15 @@ import javax.servlet.ServletContext;
/**
* For components that have a dependence on the Servlet context.
*
* @deprecated please use {@link org.apache.struts2.action.ServletContextAware} instead
*/
@Deprecated
public interface ServletContextAware {
/**
* @deprecated please use {@link org.apache.struts2.action.ServletContextAware#withServletContext(ServletContext)} instead
*/
@Deprecated
public void setServletContext(ServletContext context);
}
@@ -326,6 +326,22 @@ public class ServletConfigInterceptorTest extends StrutsInternalTestCase {
verify(mock);
}
public void testActionServletContextAware() throws Exception {
org.apache.struts2.action.ServletContextAware mock = createMock(org.apache.struts2.action.ServletContextAware.class);
MockActionInvocation mai = createActionInvocation(mock);
MockServletContext ctx = new MockServletContext();
mai.getInvocationContext().put(StrutsStatics.SERVLET_CONTEXT, ctx);
mock.withServletContext(ctx);
expectLastCall().times(1);
replay(mock);
interceptor.intercept(mai);
verify(mock);
}
private MockActionInvocation createActionInvocation(Object mock) {
MockActionInvocation mai = new MockActionInvocation();
mai.setResultCode("success");