1 18 package org.objectweb.util.monolog.wrapper.log4j; 19 20 import org.apache.log4j.Appender; 21 import org.apache.log4j.Layout; 22 import org.apache.log4j.spi.ErrorHandler; 23 import org.apache.log4j.spi.Filter; 24 import org.apache.log4j.spi.LoggingEvent; 25 import org.objectweb.util.monolog.api.Handler; 26 27 import java.util.ArrayList ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 31 47 public class GenericHandler implements Appender, Handler { 48 49 52 public static final String APPENDER_CLASS_NAME_ATTR = "appenderClassName"; 53 54 57 protected Appender appender = null; 58 59 62 protected String name = null; 63 64 67 protected HashMap prop = null; 68 69 protected ArrayList filters = null; 70 protected Layout layout = null; 71 72 public GenericHandler() { 73 } 74 75 public GenericHandler(String name) { 76 this.name = name; 77 } 78 79 public GenericHandler(Appender a) { 80 appender = a; 81 prop = new HashMap (); 82 } 83 84 public Appender getAppender() { 85 return appender; 86 } 87 88 public void setAppender(Appender a) { 89 appender = a; 90 if (layout != null) { 91 appender.setLayout(layout); 92 } 93 if (filters != null) { 94 for (Iterator it = filters.iterator(); it.hasNext();) { 95 appender.addFilter((Filter) it.next()); 96 } 97 } 98 } 99 100 103 public String getName() { 104 return name = appender.getName(); 105 } 106 107 public void setName(String n) { 108 name = n; 109 appender.setName(n); 110 } 111 112 public String getType() { 113 return "generic"; 114 } 115 116 public String [] getAttributeNames() { 117 return (String []) prop.keySet().toArray(new String [0]); 118 } 119 120 public Object getAttribute(String key) { 121 return prop.get(key); 122 } 123 124 public Object setAttribute(String key, Object value) { 125 if (APPENDER_CLASS_NAME_ATTR.equalsIgnoreCase(key)) { 126 try { 127 setAppender((Appender) 128 Class.forName( (String ) value).newInstance()); 129 } 130 catch (Exception e) { 131 } 132 } 133 return null; 134 } 135 136 137 140 public void addFilter(Filter newFilter) { 141 if (appender != null) { 142 appender.addFilter(newFilter); 143 } 144 else { 145 if (filters == null) 146 filters = new ArrayList (); 147 filters.add(newFilter); 148 } 149 } 150 151 public void clearFilters() { 152 if (appender != null) { 153 appender.clearFilters(); 154 } 155 else { 156 if (filters != null) 157 filters.clear(); 158 } 159 } 160 161 public void close() { 162 if (appender != null) { 163 appender.close(); 164 } 165 } 166 167 public void doAppend(LoggingEvent event) { 168 if (appender != null) { 169 appender.doAppend(event); 170 } 171 } 172 173 public void setErrorHandler(ErrorHandler errorHandler) { 174 if (appender != null) { 175 appender.setErrorHandler(errorHandler); 176 } 177 } 178 179 public void setLayout(Layout layout) { 180 if (appender != null) { 181 appender.setLayout(layout); 182 } 183 else { 184 this.layout = layout; 185 } 186 } 187 188 public Filter getFilter() { 189 if (appender != null) { 190 return appender.getFilter(); 191 } 192 else if (filters != null && filters.size()>0) { 193 return (Filter) filters.get(0); 194 } 195 return null; 196 } 197 198 public ErrorHandler getErrorHandler() { 199 return (appender != null ? appender.getErrorHandler():null); 200 } 201 202 public Layout getLayout() { 203 return layout; 204 } 205 206 public boolean requiresLayout() { 207 return (appender != null ? appender.requiresLayout() : true); 208 } 209 } 210 | Popular Tags |