1 7 package org.jboss.web.loadbalancer; 8 9 import java.io.IOException ; 10 import javax.management.MalformedObjectNameException ; 11 import javax.servlet.ServletConfig ; 12 import javax.servlet.ServletException ; 13 import javax.servlet.http.HttpServlet ; 14 import javax.servlet.http.HttpServletRequest ; 15 import javax.servlet.http.HttpServletResponse ; 16 17 import org.apache.commons.httpclient.HttpMethod; 18 import org.jboss.logging.Logger; 19 import org.jboss.mx.util.MBeanProxyExt; 20 import org.jboss.web.loadbalancer.scheduler.NoHostAvailableException; 21 import org.jboss.web.loadbalancer.util.Constants; 22 import org.jboss.web.loadbalancer.util.Request; 23 24 30 public class LoadbalancerServlet 31 extends HttpServlet 32 { 33 protected static Logger log = Logger.getLogger(LoadbalancerServlet.class); 34 35 protected String loadbalancerName; 36 protected LoadbalancerServiceMBean loadbalancer; 37 38 46 protected void handleRequest( 47 HttpServletRequest request, 48 HttpServletResponse response, 49 int requestMethod) throws ServletException , IOException 50 { 51 HttpMethod method = null; 52 Request schedRequest=new Request (request, response, requestMethod); 53 54 initLoadbalancerDelegate(); 55 56 try 58 { 59 loadbalancer.createMethod(schedRequest); 60 } 61 catch (NoHostAvailableException nhae) 62 { 63 log.error("We have no host to schedule request - giving up"); 64 response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, nhae.getMessage()); 65 return; 66 } 67 loadbalancer.addRequestData(schedRequest); 68 69 loadbalancer.handleRequest(schedRequest); 71 } 72 73 76 protected void doGet( 77 HttpServletRequest request, 78 HttpServletResponse response) throws ServletException , IOException 79 { 80 handleRequest(request, response,Constants.HTTP_METHOD_GET); 81 } 82 83 86 protected void doPost( 87 HttpServletRequest request, 88 HttpServletResponse response) throws ServletException , IOException 89 { 90 handleRequest(request, response, Constants.HTTP_METHOD_POST); 91 } 92 93 96 protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws 97 ServletException , IOException 98 { 99 handleRequest(request, response, Constants.HTTP_METHOD_DELETE); 100 } 101 102 105 protected void doHead(HttpServletRequest request, HttpServletResponse response) throws 106 ServletException , IOException 107 { 108 handleRequest(request, response, Constants.HTTP_METHOD_HEAD); 109 } 110 111 114 protected void doOptions(HttpServletRequest request, HttpServletResponse response) throws 115 ServletException , IOException 116 { 117 handleRequest(request, response, Constants.HTTP_METHOD_OPTIONS); 118 } 119 120 123 protected void doPut(HttpServletRequest request, HttpServletResponse response) throws 124 ServletException , IOException 125 { 126 handleRequest(request, response, Constants.HTTP_METHOD_PUT); 127 } 128 129 132 public void init(ServletConfig config) throws ServletException 133 { 134 super.init(config); 135 loadbalancerName=config.getInitParameter("loadbalancer-service-name"); 136 137 log.debug("Servlet init ready"); 138 } 139 140 143 public void destroy() 144 { 145 super.destroy(); 146 loadbalancer=null; 147 log.debug("Servlet destroyed"); 148 } 149 150 153 protected void initLoadbalancerDelegate() 154 { 155 if (loadbalancer == null) 156 { 157 try 158 { 159 loadbalancer = (LoadbalancerServiceMBean) 160 MBeanProxyExt.create(LoadbalancerServiceMBean.class, 161 loadbalancerName); 162 } 163 catch (MalformedObjectNameException ex) 164 { 165 log.error("Could not create LoadbalancerService-Proxy",ex); 166 } 167 168 } 169 } 170 } | Popular Tags |