1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.spi;
15
16 import java.util.Iterator;
17
18 import ch.qos.logback.core.Appender;
19 import ch.qos.logback.core.util.COWArrayList;
20
21
22
23
24
25
26
27 public class AppenderAttachableImpl<E> implements AppenderAttachable<E> {
28
29 @SuppressWarnings("unchecked")
30 final private COWArrayList<Appender<E>> appenderList = new COWArrayList<Appender<E>>(new Appender[0]);
31
32
33
34
35
36 public void addAppender(Appender<E> newAppender) {
37 if (newAppender == null) {
38 throw new IllegalArgumentException("Null argument disallowed");
39 }
40 appenderList.addIfAbsent(newAppender);
41 }
42
43
44
45
46 public int appendLoopOnAppenders(E e) {
47 int size = 0;
48 final Appender<E>[] appenderArray = appenderList.asTypedArray();
49 final int len = appenderArray.length;
50 for (int i = 0; i < len; i++) {
51 appenderArray[i].doAppend(e);
52 size++;
53 }
54 return size;
55 }
56
57
58
59
60
61
62
63 public Iterator<Appender<E>> iteratorForAppenders() {
64 return appenderList.iterator();
65 }
66
67
68
69
70
71
72
73 public Appender<E> getAppender(String name) {
74 if (name == null) {
75 return null;
76 }
77 for (Appender<E> appender : appenderList) {
78 if (name.equals(appender.getName())) {
79 return appender;
80 }
81 }
82 return null;
83 }
84
85
86
87
88
89
90
91 public boolean isAttached(Appender<E> appender) {
92 if (appender == null) {
93 return false;
94 }
95 for (Appender<E> a : appenderList) {
96 if (a == appender)
97 return true;
98 }
99 return false;
100 }
101
102
103
104
105 public void detachAndStopAllAppenders() {
106 for (Appender<E> a : appenderList) {
107 a.stop();
108 }
109 appenderList.clear();
110 }
111
112
113
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
126
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 }