001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 * 
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 * 
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.log4j.helpers;
019
020import org.apache.log4j.spi.AppenderAttachable;
021import org.apache.log4j.spi.LoggingEvent;
022
023import org.apache.log4j.Appender;
024import java.util.Vector;
025import java.util.Enumeration;
026
027/**
028   A straightforward implementation of the {@link AppenderAttachable}
029   interface.
030
031   @author Ceki Gülcü
032   @since version 0.9.1 */
033public class AppenderAttachableImpl implements AppenderAttachable {
034  
035  /** Array of appenders. */
036  protected Vector  appenderList;
037
038  /**
039     Attach an appender. If the appender is already in the list in
040     won't be added again.
041  */
042  public
043  void addAppender(Appender newAppender) {
044    // Null values for newAppender parameter are strictly forbidden.
045    if(newAppender == null)
046      return;
047    
048    if(appenderList == null) {
049      appenderList = new Vector(1);
050    }
051    if(!appenderList.contains(newAppender))
052      appenderList.addElement(newAppender);
053  }
054
055  /**
056     Call the <code>doAppend</code> method on all attached appenders.  */
057  public
058  int appendLoopOnAppenders(LoggingEvent event) {
059    int size = 0;
060    Appender appender;
061
062    if(appenderList != null) {
063      size = appenderList.size();
064      for(int i = 0; i < size; i++) {
065        appender = (Appender) appenderList.elementAt(i);
066        appender.doAppend(event);
067      }
068    }    
069    return size;
070  }
071
072
073  /**
074     Get all attached appenders as an Enumeration. If there are no
075     attached appenders <code>null</code> is returned.
076     
077     @return Enumeration An enumeration of attached appenders.
078   */
079  public
080  Enumeration getAllAppenders() {
081    if(appenderList == null)
082      return null;
083    else 
084      return appenderList.elements();    
085  }
086
087  /**
088     Look for an attached appender named as <code>name</code>.
089
090     <p>Return the appender with that name if in the list. Return null
091     otherwise.  
092     
093   */
094  public
095  Appender getAppender(String name) {
096     if(appenderList == null || name == null)
097      return null;
098
099     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}