KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > protomatter > syslog > xml > SyslogInitServlet


1 package com.protomatter.syslog.xml;
2
3 /**
4  * {{{ The Protomatter Software License, Version 1.0
5  * derived from The Apache Software License, Version 1.1
6  *
7  * Copyright (c) 1998-2002 Nate Sammons. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed for the
24  * Protomatter Software Project
25  * (http://protomatter.sourceforge.net/)."
26  * Alternately, this acknowledgment may appear in the software itself,
27  * if and wherever such third-party acknowledgments normally appear.
28  *
29  * 4. The names "Protomatter" and "Protomatter Software Project" must
30  * not be used to endorse or promote products derived from this
31  * software without prior written permission. For written
32  * permission, please contact support@protomatter.com.
33  *
34  * 5. Products derived from this software may not be called "Protomatter",
35  * nor may "Protomatter" appear in their name, without prior written
36  * permission of the Protomatter Software Project
37  * (support@protomatter.com).
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  * DISCLAIMED. IN NO EVENT SHALL THE PROTOMATTER SOFTWARE PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE. }}}
51  */

52
53 import java.io.*;
54 import java.net.*;
55 import java.util.*;
56 import java.text.MessageFormat JavaDoc;
57 import com.protomatter.syslog.*;
58 import javax.servlet.*;
59 import javax.servlet.http.*;
60
61 /**
62  * A servlet that configures Syslog when its <TT>init()</TT>
63  * method is called. It should be configured in a WebApp
64  * by adding the following declaration to the <TT>web.xml</TT>
65  * file for the WebApp:<P>
66  *
67  * <PRE>
68  * <B>&lt;servlet&gt;</B>
69  * <B>&lt;servlet-name&gt;</B>SyslogInitServlet<B>&lt;/servlet-name&gt;</B>
70  * <B>&lt;description&gt;</B>Syslog Initialization Servlet<B>&lt;/description&gt;</B>
71  * <B>&lt;servlet-class&gt;</B>com.protomatter.syslog.xml.SyslogInitServlet<B>&lt;/servlet-class&gt;</B>
72  * <B>&lt;init-param&gt;</B>
73  * <B>&lt;param-name&gt;</B>config.xml<B>&lt;/param-name&gt;</B>
74  * <B>&lt;param-value&gt;</B>/path/to/syslog.xml<B>&lt;/param-value&gt;</B>
75  * <B>&lt;/init-param&gt;</B>
76  * <B>&lt;init-param&gt;</B>
77  * <B>&lt;param-name&gt;</B>show.config.on.get<B>&lt;/param-name&gt;</B>
78  * <B>&lt;param-value&gt;</B>false<B>&lt;/param-value&gt;</B>
79  * <B>&lt;/init-param&gt;</B>
80  * <B>&lt;init-param&gt;</B>
81  * <B>&lt;param-name&gt;</B>load.config.as.resource<B>&lt;/param-name&gt;</B>
82  * <B>&lt;param-value&gt;</B>false<B>&lt;/param-value&gt;</B>
83  * <B>&lt;/init-param&gt;</B>
84  * <B>&lt;init-param&gt;</B>
85  * <B>&lt;param-name&gt;</B>Syslog.xml.parser<B>&lt;/param-name&gt;</B>
86  * <B>&lt;param-value&gt;</B>org.apache.xerces.parsers.SAXParser<B>&lt;/param-value&gt;</B>
87  * <B>&lt;/init-param&gt;</B>
88  * <B>&lt;init-param&gt;</B>
89  * <B>&lt;param-name&gt;</B>Syslog.classloader.warning<B>&lt;/param-name&gt;</B>
90  * <B>&lt;param-value&gt;</B>true<B>&lt;/param-value&gt;</B>
91  * <B>&lt;/init-param&gt;</B>
92  * <B>&lt;load-on-startup&gt;</B>1<B>&lt;/load-on-startup&gt;</B>
93  * <B>&lt;/servlet&gt;</B>
94  * <B>&lt;servlet-mapping&gt;</B>
95  * <B>&lt;servlet-name&gt;</B>SyslogInitServlet<B>&lt;/servlet-name&gt;</B>
96  * <B>&lt;url-pattern&gt;</B>/SyslogInitServlet<B>&lt;/url-pattern&gt;</B>
97  * <B>&lt;/servlet-mapping&gt;</B>
98  * </PRE><P>
99  *
100  * The <TT>config.xml</TT> init param is requred and should point
101  * to the XML configuration file for Syslog.<P>
102  *
103  * If the optional <TT>show.config.on.get</TT> init param is set
104  * to <TT>true</TT> (the default is <TT>false</TT>), then when
105  * the servlet is hit with a <TT>GET</TT> request, it will return the
106  * current Syslog configuration. Care should be taken when using this
107  * option since the syslog configuration file may contain database
108  * passwords, etc. It is a good idea to use declarative security
109  * to protect this servlet if you are allowing it to display the
110  * configuration file.<P>
111  *
112  * If the optional <TT>load.config.as.resource</TT> init param is set
113  * to <TT>true</TT> (the default is <TT>false</TT>), then when the
114  * servlet initializes, it will try and find the configuration file
115  * as a resource using the classloader that loaded the servlet. This
116  * is a useful option if you are using this servlet to allow you
117  * to use a different Syslog instance for each EAR in a J2EE server.
118  * For instance, if you set the value of the <TT>config.xml</TT>
119  * parameter to "<TT>syslog-config.xml</TT>", and then set
120  * <TT>load.config.as.resource</TT> to "<TT>true</TT>", then you
121  * can put a file called "<TT>syslog-config.xml</TT>" in the base
122  * of the EAR directory, and the servlet will find it. This is
123  * a nice option to use if you don't like to have hard-coded
124  * paths in the <TT>web.xml</TT> file.<P>
125  *
126  * The optional <TT>Syslog.xml.parser</TT> init param can be used
127  * to set the class name of the XML parser used to read the XML
128  * configuration file. It is possible that a confused JAXP
129  * registry can cause problems. You should set this property
130  * to "<TT>org.apache.xerces.parsers.SAXParser</TT>" or something
131  * similar. Note that this has the exact same effect as setting
132  * the same property as a system property (using the "<TT>-D</TT>"
133  * option to the JVM). It is preferable to use that property
134  * in the <TT>web.xml</TT> file instead of the system property
135  * because you can then have the ability to use different
136  * XML parsers for different EAR files.<P>
137  *
138  * The optional <TT>Syslog.classloader.warning</TT> init param can be used
139  * to turn off the classloader warning that is issued if Syslog is
140  * loaded with a ClassLoader other than the system one. The default
141  * value for this property is <TT>true</TT>, meaning that the warning
142  * <i>should</i> be displayed. Note that this has the exact same effect as setting
143  * the same property as a system property (using the "<TT>-D</TT>"
144  * option to the JVM). Some people prefer to use that property
145  * in the <TT>web.xml</TT> file instead of the system property
146  * because they don't like to have to set system properties.
147  * Please read <a HREF="../classloader-warning.html">this document</a>
148  * regarding classloader issues with Syslog.<P>
149  *
150  * The <TT>&lt;servlet-mapping&gt;</TT> tag above is optional. If
151  * it is not specified, the servlet will still initialize syslog when
152  * the WebApp comes up, but the servlet will not be visible to
153  * web browsers. This is generally a good thing.<P>
154  */

