001: /* ProcessorChain
002: *
003: * $Id: ProcessorChain.java 4434 2006-08-04 04:02:39Z gojomo $
004: *
005: * Created on Mar 1, 2004
006: *
007: * Copyright (C) 2004 Internet Archive.
008: *
009: * This file is part of the Heritrix web crawler (crawler.archive.org).
010: *
011: * Heritrix is free software; you can redistribute it and/or modify
012: * it under the terms of the GNU Lesser Public License as published by
013: * the Free Software Foundation; either version 2.1 of the License, or
014: * any later version.
015: *
016: * Heritrix is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
019: * GNU Lesser Public License for more details.
020: *
021: * You should have received a copy of the GNU Lesser Public License
022: * along with Heritrix; if not, write to the Free Software
023: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024: */
025: package org.archive.crawler.framework;
026:
027: import java.util.Iterator;
028: import java.util.logging.Logger;
029:
030: import org.archive.crawler.settings.MapType;
031:
032: /** This class groups together a number of processors that logically fit
033: * together.
034: *
035: * @author John Erik Halse
036: */
037: public class ProcessorChain {
038: private static Logger logger = Logger
039: .getLogger("org.archive.crawler.framework.ProcessorChain");
040:
041: private final MapType processorMap;
042: private ProcessorChain nextChain;
043: private Processor firstProcessor;
044:
045: /** Construct a new processor chain.
046: *
047: * @param processorMap a map of the processors belonging to this chain.
048: */
049: public ProcessorChain(MapType processorMap) {
050: this .processorMap = processorMap;
051:
052: Processor previous = null;
053:
054: for (Iterator it = processorMap.iterator(null); it.hasNext();) {
055: Processor p = (Processor) it.next();
056:
057: if (previous == null) {
058: firstProcessor = p;
059: } else {
060: previous.setDefaultNextProcessor(p);
061: }
062:
063: logger.info("Processor: " + p.getName() + " --> "
064: + p.getClass().getName());
065:
066: previous = p;
067: }
068: }
069:
070: /** Set the processor chain that the URI should be working through after
071: * finishing this one.
072: *
073: * @param nextProcessorChain the chain that should be processed after this
074: * one.
075: */
076: public void setNextChain(ProcessorChain nextProcessorChain) {
077: this .nextChain = nextProcessorChain;
078: }
079:
080: /** Get the processor chain that the URI should be working through after
081: * finishing this one.
082: *
083: * @return the next processor chain.
084: */
085: public ProcessorChain getNextProcessorChain() {
086: return nextChain;
087: }
088:
089: /** Get the first processor in the chain.
090: *
091: * @return the first processor in the chain.
092: */
093: public Processor getFirstProcessor() {
094: return firstProcessor;
095: }
096:
097: /** Get the first processor that is of class <code>classType</code> or a
098: * subclass of it.
099: *
100: * @param classType the class of the requested processor.
101: * @return the first processor matching the classType.
102: */
103: public Processor getProcessor(Class classType) {
104: for (Iterator it = processorMap.iterator(null); it.hasNext();) {
105: Processor p = (Processor) it.next();
106: if (classType.isInstance(p)) {
107: return p;
108: }
109: }
110: return null;
111: }
112:
113: /** Get the number of processors in this chain.
114: *
115: * @return the number of processors in this chain.
116: */
117: public int size() {
118: return processorMap.size(null);
119: }
120:
121: /** Get an iterator over the processors in this chain.
122: *
123: * @return an iterator over the processors in this chain.
124: */
125: public Iterator iterator() {
126: return processorMap.iterator(null);
127: }
128:
129: public void kickUpdate() {
130: Iterator iter = iterator();
131: while (iter.hasNext()) {
132: Processor p = (Processor) iter.next();
133: p.kickUpdate();
134: }
135: }
136: }
|