KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > helpers > AppenderAttachableImpl


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.log4j.helpers;
18
19 import org.apache.log4j.spi.AppenderAttachable;
20 import org.apache.log4j.spi.LoggingEvent;
21
22 import org.apache.log4j.Appender;
23 import java.util.Vector JavaDoc;
24 import java.util.Enumeration JavaDoc;
25
26 /**
27    A straightforward implementation of the {@link AppenderAttachable}
28    interface.
29
30    @author Ceki Gülcü
31    @since version 0.9.1 */

32 public class AppenderAttachableImpl implements AppenderAttachable {
33   
34   /** Array of appenders. */
35   protected Vector JavaDoc appenderList;
36
37   /**
38      Attach an appender. If the appender is already in the list in
39      won't be added again.
40   */

41   public
42   void addAppender(Appender newAppender) {
43     // Null values for newAppender parameter are strictly forbidden.
44
if(newAppender == null)
45       return;
46     
47     if(appenderList == null) {
48       appenderList = new Vector JavaDoc(1);
49     }
50     if(!appenderList.contains(newAppender))
51       appenderList.addElement(newAppender);
52   }
53
54   /**
55      Call the <code>doAppend</code> method on all attached appenders. */

56   public
57   int appendLoopOnAppenders(LoggingEvent event) {
58     int size = 0;
59     Appender appender;
60
61     if(appenderList != null) {
62       size = appenderList.size();
63       for(int i = 0; i < size; i++) {
64     appender = (Appender) appenderList.elementAt(i);
65     appender.doAppend(event);
66       }
67     }
68     return size;
69   }
70
71
72   /**
73      Get all attached appenders as an Enumeration. If there are no
74      attached appenders <code>null</code> is returned.
75      
76      @return Enumeration An enumeration of attached appenders.
77    */

78   public
79   Enumeration JavaDoc getAllAppenders() {
80     if(appenderList == null)
81       return null;
82     else
83       return appenderList.elements();
84   }
85
86   /**
87      Look for an attached appender named as <code>name</code>.
88
89      <p>Return the appender with that name if in the list. Return null
90      otherwise.
91      
92    */

93   public
94   Appender getAppender(String JavaDoc name) {
95      if(appenderList == null || name == null)
96       return null;
97
98      int size = appenderList.size();
99      Appender appender;
100      for(int i = 0; i < size; i++) {
101        appender = (Appender) appenderList.elementAt(i);
102        if(name.equals(appender.getName()))
103       return appender;
104      }
105      return null;
106   }
107
108
109   /**
110      Returns <code>true</code> if the specified appender is in the
111      list of attached appenders, <code>false</code> otherwise.
112
113      @since 1.2 */

114   public
115   boolean isAttached(Appender appender) {
116     if(appenderList == null || appender == null)
117       return false;
118
119      int size = appenderList.size();
120      Appender a;
121      for(int i = 0; i < size; i++) {
122        a = (Appender) appenderList.elementAt(i);
123        if(a == appender)
124       return true;
125      }
126      return false;
127   }
128
129
130
131   /**
132    * Remove and close all previously attached appenders.
133    * */

134   public
135   void removeAllAppenders() {
136     if(appenderList != null) {
137       int len = appenderList.size();
138       for(int i = 0; i < len; i++) {
139     Appender a = (Appender) appenderList.elementAt(i);
140     a.close();
141       }
142       appenderList.removeAllElements();
143       appenderList = null;
144     }
145   }
146
147
148   /**
149      Remove the appender passed as parameter form the list of attached
150      appenders. */

151   public
152   void removeAppender(Appender appender) {
153     if(appender == null || appenderList == null)
154       return;
155     appenderList.removeElement(appender);
156   }
157
158
159  /**
160     Remove the appender with the name passed as parameter form the
161     list of appenders.
162   */

163   public
164   void removeAppender(String JavaDoc name) {
165     if(name == null || appenderList == null) return;
166     int size = appenderList.size();
167     for(int i = 0; i < size; i++) {
168       if(name.equals(((Appender)appenderList.elementAt(i)).getName())) {
169      appenderList.removeElementAt(i);
170      break;
171       }
172     }
173   }
174
175 }
176
Popular Tags