KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log > output > PriorityFilteringTarget


1 /*
2  * Copyright 2004 The 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 package org.apache.log.output;
18
19 import org.apache.log.LogEvent;
20 import org.apache.log.LogTarget;
21 import org.apache.log.Priority;
22 import org.apache.log.util.Closeable;
23
24 /**
25  * This is a priority filtering target that forwards only requests
26  * to other (wrapped) targets that have the same or a higher
27  * priority.
28  *
29  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
30  */

31 public class PriorityFilteringTarget
32     extends AbstractTarget
33 {
34     private final Priority m_priority;
35
36     private final boolean m_closeWrapped;
37
38     /** Log targets in filter chain */
39     private LogTarget m_targets[];
40
41     /**
42      * @param priority The priority used to filter
43      * @param closeWrappedTarget see AbstractWrappingTarget
44      */

45     public PriorityFilteringTarget(Priority priority,
46                                     boolean closeWrappedTarget)
47     {
48         m_priority = priority;
49         m_closeWrapped = closeWrappedTarget;
50         open();
51     }
52
53     /**
54      * @param priority The priority used to filter
55      */

56     public PriorityFilteringTarget(Priority priority)
57     {
58         this(priority, false );
59     }
60
61     /**
62      * Add a new target to output chain.
63      *
64      * @param target the target
65      */

66     public void addTarget( final LogTarget target )
67     {
68         if( null == m_targets )
69         {
70             m_targets = new LogTarget[]{target};
71         }
72         else
73         {
74             final LogTarget oldTargets[] = m_targets;
75             m_targets = new LogTarget[ oldTargets.length + 1 ];
76             System.arraycopy( oldTargets, 0, m_targets, 0, oldTargets.length );
77             m_targets[ m_targets.length - 1 ] = target;
78         }
79     }
80
81
82     /* (non-Javadoc)
83      * @see org.apache.log.output.AbstractTarget#doProcessEvent(org.apache.log.LogEvent)
84      */

85     protected void doProcessEvent(LogEvent event) throws Exception JavaDoc
86     {
87         if ( event != null
88              && m_targets != null
89              && !event.getPriority().isLower(m_priority) )
90         {
91             for( int i = 0; i < m_targets.length; i++ )
92             {
93                 m_targets[ i ].processEvent( event );
94             }
95         }
96     }
97     
98     /* (non-Javadoc)
99      * @see org.apache.log.util.Closeable#close()
100      */

101     public void close()
102     {
103         super.close();
104
105         if( m_closeWrapped && m_targets != null )
106         {
107             for( int i = 0; i < m_targets.length; i++ )
108             {
109                 if ( m_targets[i] instanceof Closeable )
110                 {
111                     ( (Closeable)m_targets[i] ).close();
112                 }
113             }
114         }
115     }
116     
117 }
118
Popular Tags