KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > util > log > filter > AbstractFilterTarget


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 file.
7  */

8 package org.jivesoftware.util.log.filter;
9
10 import org.jivesoftware.util.log.FilterTarget;
11 import org.jivesoftware.util.log.LogEvent;
12 import org.jivesoftware.util.log.LogTarget;
13
14 /**
15  * Abstract implementation of FilterTarget.
16  * A concrete implementation has to implement filter method.
17  *
18  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
19  */

20 public abstract class AbstractFilterTarget
21         implements FilterTarget, LogTarget {
22     //Log targets in filter chain
23
private LogTarget m_targets[];
24
25     /**
26      * Add a new target to output chain.
27      *
28      * @param target the target
29      */

30     public void addTarget(final LogTarget target) {
31         if (null == m_targets) {
32             m_targets = new LogTarget[]{target};
33         }
34         else {
35             final LogTarget oldTargets[] = m_targets;
36             m_targets = new LogTarget[oldTargets.length + 1];
37             System.arraycopy(oldTargets, 0, m_targets, 0, oldTargets.length);
38             m_targets[m_targets.length - 1] = target;
39         }
40     }
41
42     /**
43      * Filter the log event.
44      *
45      * @param event the event
46      * @return return true to discard event, false otherwise
47      */

48     protected abstract boolean filter(LogEvent event);
49
50     /**
51      * Process a log event
52      *
53      * @param event the log event
54      */

55     public void processEvent(final LogEvent event) {
56         if (null == m_targets || filter(event))
57             return;
58         else {
59             for (int i = 0; i < m_targets.length; i++) {
60                 m_targets[i].processEvent(event);
61             }
62         }
63     }
64 }
65
Popular Tags