KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > servlet > Debug


1 // ===========================================================================
2
// Copyright (c) 1996 Mort Bay Consulting Pty. Ltd. All rights reserved.
3
// $Id: Debug.java,v 1.10 2005/08/13 00:01:28 gregwilkins Exp $
4
// ---------------------------------------------------------------------------
5

6 package org.mortbay.servlet;
7 import java.io.IOException JavaDoc;
8 import java.io.Writer JavaDoc;
9
10 import javax.servlet.ServletException JavaDoc;
11 import javax.servlet.http.HttpServlet JavaDoc;
12 import javax.servlet.http.HttpServletRequest JavaDoc;
13 import javax.servlet.http.HttpServletResponse JavaDoc;
14
15 import org.apache.commons.logging.Log;
16 import org.mortbay.log.LogFactory;
17 import org.mortbay.html.Block;
18 import org.mortbay.html.Break;
19 import org.mortbay.html.Font;
20 import org.mortbay.html.Page;
21 import org.mortbay.html.TableForm;
22 import org.mortbay.log.LogImpl;
23 import org.mortbay.log.LogSink;
24 import org.mortbay.log.OutputStreamLogSink;
25
26
27 /* ------------------------------------------------------------ */
28 // Don't write servlets like this one :-)
29
public class Debug extends HttpServlet JavaDoc
30 {
31     /* ------------------------------------------------------------ */
32     public void doGet(HttpServletRequest JavaDoc request,
33                       HttpServletResponse JavaDoc response)
34         throws ServletException JavaDoc, IOException JavaDoc
35     {
36         Page page= new Page();
37         page.title(getServletInfo());
38         page.attribute("text","#000000");
39         page.attribute(Page.BGCOLOR,"#FFFFFF");
40         page.attribute("link","#606CC0");
41         page.attribute("vlink","#606CC0");
42         page.attribute("alink","#606CC0");
43
44
45         Log l = LogFactory.getLog(Debug.class);
46         
47         if (!(l instanceof LogImpl))
48             return;
49         LogImpl log = (LogImpl) l;
50         
51         
52         TableForm tf = new TableForm(request.getRequestURI());
53         page.add(tf);
54         tf.table().newRow().addCell(new Block(Block.Bold)
55             .add(new Font(3,true).add(getServletInfo()))).cell().attribute("COLSPAN","2");
56         tf.table().add(Break.rule);
57         
58         tf.addCheckbox("D","Debug On",log.getDebug());
59         tf.addTextField("V","Verbosity Level",6,""+log.getVerbose());
60         tf.addTextField("P","Debug Patterns",40,log.getDebugPatterns());
61         tf.addCheckbox("W","Suppress Warnings",log.getSuppressWarnings());
62
63         
64         LogSink[] sinks = log.getLogSinks();
65         for (int s=0;sinks!=null && s<sinks.length;s++)
66         {
67             if (sinks[s]==null)
68                 continue;
69
70             tf.table().newRow().addCell(Break.rule).cell().attribute("COLSPAN","2");
71             tf.table().newRow().addCell("<B><font size=\"+1\">Log Sink "+s+":</font></B").right();
72             tf.table().addCell(sinks[s].getClass().getName()).left();
73
74             tf.addCheckbox("LSS"+s,"Started",sinks[s].isStarted());
75             
76             if (sinks[s] instanceof OutputStreamLogSink)
77             {
78                 OutputStreamLogSink sink=(OutputStreamLogSink)sinks[s];
79                 
80                 tf.addCheckbox("LT"+s,"Tag",sink.isLogTags());
81                 tf.addCheckbox("LL"+s,"Label",sink.isLogLabels());
82                 tf.addCheckbox("Ls"+s,"Stack Size",sink.isLogStackSize());
83                 tf.addCheckbox("LS"+s,"Stack Trace",sink.isLogStackTrace());
84                 tf.addCheckbox("SS"+s,"Suppress Stacks",sink.isSuppressStack());
85                 tf.addCheckbox("SL"+s,"Single Line",sink.isLogOneLine());
86                 tf.addTextField("LF"+s,"Log File Name",40,sink.getFilename());
87             }
88         }
89         
90         tf.table().newRow().addCell(Break.rule).cell().attribute("COLSPAN","2");
91         
92         tf.addTextField("LSC","Add LogSink Class",40,"org.mortbay.log.OutputStreamLogSink");
93         
94         tf.addButtonArea();
95         tf.addButton("Action","Set Options");
96         tf.addButton("Action","Add LogSink");
97         tf.addButton("Action","Delete Stopped Sinks");
98         tf.table().newRow().addCell(Break.rule).cell().attribute("COLSPAN","2");
99         
100         response.setContentType("text/html");
101         response.setHeader("Pragma", "no-cache");
102         response.setHeader("Cache-Control", "no-cache,no-store");
103         Writer JavaDoc writer=response.getWriter();
104         page.write(writer);
105         writer.flush();
106     }
107
108     /* ------------------------------------------------------------ */
109     public void doPost(HttpServletRequest JavaDoc request,
110                         HttpServletResponse JavaDoc response)
111         throws ServletException JavaDoc, IOException JavaDoc
112     {
113         String JavaDoc target=null;
114
115         Log l = LogFactory.getLog(Debug.class);
116         
117         if (!(l instanceof LogImpl))
118             return;
119         LogImpl log = (LogImpl) l;
120         String JavaDoc action=request.getParameter("Action");
121         
122         if ("Set Options".equals(action))
123         {
124             log.setDebug("on".equals(request.getParameter("D")));
125             log.setSuppressWarnings("on".equals(request.getParameter("W")));
126             String JavaDoc v=request.getParameter("V");
127             if (v!=null && v.length()>0)
128                 log.setVerbose(Integer.parseInt(v));
129             else
130                 log.setVerbose(0);
131             log.setDebugPatterns(request.getParameter("P"));
132
133
134             LogSink[] sinks = log.getLogSinks();
135             for (int s=0;sinks!=null && s<sinks.length;s++)
136             {
137                 if (sinks[s]==null)
138                     continue;
139                 
140                 if ("on".equals(request.getParameter("LSS"+s)))
141                 {
142                     if(!sinks[s].isStarted())
143                         try{sinks[s].start();}catch(Exception JavaDoc e){log.warn(e);}
144                 }
145                 else
146                 {
147                     if(sinks[s].isStarted())
148                         try{sinks[s].stop();}catch(InterruptedException JavaDoc e){}
149                 }
150
151                 String JavaDoc options=request.getParameter("LO"+s);
152                 if (options==null)
153                     options="";
154                 
155                 if (sinks[s] instanceof OutputStreamLogSink)
156                 {
157                     OutputStreamLogSink sink=(OutputStreamLogSink)sinks[s];
158                     
159                     sink.setLogTags("on".equals(request.getParameter("LT"+s)));
160                     sink.setLogLabels ("on".equals(request.getParameter("LL"+s)));
161                     sink.setLogStackSize("on".equals(request.getParameter("Ls"+s)));
162                     sink.setLogStackTrace("on".equals(request.getParameter("LS"+s)));
163                     sink.setSuppressStack("on".equals(request.getParameter("SS"+s)));
164                     sink.setLogOneLine("on".equals(request.getParameter("SL"+s)));
165
166                     sink.setFilename(request.getParameter("LF"+s));
167                 }
168                 
169             }
170         }
171         else if ("Add LogSink".equals(action))
172         {
173             System.err.println("add log sink "+request.getParameter("LSC"));
174             try
175             {
176                 log.add(request.getParameter("LSC"));
177             }
178             catch(Exception JavaDoc e)
179             {
180                 log.warn(e);
181             }
182         }
183         else if ("Delete Stopped Sinks".equals(action))
184         {
185             log.deleteStoppedLogSinks();
186         }
187         
188         response.sendRedirect(request.getContextPath()+
189                               request.getServletPath()+"/"+
190                               Long.toString(System.currentTimeMillis(),36)+
191                               (target!=null?("#"+target):""));
192     }
193     
194     /* ------------------------------------------------------------ */
195     public String JavaDoc getServletInfo()
196     {
197         return "Debug And Log Options";
198     }
199 }
200
Popular Tags