001: /* RuleSequence
002: *
003: * $Id: DecideRuleSequence.java 4912 2007-02-18 21:11:08Z gojomo $
004: *
005: * Created on Mar 3, 2005
006: *
007: * Copyright (C) 2005 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.deciderules;
026:
027: import java.util.Iterator;
028: import java.util.logging.Level;
029: import java.util.logging.Logger;
030:
031: import javax.management.AttributeNotFoundException;
032:
033: import org.archive.crawler.settings.MapType;
034:
035: /**
036: * RuleSequence represents a series of Rules, which are applied in turn
037: * to give the final result. Rules return {@link DecideRule#ACCEPT},
038: * {@link DecideRule#REJECT}, or {@link DecideRule#PASS}. The final result
039: * of a DecideRuleSequence is that of the last rule decision made, either
040: * ACCEPT or REJECT (PASS is used by rules that do not have an opinion
041: * on a particular processing pass).
042: *
043: * @author gojomo
044: */
045: public class DecideRuleSequence extends DecideRule {
046:
047: private static final long serialVersionUID = 8918111430698683110L;
048:
049: private static final Logger logger = Logger
050: .getLogger(DecideRuleSequence.class.getName());
051:
052: public static final String ATTR_RULES = "rules";
053:
054: public DecideRuleSequence(String name) {
055: this (
056: name,
057: "DecideRuleSequence. Multiple DecideRules applied in "
058: + "order with last non-PASS the resulting 'decision'.");
059: }
060:
061: public DecideRuleSequence(String name, String description) {
062: super (name);
063: setDescription(description);
064:
065: addElementToDefinition(new MapType(
066: ATTR_RULES,
067: "This is a list of DecideRules to be applied in sequence.",
068: DecideRule.class));
069: }
070:
071: public Object decisionFor(Object object) {
072: Object runningAnswer = PASS;
073: for (Iterator iter = getRules(object).iterator(object); iter
074: .hasNext();) {
075: DecideRule r = (DecideRule) iter.next();
076: if (runningAnswer == r
077: .singlePossibleNonPassDecision(object)) {
078: // there's no chance this rule will change the decision;
079: continue;
080: }
081: Object answer = r.decisionFor(object);
082: if (logger.isLoggable(Level.FINE)) {
083: logger.fine("Rule " + r.getName() + " of "
084: + this .getName() + " decided " + answer
085: + " on " + object);
086: }
087: if (answer != PASS) {
088: runningAnswer = answer;
089: }
090: }
091: if (logger.isLoggable(Level.FINE)) {
092: logger.fine("Decision of " + this .getName() + " was "
093: + runningAnswer);
094: }
095: return runningAnswer;
096: }
097:
098: protected MapType getRules(Object o) {
099: MapType rules = null;
100: try {
101: rules = (MapType) getAttribute(o, ATTR_RULES);
102: } catch (AttributeNotFoundException e) {
103: logger.severe(e.getLocalizedMessage());
104: }
105: return rules;
106: }
107:
108: /* kick-update all constituent rules
109: * (non-Javadoc)
110: * @see org.archive.crawler.deciderules.DecideRule#kickUpdate()
111: */
112: public void kickUpdate() {
113: for (Iterator iter = getRules(null).iterator(null); iter
114: .hasNext();) {
115: DecideRule r = (DecideRule) iter.next();
116: r.kickUpdate();
117: }
118: }
119: }
|