Skip to content

Commit

Permalink
added sample webapp with monitoring servlet
Browse files Browse the repository at this point in the history
  • Loading branch information
ypujante committed Jan 9, 2011
1 parent e774679 commit 127d044
Show file tree
Hide file tree
Showing 9 changed files with 355 additions and 1 deletion.
1 change: 1 addition & 0 deletions project-spec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ spec.external = [
restletExtHttpClient: "org.restlet.jse:org.restlet.ext.httpclient:${spec.versions.restlet}",
restletExtJson: "org.restlet.jse:org.restlet.ext.json:${spec.versions.restlet}",
restletExtSimple: "org.restlet.jse:org.restlet.ext.simple:${spec.versions.restlet}",
servletApi: 'javax.servlet:servlet-api:2.5',
shiro: "org.apache.shiro:shiro-all:1.0.0-incubating",
sigar: "com.hyperic:sigar:${spec.versions.sigar}",
simpleFramework: 'org.simpleframework:simple:4.1.21',
Expand Down
21 changes: 21 additions & 0 deletions samples/org.linkedin.glu.samples.sample-webapp/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2010-2011 LinkedIn, Inc
*
* 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.
*/

apply plugin: 'war'

dependencies {
providedCompile spec.external.servletApi
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2010-2011 LinkedIn, Inc
*
* 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 org.linkedin.glu.samples.webapp;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* @author yan@pongasoft.com
*/
public class HomeServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
request.getRequestDispatcher("/home.jsp").forward(request, response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright (c) 2010-2011 LinkedIn, Inc
*
* 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 org.linkedin.glu.samples.webapp;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* @author yan@pongasoft.com
*/
public class MonitorServlet extends HttpServlet
{
public static enum MonitorState
{
GOOD("Everything is fine"),
BUSY("Application is overloaded (recoverable)"),
DEAD("Application is in a dead state (non recoverable)");

private final String _description;

private MonitorState(String description)
{
_description = description;
}

public String getDescription()
{
return _description;
}
}

public static final String WEBAPP_STATE_ATTRIBUTE = "monitor.webapp.state";

public void init() throws ServletException
{
getServletContext().setAttribute(WEBAPP_STATE_ATTRIBUTE, MonitorState.GOOD);
}

@Override
protected void doHead(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
MonitorState state = (MonitorState) getServletContext().getAttribute(WEBAPP_STATE_ATTRIBUTE);

int responseCode;

switch(state)
{
case GOOD:
responseCode = HttpServletResponse.SC_OK;
break;

case BUSY:
responseCode = HttpServletResponse.SC_SERVICE_UNAVAILABLE;
break;

case DEAD:
responseCode = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
break;

default:
throw new RuntimeException("not reached");
}

response.setHeader("X-glu-monitoring", state.name());

response.sendError(responseCode, state.name());
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
request.getRequestDispatcher("/monitor.jsp").forward(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String newState = request.getParameter(WEBAPP_STATE_ATTRIBUTE);

if(newState != null)
{
getServletContext().setAttribute(WEBAPP_STATE_ATTRIBUTE, MonitorState.valueOf(newState));
}

response.sendRedirect("monitor");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2010-2011 LinkedIn, Inc
*
* 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 org.linkedin.glu.samples.webapp;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
* @author yan@pongasoft.com
*/
public class SampleListener implements ServletContextListener
{

// Public constructor is required by servlet spec
public SampleListener()
{
}

public void contextInitialized(ServletContextEvent sce)
{
sce.getServletContext().log("Initializing sample webapp...");
}

public void contextDestroyed(ServletContextEvent sce)
{
sce.getServletContext().log("Sample webapp destroyed.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2010-2011 LinkedIn, Inc
~
~ 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.
-->

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<listener>
<listener-class>org.linkedin.glu.samples.webapp.SampleListener</listener-class>
</listener>

<servlet>
<servlet-name>HomeServlet</servlet-name>
<servlet-class>org.linkedin.glu.samples.webapp.HomeServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>MonitorServlet</servlet-name>
<servlet-class>org.linkedin.glu.samples.webapp.MonitorServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>HomeServlet</servlet-name>
<url-pattern>/home</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>MonitorServlet</servlet-name>
<url-pattern>/monitor</url-pattern>
</servlet-mapping>
</web-app>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<%--
~ Copyright (c) 2010-2011 LinkedIn, Inc
~
~ 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.
--%>

<html>
<head>
<title>Home - Sample Webapp</title>
</head>
<body>
Welcome home!
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<%@ page import="org.linkedin.glu.samples.webapp.MonitorServlet" %>
<%--
~ Copyright (c) 2010-2011 LinkedIn, Inc
~
~ 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.
--%>
<html>
<head>
<title>Monitor - Sample Webapp</title>
<style type="text/css">
table {
padding-top: 2em;
}
td {
border: solid 1px black;
}
.GOOD {
color: green;
}
.BUSY {
color: orange;
}
.DEAD {
color: red;
}
</style>
</head>
<body>
<%! MonitorServlet.MonitorState currentState; %>
<% currentState = (MonitorServlet.MonitorState)
getServletConfig().getServletContext().getAttribute(MonitorServlet.WEBAPP_STATE_ATTRIBUTE); %>
<h1>Monitor</h1>
Current state reported:
<span class="<%= currentState %>" style="font-weight: bold; font-size: 2em;">
<%= currentState %>
</span>

<form method="POST" action="monitor">
<table>
<tr>
<th>Select One</th>
<th>Value</th>
<th>Description</th>
</tr>

<% for(MonitorServlet.MonitorState state : MonitorServlet.MonitorState.values()) { %>
<tr>
<td>
<input type="radio"
name="<%= MonitorServlet.WEBAPP_STATE_ATTRIBUTE %>"
value="<%= state %>"
<%= currentState == state ? "checked" : ""%>>
</td>
<td><%= state %></td>
<td><%= state.getDescription() %></td>
</tr>
<% } %>

<tr>
<td colspan="3">
<input type="submit" name="changeState" value="Change monitor state"/>
</td>
</tr>
</table>
</form>
</body>
</html>
3 changes: 2 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ include ':agent:org.linkedin.glu.agent-api',
':provisioner:org.linkedin.glu.provisioner-core',
':packaging:org.linkedin.glu.packaging-setup',
':packaging:org.linkedin.glu.packaging-all',
':scripts:org.linkedin.glu.script-jetty'
':scripts:org.linkedin.glu.script-jetty',
':samples:org.linkedin.glu.samples.sample-webapp'

0 comments on commit 127d044

Please sign in to comment.