2 * © 2009 David ‘Bombe’ Roden
4 package net.pterodactylus.arachne.core;
6 import java.net.MalformedURLException;
8 import java.util.ArrayList;
10 import java.util.concurrent.Executor;
11 import java.util.concurrent.Executors;
12 import java.util.logging.Level;
13 import java.util.logging.Logger;
15 import de.ina.util.service.AbstractService;
16 import de.ina.util.thread.DumpingThreadFactory;
17 import de.ina.util.validation.Validation;
22 * @author David ‘Bombe’ Roden <bombe@pterodactylus.net>
24 public class Core extends AbstractService {
27 private static final Logger logger = Logger.getLogger(Core.class.getName());
33 /** The host of the freenet node. */
34 private String nodeHost = "localhost";
36 /** The port of the freenet node. */
37 private int nodePort = 8888;
43 /** Thread pool for the URL fetches. */
44 private Executor urlFetcherExecutor = Executors.newFixedThreadPool(1, new DumpingThreadFactory("URLFetcher-"));
46 /** The current list of URLs to crawl. */
47 private final List<Page> pages = new ArrayList<Page>();
54 * Sets the host name of the node.
57 * The node’s host name
59 public void setNodeHost(String nodeHost) {
60 this.nodeHost = nodeHost;
68 * Adds the given URL to the list of pages to crawl.
73 public void addPage(URL url) {
74 Validation.begin().isNotNull("url", url).check().isEqual("url.getHost()", url.getHost(), (Object) nodeHost).isEqual("url.getPort()", url.getPort(), nodePort).check();
75 String path = url.getPath();
76 if (path.length() == 0) {
79 String[] pathComponents = path.split("/");
80 if (pathComponents.length < 2) {
81 throw new IllegalArgumentException("URL “" + url + "” is not a valid freenet page.");
83 String siteName = pathComponents[1];
84 String[] siteComponents = siteName.split("@");
85 if (siteComponents.length != 2) {
86 throw new IllegalArgumentException("siteName “" + siteName + "” is not a valid freenet page.");
88 if (!"USK".equals(siteComponents[0]) && !"SSK".equals(siteComponents[0]) && !"CHK".equals(siteComponents[0])) {
89 throw new IllegalArgumentException("siteName “" + siteName + "” is not a valid freenet page.");
91 if ("USK".equals(siteComponents[0])) {
92 Site site = new Site(siteComponents[1], pathComponents[2]);
93 Edition edition = new Edition(site, Integer.parseInt(pathComponents[3]));
94 Page page = new Page(edition, createPath(pathComponents, 4));
97 if ("SSK".equals(siteComponents[0])) {
98 int lastDash = pathComponents[2].lastIndexOf('-');
99 String basename = pathComponents[2].substring(0, lastDash);
100 int editionNumber = Integer.parseInt(pathComponents[2].substring(lastDash + 1));
101 Site site = new Site(siteComponents[1], basename);
102 Edition edition = new Edition(site, editionNumber);
103 Page page = new Page(edition, createPath(pathComponents, 3));
106 /* TODO: handle CHK */
110 * Adds the given URL to the list of pages to crawl.
113 * The URL of the page to crawl
114 * @throws MalformedURLException
115 * if the URL is not a valid URL
117 public void addPage(String url) throws MalformedURLException {
118 Validation.begin().isNotNull("url", (Object) url).check();
119 addPage(new URL(url));
123 * Adds the given page to the list of pages to crawl.
128 public void addPage(Page page) {
129 Validation.begin().isNotNull("page", page).check();
141 * @see de.ina.util.service.AbstractService#serviceRun()
144 protected void serviceRun() {
145 while (!shouldStop()) {
146 Page nextPage = null;
147 synchronized (syncObject) {
148 while (!shouldStop() && pages.isEmpty()) {
151 } catch (InterruptedException ie1) {
156 nextPage = pages.remove(0);
162 URL nextURL = createURL(nextPage);
163 if (nextURL == null) {
164 logger.log(Level.INFO, "Skipping “" + nextPage + "”.");
167 URLFetcher urlFetcher = new URLFetcher(this, nextURL);
168 urlFetcherExecutor.execute(urlFetcher);
177 * Creates a path from the given String array, starting at the given index.
178 * The path is created by joining all Strings from the array, separating
179 * them with a slash (‘/’).
181 * @param pathComponents
182 * The array of path components
184 * The index of the first path components
185 * @return The joined path
187 private String createPath(String[] pathComponents, int index) {
188 Validation.begin().isNotNull("pathComponents", pathComponents).check().isLessOrEqual("index", index, pathComponents.length).check();
189 StringBuilder path = new StringBuilder();
190 for (int pathComponentIndex = index; pathComponentIndex < pathComponents.length; pathComponentIndex++) {
191 if (path.length() > 0) {
194 path.append(pathComponents[pathComponentIndex]);
196 return path.toString();
200 * Creates a URL from the given page.
203 * The page to create a URL from
204 * @return The created URL, or <code>null</code> if the URL could not be
207 private URL createURL(Page page) {
209 return new URL("http://" + nodeHost + ":" + nodePort + "/SSK@" + page.getSite().getSite().getKey() + "/" + page.getSite().getSite().getBasename() + "-" + page.getSite().getEdition() + "/" + page.getPath());
210 } catch (MalformedURLException mue1) {
211 /* nearly impossible. */