javax.servlet.LoggingServletContextListener Maven / Gradle / Ivy
package javax.servlet;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.SystemPropertyUtils;
import org.springframework.util.comparator.NullSafeComparator;
import org.springframework.web.context.support.WebApplicationContextUtils;
/**
* @see javax.servlet.annotation.WebListener
*/
public class LoggingServletContextListener implements ServletContextListener {
protected final Log logger = LogFactory.getLog(getClass());
private String lineSeparator = SystemPropertyUtils.resolvePlaceholders("${line.separator}");
private String tapSeparator = SystemPropertyUtils.resolvePlaceholders("${tap.separator:\t}");
@Override
public void contextInitialized(ServletContextEvent sce) {
ServletContext sc = sce.getServletContext();
StringBuilder stringBuilder = new StringBuilder(this.lineSeparator);
stringBuilder.append("ServerInfo: ");
stringBuilder.append(sc.getServerInfo());
stringBuilder.append(this.lineSeparator);
stringBuilder.append("Version: ");
stringBuilder.append(sc.getMajorVersion());
stringBuilder.append('.');
stringBuilder.append(sc.getMinorVersion());
stringBuilder.append(this.lineSeparator);
stringBuilder.append("InitParameter: ");
stringBuilder.append(this.lineSeparator);
for (Enumeration enumeration = sc.getInitParameterNames(); enumeration.hasMoreElements();) {
String nextElement = enumeration.nextElement();
stringBuilder.append(this.tapSeparator);
stringBuilder.append(nextElement);
stringBuilder.append('=');
stringBuilder.append(sc.getInitParameter(nextElement));
stringBuilder.append(this.lineSeparator);
}
stringBuilder.append("Attribute: ");
stringBuilder.append(this.lineSeparator);
for (Enumeration enumeration = sc.getAttributeNames(); enumeration.hasMoreElements();) {
String nextElement = enumeration.nextElement();
stringBuilder.append(this.tapSeparator);
stringBuilder.append(nextElement);
stringBuilder.append(this.lineSeparator);
}
stringBuilder.append("Properties: ");
stringBuilder.append(this.lineSeparator);
stringBuilder.append(new SortProperties(System.getProperties(), this.lineSeparator, this.tapSeparator));
stringBuilder.append(this.lineSeparator);
stringBuilder.append(new SortProperties(System.getenv(), this.lineSeparator, this.tapSeparator));
if (logger.isTraceEnabled()) {
logger.trace(new String(stringBuilder));
}
if (logger.isInfoEnabled()) {
logger.info("Context Initialized " + getContext(sc));
}
}
/**
*
*
*
*
* @see java.util.Collections#list(Enumeration)
*/
@Override
public void contextDestroyed(ServletContextEvent sce) {
ServletContext sc = sce.getServletContext();
for (Enumeration enumeration = DriverManager.getDrivers(); enumeration.hasMoreElements();) {
Driver nextElement = enumeration.nextElement();
try {
DriverManager.deregisterDriver(nextElement);
}
catch (SQLException e) {
if (logger.isWarnEnabled()) {
logger.warn("Deregister Driver fail... from " + ClassUtils.getShortName(nextElement.getClass()), e);
}
}
}
if (logger.isInfoEnabled()) {
logger.info("Context Destroyed " + getContext(sc));
}
}
/**
* ({@code yyyy-MM-dd}, {@code HH:mm:ss.SSSZ}, {@code yyyy-MM-dd'T'HH:mm:ss.SSSZ},
* {@code yyyy-MM-dd'T'HH:mm:ss.SSS'Z'XXX})
*
* @see org.springframework.format.annotation.DateTimeFormat.ISO
*/
private String getContext(ServletContext servletContext) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(" ContextPath: ");
stringBuilder.append(servletContext.getContextPath());
stringBuilder.append(", ServletContextName: ");
stringBuilder.append(servletContext.getServletContextName());
stringBuilder.append(", Date: ");
stringBuilder.append(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'Z", Locale.getDefault()).format(new Date()));
stringBuilder.append(", Id: ");
stringBuilder.append(WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext).getId());
return new String(stringBuilder);
}
}
class SortProperties extends Properties {
private static final long serialVersionUID = 686680107445512538L;
private final String suffix;
private final String prefix;
public SortProperties(Map map, String suffix, String prefix) {
Assert.notEmpty(map, "Map must have entries");
Assert.notNull(this.suffix = suffix, "'suffix' must not be null");
Assert.notNull(this.prefix = prefix, "'prefix' must not be null");
putAll(map);
}
/**
* @see java.util.TreeSet#TreeSet(java.util.Collection)
*/
@SuppressWarnings("unchecked")
@Override
public synchronized Enumeration