KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > defaults > VerifyingVisitor


1 /*****************************************************************************
2  * Copyright (C) PicoContainer Organization. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  *****************************************************************************/

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 JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Set JavaDoc;
20
21
22 /**
23  * Visitor to verify {@link PicoContainer}instances. The visitor walks down the logical
24  * container hierarchy.
25  *
26  * @author Jörg Schaible
27  * @since 1.1
28  */

29 public class VerifyingVisitor
30         extends AbstractPicoVisitor {
31
32     private final List JavaDoc nestedVerificationExceptions;
33     private final Set JavaDoc verifiedComponentAdapters;
34     private final PicoVisitor componentAdapterCollector;
35     private PicoContainer currentPico;
36
37     public VerifyingVisitor() {
38         nestedVerificationExceptions = new ArrayList JavaDoc();
39         verifiedComponentAdapters = new HashSet JavaDoc();
40         componentAdapterCollector = new ComponentAdapterCollector();
41     }
42
43     /**
44      * Traverse through all components of the {@link PicoContainer} hierarchy and verify the
45      * components.
46      *
47      * @throws PicoVerificationException if some components could not be verified.
48      * @see org.picocontainer.PicoVisitor#traverse(java.lang.Object)
49      */

50     public Object JavaDoc traverse(Object JavaDoc 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 JavaDoc(nestedVerificationExceptions));
57             }
58         } finally {
59             nestedVerificationExceptions.clear();
60             verifiedComponentAdapters.clear();
61         }
62         return Void.TYPE;
63     }
64
65     /**
66      * {@inheritDoc}
67      *
68      * @see org.picocontainer.PicoVisitor#visitContainer(org.picocontainer.PicoContainer)
69      */

70     public void visitContainer(PicoContainer pico) {
71         checkTraversal();
72         currentPico = pico;
73     }
74
75     /**
76      * {@inheritDoc}
77      *
78      * @see org.picocontainer.PicoVisitor#visitComponentAdapter(org.picocontainer.ComponentAdapter)
79      */

80     public void visitComponentAdapter(ComponentAdapter componentAdapter) {
81         checkTraversal();
82         if (!verifiedComponentAdapters.contains(componentAdapter)) {
83             try {
84                 componentAdapter.verify(currentPico);
85             } catch (RuntimeException JavaDoc e) {
86                 nestedVerificationExceptions.add(e);
87             }
88             componentAdapter.accept(componentAdapterCollector);
89         }
90     }
91
92     /**
93      * {@inheritDoc}
94      *
95      * @see org.picocontainer.PicoVisitor#visitParameter(org.picocontainer.Parameter)
96      */

97     public void visitParameter(Parameter parameter) {
98         checkTraversal();
99     }
100
101     private class ComponentAdapterCollector
102             implements PicoVisitor {
103         ///CLOVER:OFF
104
public Object JavaDoc traverse(Object JavaDoc node) {
105             return null;
106         }
107
108         public void visitContainer(PicoContainer pico) {
109         }
110
111         ///CLOVER:ON
112

113         public void visitComponentAdapter(ComponentAdapter componentAdapter) {
114             verifiedComponentAdapters.add(componentAdapter);
115         }
116
117         public void visitParameter(Parameter parameter) {
118         }
119     }
120 }
121
Popular Tags