View Javadoc

1   /*
2   
3   This software is OSI Certified Open Source Software.
4   OSI Certified is a certification mark of the Open Source Initiative.
5   
6   The license (Mozilla version 1.0) can be read at the MMBase site.
7   See http://www.MMBase.org/license
8   
9   */
10  package net.sf.mmapps.modules.linkchecker.plugins;
11  import java.util.*;
12  
13  import net.sf.mmapps.modules.linkchecker.*;
14  import net.sf.mmapps.modules.linkchecker.plugins.linkchecker.LinkChecker;
15  
16  import org.apache.commons.logging.*;
17  
18  /***
19   * Reactor is the "glue" class that requests a list of urls , processes them , transforms them and mails the report 
20   * @author Kees Jongenburger
21   */
22  public class HTTPLinkCheckerPlugin implements Runnable, ReactorPlugin {
23  
24      private static Log log = LogFactory.getLog(HTTPLinkCheckerPlugin.class);
25  
26      private LinkChecker checker;
27      public final static String NUMBER_OF_THREADS_PROPERTY_NAME = "linkchecker.numberOfThreads";
28  
29      int numberOfThreads = 1;
30      private Vector links = new Vector();
31      private int total = 0;
32      private Vector linksOut = new Vector();
33  
34      public HTTPLinkCheckerPlugin() {}
35  
36      public void checkLinks() {
37          log.info("LinkChecker chekking links");
38          Thread[] threads = new Thread[numberOfThreads];
39  
40          for (int x = 0; x < threads.length; x++) {
41              threads[x] = new Thread(this);
42              threads[x].start();
43          }
44  
45          for (int x = 0; x < threads.length; x++) {
46              try {
47                  threads[x].join();
48              } catch (InterruptedException e) {
49                  log.warn("interuperd while waiting for thread to stop");
50              }
51          }
52          links.addAll(linksOut);
53      }
54  
55      public void run() {
56          while (links.size() > 0) {
57              Link link = null;
58              synchronized (links) {
59                  if (links.size() > 0) {
60                      link = (Link)links.remove(0);
61                  } else {
62                      return;
63                  }
64              }
65              if (link.mustCheck()) {
66                  checker.checkLink(link);
67              }
68              synchronized (linksOut) {
69                  linksOut.add(link);
70                  log.info(linksOut.size() + "/" + total + "  " + link.getURL() + " " + link.getStatus());
71              }
72          }
73      }
74  
75      public void init(Properties properties) {
76          checker = new LinkChecker(properties);
77          Properties p = checker.getProperties();
78          numberOfThreads = Integer.parseInt(p.getProperty(NUMBER_OF_THREADS_PROPERTY_NAME, "1"));
79          log.debug(NUMBER_OF_THREADS_PROPERTY_NAME + "=" + numberOfThreads);
80      }
81  
82      public Links handle(Links links, Properties properties) throws Exception {
83          this.links = links;
84          total = links.size();
85          checkLinks();
86          return links;
87      }
88  
89  }