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 {@link COWArrayList} based implementation of the {@link AppenderAttachable} 023 * 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 added 034 * 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> 071 * Return the appender with that name if in the list. Return null 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 /** 113 * Remove the appender passed as parameter form the list of attached appenders. 114 */ 115 public boolean detachAppender(Appender<E> appender) { 116 if (appender == null) { 117 return false; 118 } 119 boolean result; 120 result = appenderList.remove(appender); 121 return result; 122 } 123 124 /** 125 * Remove the appender with the name passed as parameter form the list of 126 * appenders. 127 */ 128 public boolean detachAppender(String name) { 129 if (name == null) { 130 return false; 131 } 132 boolean removed = false; 133 for (Appender<E> a : appenderList.asTypedArray()) { 134 if (name.equals((a).getName())) { 135 removed = appenderList.remove(a); 136 break; 137 } 138 } 139 return removed; 140 } 141}