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.core.spi;
015
016import java.util.Iterator;
017
018import ch.qos.logback.core.Appender;
019import ch.qos.logback.core.util.COWArrayList;
020
021/**
022 * A ReentrantReadWriteLock based implementation of the
023 * {@link AppenderAttachable} interface.
024 *
025 * @author Ceki Gülcü
026 */
027public class AppenderAttachableImpl<E> implements AppenderAttachable<E> {
028
029    @SuppressWarnings("unchecked")
030    final private COWArrayList<Appender<E>> appenderList = new COWArrayList<Appender<E>>(new Appender[0]);
031
032    /**
033     * Attach an appender. If the appender is already in the list in won't be
034     * added again.
035     */
036    public void addAppender(Appender<E> newAppender) {
037        if (newAppender == null) {
038            throw new IllegalArgumentException("Null argument disallowed");
039        }
040        appenderList.addIfAbsent(newAppender);
041    }
042
043    /**
044     * Call the <code>doAppend</code> method on all attached appenders.
045     */
046    public int appendLoopOnAppenders(E e) {
047        int size = 0;
048        final Appender<E>[] appenderArray = appenderList.asTypedArray();
049        final int len = appenderArray.length;
050        for (int i = 0; i < len; i++) {
051            appenderArray[i].doAppend(e);
052            size++;
053        }
054        return size;
055    }
056
057    /**
058     * Get all attached appenders as an Enumeration. If there are no attached
059     * appenders <code>null</code> is returned.
060     *
061     * @return Iterator An iterator of attached appenders.
062     */
063    public Iterator<Appender<E>> iteratorForAppenders() {
064        return appenderList.iterator();
065    }
066
067    /**
068     * Look for an attached appender named as <code>name</code>.
069     * 
070     * <p> Return the appender with that name if in the list. Return null
071     * otherwise.
072     */
073    public Appender<E> getAppender(String name) {
074        if (name == null) {
075            return null;
076        }
077        for (Appender<E> appender : appenderList) {
078            if (name.equals(appender.getName())) {
079                return appender;
080            }
081        }
082        return null;
083    }
084
085    /**
086     * Returns <code>true</code> if the specified appender is in the list of
087     * attached appenders, <code>false</code> otherwise.
088     *
089     * @since 1.2
090     */
091    public boolean isAttached(Appender<E> appender) {
092        if (appender == null) {
093            return false;
094        }
095        for (Appender<E> a : appenderList) {
096            if (a == appender)
097                return true;
098        }
099        return false;
100    }
101
102    /**
103     * Remove and processPriorToRemoval all previously attached appenders.
104     */
105    public void detachAndStopAllAppenders() {
106        for (Appender<E> a : appenderList) {
107            a.stop();
108        }
109        appenderList.clear();
110    }
111
112    static final long START = System.currentTimeMillis();
113
114    /**
115     * Remove the appender passed as parameter form the list of attached
116     * appenders.
117     */
118    public boolean detachAppender(Appender<E> appender) {
119        if (appender == null) {
120            return false;
121        }
122        boolean result;
123        result = appenderList.remove(appender);
124        return result;
125    }
126
127    /**
128     * Remove the appender with the name passed as parameter form the list of
129     * appenders.
130     */
131    public boolean detachAppender(String name) {
132        if (name == null) {
133            return false;
134        }
135        boolean removed = false;
136        for (Appender<E> a : appenderList) {
137            if (name.equals((a).getName())) {
138                removed = appenderList.remove(a);
139                break;
140            }
141        }
142        return removed;
143    }
144}