/[projects]/miscJava/CircuitBreaker/src/main/java/dk/thoerup/circuitbreaker/web/CircuitBreakerServletBase.java
ViewVC logotype

Contents of /miscJava/CircuitBreaker/src/main/java/dk/thoerup/circuitbreaker/web/CircuitBreakerServletBase.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2569 - (show annotations) (download)
Tue Jun 9 08:55:10 2015 UTC (8 years, 11 months ago) by torben
File size: 3045 byte(s)
Statistics should be added by composition instead of inheritance
1 package dk.thoerup.circuitbreaker.web;
2
3 import java.io.IOException;
4
5 import javax.servlet.ServletException;
6 import javax.servlet.http.HttpServletRequest;
7 import javax.servlet.http.HttpServletResponse;
8
9 /**
10 * This is a web interface for viewing and controlling the CircuitBreakers currently registered in CircuitBreakerManager
11 * <p>
12 * To use this servlet in you .war module just create a simple servlet that extends this class (instead of HttpServlet)
13 * The new custom servlet doesn't need to implement any of the standard servlet methods - its all done in this implementation.
14 *
15 * <p>
16 * Example:
17 * <pre>
18 * public class CircuitBreakerServlet extends CircuitBreakerServletBase {
19 * private static final long serialVersionUID = 1L;
20 * }
21 * </pre>
22 *
23 * If you add a servlet init pameter to your deployment descriptor with the name 'readonly' and a integer value > 0, you can disable the
24 * control buttons:
25 * <p>
26 * &lt;init-param>&lt;param-name>readonly&lt;/param-name>&lt;param-value>1&lt;/param-value>&lt;/init-param>
27 * <!-- <init-param><param-name>readonly</param-name><param-value>1</param-value></init-param> -->
28 *
29 * You could also configure it dynamically from a ContextListener:
30 * ServletRegistration.Dynamic dynconf = sce.getServletContext().addServlet("circuitbreaker", dk.thoerup.circuitbreaker.web.CircuitBreakerServletBase.class );
31 * dynconf.addMapping("/CircuitBreakerServlet");
32 * dynconf.setInitParameter("readonly", "1");
33 *
34 */
35
36 public class CircuitBreakerServletBase extends javax.servlet.http.HttpServlet {
37
38 private static final long serialVersionUID = 1L;
39
40 private boolean readOnly = false;
41
42
43
44 @Override
45 public void init() throws ServletException {
46 super.init();
47
48 String readOnlyStr = getInitParameter("readonly");
49 if (readOnlyStr != null && readOnlyStr.trim().length() >0) {
50 int readOnlyInt = Integer.parseInt(readOnlyStr.trim());
51 readOnly = (readOnlyInt > 0);
52 }
53 }
54
55 private Command getCommand(String command) throws ServletException {
56 if (command == null || command.equals("list"))
57 return new ListCircuitBreakers();
58
59 if (command.equals("view"))
60 return new ViewCircuitBreaker(readOnly);
61 if (command.equals("action"))
62 return new ActionCommand(readOnly);
63 if (command.equals("log"))
64 return new LogViewCommand();
65
66 throw new ServletException("No such command:" + command);
67 }
68
69 @Override
70 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
71 Command cmd = getCommand( req.getParameter("command"));
72
73 String response = cmd.execute(req,resp);
74
75 resp.setDateHeader("Expires", 0);
76 resp.setHeader("Pragma", "no-cache");
77 resp.setHeader("Cache-Control", "no-cache");
78 resp.setContentType("text/html");
79
80 resp.getWriter().print(response);
81 }
82
83 @Override
84 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
85 doGet(req, resp);
86 }
87
88 }

Properties

Name Value
svn:mergeinfo

  ViewVC Help
Powered by ViewVC 1.1.20