01: /*
02: * Copyright (C) The Apache Software Foundation. All rights reserved.
03: *
04: * This software is published under the terms of the Apache Software License
05: * version 1.1, a copy of which has been included with this distribution in
06: * the LICENSE file.
07: */
08: package org.jivesoftware.util.log.filter;
09:
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 implements FilterTarget,
21: 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: } else {
34: final LogTarget oldTargets[] = m_targets;
35: m_targets = new LogTarget[oldTargets.length + 1];
36: System.arraycopy(oldTargets, 0, m_targets, 0,
37: 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: }
|