KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > psibt > framework > net > Log4jRequestHandler


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.psibt.framework.net;
17
18 import java.io.*;
19 import java.net.*;
20 import java.util.*;
21 import org.apache.log4j.*;
22
23 /**
24  * This class implements a RequestHandler for log4j configuration. It serves the "/log4j/" path
25  * in the PluggableHTTPServer. If this path is requested a list of all current log4j categories
26  * with their current priorities is created. All priority settings can be changed by the user
27  * and can be submitted and taken over.
28  *
29  * @author <a HREF="mailto:V.Mentzner@psi-bt.de">Volker Mentzner</a>
30  */

31 public class Log4jRequestHandler extends RootRequestHandler {
32
33   private Priority[] prios = Priority.getAllPossiblePriorities();
34
35  /**
36    * Creates a new Log4jRequestHandler object
37    */

38   public Log4jRequestHandler() {
39     this.setTitle("log4j");
40     this.setDescription("log4j configuration");
41     this.setHandledPath("/log4j/");
42   }
43
44  /**
45    * Handles the given request and writes the reply to the given out-stream.
46    *
47    * @param request - client browser request
48    * @param out - Out stream for sending data to client browser
49    * @return if the request was handled by this handler : true, else : false
50    */

51   public boolean handleRequest(String JavaDoc request, Writer out) {
52     String JavaDoc path = "";
53     String JavaDoc query = null;
54     String JavaDoc name;
55     try {
56       // check request url
57
URL url = new URL("http://localhost"+request);
58       path = url.getPath();
59       query = url.getQuery();
60       if (path.startsWith(this.getHandledPath()) == false) {
61         return false;
62       }
63
64       out.write("HTTP/1.0 200 OK\r\n");
65       out.write("Content-type: text/html\r\n\r\n");
66       out.write("<HTML><HEAD><TITLE>" + this.getTitle() + "</TITLE></HEAD>\r\n");
67       out.write("<BODY><H1>log4j</H1>\r\n");
68       out.write(this.getDescription() + "<br><br>\r\n");
69
70       // handle a request with query
71
if ((query != null) && (query.length() >= 0)) {
72         StringTokenizer st = new StringTokenizer(query, "&");
73         String JavaDoc cmd;
74         String JavaDoc catname;
75         String JavaDoc catval;
76         int idx;
77         while (st.hasMoreTokens()) {
78           cmd = st.nextToken();
79           idx = cmd.indexOf("=");
80           catname = cmd.substring(0, idx);
81           catval = cmd.substring(idx+1, cmd.length());
82           if (catname.equalsIgnoreCase("root"))
83             Category.getRoot().setPriority(Priority.toPriority(catval));
84           else
85             Category.getInstance(catname).setPriority(Priority.toPriority(catval));
86         }
87       }
88
89       // output category information in a form with a simple table
90
out.write("<form name=\"Formular\" ACTION=\""+this.getHandledPath()+"\" METHOD=\"PUT\">");
91       out.write("<table cellpadding=4>\r\n");
92       out.write(" <tr>\r\n");
93       out.write(" <td><b>Category</b></td>\r\n");
94       out.write(" <td><b>Priority</b></td>\r\n");
95       out.write(" <td><b>Appender</b></td>\r\n");
96       out.write(" </tr>\r\n");
97
98       // output for root category
99
Category cat = Category.getRoot();
100       out.write(" <tr><td>root</td>\r\n");
101       out.write(" <td>\r\n");
102       out.write(" <select size=1 name=\""+ cat.getName() +"\">");
103       for (int i = 0; i < prios.length; i++) {
104         if (cat.getChainedPriority().toString().equals(prios[i].toString()))
105           out.write("<option selected>"+prios[i].toString());
106         else
107           out.write("<option>"+prios[i].toString());
108       }
109       out.write("</select>\r\n");
110       out.write(" </td>\r\n");
111       out.write(" <td>\r\n");
112       for (Enumeration apds = cat.getAllAppenders(); apds.hasMoreElements();) {
113         Appender apd = (Appender)apds.nextElement();
114         name = apd.getName();
115         if (name == null)
116           name = "<i>(no name)</i>";
117         out.write(name);
118         if (apd instanceof AppenderSkeleton) {
119           try {
120             AppenderSkeleton apskel = (AppenderSkeleton)apd;
121             out.write(" [" + apskel.getThreshold().toString() + "]");
122           } catch (Exception JavaDoc ex) {
123           }
124         }
125         if (apds.hasMoreElements())
126           out.write(", ");
127       }
128       out.write(" </td>\r\n");
129       out.write(" </tr>\r\n");
130
131       // output for all other categories
132
for (Enumeration en = Category.getCurrentCategories(); en.hasMoreElements();) {
133         cat = (Category)en.nextElement();
134         out.write(" <tr>\r\n");
135         out.write(" <td>" + cat.getName() + "</td>\r\n");
136         out.write(" <td>\r\n");
137         out.write(" <select size=1 name=\""+ cat.getName() +"\">");
138         for (int i = 0; i < prios.length; i++) {
139           if (cat.getChainedPriority().toString().equals(prios[i].toString()))
140             out.write("<option selected>"+prios[i].toString());
141           else
142             out.write("<option>"+prios[i].toString());
143         }
144         out.write("</select>\r\n");
145         out.write(" </td>\r\n");
146         out.write(" <td>\r\n");
147         for (Enumeration apds = cat.getAllAppenders(); apds.hasMoreElements();) {
148           Appender apd = (Appender)apds.nextElement();
149           name = apd.getName();
150           if (name == null)
151             name = "<i>(no name)</i>";
152           out.write(name);
153           if (apd instanceof AppenderSkeleton) {
154             try {
155               AppenderSkeleton apskel = (AppenderSkeleton)apd;
156               out.write(" [" + apskel.getThreshold().toString() + "]");
157             } catch (Exception JavaDoc ex) {
158             }
159           }
160           if (apds.hasMoreElements())
161             out.write(", ");
162         }
163         out.write(" </td>\r\n");
164         out.write(" </tr>\r\n");
165       }
166       out.write("</table>\r\n");
167       out.write("<input type=submit value=\"Submit\">");
168       out.write("</form>");
169       out.write("</BODY></HTML>\r\n");
170       out.flush();
171       return true;
172     } catch (Exception JavaDoc ex) {
173       return false;
174     }
175   }
176 }
Popular Tags