KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > web > loadbalancer > LoadbalancerServlet


1 /*
2  * JBoss, the OpenSource WebOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.web.loadbalancer;
8
9 import java.io.IOException JavaDoc;
10 import javax.management.MalformedObjectNameException JavaDoc;
11 import javax.servlet.ServletConfig JavaDoc;
12 import javax.servlet.ServletException JavaDoc;
13 import javax.servlet.http.HttpServlet JavaDoc;
14 import javax.servlet.http.HttpServletRequest JavaDoc;
15 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /**
25  * A servlet that does the job of a reverse proxy.
26  *
27  * @author Thomas Peuss <jboss@peuss.de>
28  * @version $Revision: 1.7 $
29  */

30 public class LoadbalancerServlet
31     extends HttpServlet JavaDoc
32 {
33   protected static Logger log = Logger.getLogger(LoadbalancerServlet.class);
34
35   protected String JavaDoc loadbalancerName;
36   protected LoadbalancerServiceMBean loadbalancer;
37
38   /**
39    * Handle the request for all request methods.
40    * @param request
41    * @param response
42    * @param requestMethod
43    * @throws ServletException
44    * @throws IOException
45    */

46   protected void handleRequest(
47       HttpServletRequest JavaDoc request,
48       HttpServletResponse JavaDoc response,
49       int requestMethod) throws ServletException JavaDoc, IOException JavaDoc
50   {
51     HttpMethod method = null;
52     Request JavaDoc schedRequest=new Request JavaDoc(request, response, requestMethod);
53
54     initLoadbalancerDelegate();
55
56     // get target host from scheduler
57
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     // do handle the request
70
loadbalancer.handleRequest(schedRequest);
71   }
72
73   /* Handler for GET-requests
74    * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
75    */

76   protected void doGet(
77       HttpServletRequest JavaDoc request,
78       HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc
79   {
80     handleRequest(request, response,Constants.HTTP_METHOD_GET);
81   }
82
83   /* Handler for POST-requests
84    * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
85    */

86   protected void doPost(
87       HttpServletRequest JavaDoc request,
88       HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc
89   {
90     handleRequest(request, response, Constants.HTTP_METHOD_POST);
91   }
92
93   /* Handler for DELETE-requests
94    * @see javax.servlet.http.HttpServlet#doDelete(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
95    */

96   protected void doDelete(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws
97       ServletException JavaDoc, IOException JavaDoc
98   {
99     handleRequest(request, response, Constants.HTTP_METHOD_DELETE);
100   }
101
102   /* Handler for HEAD-requests
103    * @see javax.servlet.http.HttpServlet#doHead(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
104    */

105   protected void doHead(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws
106       ServletException JavaDoc, IOException JavaDoc
107   {
108     handleRequest(request, response, Constants.HTTP_METHOD_HEAD);
109   }
110
111   /* Handler for OPTIONS-requests
112    * @see javax.servlet.http.HttpServlet#doOptions(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
113    */

114   protected void doOptions(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws
115       ServletException JavaDoc, IOException JavaDoc
116   {
117     handleRequest(request, response, Constants.HTTP_METHOD_OPTIONS);
118   }
119
120   /* Handler for PUT-requests
121    * @see javax.servlet.http.HttpServlet#doPut(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
122    */

123   protected void doPut(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws
124       ServletException JavaDoc, IOException JavaDoc
125   {
126     handleRequest(request, response, Constants.HTTP_METHOD_PUT);
127   }
128
129   /* Servlet initialisation.
130    * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
131    */

132   public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc
133   {
134     super.init(config);
135     loadbalancerName=config.getInitParameter("loadbalancer-service-name");
136
137     log.debug("Servlet init ready");
138   }
139
140   /* Servlet deinit.
141    * @see javax.servlet.Servlet#destroy()
142    */

143   public void destroy()
144   {
145     super.destroy();
146     loadbalancer=null;
147     log.debug("Servlet destroyed");
148   }
149
150   /**
151    * Create a proxy to the loadbalancer
152    */

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 JavaDoc ex)
164       {
165         log.error("Could not create LoadbalancerService-Proxy",ex);
166       }
167
168     }
169   }
170 }
Popular Tags