155
156 public class SyslogInitServlet
157             extends HttpServlet
158 {
159     private boolean showConfigurationOnGet = false;
160     private boolean loadConfigAsResource = false;
161
162     // By default, we show the classloader warning
163
private static final boolean WARNING_DEFAULT = true;
164
165     private static final String JavaDoc WARNING_OFF = "off";
166
167     private static final String JavaDoc TRUE = "true";
168
169     private static final String JavaDoc CONFIG_FILE_PARAM = "config.xml";
170
171     private static final String JavaDoc SHOW_CONFIG_ON_GET_PARAM = "show.config.on.get";
172
173     private static final String JavaDoc LOAD_CONFIG_AS_RESOURCE_PARAM = "load.config.as.resource";
174
175     /**
176      * Default constructor.
177      */

178     public SyslogInitServlet()
179     {
180         super();
181     }
182
183     /**
184      * Initialize syslog.
185      */

186     public void init(ServletConfig config)
187     throws ServletException
188     {
189         super.init(config);
190
191         try
192         {
193             if (TRUE.equalsIgnoreCase(config.getInitParameter(SHOW_CONFIG_ON_GET_PARAM)))
194                 this.showConfigurationOnGet = true;
195
196             if (TRUE.equalsIgnoreCase(config.getInitParameter(LOAD_CONFIG_AS_RESOURCE_PARAM)))
197                 this.loadConfigAsResource = true;
198
199             boolean showClassloaderWarning = WARNING_DEFAULT;
200
201             if (WARNING_OFF.equalsIgnoreCase(config.getInitParameter(Syslog.WARNING_PROPERTY)))
202                 showClassloaderWarning = false;
203
204             String JavaDoc filename = config.getInitParameter(CONFIG_FILE_PARAM);
205
206             if (filename == null)
207                 throw new IllegalArgumentException JavaDoc(MessageFormat.format(
208                                                        Syslog.getResourceString(MessageConstants.MUST_SPECIFY_INIT_PARAM_MESSAGE),
209                                                        new Object JavaDoc[] { CONFIG_FILE_PARAM } ));
210
211             String JavaDoc xmlParserClass = System.getProperty(SyslogXML.XML_PARSER_PROPERTY);
212
213             if (isValid(config.getInitParameter(SyslogXML.XML_PARSER_PROPERTY)))
214             {
215                 xmlParserClass = config.getInitParameter(SyslogXML.XML_PARSER_PROPERTY).trim();
216             }
217
218             if (loadConfigAsResource)
219             {
220                 // try and find the file as a resource using the
221
// ClassLoader that we were loaded with.
222
ClassLoader JavaDoc loader = this.getClass().getClassLoader();
223                 InputStream input = loader.getResourceAsStream(filename);
224
225                 if (input != null)
226                 {
227                     SyslogXML.configure(input, xmlParserClass, showClassloaderWarning);
228                 }
229                 else
230                 {
231                     throw new SyslogInitException(MessageFormat.format(
232                                                       Syslog.getResourceString(MessageConstants.RESOURCE_NOT_FOUND),
233                                                       new Object JavaDoc[] { filename }));
234                 }
235
236             }
237             else
238             {
239                 File file = new File(filename);
240                 SyslogXML.configure(file, xmlParserClass, showClassloaderWarning);
241             }
242
243         }
244         catch (SyslogInitException x)
245         {
246             throw new ServletException(Syslog.getResourceString(MessageConstants.CANNOT_CONFIGURE_MESSAGE), x);
247         }
248     }
249
250     /**
251      * Showdown syslog.
252      */

253     public void destroy()
254     {
255         Syslog.shutdown();
256     }
257
258     private static boolean isValid(String JavaDoc s)
259     {
260         if ((s == null) || (s.trim().length() == 0))
261             return false;
262
263         return true;
264     }
265
266     public void doGet(HttpServletRequest req, HttpServletResponse resp)
267     throws ServletException, IOException
268     {
269         if (this.showConfigurationOnGet)
270         {
271             resp.setContentType("text/plain");
272             OutputStream out = resp.getOutputStream();
273             SyslogXML.writeConfiguration(out);
274         }
275         else
276         {
277             resp.setContentType("text/html");
278             PrintWriter out = resp.getWriter();
279             out.println(Syslog.getResourceString(MessageConstants.SERVLET_SNIPPY_RESPONSE));
280         }
281     }
282 }
283
Popular Tags