Add enabled flag to RegistrationBean
Default to true but allow user to switch off a @Bean of type Filter (for example) by wrapping it in a disabled registration. Fixes gh-655
This commit is contained in:
parent
10b177fb68
commit
533e920fe5
|
|
@ -231,6 +231,10 @@ public class FilterRegistrationBean extends RegistrationBean {
|
||||||
public void onStartup(ServletContext servletContext) throws ServletException {
|
public void onStartup(ServletContext servletContext) throws ServletException {
|
||||||
Assert.notNull(this.filter, "Filter must not be null");
|
Assert.notNull(this.filter, "Filter must not be null");
|
||||||
String name = getOrDeduceName(this.filter);
|
String name = getOrDeduceName(this.filter);
|
||||||
|
if (!isEnabled()) {
|
||||||
|
logger.info("Filter " + name + " was not registered (disabled)");
|
||||||
|
return;
|
||||||
|
}
|
||||||
FilterRegistration.Dynamic added = servletContext.addFilter(name, this.filter);
|
FilterRegistration.Dynamic added = servletContext.addFilter(name, this.filter);
|
||||||
if (added == null) {
|
if (added == null) {
|
||||||
logger.info("Filter " + name + " was not registered "
|
logger.info("Filter " + name + " was not registered "
|
||||||
|
|
|
||||||
|
|
@ -41,6 +41,8 @@ public abstract class RegistrationBean implements ServletContextInitializer, Ord
|
||||||
|
|
||||||
private boolean asyncSupported = true;
|
private boolean asyncSupported = true;
|
||||||
|
|
||||||
|
private boolean enabled = true;
|
||||||
|
|
||||||
private Map<String, String> initParameters = new LinkedHashMap<String, String>();
|
private Map<String, String> initParameters = new LinkedHashMap<String, String>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -66,6 +68,22 @@ public abstract class RegistrationBean implements ServletContextInitializer, Ord
|
||||||
return this.asyncSupported;
|
return this.asyncSupported;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Flag to indicate that the registration is enabled.
|
||||||
|
*
|
||||||
|
* @param enabled the enabled to set
|
||||||
|
*/
|
||||||
|
public void setEnabled(boolean enabled) {
|
||||||
|
this.enabled = enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the enabled flag (default true)
|
||||||
|
*/
|
||||||
|
public boolean isEnabled() {
|
||||||
|
return this.enabled;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set init-parameters for this registration. Calling this method will replace any
|
* Set init-parameters for this registration. Calling this method will replace any
|
||||||
* existing init-parameters.
|
* existing init-parameters.
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,8 @@ import javax.servlet.ServletRequestListener;
|
||||||
import javax.servlet.http.HttpSessionAttributeListener;
|
import javax.servlet.http.HttpSessionAttributeListener;
|
||||||
import javax.servlet.http.HttpSessionListener;
|
import javax.servlet.http.HttpSessionListener;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.ClassUtils;
|
import org.springframework.util.ClassUtils;
|
||||||
|
|
||||||
|
|
@ -55,6 +57,8 @@ import org.springframework.util.ClassUtils;
|
||||||
public class ServletListenerRegistrationBean<T extends EventListener> extends
|
public class ServletListenerRegistrationBean<T extends EventListener> extends
|
||||||
RegistrationBean {
|
RegistrationBean {
|
||||||
|
|
||||||
|
private static Log logger = LogFactory.getLog(ServletListenerRegistrationBean.class);
|
||||||
|
|
||||||
private static final Set<Class<?>> SUPPORTED_TYPES;
|
private static final Set<Class<?>> SUPPORTED_TYPES;
|
||||||
static {
|
static {
|
||||||
Set<Class<?>> types = new HashSet<Class<?>>();
|
Set<Class<?>> types = new HashSet<Class<?>>();
|
||||||
|
|
@ -97,6 +101,10 @@ public class ServletListenerRegistrationBean<T extends EventListener> extends
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStartup(ServletContext servletContext) throws ServletException {
|
public void onStartup(ServletContext servletContext) throws ServletException {
|
||||||
|
if (!isEnabled()) {
|
||||||
|
logger.info("Listener " + this.listener + " was not registered (disabled)");
|
||||||
|
return;
|
||||||
|
}
|
||||||
servletContext.addListener(this.listener);
|
servletContext.addListener(this.listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -158,6 +158,10 @@ public class ServletRegistrationBean extends RegistrationBean {
|
||||||
public void onStartup(ServletContext servletContext) throws ServletException {
|
public void onStartup(ServletContext servletContext) throws ServletException {
|
||||||
Assert.notNull(this.servlet, "Servlet must not be null");
|
Assert.notNull(this.servlet, "Servlet must not be null");
|
||||||
String name = getServletName();
|
String name = getServletName();
|
||||||
|
if (!isEnabled()) {
|
||||||
|
logger.info("Filter " + name + " was not registered (disabled)");
|
||||||
|
return;
|
||||||
|
}
|
||||||
logger.info("Mapping servlet: '" + name + "' to " + this.urlMappings);
|
logger.info("Mapping servlet: '" + name + "' to " + this.urlMappings);
|
||||||
Dynamic added = servletContext.addServlet(name, this.servlet);
|
Dynamic added = servletContext.addServlet(name, this.servlet);
|
||||||
if (added == null) {
|
if (added == null) {
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,7 @@ import org.mockito.MockitoAnnotations;
|
||||||
import static org.mockito.BDDMockito.given;
|
import static org.mockito.BDDMockito.given;
|
||||||
import static org.mockito.Matchers.anyObject;
|
import static org.mockito.Matchers.anyObject;
|
||||||
import static org.mockito.Matchers.anyString;
|
import static org.mockito.Matchers.anyString;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -122,6 +123,15 @@ public class FilterRegistrationBeanTests {
|
||||||
verify(this.servletContext).addFilter("mockFilter", this.filter);
|
verify(this.servletContext).addFilter("mockFilter", this.filter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void disable() throws Exception {
|
||||||
|
FilterRegistrationBean bean = new FilterRegistrationBean();
|
||||||
|
bean.setFilter(this.filter);
|
||||||
|
bean.setEnabled(false);
|
||||||
|
bean.onStartup(this.servletContext);
|
||||||
|
verify(this.servletContext, times(0)).addFilter("mockFilter", this.filter);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void setFilterMustNotBeNull() throws Exception {
|
public void setFilterMustNotBeNull() throws Exception {
|
||||||
FilterRegistrationBean bean = new FilterRegistrationBean();
|
FilterRegistrationBean bean = new FilterRegistrationBean();
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,8 @@ import org.mockito.Mock;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
|
|
||||||
|
import static org.mockito.Matchers.any;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -60,6 +62,16 @@ public class ServletListenerRegistrationBeanTests {
|
||||||
verify(this.servletContext).addListener(this.listener);
|
verify(this.servletContext).addListener(this.listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void disable() throws Exception {
|
||||||
|
ServletListenerRegistrationBean<ServletContextListener> bean = new ServletListenerRegistrationBean<ServletContextListener>(
|
||||||
|
this.listener);
|
||||||
|
bean.setEnabled(false);
|
||||||
|
bean.onStartup(this.servletContext);
|
||||||
|
verify(this.servletContext, times(0)).addListener(
|
||||||
|
any(ServletContextListener.class));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void cannotRegisterUnsupportedType() throws Exception {
|
public void cannotRegisterUnsupportedType() throws Exception {
|
||||||
this.thrown.expect(IllegalArgumentException.class);
|
this.thrown.expect(IllegalArgumentException.class);
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,7 @@ import static org.mockito.BDDMockito.given;
|
||||||
import static org.mockito.Matchers.anyObject;
|
import static org.mockito.Matchers.anyObject;
|
||||||
import static org.mockito.Matchers.anyString;
|
import static org.mockito.Matchers.anyString;
|
||||||
import static org.mockito.Mockito.never;
|
import static org.mockito.Mockito.never;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -129,6 +130,15 @@ public class ServletRegistrationBeanTests {
|
||||||
verify(this.servletContext).addServlet("mockServlet", this.servlet);
|
verify(this.servletContext).addServlet("mockServlet", this.servlet);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void disable() throws Exception {
|
||||||
|
ServletRegistrationBean bean = new ServletRegistrationBean();
|
||||||
|
bean.setServlet(this.servlet);
|
||||||
|
bean.setEnabled(false);
|
||||||
|
bean.onStartup(this.servletContext);
|
||||||
|
verify(this.servletContext, times(0)).addServlet("mockServlet", this.servlet);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void setServletMustNotBeNull() throws Exception {
|
public void setServletMustNotBeNull() throws Exception {
|
||||||
ServletRegistrationBean bean = new ServletRegistrationBean();
|
ServletRegistrationBean bean = new ServletRegistrationBean();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue