KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.OutputStream JavaDoc;
11 import org.apache.avalon.excalibur.logger.factory.AbstractTargetFactory;
12 import org.apache.avalon.framework.configuration.Configuration;
13 import org.apache.avalon.framework.configuration.ConfigurationException;
14 import org.apache.avalon.framework.context.Context;
15 import org.apache.avalon.framework.context.ContextException;
16 import org.apache.avalon.framework.context.Contextualizable;
17 import org.apache.avalon.framework.context.DefaultContext;
18 import org.apache.log.LogTarget;
19 import org.apache.log.format.ExtendedPatternFormatter;
20 import org.apache.log.format.Formatter;
21 import org.apache.log.format.PatternFormatter;
22 import org.apache.log.format.RawFormatter;
23 import org.apache.log.output.io.StreamTarget;
24
25 /**
26  * TargetFactory for <code>org.apache.log.output.io.StreamTarget</code>
27  *
28  * This factory is able to create different StreamTargets according to the following
29  * configuration syntax :
30  * <pre>
31  * &lt;stream id="foo"&gt;
32  * &lt;stream&gt;<i>stream-context-name</i>&lt;/stream&gt;
33  * &lt;format type="<i>raw|pattern|extended</i>"&gt;<i>pattern to be used if needed</i>&lt;/format&gt;
34  * &lt;/stream&gt;
35  * </pre>
36  *
37  * <p>The "stream-context-name" is the name of an <code>java.io.OutputStream</code> that
38  * is fetched in the context. This context contains two predefined streams :
39  * <li>"<code>System.out</code>" for the system output stream,</li>
40  * <li>"<code>System.err</code>" for the system error stream.</li>
41  * </p>
42  *
43  * <p>The syntax of "format" is the same as in <code>FileTargetFactory</code>.</p>
44  *
45  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
46  * @version CVS $Revision: 1.3 $ $Date: 2001/12/19 23:34:50 $
47  */

48 public class StreamTargetFactory
49     extends AbstractTargetFactory
50     implements Contextualizable
51 {
52
53     /**
54      * Create a LogTarget based on a Configuration
55      */

56     public LogTarget createTarget( final Configuration configuration )
57         throws ConfigurationException
58     {
59         OutputStream JavaDoc stream;
60
61         final Configuration streamConfig = configuration.getChild( "stream", false );
62         if ( null == streamConfig )
63         {
64             stream = System.err;
65         }
66         else
67         {
68             final String JavaDoc streamName = streamConfig.getValue();
69             try
70             {
71                 stream = (OutputStream JavaDoc)m_context.get( streamName );
72             }
73             catch( Exception JavaDoc e)
74             {
75                 throw new ConfigurationException( "Error resolving stream '" +
76                                                   streamName + "' at " +
77                                                   streamConfig.getLocation(), e );
78             }
79         }
80
81         final Configuration formatterConf = configuration.getChild( "format" );
82         final Formatter formatter = getFormatter( formatterConf );
83  
84         return new StreamTarget( stream, formatter );
85     }
86
87  
88     public void contextualize( final Context context )
89         throws ContextException
90     {
91         // Add System output streams
92
final DefaultContext newContext = new DefaultContext( context );
93
94         newContext.put( "System.out", System.out );
95         newContext.put( "System.err", System.err );
96
97         super.contextualize( newContext );
98     }
99
100     protected Formatter getFormatter( final Configuration conf )
101     {
102         Formatter formatter = null;
103
104         if ( null != conf )
105         {
106             final FormatterFactory formatterFactory = new FormatterFactory();
107             formatter = formatterFactory.createFormatter( conf );
108         }
109         
110         return formatter;
111     }
112 }
113
114
Popular Tags