KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > logger > factory > ServletTargetFactory


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.logger.factory;
9
10 import javax.servlet.ServletContext JavaDoc;
11 import org.apache.avalon.framework.configuration.Configuration;
12 import org.apache.avalon.framework.configuration.ConfigurationException;
13 import org.apache.avalon.framework.context.ContextException;
14 import org.apache.log.LogTarget;
15 import org.apache.log.format.ExtendedPatternFormatter;
16 import org.apache.log.format.Formatter;
17 import org.apache.log.format.PatternFormatter;
18 import org.apache.log.format.RawFormatter;
19 import org.apache.log.output.ServletOutputLogTarget;
20
21 /**
22  * ServletTargetFactory class.
23  *
24  * This factory creates a ServletOutputLogTargets. It uses the context-key attribute
25  * to locate the ServletContext needed out of the Context object passed to this factory.
26  * The default context-key is "servlet-context".
27  *
28  * <pre>
29  *
30  * &lt;servlet id="target-id" context-key="context-key-to-servlet-context-object"&gt;
31  * &lt;format type="raw|pattern|extended"&gt;pattern to be used if needed&lt;/format&gt;
32  * &lt;/servlet&gt;
33  *
34  * </pre>
35  * <dl>
36  * <dt>&lt;format&gt;</dt>
37  * <dd>
38  * The type attribute of the pattern element denotes the type of
39  * Formatter to be used and according to it the pattern to use for.
40  * This elements defaults to:
41  * <p>
42  * %7.7{priority} %5.5{time} [%8.8{category}] (%{context}): %{message}\\n%{throwable}
43  * </p>
44  * </dd>
45  * </dl>
46  *
47  * @author <a HREF="mailto:giacomo@apache,org">Giacomo Pati</a>
48  * @version CVS $Revision: 1.4 $ $Date: 2001/12/19 23:34:50 $
49  * @since 4.0
50  */

51 public final class ServletTargetFactory
52     extends AbstractTargetFactory
53 {
54
55     /**
56      * create a LogTarget based on a Configuration
57      */

58     public final LogTarget createTarget( final Configuration configuration )
59         throws ConfigurationException
60     {
61         final String JavaDoc contextkey =
62             m_configuration.getAttribute( "context-key", "servlet-context" );
63         final ServletContext JavaDoc sctx;
64
65         final Configuration conf_format = configuration.getChild( "format" );
66         final Formatter formatter = getFormatter( conf_format );
67
68         try
69         {
70             sctx = (ServletContext JavaDoc) m_context.get( contextkey );
71         }
72         catch( final ContextException ce )
73         {
74             throw new ConfigurationException( "Cannot find ServletContext object in " +
75                                               "application context", ce );
76         }
77
78         return new ServletOutputLogTarget( sctx );
79     }
80
81     protected Formatter getFormatter( final Configuration conf )
82     {
83         Formatter formatter = null;
84
85         if ( null != conf )
86         {
87             final FormatterFactory formatterFactory = new FormatterFactory();
88             formatter = formatterFactory.createFormatter( conf );
89         }
90         
91         return formatter;
92     }
93 }
94
Popular Tags