001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.classic.spi;
015
016import java.util.concurrent.CopyOnWriteArrayList;
017
018import org.slf4j.Marker;
019
020import ch.qos.logback.classic.Level;
021import ch.qos.logback.classic.Logger;
022import ch.qos.logback.classic.turbo.TurboFilter;
023import ch.qos.logback.core.spi.FilterReply;
024
025/**
026 * Implementation of TurboFilterAttachable.
027 * 
028 * @author Ceki Gülcü
029 */
030final public class TurboFilterList extends CopyOnWriteArrayList<TurboFilter> {
031
032    private static final long serialVersionUID = 1L;
033
034    /**
035     * Loop through the filters in the chain. As soon as a filter decides on ACCEPT
036     * or DENY, then that value is returned. If all of the filters return NEUTRAL,
037     * then NEUTRAL is returned.
038     */
039    public FilterReply getTurboFilterChainDecision(final Marker marker, final Logger logger, final Level level,
040            final String format, final Object[] params, final Throwable t) {
041
042        final int size = size();
043        // if (size == 0) {
044        // return FilterReply.NEUTRAL;
045        // }
046        if (size == 1) {
047            try {
048                TurboFilter tf = get(0);
049                return tf.decide(marker, logger, level, format, params, t);
050            } catch (IndexOutOfBoundsException iobe) {
051                return FilterReply.NEUTRAL;
052            }
053        }
054
055        Object[] tfa = toArray();
056        final int len = tfa.length;
057        for (int i = 0; i < len; i++) {
058            // for (TurboFilter tf : this) {
059            final TurboFilter tf = (TurboFilter) tfa[i];
060            final FilterReply r = tf.decide(marker, logger, level, format, params, t);
061            if (r == FilterReply.DENY || r == FilterReply.ACCEPT) {
062                return r;
063            }
064        }
065        return FilterReply.NEUTRAL;
066    }
067
068    // public boolean remove(TurboFilter turboFilter) {
069    // return tfList.remove(turboFilter);
070    // }
071    //
072    // public TurboFilter remove(int index) {
073    // return tfList.remove(index);
074    // }
075    //
076    // final public int size() {
077    // return tfList.size();
078    // }
079
080}