View Javadoc

1   /* ProcessorChain
2    *
3    * $Id: ProcessorChain.java 4434 2006-08-04 04:02:39Z gojomo $
4    *
5    * Created on Mar 1, 2004
6    *
7    * Copyright (C) 2004 Internet Archive.
8    *
9    * This file is part of the Heritrix web crawler (crawler.archive.org).
10   *
11   * Heritrix is free software; you can redistribute it and/or modify
12   * it under the terms of the GNU Lesser Public License as published by
13   * the Free Software Foundation; either version 2.1 of the License, or
14   * any later version.
15   *
16   * Heritrix is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU Lesser Public License for more details.
20   *
21   * You should have received a copy of the GNU Lesser Public License
22   * along with Heritrix; if not, write to the Free Software
23   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 }