KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.avalon.excalibur.logger.LogTargetFactory;
11 import org.apache.avalon.excalibur.logger.LogTargetFactoryManageable;
12 import org.apache.avalon.excalibur.logger.LogTargetFactoryManager;
13 import org.apache.avalon.framework.configuration.Configuration;
14 import org.apache.avalon.framework.configuration.ConfigurationException;
15 import org.apache.avalon.framework.context.ContextException;
16 import org.apache.log.LogTarget;
17 import org.apache.log.output.AsyncLogTarget;
18
19 /**
20  * AsyncLogTargetFactory class.
21  *
22  * This factory creates LogTargets with a wrapped AsyncLogTarget around it:
23  *
24  * <pre>
25  *
26  * &lt;async-target id="target-id" queue-size=".." priority="MIN|NORM|MAX|n"&gt;
27  * &lt;any-target-definition/&gt;
28  * &lt;/async-target&gt;
29  *
30  * </pre>
31  * <p>
32  * This factory creates a AsyncLogTarget object with a specified queue-size
33  * attribute (which defaults to what the AsyncLogTarget uses if absent).
34  * The LogTarget to wrap is described in a child element of the configuration (in
35  * the sample above named as &lt;any-target-definition/&gt;).
36  * The Thread of the created AsyncLogTarget will have a priority specified by the
37  * priotity attribute (which defaults to Thread.MIN_PRIORITY). The priority values
38  * corresponds to those defined in the Thread class which are:
39  * </p>
40  * <p>
41  * <blockquote>
42  * MIN=Thread.MIN_PRIORITY<br>
43  * NORM=Thread.NORM_PRIORITY<br>
44  * MAX=Thread.MAX_PRIORITY<br>
45  * number=priority number (see class java.lang.Thread)<br>
46  * </blockquote>
47  * </p>
48  *
49  * @author <a HREF="mailto:giacomo@apache,org">Giacomo Pati</a>
50  * @version CVS $Revision: 1.4 $ $Date: 2002/01/14 21:49:35 $
51  * @since 4.0
52  */

53 public final class AsyncLogTargetFactory
54     extends AbstractTargetFactory
55     implements LogTargetFactoryManageable
56 {
57     /** The LogTargetFactoryManager */
58     protected LogTargetFactoryManager m_logTargetFactoryManager;
59
60     /**
61      * create a LogTarget based on a Configuration
62      */

63     public final LogTarget createTarget( final Configuration configuration )
64         throws ConfigurationException
65     {
66         final int queuesize = configuration.getAttributeAsInteger( "queue-size", -1 );
67         final Configuration config = configuration.getChildren()[0];
68         final LogTargetFactory factory = m_logTargetFactoryManager.getLogTargetFactory( config.getName() );
69         final LogTarget target = factory.createTarget( config );
70         final AsyncLogTarget asyncTarget;
71         if( queuesize == -1 )
72         {
73             asyncTarget = new AsyncLogTarget( target );
74         }
75         else
76         {
77             asyncTarget = new AsyncLogTarget( target, queuesize );
78         }
79
80         final String JavaDoc priority = configuration.getAttribute( "priority", null );
81         final int thread_priority;
82         if( "MIN".equalsIgnoreCase( priority ) )
83         {
84             thread_priority = Thread.MIN_PRIORITY;
85         }
86         else if( "NORM".equalsIgnoreCase( priority ) )
87         {
88             thread_priority = Thread.NORM_PRIORITY;
89         }
90         else if( "NORM".equalsIgnoreCase( priority ) )
91         {
92             thread_priority = Thread.NORM_PRIORITY;
93         }
94         else
95         {
96             thread_priority = configuration.getAttributeAsInteger( "priority", 1 );
97         }
98         final Thread JavaDoc thread = new Thread JavaDoc( asyncTarget );
99         thread.setPriority( thread_priority );
100         thread.setDaemon( true );
101         thread.start();
102         return asyncTarget;
103     }
104
105     /**
106      * get the LogTargetFactoryManager
107      */

108     public final void setLogTargetFactoryManager( LogTargetFactoryManager logTargetFactoryManager )
109     {
110         m_logTargetFactoryManager = logTargetFactoryManager;
111     }
112
113 }
114
115
Popular Tags