01: package net.javacoding.jspider.mod.rule;
02:
03: import net.javacoding.jspider.core.rule.impl.BaseRuleImpl;
04: import net.javacoding.jspider.core.util.config.PropertySet;
05: import net.javacoding.jspider.core.util.URLUtil;
06: import net.javacoding.jspider.core.SpiderContext;
07: import net.javacoding.jspider.core.model.DecisionInternal;
08: import net.javacoding.jspider.core.logging.LogFactory;
09: import net.javacoding.jspider.core.logging.Log;
10: import net.javacoding.jspider.api.model.Decision;
11: import net.javacoding.jspider.api.model.Site;
12:
13: import java.net.URL;
14: import java.util.Map;
15: import java.util.HashMap;
16:
17: /**
18: * $Id: MaxResourcesPerSiteRule.java,v 1.1 2003/04/07 15:51:00 vanrogu Exp $
19: */
20: public class MaxResourcesPerSiteRule extends BaseRuleImpl {
21:
22: public static final String MAX = "max";
23:
24: protected int max;
25: protected Map counters;
26:
27: public MaxResourcesPerSiteRule(PropertySet config) {
28: Log log = LogFactory.getLog(MaxResourcesPerSiteRule.class);
29: max = config.getInteger(MaxResourcesPerSiteRule.MAX, 0);
30: this .counters = new HashMap();
31: log.info("maximum resources per site set to " + max);
32: }
33:
34: public synchronized Decision apply(SpiderContext context,
35: Site currentSite, URL url) {
36: URL siteURL = URLUtil.getSiteURL(url);
37:
38: Integer counter = (Integer) counters.get(siteURL);
39: if (counter == null) {
40: counter = new Integer(0);
41: }
42:
43: Decision decision = null;
44:
45: if (counter.intValue() + 1 > max) {
46: decision = new DecisionInternal(Decision.RULE_IGNORE,
47: "counter for site is " + counter.intValue()
48: + ", max is " + max
49: + ", so not allowed anymore!");
50: } else {
51: decision = new DecisionInternal(Decision.RULE_ACCEPT,
52: "counter for site is " + counter.intValue()
53: + ", max is " + max + ", so allowed!");
54: counter = new Integer(counter.intValue() + 1);
55: counters.put(siteURL, counter);
56: }
57: return decision;
58: }
59:
60: }
|