MaxResourcesPerSiteRule.java
上传用户:qing5858
上传日期:2015-10-27
资源大小:6056k
文件大小:2k
源码类别:

搜索引擎

开发平台:

Java

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