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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.log4j.helpers;
20 import org.apache.log4j.spi.AppenderAttachable;
21 import org.apache.log4j.spi.LoggingEvent;
23 import org.apache.log4j.Appender;
24 import java.util.Vector;
25 import java.util.Enumeration;
28 A straightforward implementation of the {@link AppenderAttachable}
31 @author Ceki Gülcü
32 @since version 0.9.1 */
33 public class AppenderAttachableImpl implements AppenderAttachable {
35 /** Array of appenders. */
36 protected Vector appenderList;
39 Attach an appender. If the appender is already in the list in
43 void addAppender(Appender newAppender) {
44 // Null values for newAppender parameter are strictly forbidden.
45 if(newAppender == null) {
49 if(appenderList == null) {
50 appenderList = new Vector(1);
52 if(!appenderList.contains(newAppender)) {
53 appenderList.addElement(newAppender);
58 Call the <code>doAppend</code> method on all attached appenders. */
60 int appendLoopOnAppenders(LoggingEvent event) {
64 if(appenderList != null) {
65 size = appenderList.size();
66 for(int i = 0; i < size; i++) {
67 appender = (Appender) appenderList.elementAt(i);
68 appender.doAppend(event);
76 Get all attached appenders as an Enumeration. If there are no
77 attached appenders <code>null</code> is returned.
79 @return Enumeration An enumeration of attached appenders.
82 Enumeration getAllAppenders() {
83 if(appenderList == null) {
86 return appenderList.elements();
91 Look for an attached appender named as <code>name</code>.
93 <p>Return the appender with that name if in the list. Return null
98 Appender getAppender(String name) {
99 if(appenderList == null || name == null) {
103 int size = appenderList.size();
105 for(int i = 0; i < size; i++) {
106 appender = (Appender) appenderList.elementAt(i);
107 if(name.equals(appender.getName())) {
116 Returns <code>true</code> if the specified appender is in the
117 list of attached appenders, <code>false</code> otherwise.
121 boolean isAttached(Appender appender) {
122 if(appenderList == null || appender == null) {
126 int size = appenderList.size();
128 for(int i = 0; i < size; i++) {
129 a = (Appender) appenderList.elementAt(i);
140 * Remove and close all previously attached appenders.
143 void removeAllAppenders() {
144 if(appenderList != null) {
145 int len = appenderList.size();
146 for(int i = 0; i < len; i++) {
147 Appender a = (Appender) appenderList.elementAt(i);
150 appenderList.removeAllElements();
157 Remove the appender passed as parameter form the list of attached
160 void removeAppender(Appender appender) {
161 if(appender == null || appenderList == null) {
164 appenderList.removeElement(appender);
169 Remove the appender with the name passed as parameter form the
173 void removeAppender(String name) {
174 if(name == null || appenderList == null) {
177 int size = appenderList.size();
178 for(int i = 0; i < size; i++) {
179 if(name.equals(((Appender)appenderList.elementAt(i)).getName())) {
180 appenderList.removeElementAt(i);