1 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 ; 24 import java.util.Enumeration ; 25 26 32 public class AppenderAttachableImpl implements AppenderAttachable { 33 34 35 protected Vector appenderList; 36 37 41 public 42 void addAppender(Appender newAppender) { 43 if(newAppender == null) 45 return; 46 47 if(appenderList == null) { 48 appenderList = new Vector (1); 49 } 50 if(!appenderList.contains(newAppender)) 51 appenderList.addElement(newAppender); 52 } 53 54 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 78 public 79 Enumeration getAllAppenders() { 80 if(appenderList == null) 81 return null; 82 else 83 return appenderList.elements(); 84 } 85 86 93 public 94 Appender getAppender(String 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 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 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 151 public 152 void removeAppender(Appender appender) { 153 if(appender == null || appenderList == null) 154 return; 155 appenderList.removeElement(appender); 156 } 157 158 159 163 public 164 void removeAppender(String 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 |