KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > appserver > debugger > EnhydraDebuggerValve


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