NIHVIVO-336 Create StartupStatusDisplayFilter which will forcibly display the StartupStatus if there are warnings or errors.

This commit is contained in:
j2blake 2011-09-23 18:53:36 +00:00
parent e6b4238019
commit 39dc9736a2
3 changed files with 184 additions and 0 deletions

View file

@ -0,0 +1,79 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.filters;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import edu.cornell.mannlib.vitro.webapp.startup.StartupStatus;
import freemarker.cache.WebappTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
/**
* No matter what URL is requested, check to see whether the StartupStatus
* contains errors or warnings. If it does, hijack the request to show the
* StartupStatus display page.
*
* This only happens once. This filter does nothing after displaying the startup
* status page one time.
*/
public class StartupStatusDisplayFilter implements Filter {
private static final String TEMPLATE_PATH = "/templates/freemarker/body/admin/startupStatus-displayRaw.ftl";
private ServletContext ctx;
private boolean statusAlreadyDisplayed;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
ctx = filterConfig.getServletContext();
statusAlreadyDisplayed = false;
}
@Override
public void destroy() {
// nothing to do.
}
@Override
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
StartupStatus ss = StartupStatus.getBean(ctx);
if (ss.allClear() || statusAlreadyDisplayed) {
chain.doFilter(req, resp);
return;
}
displayStartupStatus(req, resp);
statusAlreadyDisplayed = true;
}
private void displayStartupStatus(ServletRequest req, ServletResponse resp)
throws IOException, ServletException {
Configuration cfg = new Configuration();
cfg.setTemplateLoader(new WebappTemplateLoader(ctx));
Template tpl = cfg.getTemplate(TEMPLATE_PATH);
Map<String, Object> bodyMap = new HashMap<String, Object>();
bodyMap.put("status", StartupStatus.getBean(ctx));
try {
PrintWriter out = resp.getWriter();
tpl.process(bodyMap, out);
out.flush();
} catch (TemplateException e) {
throw new ServletException("Problem with Freemarker Template", e);
}
}
}

View file

@ -0,0 +1,28 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
/* Styles for Freemarker template startupStatus-display and startupStatus-displayRaw */
table.item {
border: thin solid black;
font-family: monospace;
width: 100%;
}
table.item tr.top {
font-size: larger;
}
table.item td {
border: thin solid black;
}
.error td {
background: #FFDDDD;
font-weight: bolder;
}
.warning td {
background: #FFFFDD;
}
.info td {
background: #DDFFDD;
}
.not_executed td {
color: #444444;
}

View file

@ -0,0 +1,77 @@
<#-- $This file is distributed under the terms of the license in /doc/license.txt$ -->
<#--
Template for the raw page that displays the StartupStatus if there
are warnings or errors.
"raw" because this template works outside of the usual framework, in
case the Freemarker context didn't initialize properly.
-->
<#macro statusItem item>
<#if item.level = "FATAL">
<#assign color = "error" >
<#elseif item.level = "WARNING">
<#assign color = "warning" >
<#elseif item.level = "INFO">
<#assign color = "info" >
<#elseif item.level = "NOT_EXECUTED">
<#assign color = "not_executed" >
<#else>
<#assign color = "" >
</#if>
<tr><td>
<table cellspacing="0" class="item ${color}">
<tr class="top">
<td width="20%">${item.level}</td>
<td>${item.shortSourceName}</td>
</tr>
<tr>
<td colspan="2">${item.message}</td>
</tr>
<tr>
<td colspan="2">${item.sourceName}</td>
</tr>
<#if item.cause??>
<tr>
<td colspan="2">${item.cause}</td>
</tr>
</#if>
</table>
</td></tr>
</#macro>
<html>
<head>
<title>Startup Status</title>
<link rel="stylesheet" type="text/css" href="./css/startupStatus.css">
</head>
<body>
<#if status.errorItems?has_content>
<h2>Fatal error</h2>
<p>VIVO detected a fatal error during startup.</p>
<p><a href=".">Continue</a></p>
<#list status.errorItems as item>
<@statusItem item=item />
</#list>
</#if>
<#if status.warningItems?has_content>
<h2>Warning</h2>
<p>VIVO issued warnings during startup.</p>
<p><a href=".">Continue</a></p>
<#list status.warningItems as item>
<@statusItem item=item />
</#list>
</#if>
<h2>Startup trace</h2>
<p>The full list of startup events and messages.</p>
<table cellspacing="0" class="trace">
<#list status.statusItems as item>
<@statusItem item=item />
</#list>
</table>
</body>
</html>