KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > logging > logkit > factory > StreamTargetFactory


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

17
18 package org.apache.avalon.logging.logkit.factory;
19
20 import java.io.OutputStream JavaDoc;
21
22 import org.apache.avalon.framework.configuration.Configuration;
23 import org.apache.avalon.framework.configuration.ConfigurationException;
24
25 import org.apache.avalon.logging.logkit.LogTargetFactory;
26 import org.apache.avalon.logging.logkit.FormatterFactory;
27
28 import org.apache.log.LogTarget;
29 import org.apache.log.format.Formatter;
30 import org.apache.log.output.io.StreamTarget;
31
32 /**
33  * TargetFactory for {@link org.apache.log.output.io.StreamTarget}.
34  *
35  * This factory is able to create different StreamTargets according to the following
36  * configuration syntax:
37  * <pre>
38  * &lt;stream id="foo"&gt;
39  * &lt;name&gt;<i>stream-context-name</i>&lt;/name&gt;
40  * &lt;format type="<i>raw|pattern|extended</i>"&gt;<i>pattern to be used if needed</i>&lt;/format&gt;
41  * &lt;/stream&gt;
42  * </pre>
43  *
44  * <p>The "stream-context-name" is the name of an <code>java.io.OutputStream</code>.
45  * Two stream names are supported:
46  * <li>"<code>System.out</code>" for the system output stream,</li>
47  * <li>"<code>System.err</code>" for the system error stream.</li>
48  * </p>
49  *
50  * <p>The syntax of "format" is the same as in <code>FileTargetFactory</code>.</p>
51  *
52  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
53  * @version CVS $Revision: 1.2 $ $Date: 2004/02/24 21:55:39 $
54  */

55 public class StreamTargetFactory implements LogTargetFactory
56 {
57     //--------------------------------------------------------------
58
// immutable state
59
//--------------------------------------------------------------
60

61     private final FormatterFactory m_formatter;
62
63     //--------------------------------------------------------------
64
// constructor
65
//--------------------------------------------------------------
66

67     public StreamTargetFactory( FormatterFactory formatter )
68     {
69         m_formatter = formatter;
70     }
71
72     //--------------------------------------------------------------
73
// LogTargetFactory
74
//--------------------------------------------------------------
75

76     /**
77      * Create a LogTarget based on a Configuration
78      */

79     public LogTarget createTarget( final Configuration configuration )
80     {
81         OutputStream JavaDoc stream;
82
83         final Configuration streamConfig =
84           configuration.getChild( "name", false );
85         if( null == streamConfig )
86         {
87             stream = System.out;
88         }
89         else
90         {
91             final String JavaDoc streamName = streamConfig.getValue( "" );
92             if( streamName.equals( "System.out" ) )
93             {
94                 stream = System.out;
95             }
96             else
97             {
98                 stream = System.err;
99             }
100         }
101
102         Configuration formatConfig = configuration.getChild( "format", false );
103         final Formatter formatter =
104           m_formatter.createFormatter( formatConfig );
105
106         return new StreamTarget( stream, formatter );
107     }
108 }
109
110
Popular Tags