Commit 9cb5f3da authored by Phillip Webb's avatar Phillip Webb

Create DynamicRegistrationBean

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
parent 85d3f5a1
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -56,8 +56,8 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplicat ...@@ -56,8 +56,8 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplicat
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration; import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties; 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.FilterRegistrationBean;
import org.springframework.boot.web.servlet.RegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
...@@ -181,7 +181,7 @@ public class JerseyAutoConfiguration implements ServletContextAware { ...@@ -181,7 +181,7 @@ public class JerseyAutoConfiguration implements ServletContextAware {
return ClassUtils.getUserClass(this.config.getClass()).getName(); 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()) { for (Entry<String, String> entry : this.jersey.getInit().entrySet()) {
registration.addInitParameter(entry.getKey(), entry.getValue()); registration.addInitParameter(entry.getKey(), entry.getValue());
} }
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -26,8 +26,8 @@ import java.util.Set; ...@@ -26,8 +26,8 @@ import java.util.Set;
import javax.servlet.DispatcherType; import javax.servlet.DispatcherType;
import javax.servlet.Filter; import javax.servlet.Filter;
import javax.servlet.FilterRegistration; import javax.servlet.FilterRegistration;
import javax.servlet.FilterRegistration.Dynamic;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
...@@ -41,7 +41,8 @@ import org.springframework.util.Assert; ...@@ -41,7 +41,8 @@ import org.springframework.util.Assert;
* @param <T> the type of {@link Filter} to register * @param <T> the type of {@link Filter} to register
* @author Phillip Webb * @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. * Filters that wrap the servlet request should be ordered less than or equal to this.
...@@ -208,34 +209,24 @@ abstract class AbstractFilterRegistrationBean<T extends Filter> extends Registra ...@@ -208,34 +209,24 @@ abstract class AbstractFilterRegistrationBean<T extends Filter> extends Registra
} }
@Override @Override
public void onStartup(ServletContext servletContext) throws ServletException { protected String getDescription() {
Filter filter = getFilter(); Filter filter = getFilter();
Assert.notNull(filter, "Filter must not be null"); Assert.notNull(filter, "Filter must not be null");
String name = getOrDeduceName(filter); return "filter " + 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);
} }
/** @Override
* Return the {@link Filter} to be registered. protected Dynamic addRegistration(String description, ServletContext servletContext) {
* @return the filter Filter filter = getFilter();
*/ return servletContext.addFilter(getOrDeduceName(filter), filter);
public abstract T getFilter(); }
/** /**
* Configure registration settings. Subclasses can override this method to perform * Configure registration settings. Subclasses can override this method to perform
* additional configuration if required. * additional configuration if required.
* @param registration the registration * @param registration the registration
*/ */
@Override
protected void configure(FilterRegistration.Dynamic registration) { protected void configure(FilterRegistration.Dynamic registration) {
super.configure(registration); super.configure(registration);
EnumSet<DispatcherType> dispatcherTypes = this.dispatcherTypes; EnumSet<DispatcherType> dispatcherTypes = this.dispatcherTypes;
...@@ -269,4 +260,10 @@ abstract class AbstractFilterRegistrationBean<T extends Filter> extends Registra ...@@ -269,4 +260,10 @@ abstract class AbstractFilterRegistrationBean<T extends Filter> extends Registra
} }
} }
/**
* Return the {@link Filter} to be registered.
* @return the filter
*/
public abstract T getFilter();
} }
/*
* 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));
}
}
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -16,14 +16,14 @@ ...@@ -16,14 +16,14 @@
package org.springframework.boot.web.servlet; package org.springframework.boot.web.servlet;
import java.util.LinkedHashMap; import javax.servlet.ServletContext;
import java.util.Map; 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.core.Ordered;
import org.springframework.util.Assert; import org.springframework.util.StringUtils;
/** /**
* Base class for Servlet 3.0+ based registration beans. * Base class for Servlet 3.0+ based registration beans.
...@@ -37,41 +37,35 @@ import org.springframework.util.Assert; ...@@ -37,41 +37,35 @@ import org.springframework.util.Assert;
*/ */
public abstract class RegistrationBean implements ServletContextInitializer, Ordered { 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 int order = Ordered.LOWEST_PRECEDENCE;
private boolean asyncSupported = true;
private boolean enabled = true; private boolean enabled = true;
private Map<String, String> initParameters = new LinkedHashMap<>(); @Override
public final void onStartup(ServletContext servletContext) throws ServletException {
/** String description = getDescription();
* Set the name of this registration. If not specified the bean name will be used. if (!isEnabled()) {
* @param name the name of the registration logger.info(StringUtils.capitalize(description)
*/ + " was not registered (disabled)");
public void setName(String name) { return;
Assert.hasLength(name, "Name must not be empty"); }
this.name = name; register(description, servletContext);
} }
/** /**
* Sets if asynchronous operations are support for this registration. If not specified * Return a description of the registration. For example "Servlet resourceServlet"
* defaults to {@code true}. * @return a description of the registration
* @param asyncSupported if async is supported
*/ */
public void setAsyncSupported(boolean asyncSupported) { protected abstract String getDescription();
this.asyncSupported = asyncSupported;
}
/** /**
* Returns if asynchronous operations are support for this registration. * Register this bean with the servlet context.
* @return if async is supported * @param description a description of the item being registered
* @param servletContext the servlet context
*/ */
public boolean isAsyncSupported() { protected abstract void register(String description, ServletContext servletContext);
return this.asyncSupported;
}
/** /**
* Flag to indicate that the registration is enabled. * Flag to indicate that the registration is enabled.
...@@ -89,60 +83,6 @@ public abstract class RegistrationBean implements ServletContextInitializer, Ord ...@@ -89,60 +83,6 @@ public abstract class RegistrationBean implements ServletContextInitializer, Ord
return this.enabled; 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. * Set the order of the registration bean.
* @param order the order * @param order the order
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -188,7 +188,6 @@ public class ServletContextInitializerBeans ...@@ -188,7 +188,6 @@ public class ServletContextInitializerBeans
// One that we haven't already seen // One that we haven't already seen
RegistrationBean registration = adapter.createRegistrationBean(beanName, RegistrationBean registration = adapter.createRegistrationBean(beanName,
bean.getValue(), beans.size()); bean.getValue(), beans.size());
registration.setName(beanName);
registration.setOrder(order); registration.setOrder(order);
this.initializers.add(type, registration); this.initializers.add(type, registration);
if (ServletContextInitializerBeans.logger.isDebugEnabled()) { if (ServletContextInitializerBeans.logger.isDebugEnabled()) {
...@@ -247,8 +246,8 @@ public class ServletContextInitializerBeans ...@@ -247,8 +246,8 @@ public class ServletContextInitializerBeans
} }
/** /**
* Adapter to convert a given Bean type into a {@link RegistrationBean} (and hence a * Adapter to convert a given Bean type into a {@link DynaimcRegistrationBean} (and
* {@link ServletContextInitializer}. * hence a {@link ServletContextInitializer}.
*/ */
private interface RegistrationBeanAdapter<T> { private interface RegistrationBeanAdapter<T> {
...@@ -278,6 +277,7 @@ public class ServletContextInitializerBeans ...@@ -278,6 +277,7 @@ public class ServletContextInitializerBeans
} }
ServletRegistrationBean<Servlet> bean = new ServletRegistrationBean<>(source, ServletRegistrationBean<Servlet> bean = new ServletRegistrationBean<>(source,
url); url);
bean.setName(name);
bean.setMultipartConfig(this.multipartConfig); bean.setMultipartConfig(this.multipartConfig);
return bean; return bean;
} }
...@@ -293,7 +293,9 @@ public class ServletContextInitializerBeans ...@@ -293,7 +293,9 @@ public class ServletContextInitializerBeans
@Override @Override
public RegistrationBean createRegistrationBean(String name, Filter source, public RegistrationBean createRegistrationBean(String name, Filter source,
int totalNumberOfSourceBeans) { int totalNumberOfSourceBeans) {
return new FilterRegistrationBean<>(source); FilterRegistrationBean<Filter> bean = new FilterRegistrationBean<>(source);
bean.setName(name);
return bean;
} }
} }
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -19,21 +19,16 @@ package org.springframework.boot.web.servlet; ...@@ -19,21 +19,16 @@ package org.springframework.boot.web.servlet;
import java.util.Collections; import java.util.Collections;
import java.util.EventListener; import java.util.EventListener;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map;
import java.util.Set; import java.util.Set;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeListener; import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextListener; import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;
import javax.servlet.ServletRequestAttributeListener; import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.ServletRequestListener; 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;
...@@ -61,9 +56,6 @@ import org.springframework.util.ClassUtils; ...@@ -61,9 +56,6 @@ import org.springframework.util.ClassUtils;
public class ServletListenerRegistrationBean<T extends EventListener> public class ServletListenerRegistrationBean<T extends EventListener>
extends RegistrationBean { extends RegistrationBean {
private static final Log logger = LogFactory
.getLog(ServletListenerRegistrationBean.class);
private static final Set<Class<?>> SUPPORTED_TYPES; private static final Set<Class<?>> SUPPORTED_TYPES;
static { static {
...@@ -106,80 +98,21 @@ public class ServletListenerRegistrationBean<T extends EventListener> ...@@ -106,80 +98,21 @@ public class ServletListenerRegistrationBean<T extends EventListener>
} }
/** /**
* Set the name of this registration. If not specified the bean name will be used. * Return the listener to be registered.
* @param name the name of the registration * @return the listener to be registered
* @deprecated as of 1.5 since not applicable to listeners
*/
@Override
@Deprecated
public void setName(String name) {
super.setName(name);
}
/**
* Sets if asynchronous operations are support for this registration. If not specified
* defaults to {@code true}.
* @param asyncSupported if async is supported
* @deprecated as of 1.5 since not applicable to listeners
*/
@Override
@Deprecated
public void setAsyncSupported(boolean asyncSupported) {
super.setAsyncSupported(asyncSupported);
}
/**
* Returns if asynchronous operations are support for this registration.
* @return if async is supported
* @deprecated as of 1.5 since not applicable to listeners
*/
@Override
@Deprecated
public boolean isAsyncSupported() {
return super.isAsyncSupported();
}
/**
* Set init-parameters for this registration. Calling this method will replace any
* existing init-parameters.
* @param initParameters the init parameters
* @deprecated as of 1.5 since not applicable to listeners
*/
@Override
@Deprecated
public void setInitParameters(Map<String, String> initParameters) {
super.setInitParameters(initParameters);
}
/**
* Returns a mutable Map of the registration init-parameters.
* @return the init parameters
* @deprecated as of 1.5 since not applicable to listeners
*/ */
@Override public T getListener() {
@Deprecated return this.listener;
public Map<String, String> getInitParameters() {
return super.getInitParameters();
} }
/**
* 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
* @deprecated as of 1.5 since not applicable to listeners
*/
@Override @Override
@Deprecated protected String getDescription() {
public void addInitParameter(String name, String value) { Assert.notNull(this.listener, "Listener must not be null");
super.addInitParameter(name, value); return "listener " + this.listener;
} }
@Override @Override
public void onStartup(ServletContext servletContext) throws ServletException { protected void register(String description, ServletContext servletContext) {
if (!isEnabled()) {
logger.info("Listener " + this.listener + " was not registered (disabled)");
return;
}
try { try {
servletContext.addListener(this.listener); servletContext.addListener(this.listener);
} }
...@@ -190,10 +123,6 @@ public class ServletListenerRegistrationBean<T extends EventListener> ...@@ -190,10 +123,6 @@ public class ServletListenerRegistrationBean<T extends EventListener>
} }
} }
public T getListener() {
return this.listener;
}
/** /**
* Returns {@code true} if the specified listener is one of the supported types. * Returns {@code true} if the specified listener is one of the supported types.
* @param listener the listener to test * @param listener the listener to test
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -24,9 +24,7 @@ import java.util.Set; ...@@ -24,9 +24,7 @@ import java.util.Set;
import javax.servlet.MultipartConfigElement; import javax.servlet.MultipartConfigElement;
import javax.servlet.Servlet; import javax.servlet.Servlet;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration; import javax.servlet.ServletRegistration;
import javax.servlet.ServletRegistration.Dynamic;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
...@@ -52,7 +50,8 @@ import org.springframework.util.ObjectUtils; ...@@ -52,7 +50,8 @@ import org.springframework.util.ObjectUtils;
* @see ServletContextInitializer * @see ServletContextInitializer
* @see ServletContext#addServlet(String, Servlet) * @see ServletContext#addServlet(String, Servlet)
*/ */
public class ServletRegistrationBean<T extends Servlet> extends RegistrationBean { public class ServletRegistrationBean<T extends Servlet>
extends DynaimcRegistrationBean<ServletRegistration.Dynamic> {
private static final Log logger = LogFactory.getLog(ServletRegistrationBean.class); private static final Log logger = LogFactory.getLog(ServletRegistrationBean.class);
...@@ -172,30 +171,18 @@ public class ServletRegistrationBean<T extends Servlet> extends RegistrationBean ...@@ -172,30 +171,18 @@ public class ServletRegistrationBean<T extends Servlet> extends RegistrationBean
return this.multipartConfig; return this.multipartConfig;
} }
/** @Override
* Returns the servlet name that will be registered. protected String getDescription() {
* @return the servlet name Assert.notNull(this.servlet, "Servlet must not be null");
*/ return "servlet " + getServletName();
public String getServletName() {
return getOrDeduceName(this.servlet);
} }
@Override @Override
public void onStartup(ServletContext servletContext) throws ServletException { protected ServletRegistration.Dynamic addRegistration(String description,
Assert.notNull(this.servlet, "Servlet must not be null"); ServletContext servletContext) {
String name = getServletName(); String name = getServletName();
if (!isEnabled()) { logger.info("Servlet " + name + " mapped to " + this.urlMappings);
logger.info("Servlet " + name + " was not registered (disabled)"); return servletContext.addServlet(name, this.servlet);
return;
}
logger.info("Mapping servlet: '" + name + "' to " + this.urlMappings);
Dynamic added = servletContext.addServlet(name, this.servlet);
if (added == null) {
logger.info("Servlet " + name + " was not registered "
+ "(possibly already registered?)");
return;
}
configure(added);
} }
/** /**
...@@ -203,6 +190,7 @@ public class ServletRegistrationBean<T extends Servlet> extends RegistrationBean ...@@ -203,6 +190,7 @@ public class ServletRegistrationBean<T extends Servlet> extends RegistrationBean
* additional configuration if required. * additional configuration if required.
* @param registration the registration * @param registration the registration
*/ */
@Override
protected void configure(ServletRegistration.Dynamic registration) { protected void configure(ServletRegistration.Dynamic registration) {
super.configure(registration); super.configure(registration);
String[] urlMapping = this.urlMappings String[] urlMapping = this.urlMappings
...@@ -219,4 +207,11 @@ public class ServletRegistrationBean<T extends Servlet> extends RegistrationBean ...@@ -219,4 +207,11 @@ public class ServletRegistrationBean<T extends Servlet> extends RegistrationBean
} }
} }
/**
* Returns the servlet name that will be registered.
* @return the servlet name
*/
public String getServletName() {
return getOrDeduceName(this.servlet);
}
} }
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment