KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > appserver > debugger > business > DebuggerValve


1 package com.lutris.appserver.debugger.business;
2
3 import java.util.Vector JavaDoc;
4 import java.util.Enumeration JavaDoc;
5 import java.util.Hashtable JavaDoc;
6 import javax.servlet.http.*;
7 import java.io.IOException JavaDoc;
8 import javax.servlet.*;
9 import javax.servlet.http.HttpServletResponse JavaDoc;
10 import org.apache.catalina.*;
11 import org.apache.catalina.core.*;
12 import org.apache.catalina.valves.ValveBase;
13 import javax.servlet.http.HttpServletRequest JavaDoc;
14 import org.apache.catalina.connector.*;
15 import javax.servlet.http.Cookie JavaDoc;
16 import com.lutris.util.*;
17 import com.lutris.appserver.server.session.Session;
18 import com.lutris.appserver.server.sessionEnhydra.StandardSession;
19 import com.lutris.appserver.server.Application;
20 import com.lutris.appserver.server.session.SessionData;
21 import com.lutris.appserver.server.httpPresentation.servlet.*;
22 import com.lutris.appserver.server.sessionContainerAdapter.ContainerAdapterSessionManager;
23 import com.lutris.appserver.server.sessionContainerAdapter.JmxContainerAdapterSessionManager;
24 import com.lutris.appserver.server.sessionEnhydra.SimpleServletSessionManager;
25 import com.lutris.appserver.server.sessionEnhydra.StandardSessionManager;
26
27 public class DebuggerValve extends ValveBase {
28
29    
30    private static Object JavaDoc sinc = new Object JavaDoc();
31    private String JavaDoc servletName;
32    private StandardWrapper wrapper;
33    public boolean toProcess=false;
34
35
36
37
38 public DebuggerValve(String JavaDoc servletName,StandardContext servlet)
39     {
40       super();
41     this.servletName=servletName;
42     Container[] wrappers=servlet.findChildren();
43         this.wrapper=(StandardWrapper)wrappers[0];
44     }
45
46
47
48 public String JavaDoc getInfo(){
49   return "com.lutris.appserver.debugger.business.DebuggerValve";
50 }
51
52
53 public void invoke(Request JavaDoc request, Response JavaDoc response)
54         throws IOException JavaDoc, ServletException
55  {
56
57
58   
59   Hashtable JavaDoc record=new Hashtable JavaDoc();
60  
61   
62        
63  if(toProcess)
64    manageRequest(record,request);
65  
66
67 getNext().invoke(request, response);
68   
69   if(toProcess){
70    manageResponse(record,response,request);
71  
72     Globals.addTransaction(record);
73   
74     ServletMonitorImpl.newRequest();
75  }
76  }
77
78
79
80
81
82 public StringBuffer JavaDoc getAttribuestAsStringBuffer(HttpSession httpsession)
83     {
84         StringBuffer JavaDoc stringbuffer = new StringBuffer JavaDoc();
85         Enumeration JavaDoc enumeration = httpsession.getAttributeNames();
86         if(httpsession != null)
87         {
88             stringbuffer.append("<UL>\n");
89             for(; enumeration.hasMoreElements(); stringbuffer.append("\n"))
90             {
91                 String JavaDoc s = (String JavaDoc)enumeration.nextElement();
92                 Object JavaDoc obj = httpsession.getAttribute(s);
93                 stringbuffer.append("<LI> <TT>");
94                 stringbuffer.append(s);
95                 stringbuffer.append(": </TT>");
96                 stringbuffer.append(obj.toString());
97             }
98
99             stringbuffer.append("</UL>\n");
100         }
101         return stringbuffer;
102     }
103
104
105 private static void wait(int wait)
106  {
107         
108          long now = System.currentTimeMillis();
109          long giveUpTime = now + (wait *10);
110         
111           long leftToGo = giveUpTime - now;
112             
113             synchronized (sinc) {
114                  try {
115                      sinc.wait(leftToGo);
116                  } catch (InterruptedException JavaDoc e) {
117                  
118              }
119             }
120   }
121     
122
123
124 private String JavaDoc getSessionData(HttpServletRequest JavaDoc request){
125     
126         Session session;
127         HttpSession httpsession;
128         session = null;
129         httpsession = request.getSession(false);
130         javax.servlet.Servlet JavaDoc servlet;
131         
132        try
133         {
134           servlet = wrapper.allocate();
135         }catch(ServletException e)
136         {
137             return "<I>No Session</I>";
138         }
139      
140      
141      
142       if(servlet instanceof HttpPresentationServlet)
143        {
144         //enhydra servlets
145
Application app = ((HttpPresentationServlet)servlet).getApplication();
146        
147           com.lutris.appserver.server.session.SessionManager sm = app.getSessionManager();
148        
149          if((sm instanceof StandardSessionManager) || (sm instanceof SimpleServletSessionManager))
150           {
151               session = ((HttpPresentationServlet)servlet).getSession(request);
152             if(!(sm instanceof SimpleServletSessionManager) && session != null)
153                 httpsession = session.getHttpSession();
154           
155           
156           }
157         if((sm instanceof ContainerAdapterSessionManager) || (sm instanceof JmxContainerAdapterSessionManager))
158          {
159           if(httpsession != null)
160              session = (Session)httpsession.getAttribute("session");
161           else{
162             try
163               {
164                 wrapper.deallocate(servlet);
165               }
166                 catch(ServletException e)
167               {
168                 return "<I>No Session</I>";
169               }
170             return "<I>No Session</I>";
171             
172            }
173          }
174       }else
175       {
176        //non-enhydra servlets
177
String JavaDoc s;
178      if(httpsession != null)
179        {
180         s = "";
181         StringBuffer JavaDoc stringbuffer = getAttribuestAsStringBuffer(httpsession);
182         s = s + stringbuffer.toString();
183         if(s.length() == 0)
184             s = "<I>Empty</I>";
185             try
186               {
187                 wrapper.deallocate(servlet);
188               }
189              catch(ServletException e)
190               {
191              return s;
192                }
193      
194      
195         return s;
196        }else{
197         try
198         {
199             wrapper.deallocate(servlet);
200         }
201         catch(ServletException e)
202         {
203             return "<I>No Session</I>";
204         }
205          return "<I>No Session</I>";
206       }
207      }
208     
209       
210       try
211         {
212             wrapper.deallocate(servlet);
213         }
214         catch(ServletException e)
215         {
216             return "<I>No Session</I>";
217         }
218     
219     
220 //enhydra
221
if(httpsession==null)
222    return "<I>No Session</I>";
223 else
224   {
225   if(session == null)
226     {
227         String JavaDoc s="";
228         StringBuffer JavaDoc stringbuffer = getAttribuestAsStringBuffer(httpsession);
229         s = s + stringbuffer.toString();
230         if(s.length() == 0)
231             s = "<I>Empty</I>";
232         return s;
233      
234     }else{
235         String JavaDoc s="";
236         SessionData sessiondata = session.getSessionData();
237         s = sessiondata == null ? "" : sessiondata.toHtml();
238         if(s == null)
239             s = "";
240         StringBuffer JavaDoc stringbuffer = getAttribuestAsStringBuffer(httpsession);
241         s = s + stringbuffer.toString();
242         if(s.length() == 0)
243             s = "<I>Empty</I>";
244         return s;
245      }
246    }
247  
248   
249 }
250
251
252 private String JavaDoc check(String JavaDoc data){
253   if(data==null || data=="")
254     return "N/A";
255   else
256     return new String JavaDoc(data);
257 }
258
259
260 private void manageRequest(Hashtable JavaDoc record,Request JavaDoc req){
261 String JavaDoc sessionDataRequest=getSessionData(req.getRequest());
262 record.put("S",sessionDataRequest);
263 record.put("ServletName",servletName);
264 record.put("RemoteHost",check(req.getRemoteHost()));
265 record.put("RemoteAdr",check(req.getRemoteAddr()));
266 record.put("Method",check(req.getMethod()));
267 record.put("ServerName",check(req.getServerName()));
268 record.put("ServerPort",new Integer JavaDoc(req.getServerPort()));
269 record.put("RequestURI",check(req.getRequestURI()));
270 record.put("Scheme",check(req.getScheme()));
271 record.put("QueryString",check(req.getQueryString()));
272 record.put("RemoteUser",check(req.getRemoteUser()));
273 record.put("AuthType",check(req.getAuthType()));
274 record.put("ContentLength",new Integer JavaDoc(req.getContentLength()));
275 record.put("ContentType",check(req.getContentType()));
276 record.put("PathInfo",check(req.getPathInfo()));
277 record.put("PathTranslated",check(req.getPathTranslated()));
278 record.put("Protocol",check(req.getProtocol()));
279     
280
281 Enumeration JavaDoc e = req.getHeaderNames();
282 Vector JavaDoc names=new Vector JavaDoc();
283
284 while (e.hasMoreElements())
285   {
286    String JavaDoc name= (String JavaDoc)e.nextElement();
287    String JavaDoc header = req.getHeader(name);
288    names.add(name);
289    record.put(name,header);
290   }
291 record.put("HeaderNames",names);
292     
293
294 Cookie JavaDoc[] temp=req.getCookies();
295 if(temp!=null)
296  record.put("Cookies",temp);
297 else
298  record.put("Cookies",new Cookie JavaDoc[0]);
299     
300  
301 Enumeration JavaDoc en = req.getParameterNames();
302 Vector JavaDoc namess=new Vector JavaDoc();
303     
304 while (en.hasMoreElements())
305  {
306   String JavaDoc name= (String JavaDoc)en.nextElement();
307   String JavaDoc[] values = req.getParameterValues(name);
308   namess.add(name);
309   record.put(name,values);
310   }
311 record.put("ParameterNames",namess);
312     
313  
314  
315 StringBuffer JavaDoc uri = new StringBuffer JavaDoc();
316 uri.append(req.getRequestURI());
317     
318
319 if(req.getQueryString() != null)
320             {
321           uri.append('?');
322                 uri.append(req.getQueryString());
323             }
324                        
325 record.put("RequestURL",uri.toString());
326     
327
328 }
329
330
331 private void manageResponse(Hashtable JavaDoc record,Response JavaDoc response,Request JavaDoc hreq)
332  {
333     
334    String JavaDoc sessionDataResponse=getSessionData(hreq.getRequest());
335    record.put("S1",sessionDataResponse);
336
337    Cookie JavaDoc[] coc=response.getCookies();
338
339    if(coc==null)
340      record.put("CookiesResponse",new Cookie JavaDoc[0]);
341    else
342      record.put("CookiesResponse",coc);
343
344     
345    Integer JavaDoc status=new Integer JavaDoc(response.getStatus());
346    record.put("StatusCode",status);
347
348   
349
350  record.put("Message",check(response.getMessage()));
351
352    Integer JavaDoc totalBytes=new Integer JavaDoc(response.getContentCount());
353    
354    if(totalBytes==null ||totalBytes.intValue()==0)
355      record.put("TotalBytesResponse",new Integer JavaDoc(-1));
356    else
357      record.put("TotalBytesResponse",totalBytes);
358
359    record.put("ContentLengthResponse",new Integer JavaDoc(response.getContentLength()));
360    record.put("ContentTypeResponse",check(response.getContentType()));
361    record.put("StatusResponse",new Integer JavaDoc(response.getStatus()));
362    
363    String JavaDoc[] hnames=(response.getHeaderNames());
364
365    for(int u=0;u<hnames.length;u++)
366     {
367       String JavaDoc header=response.getHeader(hnames[u]);
368       record.put("Response"+hnames[u],header);
369     }
370
371    record.put("HeaderNamesResponse",hnames);
372  }
373
374 }
375
376
377
378
379
380
Popular Tags