1 /**
2 * Logback: the reliable, generic, fast and flexible logging framework.
3 * Copyright (C) 1999-2011, QOS.ch. All rights reserved.
4 *
5 * This program and the accompanying materials are dual-licensed under
6 * either the terms of the Eclipse Public License v1.0 as published by
7 * the Eclipse Foundation
8 *
9 * or (per the licensee's choosing)
10 *
11 * under the terms of the GNU Lesser General Public License version 2.1
12 * as published by the Free Software Foundation.
13 */
14 package ch.qos.logback.core.spi;
15
16 import java.util.Iterator;
17 import java.util.concurrent.CopyOnWriteArrayList;
18
19 import ch.qos.logback.core.Appender;
20
21 /**
22 * A ReentrantReadWriteLock based implementation of the
23 * {@link AppenderAttachable} interface.
24 *
25 * @author Ceki Gülcü
26 */
27 public class AppenderAttachableImpl<E> implements AppenderAttachable<E> {
28
29 final private CopyOnWriteArrayList<Appender<E>> appenderList = new CopyOnWriteArrayList<Appender<E>>();
30
31 // volatile private ReadWriteLock rwLock = new ReentrantReadWriteLock();
32 // private Lock r = rwLock.readLock();
33 // private Lock w = rwLock.writeLock();
34
35 /**
36 * Attach an appender. If the appender is already in the list in won't be
37 * added again.
38 */
39 public void addAppender(Appender<E> newAppender) {
40 p("addAppender(Appender)");
41 if (newAppender == null) {
42 throw new IllegalArgumentException("Null argument disallowed");
43 }
44 appenderList.addIfAbsent(newAppender);
45 }
46
47 /**
48 * Call the <code>doAppend</code> method on all attached appenders.
49 */
50 public int appendLoopOnAppenders(E e) {
51 int size = 0;
52 for (Appender<E> appender : appenderList) {
53 appender.doAppend(e);
54 size++;
55 }
56 return size;
57 }
58
59 /**
60 * Get all attached appenders as an Enumeration. If there are no attached
61 * appenders <code>null</code> is returned.
62 *
63 * @return Iterator An iterator of attached appenders.
64 */
65 public Iterator<Appender<E>> iteratorForAppenders() {
66 return appenderList.iterator();
67 }
68
69 /**
70 * Look for an attached appender named as <code>name</code>.
71 * <p/>
72 * <p> Return the appender with that name if in the list. Return null
73 * otherwise.
74 */
75 public Appender<E> getAppender(String name) {
76 if (name == null) {
77 return null;
78 }
79 Appender<E> found = null;
80 for (Appender<E> appender : appenderList) {
81 if (name.equals(appender.getName())) {
82 return appender;
83 }
84 }
85 return null;
86 }
87
88 /**
89 * Returns <code>true</code> if the specified appender is in the list of
90 * attached appenders, <code>false</code> otherwise.
91 *
92 * @since 1.2
93 */
94 public boolean isAttached(Appender<E> appender) {
95 if (appender == null) {
96 return false;
97 }
98 for (Appender<E> a : appenderList) {
99 if (a == appender) return true;
100 }
101 return false;
102 }
103
104 /**
105 * Remove and stop all previously attached appenders.
106 */
107 public void detachAndStopAllAppenders() {
108 for (Appender<E> a : appenderList) {
109 a.stop();
110 }
111 appenderList.clear();
112 }
113
114 static final long START = System.currentTimeMillis();
115
116 void p(String msg) {
117 //System.out.println((System.currentTimeMillis()-START) + " "+ Thread.currentThread().getName() + msg);
118 //System.out.println("------------------------");
119
120 }
121
122 /**
123 * Remove the appender passed as parameter form the list of attached
124 * appenders.
125 */
126 public boolean detachAppender(Appender<E> appender) {
127 if (appender == null) {
128 return false;
129 }
130 boolean result;
131 result = appenderList.remove(appender);
132 return result;
133 }
134
135 /**
136 * Remove the appender with the name passed as parameter form the list of
137 * appenders.
138 */
139 public boolean detachAppender(String name) {
140 if (name == null) {
141 return false;
142 }
143 boolean removed = false;
144 for (Appender<E> a : appenderList) {
145 if (name.equals((a).getName())) {
146 removed = appenderList.remove(a);
147 break;
148 }
149 }
150 return removed;
151 }
152 }