1 8 package org.picocontainer.defaults; 9 10 import org.picocontainer.ComponentAdapter; 11 import org.picocontainer.Parameter; 12 import org.picocontainer.PicoContainer; 13 import org.picocontainer.PicoVerificationException; 14 import org.picocontainer.PicoVisitor; 15 16 import java.util.ArrayList ; 17 import java.util.HashSet ; 18 import java.util.List ; 19 import java.util.Set ; 20 21 22 29 public class VerifyingVisitor 30 extends AbstractPicoVisitor { 31 32 private final List nestedVerificationExceptions; 33 private final Set verifiedComponentAdapters; 34 private final PicoVisitor componentAdapterCollector; 35 private PicoContainer currentPico; 36 37 public VerifyingVisitor() { 38 nestedVerificationExceptions = new ArrayList (); 39 verifiedComponentAdapters = new HashSet (); 40 componentAdapterCollector = new ComponentAdapterCollector(); 41 } 42 43 50 public Object traverse(Object node) throws PicoVerificationException { 51 nestedVerificationExceptions.clear(); 52 verifiedComponentAdapters.clear(); 53 try { 54 super.traverse(node); 55 if (!nestedVerificationExceptions.isEmpty()) { 56 throw new PicoVerificationException(new ArrayList (nestedVerificationExceptions)); 57 } 58 } finally { 59 nestedVerificationExceptions.clear(); 60 verifiedComponentAdapters.clear(); 61 } 62 return Void.TYPE; 63 } 64 65 70 public void visitContainer(PicoContainer pico) { 71 checkTraversal(); 72 currentPico = pico; 73 } 74 75 80 public void visitComponentAdapter(ComponentAdapter componentAdapter) { 81 checkTraversal(); 82 if (!verifiedComponentAdapters.contains(componentAdapter)) { 83 try { 84 componentAdapter.verify(currentPico); 85 } catch (RuntimeException e) { 86 nestedVerificationExceptions.add(e); 87 } 88 componentAdapter.accept(componentAdapterCollector); 89 } 90 } 91 92 97 public void visitParameter(Parameter parameter) { 98 checkTraversal(); 99 } 100 101 private class ComponentAdapterCollector 102 implements PicoVisitor { 103 public Object traverse(Object node) { 105 return null; 106 } 107 108 public void visitContainer(PicoContainer pico) { 109 } 110 111 113 public void visitComponentAdapter(ComponentAdapter componentAdapter) { 114 verifiedComponentAdapters.add(componentAdapter); 115 } 116 117 public void visitParameter(Parameter parameter) { 118 } 119 } 120 } 121 | Popular Tags |