KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > archive > crawler > deciderules > DecideRuleSequence


1 /* RuleSequence
2 *
3 * $Id: DecideRuleSequence.java,v 1.6.14.1 2007/01/13 01:31:13 stack-sf Exp $
4 *
5 * Created on Mar 3, 2005
6 *
7 * Copyright (C) 2005 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.deciderules;
26
27 import java.util.Iterator JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import java.util.logging.Logger JavaDoc;
30
31 import javax.management.AttributeNotFoundException JavaDoc;
32
33 import org.archive.crawler.settings.MapType;
34
35 /**
36  * RuleSequence represents a series of Rules, which are applied in turn
37  * to give the final result. Rules return {@link DecideRule#ACCEPT},
38  * {@link DecideRule#REJECT}, or {@link DecideRule#PASS}. The final result
39  * of a DecideRuleSequence is that of the last rule decision made, either
40  * ACCEPT or REJECT (PASS is used by rules that do not have an opinion
41  * on a particular processing pass).
42  *
43  * @author gojomo
44  */

45 public class DecideRuleSequence extends DecideRule {
46
47     private static final long serialVersionUID = 8918111430698683110L;
48
49     private static final Logger JavaDoc logger =
50         Logger.getLogger(DecideRuleSequence.class.getName());
51
52     public static final String JavaDoc ATTR_RULES = "rules";
53     
54     public DecideRuleSequence(String JavaDoc name) {
55         super(name);
56         setDescription("DecideRuleSequence. Multiple DecideRules applied in " +
57             "order with last non-PASS the resulting 'decision'.");
58         
59         addElementToDefinition(new MapType(ATTR_RULES,
60                 "This is a list of DecideRules to be applied in sequence.",
61                 DecideRule.class));
62     }
63
64     public Object JavaDoc decisionFor(Object JavaDoc object) {
65         Object JavaDoc runningAnswer = PASS;
66         for(Iterator JavaDoc iter = getRules(object).iterator(object);
67                 iter.hasNext();) {
68             DecideRule r = (DecideRule)iter.next();
69             if(runningAnswer==r.singlePossibleNonPassDecision(object)) {
70                 // there's no chance this rule will change the decision;
71
continue;
72             }
73             Object JavaDoc answer = r.decisionFor(object);
74             if (logger.isLoggable(Level.FINE)) {
75                 logger.fine("Rule " + r.getName() + " of " + this.getName() +
76                     " decided " + answer + " on " + object);
77             }
78             if (answer != PASS) {
79                 runningAnswer = answer;
80             }
81         }
82         if (logger.isLoggable(Level.FINE)) {
83             logger.fine("Decision of " + this.getName() + " was " +
84                 runningAnswer);
85         }
86         return runningAnswer;
87     }
88
89     protected MapType getRules(Object JavaDoc o) {
90         MapType rules = null;
91         try {
92             rules = (MapType)getAttribute(o, ATTR_RULES);
93         } catch (AttributeNotFoundException JavaDoc e) {
94             logger.severe(e.getLocalizedMessage());
95         }
96         return rules;
97     }
98     
99     /* kick-update all constituent rules
100      * (non-Javadoc)
101      * @see org.archive.crawler.deciderules.DecideRule#kickUpdate()
102      */

103     public void kickUpdate() {
104         for(Iterator JavaDoc iter = getRules(null).iterator(null);
105                 iter.hasNext();) {
106             DecideRule r = (DecideRule)iter.next();
107             r.kickUpdate();
108         }
109     }
110 }
111
Popular Tags