2 * © 2009 David ‘Bombe’ Roden
4 package net.pterodactylus.arachne.core;
6 import java.net.MalformedURLException;
8 import java.security.NoSuchAlgorithmException;
9 import java.util.ArrayList;
10 import java.util.List;
11 import java.util.concurrent.Executor;
12 import java.util.concurrent.Executors;
13 import java.util.logging.Level;
14 import java.util.logging.Logger;
16 import net.pterodactylus.arachne.parser.ParserFactory;
17 import de.ina.util.service.AbstractService;
18 import de.ina.util.thread.DumpingThreadFactory;
19 import de.ina.util.validation.Validation;
24 * @author David ‘Bombe’ Roden <bombe@pterodactylus.net>
26 public class Core extends AbstractService {
29 private static final Logger logger = Logger.getLogger(Core.class.getName());
35 /** The host of the freenet node. */
36 private String nodeHost = "localhost";
38 /** The port of the freenet node. */
39 private int nodePort = 8888;
45 /** The parser factory. */
46 private ParserFactory parserFactory = new ParserFactory();
48 /** Thread pool for the URL fetches. */
49 private Executor urlFetcherExecutor = Executors.newFixedThreadPool(1, new DumpingThreadFactory("URLFetcher-"));
51 /** The current list of URLs to crawl. */
52 private final List<Page> pages = new ArrayList<Page>();
59 * Sets the host name of the node.
62 * The node’s host name
64 public void setNodeHost(String nodeHost) {
65 this.nodeHost = nodeHost;
73 * Adds the given URL to the list of pages to crawl.
78 public void addPage(URL url) {
79 Validation.begin().isNotNull("url", url).check().isEqual("url.getHost()", url.getHost(), (Object) nodeHost).isEqual("url.getPort()", url.getPort(), nodePort).check();
80 addPage(Page.fromURL(url));
84 * Adds the given URL to the list of pages to crawl.
87 * The URL of the page to crawl
88 * @throws MalformedURLException
89 * if the URL is not a valid URL
91 public void addPage(String url) throws MalformedURLException {
92 Validation.begin().isNotNull("url", (Object) url).check();
93 addPage(new URL(url));
97 * Adds the given page to the list of pages to crawl.
102 public void addPage(Page page) {
103 Validation.begin().isNotNull("page", page).check();
115 * @see de.ina.util.service.AbstractService#serviceRun()
118 @SuppressWarnings("null")
119 protected void serviceRun() {
120 while (!shouldStop()) {
121 Page nextPage = null;
122 synchronized (syncObject) {
123 while (!shouldStop() && pages.isEmpty()) {
126 } catch (InterruptedException ie1) {
131 nextPage = pages.remove(0);
137 URL nextURL = nextPage.toURL(nodeHost, nodePort);
138 if (nextURL == null) {
139 logger.log(Level.INFO, "Skipping “" + nextPage + "”.");
142 final URLFetcher urlFetcher;
144 logger.log(Level.INFO, "Fetching “" + nextURL + "”...");
145 urlFetcher = new URLFetcher(parserFactory, nextURL);
146 urlFetcherExecutor.execute(new Runnable() {
150 for (Page page : urlFetcher.getCollectedPages()) {
155 } catch (NoSuchAlgorithmException nsae1) {
156 logger.log(Level.SEVERE, "Could not get “SHA-256” message digest!", nsae1);