Skip to content

Commit

Permalink
Create DynamicRegistrationBean
Browse files Browse the repository at this point in the history
Extract functionality from the `RegistrationBean` into a new class
designed to work with dynamic registration. Servet and Filter
registration beans now extend from `DynaimcRegistrationBean`, where as
`ServletListenerRegistrationBean` extends directly from
`RegistrationBean`.

This refactor allows the removal of `ServletListenerRegistrationBean`
deprecated methods.

Fixes gh-11344
  • Loading branch information
philwebb committed Jan 9, 2018
1 parent 85d3f5a commit 9cb5f3d
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 217 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -56,8 +56,8 @@
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.DynaimcRegistrationBean;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.RegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down Expand Up @@ -181,7 +181,7 @@ private String getServletRegistrationName() {
return ClassUtils.getUserClass(this.config.getClass()).getName();
}

private void addInitParameters(RegistrationBean registration) {
private void addInitParameters(DynaimcRegistrationBean<?> registration) {
for (Entry<String, String> entry : this.jersey.getInit().entrySet()) {
registration.addInitParameter(entry.getKey(), entry.getValue());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,8 +26,8 @@
import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.FilterRegistration;
import javax.servlet.FilterRegistration.Dynamic;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand All @@ -41,7 +41,8 @@
* @param <T> the type of {@link Filter} to register
* @author Phillip Webb
*/
abstract class AbstractFilterRegistrationBean<T extends Filter> extends RegistrationBean {
abstract class AbstractFilterRegistrationBean<T extends Filter>
extends DynaimcRegistrationBean<FilterRegistration.Dynamic> {

/**
* Filters that wrap the servlet request should be ordered less than or equal to this.
Expand Down Expand Up @@ -208,34 +209,24 @@ public boolean isMatchAfter() {
}

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
protected String getDescription() {
Filter filter = getFilter();
Assert.notNull(filter, "Filter must not be null");
String name = getOrDeduceName(filter);
if (!isEnabled()) {
this.logger.info("Filter " + name + " was not registered (disabled)");
return;
}
FilterRegistration.Dynamic added = servletContext.addFilter(name, filter);
if (added == null) {
this.logger.info("Filter " + name + " was not registered "
+ "(possibly already registered?)");
return;
}
configure(added);
return "filter " + getOrDeduceName(filter);
}

/**
* Return the {@link Filter} to be registered.
* @return the filter
*/
public abstract T getFilter();
@Override
protected Dynamic addRegistration(String description, ServletContext servletContext) {
Filter filter = getFilter();
return servletContext.addFilter(getOrDeduceName(filter), filter);
}

/**
* Configure registration settings. Subclasses can override this method to perform
* additional configuration if required.
* @param registration the registration
*/
@Override
protected void configure(FilterRegistration.Dynamic registration) {
super.configure(registration);
EnumSet<DispatcherType> dispatcherTypes = this.dispatcherTypes;
Expand Down Expand Up @@ -269,4 +260,10 @@ protected void configure(FilterRegistration.Dynamic registration) {
}
}

/**
* Return the {@link Filter} to be registered.
* @return the filter
*/
public abstract T getFilter();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* Copyright 2012-2018 the original author or authors.
*
* Licensed 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.springframework.boot.web.servlet;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.Registration;
import javax.servlet.ServletContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.core.Conventions;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
* Base class for Servlet 3.0+ {@link javax.servlet.Registration.Dynamic dynamic} based
* registration beans.
*
* @param <D> The dynamic registration result
* @author Phillip Webb
* @since 2.0.0
*/
public abstract class DynaimcRegistrationBean<D extends Registration.Dynamic>
extends RegistrationBean {

private static final Log logger = LogFactory.getLog(RegistrationBean.class);

private String name;

private boolean asyncSupported = true;

private Map<String, String> initParameters = new LinkedHashMap<>();

/**
* Set the name of this registration. If not specified the bean name will be used.
* @param name the name of the registration
*/
public void setName(String name) {
Assert.hasLength(name, "Name must not be empty");
this.name = name;
}

/**
* Sets if asynchronous operations are support for this registration. If not specified
* defaults to {@code true}.
* @param asyncSupported if async is supported
*/
public void setAsyncSupported(boolean asyncSupported) {
this.asyncSupported = asyncSupported;
}

/**
* Returns if asynchronous operations are support for this registration.
* @return if async is supported
*/
public boolean isAsyncSupported() {
return this.asyncSupported;
}

/**
* Set init-parameters for this registration. Calling this method will replace any
* existing init-parameters.
* @param initParameters the init parameters
* @see #getInitParameters
* @see #addInitParameter
*/
public void setInitParameters(Map<String, String> initParameters) {
Assert.notNull(initParameters, "InitParameters must not be null");
this.initParameters = new LinkedHashMap<>(initParameters);
}

/**
* Returns a mutable Map of the registration init-parameters.
* @return the init parameters
*/
public Map<String, String> getInitParameters() {
return this.initParameters;
}

/**
* Add a single init-parameter, replacing any existing parameter with the same name.
* @param name the init-parameter name
* @param value the init-parameter value
*/
public void addInitParameter(String name, String value) {
Assert.notNull(name, "Name must not be null");
this.initParameters.put(name, value);
}

@Override
protected final void register(String description, ServletContext servletContext) {
D registration = addRegistration(description, servletContext);
if (registration == null) {
logger.info(StringUtils.capitalize(description) + " was not registered "
+ "(possibly already registered?)");
return;
}
Assert.state(registration != null,
() -> "Registration is null. Was something already registered for "
+ description + "?");
configure(registration);
}

protected abstract D addRegistration(String description,
ServletContext servletContext);

protected void configure(D registration) {
registration.setAsyncSupported(this.asyncSupported);
if (!this.initParameters.isEmpty()) {
registration.setInitParameters(this.initParameters);
}
}

/**
* Deduces the name for this registration. Will return user specified name or fallback
* to convention based naming.
* @param value the object used for convention based names
* @return the deduced name
*/
protected final String getOrDeduceName(Object value) {
return (this.name != null ? this.name : Conventions.getVariableName(value));
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,14 +16,14 @@

package org.springframework.boot.web.servlet;

import java.util.LinkedHashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import javax.servlet.Registration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.core.Conventions;
import org.springframework.core.Ordered;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
* Base class for Servlet 3.0+ based registration beans.
Expand All @@ -37,41 +37,35 @@
*/
public abstract class RegistrationBean implements ServletContextInitializer, Ordered {

private String name;
private static final Log logger = LogFactory.getLog(RegistrationBean.class);

private int order = Ordered.LOWEST_PRECEDENCE;

private boolean asyncSupported = true;

private boolean enabled = true;

private Map<String, String> initParameters = new LinkedHashMap<>();

/**
* Set the name of this registration. If not specified the bean name will be used.
* @param name the name of the registration
*/
public void setName(String name) {
Assert.hasLength(name, "Name must not be empty");
this.name = name;
@Override
public final void onStartup(ServletContext servletContext) throws ServletException {
String description = getDescription();
if (!isEnabled()) {
logger.info(StringUtils.capitalize(description)
+ " was not registered (disabled)");
return;
}
register(description, servletContext);
}

/**
* Sets if asynchronous operations are support for this registration. If not specified
* defaults to {@code true}.
* @param asyncSupported if async is supported
* Return a description of the registration. For example "Servlet resourceServlet"
* @return a description of the registration
*/
public void setAsyncSupported(boolean asyncSupported) {
this.asyncSupported = asyncSupported;
}
protected abstract String getDescription();

/**
* Returns if asynchronous operations are support for this registration.
* @return if async is supported
* Register this bean with the servlet context.
* @param description a description of the item being registered
* @param servletContext the servlet context
*/
public boolean isAsyncSupported() {
return this.asyncSupported;
}
protected abstract void register(String description, ServletContext servletContext);

/**
* Flag to indicate that the registration is enabled.
Expand All @@ -89,60 +83,6 @@ public boolean isEnabled() {
return this.enabled;
}

/**
* Set init-parameters for this registration. Calling this method will replace any
* existing init-parameters.
* @param initParameters the init parameters
* @see #getInitParameters
* @see #addInitParameter
*/
public void setInitParameters(Map<String, String> initParameters) {
Assert.notNull(initParameters, "InitParameters must not be null");
this.initParameters = new LinkedHashMap<>(initParameters);
}

/**
* Returns a mutable Map of the registration init-parameters.
* @return the init parameters
*/
public Map<String, String> getInitParameters() {
return this.initParameters;
}

/**
* Add a single init-parameter, replacing any existing parameter with the same name.
* @param name the init-parameter name
* @param value the init-parameter value
*/
public void addInitParameter(String name, String value) {
Assert.notNull(name, "Name must not be null");
this.initParameters.put(name, value);
}

/**
* Deduces the name for this registration. Will return user specified name or fallback
* to convention based naming.
* @param value the object used for convention based names
* @return the deduced name
*/
protected final String getOrDeduceName(Object value) {
return (this.name != null ? this.name : Conventions.getVariableName(value));
}

/**
* Configure registration base settings.
* @param registration the registration
*/
protected void configure(Registration.Dynamic registration) {
Assert.state(registration != null,
() -> "Registration is null. Was something already registered for name=["
+ this.name + "]?");
registration.setAsyncSupported(this.asyncSupported);
if (!this.initParameters.isEmpty()) {
registration.setInitParameters(this.initParameters);
}
}

/**
* Set the order of the registration bean.
* @param order the order
Expand Down
Loading

0 comments on commit 9cb5f3d

Please sign in to comment.