001/* 002 * Copyright 2012-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.web.servlet; 018 019import javax.servlet.ServletContext; 020import javax.servlet.ServletException; 021 022import org.apache.commons.logging.Log; 023import org.apache.commons.logging.LogFactory; 024 025import org.springframework.core.Ordered; 026import org.springframework.util.StringUtils; 027 028/** 029 * Base class for Servlet 3.0+ based registration beans. 030 * 031 * @author Phillip Webb 032 * @since 1.4.0 033 * @see ServletRegistrationBean 034 * @see FilterRegistrationBean 035 * @see DelegatingFilterProxyRegistrationBean 036 * @see ServletListenerRegistrationBean 037 */ 038public abstract class RegistrationBean implements ServletContextInitializer, Ordered { 039 040 private static final Log logger = LogFactory.getLog(RegistrationBean.class); 041 042 private int order = Ordered.LOWEST_PRECEDENCE; 043 044 private boolean enabled = true; 045 046 @Override 047 public final void onStartup(ServletContext servletContext) throws ServletException { 048 String description = getDescription(); 049 if (!isEnabled()) { 050 logger.info(StringUtils.capitalize(description) 051 + " was not registered (disabled)"); 052 return; 053 } 054 register(description, servletContext); 055 } 056 057 /** 058 * Return a description of the registration. For example "Servlet resourceServlet" 059 * @return a description of the registration 060 */ 061 protected abstract String getDescription(); 062 063 /** 064 * Register this bean with the servlet context. 065 * @param description a description of the item being registered 066 * @param servletContext the servlet context 067 */ 068 protected abstract void register(String description, ServletContext servletContext); 069 070 /** 071 * Flag to indicate that the registration is enabled. 072 * @param enabled the enabled to set 073 */ 074 public void setEnabled(boolean enabled) { 075 this.enabled = enabled; 076 } 077 078 /** 079 * Return if the registration is enabled. 080 * @return if enabled (default {@code true}) 081 */ 082 public boolean isEnabled() { 083 return this.enabled; 084 } 085 086 /** 087 * Set the order of the registration bean. 088 * @param order the order 089 */ 090 public void setOrder(int order) { 091 this.order = order; 092 } 093 094 /** 095 * Get the order of the registration bean. 096 * @return the order 097 */ 098 @Override 099 public int getOrder() { 100 return this.order; 101 } 102 103}