JAL-3048 test updated for AlignExportSettings changes
[jalview.git] / srcjar / org / apache / log4j / helpers / AppenderAttachableImpl.java
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     
49     if(appenderList == null) {
50       appenderList = new Vector(1);
51     }
52     if(!appenderList.contains(newAppender)) {
53         appenderList.addElement(newAppender);
54     }
55   }
56
57   /**
58      Call the <code>doAppend</code> method on all attached appenders.  */
59   public
60   int appendLoopOnAppenders(LoggingEvent event) {
61     int size = 0;
62     Appender appender;
63
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);
69       }
70     }    
71     return size;
72   }
73
74
75   /**
76      Get all attached appenders as an Enumeration. If there are no
77      attached appenders <code>null</code> is returned.
78      
79      @return Enumeration An enumeration of attached appenders.
80    */
81   public
82   Enumeration getAllAppenders() {
83     if(appenderList == null) {
84         return null;
85     } else {
86         return appenderList.elements();
87     }    
88   }
89
90   /**
91      Look for an attached appender named as <code>name</code>.
92
93      <p>Return the appender with that name if in the list. Return null
94      otherwise.  
95      
96    */
97   public
98   Appender getAppender(String name) {
99      if(appenderList == null || name == null) {
100         return null;
101     }
102
103      int size = appenderList.size();
104      Appender appender;
105      for(int i = 0; i < size; i++) {
106        appender = (Appender) appenderList.elementAt(i);
107        if(name.equals(appender.getName())) {
108         return appender;
109     }
110      }
111      return null;    
112   }
113
114
115   /**
116      Returns <code>true</code> if the specified appender is in the
117      list of attached appenders, <code>false</code> otherwise.
118
119      @since 1.2 */
120   public 
121   boolean isAttached(Appender appender) {
122     if(appenderList == null || appender == null) {
123         return false;
124     }
125
126      int size = appenderList.size();
127      Appender a;
128      for(int i = 0; i < size; i++) {
129        a  = (Appender) appenderList.elementAt(i);
130        if(a == appender) {
131         return true;
132     }
133      }
134      return false;    
135   }
136
137
138
139   /**
140    * Remove and close all previously attached appenders.
141    * */
142   public
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);
148         a.close();
149       }
150       appenderList.removeAllElements();
151       appenderList = null;      
152     }
153   }
154
155
156   /**
157      Remove the appender passed as parameter form the list of attached
158      appenders.  */
159   public
160   void removeAppender(Appender appender) {
161     if(appender == null || appenderList == null) {
162         return;
163     }
164     appenderList.removeElement(appender);    
165   }
166
167
168  /**
169     Remove the appender with the name passed as parameter form the
170     list of appenders.  
171   */
172   public
173   void removeAppender(String name) {
174     if(name == null || appenderList == null) {
175         return;
176     }
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);
181          break;
182       }
183     }
184   }
185
186 }