1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package org.archive.crawler.framework;
26
27 import java.util.Iterator;
28 import java.util.logging.Logger;
29
30 import org.archive.crawler.settings.MapType;
31
32
33 /*** This class groups together a number of processors that logically fit
34 * together.
35 *
36 * @author John Erik Halse
37 */
38 public class ProcessorChain {
39 private static Logger logger =
40 Logger.getLogger("org.archive.crawler.framework.ProcessorChain");
41
42 private final MapType processorMap;
43 private ProcessorChain nextChain;
44 private Processor firstProcessor;
45
46 /*** Construct a new processor chain.
47 *
48 * @param processorMap a map of the processors belonging to this chain.
49 */
50 public ProcessorChain(MapType processorMap) {
51 this.processorMap = processorMap;
52
53 Processor previous = null;
54
55 for (Iterator it = processorMap.iterator(null); it.hasNext();) {
56 Processor p = (Processor) it.next();
57
58 if (previous == null) {
59 firstProcessor = p;
60 } else {
61 previous.setDefaultNextProcessor(p);
62 }
63
64 logger.info(
65 "Processor: " + p.getName() + " --> " + p.getClass().getName());
66
67 previous = p;
68 }
69 }
70
71 /*** Set the processor chain that the URI should be working through after
72 * finishing this one.
73 *
74 * @param nextProcessorChain the chain that should be processed after this
75 * one.
76 */
77 public void setNextChain(ProcessorChain nextProcessorChain) {
78 this.nextChain = nextProcessorChain;
79 }
80
81 /*** Get the processor chain that the URI should be working through after
82 * finishing this one.
83 *
84 * @return the next processor chain.
85 */
86 public ProcessorChain getNextProcessorChain() {
87 return nextChain;
88 }
89
90 /*** Get the first processor in the chain.
91 *
92 * @return the first processor in the chain.
93 */
94 public Processor getFirstProcessor() {
95 return firstProcessor;
96 }
97
98 /*** Get the first processor that is of class <code>classType</code> or a
99 * subclass of it.
100 *
101 * @param classType the class of the requested processor.
102 * @return the first processor matching the classType.
103 */
104 public Processor getProcessor(Class classType) {
105 for (Iterator it = processorMap.iterator(null); it.hasNext();) {
106 Processor p = (Processor) it.next();
107 if (classType.isInstance(p)) {
108 return p;
109 }
110 }
111 return null;
112 }
113
114 /*** Get the number of processors in this chain.
115 *
116 * @return the number of processors in this chain.
117 */
118 public int size() {
119 return processorMap.size(null);
120 }
121
122 /*** Get an iterator over the processors in this chain.
123 *
124 * @return an iterator over the processors in this chain.
125 */
126 public Iterator iterator() {
127 return processorMap.iterator(null);
128 }
129
130 public void kickUpdate() {
131 Iterator iter = iterator();
132 while(iter.hasNext()) {
133 Processor p = (Processor) iter.next();
134 p.kickUpdate();
135 }
136 }
137 }