1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.log4j.helpers;
19
20 import org.apache.log4j.spi.AppenderAttachable;
21 import org.apache.log4j.spi.LoggingEvent;
22
23 import org.apache.log4j.Appender;
24 import java.util.Vector;
25 import java.util.Enumeration;
26
27 /**
28 A straightforward implementation of the {@link AppenderAttachable}
29 interface.
30
31 @author Ceki Gülcü
32 @since version 0.9.1 */
33 public class AppenderAttachableImpl implements AppenderAttachable {
34
35 /** Array of appenders. */
36 protected Vector appenderList;
37
38 /**
39 Attach an appender. If the appender is already in the list in
40 won't be added again.
41 */
42 public
43 void addAppender(Appender newAppender) {
44 // Null values for newAppender parameter are strictly forbidden.
45 if(newAppender == null)
46 return;
47
48 if(appenderList == null) {
49 appenderList = new Vector(1);
50 }
51 if(!appenderList.contains(newAppender))
52 appenderList.addElement(newAppender);
53 }
54
55 /**
56 Call the <code>doAppend</code> method on all attached appenders. */
57 public
58 int appendLoopOnAppenders(LoggingEvent event) {
59 int size = 0;
60 Appender appender;
61
62 if(appenderList != null) {
63 size = appenderList.size();
64 for(int i = 0; i < size; i++) {
65 appender = (Appender) appenderList.elementAt(i);
66 appender.doAppend(event);
67 }
68 }
69 return size;
70 }
71
72
73 /**
74 Get all attached appenders as an Enumeration. If there are no
75 attached appenders <code>null</code> is returned.
76
77 @return Enumeration An enumeration of attached appenders.
78 */
79 public
80 Enumeration getAllAppenders() {
81 if(appenderList == null)
82 return null;
83 else
84 return appenderList.elements();
85 }
86
87 /**
88 Look for an attached appender named as <code>name</code>.
89
90 <p>Return the appender with that name if in the list. Return null
91 otherwise.
92
93 */
94 public
95 Appender getAppender(String name) {
96 if(appenderList == null || name == null)
97 return null;
98
99 int size = appenderList.size();
100 Appender appender;
101 for(int i = 0; i < size; i++) {
102 appender = (Appender) appenderList.elementAt(i);
103 if(name.equals(appender.getName()))
104 return appender;
105 }
106 return null;
107 }
108
109
110 /**
111 Returns <code>true</code> if the specified appender is in the
112 list of attached appenders, <code>false</code> otherwise.
113
114 @since 1.2 */
115 public
116 boolean isAttached(Appender appender) {
117 if(appenderList == null || appender == null)
118 return false;
119
120 int size = appenderList.size();
121 Appender a;
122 for(int i = 0; i < size; i++) {
123 a = (Appender) appenderList.elementAt(i);
124 if(a == appender)
125 return true;
126 }
127 return false;
128 }
129
130
131
132 /**
133 * Remove and close all previously attached appenders.
134 * */
135 public
136 void removeAllAppenders() {
137 if(appenderList != null) {
138 int len = appenderList.size();
139 for(int i = 0; i < len; i++) {
140 Appender a = (Appender) appenderList.elementAt(i);
141 a.close();
142 }
143 appenderList.removeAllElements();
144 appenderList = null;
145 }
146 }
147
148
149 /**
150 Remove the appender passed as parameter form the list of attached
151 appenders. */
152 public
153 void removeAppender(Appender appender) {
154 if(appender == null || appenderList == null)
155 return;
156 appenderList.removeElement(appender);
157 }
158
159
160 /**
161 Remove the appender with the name passed as parameter form the
162 list of appenders.
163 */
164 public
165 void removeAppender(String name) {
166 if(name == null || appenderList == null) return;
167 int size = appenderList.size();
168 for(int i = 0; i < size; i++) {
169 if(name.equals(((Appender)appenderList.elementAt(i)).getName())) {
170 appenderList.removeElementAt(i);
171 break;
172 }
173 }
174 }
175
176 }
177