Browse Source

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
pull/550/merge
Dave Syer 12 years ago
parent
commit
533e920fe5
  1. 4
      spring-boot/src/main/java/org/springframework/boot/context/embedded/FilterRegistrationBean.java
  2. 18
      spring-boot/src/main/java/org/springframework/boot/context/embedded/RegistrationBean.java
  3. 8
      spring-boot/src/main/java/org/springframework/boot/context/embedded/ServletListenerRegistrationBean.java
  4. 4
      spring-boot/src/main/java/org/springframework/boot/context/embedded/ServletRegistrationBean.java
  5. 10
      spring-boot/src/test/java/org/springframework/boot/context/embedded/FilterRegistrationBeanTests.java
  6. 12
      spring-boot/src/test/java/org/springframework/boot/context/embedded/ServletListenerRegistrationBeanTests.java
  7. 10
      spring-boot/src/test/java/org/springframework/boot/context/embedded/ServletRegistrationBeanTests.java

4
spring-boot/src/main/java/org/springframework/boot/context/embedded/FilterRegistrationBean.java

@ -231,6 +231,10 @@ public class FilterRegistrationBean extends RegistrationBean { @@ -231,6 +231,10 @@ public class FilterRegistrationBean extends RegistrationBean {
public void onStartup(ServletContext servletContext) throws ServletException {
Assert.notNull(this.filter, "Filter must not be null");
String name = getOrDeduceName(this.filter);
if (!isEnabled()) {
logger.info("Filter " + name + " was not registered (disabled)");
return;
}
FilterRegistration.Dynamic added = servletContext.addFilter(name, this.filter);
if (added == null) {
logger.info("Filter " + name + " was not registered "

18
spring-boot/src/main/java/org/springframework/boot/context/embedded/RegistrationBean.java

@ -41,6 +41,8 @@ public abstract class RegistrationBean implements ServletContextInitializer, Ord @@ -41,6 +41,8 @@ public abstract class RegistrationBean implements ServletContextInitializer, Ord
private boolean asyncSupported = true;
private boolean enabled = true;
private Map<String, String> initParameters = new LinkedHashMap<String, String>();
/**
@ -66,6 +68,22 @@ public abstract class RegistrationBean implements ServletContextInitializer, Ord @@ -66,6 +68,22 @@ public abstract class RegistrationBean implements ServletContextInitializer, Ord
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
* existing init-parameters.

8
spring-boot/src/main/java/org/springframework/boot/context/embedded/ServletListenerRegistrationBean.java

@ -30,6 +30,8 @@ import javax.servlet.ServletRequestListener; @@ -30,6 +30,8 @@ import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpSessionAttributeListener;
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.ClassUtils;
@ -55,6 +57,8 @@ import org.springframework.util.ClassUtils; @@ -55,6 +57,8 @@ import org.springframework.util.ClassUtils;
public class ServletListenerRegistrationBean<T extends EventListener> extends
RegistrationBean {
private static Log logger = LogFactory.getLog(ServletListenerRegistrationBean.class);
private static final Set<Class<?>> SUPPORTED_TYPES;
static {
Set<Class<?>> types = new HashSet<Class<?>>();
@ -97,6 +101,10 @@ public class ServletListenerRegistrationBean<T extends EventListener> extends @@ -97,6 +101,10 @@ public class ServletListenerRegistrationBean<T extends EventListener> extends
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
if (!isEnabled()) {
logger.info("Listener " + this.listener + " was not registered (disabled)");
return;
}
servletContext.addListener(this.listener);
}

4
spring-boot/src/main/java/org/springframework/boot/context/embedded/ServletRegistrationBean.java

@ -158,6 +158,10 @@ public class ServletRegistrationBean extends RegistrationBean { @@ -158,6 +158,10 @@ public class ServletRegistrationBean extends RegistrationBean {
public void onStartup(ServletContext servletContext) throws ServletException {
Assert.notNull(this.servlet, "Servlet must not be null");
String name = getServletName();
if (!isEnabled()) {
logger.info("Filter " + name + " was not registered (disabled)");
return;
}
logger.info("Mapping servlet: '" + name + "' to " + this.urlMappings);
Dynamic added = servletContext.addServlet(name, this.servlet);
if (added == null) {

10
spring-boot/src/test/java/org/springframework/boot/context/embedded/FilterRegistrationBeanTests.java

@ -36,6 +36,7 @@ import org.mockito.MockitoAnnotations; @@ -36,6 +36,7 @@ import org.mockito.MockitoAnnotations;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
/**
@ -122,6 +123,15 @@ public class FilterRegistrationBeanTests { @@ -122,6 +123,15 @@ public class FilterRegistrationBeanTests {
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
public void setFilterMustNotBeNull() throws Exception {
FilterRegistrationBean bean = new FilterRegistrationBean();

12
spring-boot/src/test/java/org/springframework/boot/context/embedded/ServletListenerRegistrationBeanTests.java

@ -29,6 +29,8 @@ import org.mockito.Mock; @@ -29,6 +29,8 @@ import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
/**
@ -60,6 +62,16 @@ public class ServletListenerRegistrationBeanTests { @@ -60,6 +62,16 @@ public class ServletListenerRegistrationBeanTests {
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
public void cannotRegisterUnsupportedType() throws Exception {
this.thrown.expect(IllegalArgumentException.class);

10
spring-boot/src/test/java/org/springframework/boot/context/embedded/ServletRegistrationBeanTests.java

@ -39,6 +39,7 @@ import static org.mockito.BDDMockito.given; @@ -39,6 +39,7 @@ import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
/**
@ -129,6 +130,15 @@ public class ServletRegistrationBeanTests { @@ -129,6 +130,15 @@ public class ServletRegistrationBeanTests {
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
public void setServletMustNotBeNull() throws Exception {
ServletRegistrationBean bean = new ServletRegistrationBean();

Loading…
Cancel
Save