Commits

Zemian Deng committed a980723

Added HealthServlet.

Comments (0)

Files changed (3)

timemachine-web/src/main/java/timemachine/schedulerweb/HealthServlet.java

+/*
+ * Copyright 2012 Zemian Deng
+ * 
+ * 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 timemachine.schedulerweb;
+
+import java.io.IOException;
+import java.util.Date;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.commons.lang.time.DurationFormatUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Serve health page.
+ *
+ * @author Zemian Deng
+ */
+public class HealthServlet extends HttpServlet
+{
+	private static Logger logger = LoggerFactory.getLogger(HealthServlet.class);
+	
+	private static final long serialVersionUID = 1L;
+	
+	private static final String VIEW = "health.jsp";
+	
+	@Override
+	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
+	{
+		long startTime = ((Date)getServletContext().getAttribute("webappStartTime")).getTime();
+		String webappUpTime = DurationFormatUtils.formatDurationHMS(System.currentTimeMillis() - startTime); 
+		logger.trace("Getting health info.");
+		req.setAttribute("serverInfo", getServletContext().getServerInfo());
+		req.setAttribute("serverTime", new Date().getTime());
+		req.setAttribute("webappUpTime", webappUpTime);
+		req.getRequestDispatcher(VIEW).forward(req, resp);
+	}
+}

timemachine-web/src/main/webapp/WEB-INF/web.xml

 		<servlet-name>EventHistoryServlet</servlet-name>
 		<url-pattern>/event-history</url-pattern>
 	</servlet-mapping>
+	<servlet>
+		<servlet-name>HealthServlet</servlet-name>
+		<servlet-class>timemachine.schedulerweb.HealthServlet</servlet-class>
+	</servlet>
+	<servlet-mapping>
+		<servlet-name>HealthServlet</servlet-name>
+		<url-pattern>/health</url-pattern>
+	</servlet-mapping>
 
 	<context-param>
 		<param-name>schedulerConfigProps</param-name>

timemachine-web/src/main/webapp/health.jsp

 	<schedulerNodeId>${ scheduler.schedulerNode.id }</schedulerNodeId>
 	<schedulerStarted>${ scheduler.started }</schedulerStarted>
 	<schedulerVersion>${ schedulerVersion }</schedulerVersion>
-	<serverInfo><%= application.getServerInfo() %></serverInfo>
-	<serverTime><%= new java.util.Date().getTime() %></serverTime>
-	<webappUpTime>${ webappStartTime }</webappUpTime>
+	<serverInfo>${ serverInfo }</serverInfo>
+	<serverTime>${ serverTime }</serverTime>
+	<webappUpTime>${ webappUpTime }</webappUpTime>
 </health>