001: /*****************************************************************************
002: * Copyright (C) PicoContainer Organization. All rights reserved. *
003: * ------------------------------------------------------------------------- *
004: * The software in this package is published under the terms of the BSD *
005: * style license a copy of which has been included with this distribution in *
006: * the LICENSE.txt file. *
007: *****************************************************************************/package org.picocontainer.visitors;
008:
009: import org.picocontainer.ComponentAdapter;
010: import org.picocontainer.Parameter;
011: import org.picocontainer.PicoContainer;
012: import org.picocontainer.PicoVerificationException;
013: import org.picocontainer.PicoVisitor;
014: import org.picocontainer.visitors.TraversalCheckingVisitor;
015:
016: import java.util.ArrayList;
017: import java.util.HashSet;
018: import java.util.List;
019: import java.util.Set;
020:
021: /**
022: * Visitor to verify {@link PicoContainer} instances. The visitor walks down the logical container hierarchy.
023: *
024: * @author Jörg Schaible
025: */
026: public class VerifyingVisitor extends TraversalCheckingVisitor {
027:
028: private final List nestedVerificationExceptions;
029: private final Set verifiedComponentAdapters;
030: private final PicoVisitor componentAdapterCollector;
031: private PicoContainer currentPico;
032:
033: /**
034: * Construct a VerifyingVisitor.
035: */
036: public VerifyingVisitor() {
037: nestedVerificationExceptions = new ArrayList();
038: verifiedComponentAdapters = new HashSet();
039: componentAdapterCollector = new ComponentAdapterCollector();
040: }
041:
042: /**
043: * Traverse through all components of the {@link PicoContainer} hierarchy and verify the components.
044: *
045: * @throws PicoVerificationException if some components could not be verified.
046: * @see org.picocontainer.PicoVisitor#traverse(java.lang.Object)
047: */
048: public Object traverse(Object node)
049: throws PicoVerificationException {
050: nestedVerificationExceptions.clear();
051: verifiedComponentAdapters.clear();
052: try {
053: super .traverse(node);
054: if (!nestedVerificationExceptions.isEmpty()) {
055: throw new PicoVerificationException(new ArrayList(
056: nestedVerificationExceptions));
057: }
058: } finally {
059: nestedVerificationExceptions.clear();
060: verifiedComponentAdapters.clear();
061: }
062: return Void.TYPE;
063: }
064:
065: public void visitContainer(PicoContainer pico) {
066: super .visitContainer(pico);
067: currentPico = pico;
068: }
069:
070: public void visitComponentAdapter(ComponentAdapter componentAdapter) {
071: super .visitComponentAdapter(componentAdapter);
072: if (!verifiedComponentAdapters.contains(componentAdapter)) {
073: try {
074: componentAdapter.verify(currentPico);
075: } catch (RuntimeException e) {
076: nestedVerificationExceptions.add(e);
077: }
078: componentAdapter.accept(componentAdapterCollector);
079: }
080: }
081:
082: private class ComponentAdapterCollector implements PicoVisitor {
083: // /CLOVER:OFF
084: public Object traverse(Object node) {
085: return null;
086: }
087:
088: public void visitContainer(PicoContainer pico) {
089: }
090:
091: // /CLOVER:ON
092:
093: public void visitComponentAdapter(
094: ComponentAdapter componentAdapter) {
095: verifiedComponentAdapters.add(componentAdapter);
096: }
097:
098: public void visitParameter(Parameter parameter) {
099: }
100: }
101: }
|