1 22 package org.jboss.logging.filter; 23 24 25 26 import java.lang.reflect.Method ; 27 28 import java.net.URL ; 29 30 31 32 import org.apache.log4j.spi.Filter; 33 34 import org.apache.log4j.spi.LoggingEvent; 35 36 37 38 import org.jboss.util.collection.WeakSet; 39 40 41 42 81 82 public class TCLFilter extends Filter 83 84 { 85 86 87 88 private WeakSet matchSet = new WeakSet(); 89 90 91 92 private WeakSet missSet = new WeakSet(); 93 94 95 96 private String deployURL; 97 98 99 100 private boolean acceptOnMatch = true; 101 102 103 104 public boolean isAcceptOnMatch() 105 106 { 107 108 return acceptOnMatch; 109 110 } 111 112 public void setAcceptOnMatch(boolean acceptOnMatch) 113 114 { 115 116 this.acceptOnMatch = acceptOnMatch; 117 118 } 119 120 public String getDeployURL() 121 122 { 123 124 return deployURL; 125 126 } 127 128 public void setDeployURL(String deployURL) 129 130 { 131 132 this.deployURL = deployURL; 133 134 } 135 136 137 138 public int decide(LoggingEvent event) 139 140 { 141 142 int ok = Filter.DENY; 143 144 if( acceptOnMatch == true ) 145 146 { 147 148 ok = Filter.DENY; 149 150 if( isMatchingTCL() ) 151 152 ok = Filter.ACCEPT; 153 154 } 155 156 else 157 158 { 159 160 ok = Filter.ACCEPT; 161 162 if( isMatchingTCL() ) 163 164 ok = Filter.DENY; 165 166 } 167 168 return ok; 169 170 } 171 172 173 174 179 180 private boolean isMatchingTCL() 181 182 { 183 184 ClassLoader tcl = Thread.currentThread().getContextClassLoader(); 185 186 if( matchSet.contains(tcl) ) 187 188 return true; 189 190 if( missSet.contains(tcl) ) 191 192 return false; 193 194 195 196 198 ClassLoader cl = tcl; 199 200 boolean match = false; 201 202 while( cl != null ) 203 204 { 205 206 URL [] urls = getClassLoaderURLs(cl); 207 208 for(int n = 0; n < urls.length; n ++) 209 210 { 211 212 URL u = urls[n]; 213 214 String file = u.getFile(); 215 216 if( file.indexOf(deployURL) > 0 ) 217 218 { 219 220 match = true; 221 222 break; 223 224 } 225 226 } 227 228 cl = cl.getParent(); 229 230 } 231 232 if( match == true ) 233 234 matchSet.add(tcl); 235 236 else 237 238 missSet.add(tcl); 239 240 241 242 return match; 243 244 } 245 246 247 248 253 254 private static URL [] getClassLoaderURLs(ClassLoader cl) 255 256 { 257 258 URL [] urls = {}; 259 260 try 261 262 { 263 264 Class returnType = urls.getClass(); 265 266 Class [] parameterTypes = {}; 267 268 Method getURLs = cl.getClass().getMethod("getURLs", parameterTypes); 269 270 if( returnType.isAssignableFrom(getURLs.getReturnType()) ) 271 272 { 273 274 Object [] args = {}; 275 276 urls = (URL []) getURLs.invoke(cl, args); 277 278 } 279 280 if( urls == null || urls.length == 0 ) 281 282 { 283 284 getURLs = cl.getClass().getMethod("getClasspath", parameterTypes); 285 286 if( returnType.isAssignableFrom(getURLs.getReturnType()) ) 287 288 { 289 290 Object [] args = {}; 291 292 urls = (URL []) getURLs.invoke(cl, args); 293 294 } 295 296 } 297 298 } 299 300 catch(Exception ignore) 301 302 { 303 304 } 305 306 return urls; 307 308 } 309 310 311 312 } 313 314 315 316 | Popular Tags |