001: package liquibase.servlet;
002:
003: import javax.servlet.http.HttpServlet;
004: import javax.servlet.http.HttpServletRequest;
005: import javax.servlet.http.HttpServletResponse;
006: import javax.servlet.ServletException;
007: import java.util.logging.LogRecord;
008: import java.util.logging.Level;
009: import java.util.List;
010: import java.util.ArrayList;
011: import java.util.Date;
012: import java.io.IOException;
013: import java.io.PrintWriter;
014: import java.text.DateFormat;
015:
016: /**
017: * Servlet that can be registered via web.xml to view the log of the LiquiBase run from the LiquibaseServletListener.
018: */
019: public class LiquibaseStatusServlet extends HttpServlet {
020:
021: private static List<LogRecord> liquibaseRunLog = new ArrayList<LogRecord>();
022:
023: public static synchronized void logMessage(LogRecord message) {
024: liquibaseRunLog.add(message);
025:
026: }
027:
028: protected void doGet(HttpServletRequest httpServletRequest,
029: HttpServletResponse httpServletResponse)
030: throws ServletException, IOException {
031: httpServletResponse.setContentType("text/html");
032:
033: String logLevelToDisplay = httpServletRequest
034: .getParameter("logLevel");
035: Level currentLevel = Level.INFO;
036: if (logLevelToDisplay != null) {
037: currentLevel = Level.parse(logLevelToDisplay);
038: }
039:
040: PrintWriter writer = httpServletResponse.getWriter();
041: writer.println("<html>");
042: writer.println("<head><title>LiquiBase Status</title></head>");
043: writer.println("<body>");
044: if (liquibaseRunLog.size() == 0) {
045: writer.println("<b>LiquiBase did not run</b>");
046: } else {
047: writer.println("<b>View level: "
048: + getLevelLink(Level.SEVERE, currentLevel,
049: httpServletRequest)
050: + " "
051: + getLevelLink(Level.WARNING, currentLevel,
052: httpServletRequest)
053: + " "
054: + getLevelLink(Level.INFO, currentLevel,
055: httpServletRequest)
056: + " "
057: + getLevelLink(Level.CONFIG, currentLevel,
058: httpServletRequest)
059: + " "
060: + getLevelLink(Level.FINE, currentLevel,
061: httpServletRequest)
062: + " "
063: + getLevelLink(Level.FINER, currentLevel,
064: httpServletRequest)
065: + " "
066: + getLevelLink(Level.FINEST, currentLevel,
067: httpServletRequest) + "</b>");
068:
069: writer.println("<hr>");
070: writer.println("<b>LiquiBase started at "
071: + DateFormat.getDateTimeInstance()
072: .format(
073: new Date(liquibaseRunLog.get(0)
074: .getMillis())));
075: writer.println("<hr>");
076: writer.println("<pre>");
077: for (LogRecord record : liquibaseRunLog) {
078: if (record.getLevel().intValue() >= currentLevel
079: .intValue()) {
080: writer.println(record.getLevel() + ": "
081: + record.getMessage());
082: if (record.getThrown() != null) {
083: record.getThrown().printStackTrace(writer);
084: }
085: }
086: }
087: writer.println("");
088: writer.println("");
089:
090: writer.println("</pre>");
091: writer.println("<hr>");
092: writer.println("<b>LiquiBase finished at "
093: + DateFormat.getDateTimeInstance().format(
094: new Date(liquibaseRunLog.get(
095: liquibaseRunLog.size() - 1)
096: .getMillis())));
097: }
098: writer.println("</body>");
099: writer.println("</html>");
100: }
101:
102: private String getLevelLink(Level level, Level currentLevel,
103: HttpServletRequest request) {
104: if (currentLevel.equals(level)) {
105: return level.getName();
106: } else {
107: return "<a href=" + request.getRequestURI() + "?logLevel="
108: + level.getName() + ">" + level.getName() + "</a>";
109: }
110: }
111: }
|