package dk.thoerup.circuitbreaker.web; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * This is a web interface for viewing and controlling the CircuitBreakers currently registered in CircuitBreakerManager *

* To use this servlet in you .war module just create a simple servlet that extends this class (instead of HttpServlet) * The new custom servlet doesn't need to implement any of the standard servlet methods - its all done in this implementation. * *

* Example: *

 * public class CircuitBreakerServlet extends CircuitBreakerServletBase {
 *   private static final long serialVersionUID = 1L;
 * }
 * 
* * If you add a servlet init pameter to your deployment descriptor with the name 'readonly' and a integer value > 0, you can disable the * control buttons: *

* <init-param><param-name>readonly</param-name><param-value>1</param-value></init-param> * * * You could also configure it dynamically from a ContextListener: * ServletRegistration.Dynamic dynconf = sce.getServletContext().addServlet("circuitbreaker", dk.thoerup.circuitbreaker.web.CircuitBreakerServletBase.class ); * dynconf.addMapping("/CircuitBreakerServlet"); * dynconf.setInitParameter("readonly", "1"); * */ public class CircuitBreakerServletBase extends javax.servlet.http.HttpServlet { private static final long serialVersionUID = 1L; private boolean readOnly = false; @Override public void init() throws ServletException { super.init(); String readOnlyStr = getInitParameter("readonly"); if (readOnlyStr != null && readOnlyStr.trim().length() >0) { int readOnlyInt = Integer.parseInt(readOnlyStr.trim()); readOnly = (readOnlyInt > 0); } } private Command getCommand(String command) throws ServletException { if (command == null || command.equals("list")) return new ListCircuitBreakers(); if (command.equals("view")) return new ViewCircuitBreaker(readOnly); if (command.equals("action")) return new ActionCommand(readOnly); if (command.equals("log")) return new LogViewCommand(); throw new ServletException("No such command:" + command); } @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Command cmd = getCommand( req.getParameter("command")); String response = cmd.execute(req,resp); resp.setDateHeader("Expires", 0); resp.setHeader("Pragma", "no-cache"); resp.setHeader("Cache-Control", "no-cache"); resp.setContentType("text/html"); resp.getWriter().print(response); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }