forked from pongasoft/glu
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added sample webapp with monitoring servlet
- Loading branch information
Showing
9 changed files
with
355 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
samples/org.linkedin.glu.samples.sample-webapp/build.gradle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
37 changes: 37 additions & 0 deletions
37
....glu.samples.sample-webapp/src/main/java/org/linkedin/glu/samples/webapp/HomeServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
...u.samples.sample-webapp/src/main/java/org/linkedin/glu/samples/webapp/MonitorServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...u.samples.sample-webapp/src/main/java/org/linkedin/glu/samples/webapp/SampleListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
samples/org.linkedin.glu.samples.sample-webapp/src/main/webapp/WEB-INF/web.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
24 changes: 24 additions & 0 deletions
24
samples/org.linkedin.glu.samples.sample-webapp/src/main/webapp/home.jsp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
77 changes: 77 additions & 0 deletions
77
samples/org.linkedin.glu.samples.sample-webapp/src/main/webapp/monitor.jsp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters