1 18 19 20 package org.apache.tomcat.util.digester; 21 22 import org.xml.sax.Attributes ; 23 24 25 35 36 public class FactoryCreateRule extends Rule { 37 38 40 41 private boolean ignoreCreateExceptions; 42 43 private ArrayStack exceptionIgnoredStack; 44 45 47 48 59 public FactoryCreateRule(Digester digester, String className) { 60 61 this(className); 62 63 } 64 65 66 77 public FactoryCreateRule(Digester digester, Class clazz) { 78 79 this(clazz); 80 81 } 82 83 84 98 public FactoryCreateRule(Digester digester, 99 String className, String attributeName) { 100 101 this(className, attributeName); 102 103 } 104 105 106 120 public FactoryCreateRule(Digester digester, 121 Class clazz, String attributeName) { 122 123 this(clazz, attributeName); 124 125 } 126 127 128 138 public FactoryCreateRule(Digester digester, 139 ObjectCreationFactory creationFactory) { 140 141 this(creationFactory); 142 143 } 144 145 154 public FactoryCreateRule(String className) { 155 156 this(className, false); 157 158 } 159 160 161 170 public FactoryCreateRule(Class clazz) { 171 172 this(clazz, false); 173 174 } 175 176 177 189 public FactoryCreateRule(String className, String attributeName) { 190 191 this(className, attributeName, false); 192 193 } 194 195 196 208 public FactoryCreateRule(Class clazz, String attributeName) { 209 210 this(clazz, attributeName, false); 211 212 } 213 214 215 223 public FactoryCreateRule(ObjectCreationFactory creationFactory) { 224 225 this(creationFactory, false); 226 227 } 228 229 239 public FactoryCreateRule(String className, boolean ignoreCreateExceptions) { 240 241 this(className, null, ignoreCreateExceptions); 242 243 } 244 245 246 256 public FactoryCreateRule(Class clazz, boolean ignoreCreateExceptions) { 257 258 this(clazz, null, ignoreCreateExceptions); 259 260 } 261 262 263 275 public FactoryCreateRule( 276 String className, 277 String attributeName, 278 boolean ignoreCreateExceptions) { 279 280 this.className = className; 281 this.attributeName = attributeName; 282 this.ignoreCreateExceptions = ignoreCreateExceptions; 283 284 } 285 286 287 299 public FactoryCreateRule( 300 Class clazz, 301 String attributeName, 302 boolean ignoreCreateExceptions) { 303 304 this(clazz.getName(), attributeName, ignoreCreateExceptions); 305 306 } 307 308 309 317 public FactoryCreateRule( 318 ObjectCreationFactory creationFactory, 319 boolean ignoreCreateExceptions) { 320 321 this.creationFactory = creationFactory; 322 this.ignoreCreateExceptions = ignoreCreateExceptions; 323 } 324 325 327 328 331 protected String attributeName = null; 332 333 334 338 protected String className = null; 339 340 341 346 protected ObjectCreationFactory creationFactory = null; 347 348 349 351 352 357 public void begin(String namespace, String name, Attributes attributes) throws Exception { 358 359 if (ignoreCreateExceptions) { 360 361 if (exceptionIgnoredStack == null) { 362 exceptionIgnoredStack = new ArrayStack(); 363 } 364 365 try { 366 Object instance = getFactory(attributes).createObject(attributes); 367 368 if (digester.log.isDebugEnabled()) { 369 digester.log.debug("[FactoryCreateRule]{" + digester.match + 370 "} New " + instance.getClass().getName()); 371 } 372 digester.push(instance); 373 exceptionIgnoredStack.push(Boolean.FALSE); 374 375 } catch (Exception e) { 376 if (digester.log.isInfoEnabled()) { 378 digester.log.info("[FactoryCreateRule] Create exception ignored: " + 379 ((e.getMessage() == null) ? e.getClass().getName() : e.getMessage())); 380 if (digester.log.isDebugEnabled()) { 381 digester.log.debug("[FactoryCreateRule] Ignored exception:", e); 382 } 383 } 384 exceptionIgnoredStack.push(Boolean.TRUE); 385 } 386 387 } else { 388 Object instance = getFactory(attributes).createObject(attributes); 389 390 if (digester.log.isDebugEnabled()) { 391 digester.log.debug("[FactoryCreateRule]{" + digester.match + 392 "} New " + instance.getClass().getName()); 393 } 394 digester.push(instance); 395 } 396 } 397 398 399 402 public void end(String namespace, String name) throws Exception { 403 404 if ( 407 ignoreCreateExceptions && 408 exceptionIgnoredStack != null && 409 !(exceptionIgnoredStack.empty())) { 410 411 if (((Boolean ) exceptionIgnoredStack.pop()).booleanValue()) { 412 if (digester.log.isTraceEnabled()) { 415 digester.log.trace("[FactoryCreateRule] No creation so no push so no pop"); 416 } 417 return; 418 } 419 } 420 421 Object top = digester.pop(); 422 if (digester.log.isDebugEnabled()) { 423 digester.log.debug("[FactoryCreateRule]{" + digester.match + 424 "} Pop " + top.getClass().getName()); 425 } 426 427 } 428 429 430 433 public void finish() throws Exception { 434 435 if (attributeName != null) { 436 creationFactory = null; 437 } 438 439 } 440 441 442 445 public String toString() { 446 447 StringBuffer sb = new StringBuffer ("FactoryCreateRule["); 448 sb.append("className="); 449 sb.append(className); 450 sb.append(", attributeName="); 451 sb.append(attributeName); 452 if (creationFactory != null) { 453 sb.append(", creationFactory="); 454 sb.append(creationFactory); 455 } 456 sb.append("]"); 457 return (sb.toString()); 458 459 } 460 461 462 464 465 473 protected ObjectCreationFactory getFactory(Attributes attributes) 474 throws Exception { 475 476 if (creationFactory == null) { 477 String realClassName = className; 478 if (attributeName != null) { 479 String value = attributes.getValue(attributeName); 480 if (value != null) { 481 realClassName = value; 482 } 483 } 484 if (digester.log.isDebugEnabled()) { 485 digester.log.debug("[FactoryCreateRule]{" + digester.match + 486 "} New factory " + realClassName); 487 } 488 Class clazz = digester.getClassLoader().loadClass(realClassName); 489 creationFactory = (ObjectCreationFactory) 490 clazz.newInstance(); 491 creationFactory.setDigester(digester); 492 } 493 return (creationFactory); 494 495 } 496 } 497 | Popular Tags |