X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FSoneDownloaderImpl.java;h=43a87fbf4fbc0af2623945069022c8e80934cfde;hb=c00c2dea5bbe698a46c443d3610855720baa69a4;hp=9f0559cc84f0e85ba3b2353410af228af8bb1546;hpb=4b42574c9ec4490100ac3e87b9c2f3aac965a202;p=Sone.git
diff --git a/src/main/java/net/pterodactylus/sone/core/SoneDownloaderImpl.java b/src/main/java/net/pterodactylus/sone/core/SoneDownloaderImpl.java
index 9f0559c..43a87fb 100644
--- a/src/main/java/net/pterodactylus/sone/core/SoneDownloaderImpl.java
+++ b/src/main/java/net/pterodactylus/sone/core/SoneDownloaderImpl.java
@@ -1,5 +1,5 @@
/*
- * Sone - SoneDownloaderImpl.java - Copyright © 2010â2016 David Roden
+ * Sone - SoneDownloaderImpl.java - Copyright © 2010â2020 David Roden
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -17,7 +17,6 @@
package net.pterodactylus.sone.core;
-import static freenet.support.io.Closer.close;
import static java.lang.String.format;
import static java.lang.System.currentTimeMillis;
import static java.util.concurrent.TimeUnit.DAYS;
@@ -31,7 +30,6 @@ import java.util.logging.Logger;
import javax.inject.Inject;
-import net.pterodactylus.sone.core.FreenetInterface.Fetched;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.data.Sone.SoneStatus;
import net.pterodactylus.util.service.AbstractService;
@@ -44,12 +42,8 @@ import freenet.keys.USK;
import freenet.node.RequestStarter;
import freenet.support.api.Bucket;
-import com.google.common.annotations.VisibleForTesting;
-
/**
* The Sone downloader is responsible for download Sones as they are updated.
- *
- * @author David âBombeâ Roden
*/
public class SoneDownloaderImpl extends AbstractService implements SoneDownloader {
@@ -60,41 +54,19 @@ public class SoneDownloaderImpl extends AbstractService implements SoneDownloade
private static final int MAX_PROTOCOL_VERSION = 0;
/** The core. */
- private final Core core;
+ private final UpdatedSoneProcessor updatedSoneProcessor;
private final SoneParser soneParser;
/** The Freenet interface. */
private final FreenetInterface freenetInterface;
/** The sones to update. */
- private final Set sones = new HashSet();
+ private final Set sones = new HashSet<>();
- /**
- * Creates a new Sone downloader.
- *
- * @param core
- * The core
- * @param freenetInterface
- * The Freenet interface
- */
@Inject
- public SoneDownloaderImpl(Core core, FreenetInterface freenetInterface) {
- this(core, freenetInterface, new SoneParser(core));
- }
-
- /**
- * Creates a new Sone downloader.
- *
- * @param core
- * The core
- * @param freenetInterface
- * The Freenet interface
- * @param soneParser
- */
- @VisibleForTesting
- SoneDownloaderImpl(Core core, FreenetInterface freenetInterface, SoneParser soneParser) {
+ SoneDownloaderImpl(UpdatedSoneProcessor updatedSoneProcessor, FreenetInterface freenetInterface, SoneParser soneParser) {
super("Sone Downloader", false);
- this.core = core;
+ this.updatedSoneProcessor = updatedSoneProcessor;
this.freenetInterface = freenetInterface;
this.soneParser = soneParser;
}
@@ -188,7 +160,7 @@ public class SoneDownloaderImpl extends AbstractService implements SoneDownloade
if (parsedSone != null) {
if (!fetchOnly) {
parsedSone.setStatus((parsedSone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle);
- core.updateSone(parsedSone);
+ updatedSoneProcessor.updateSone(parsedSone);
addSone(parsedSone);
}
}
@@ -210,22 +182,19 @@ public class SoneDownloaderImpl extends AbstractService implements SoneDownloade
* @return The parsed Sone, or {@code null} if the Sone could not be parsed
*/
private Sone parseSone(Sone originalSone, FetchResult fetchResult, FreenetURI requestUri) {
- logger.log(Level.FINEST, String.format("Parsing FetchResult (%d bytes, %s) for %sâ¦", fetchResult.size(), fetchResult.getMimeType(), originalSone));
+ logger.finest(() -> format("Parsing FetchResult (%d bytes, %s) for %sâ¦", fetchResult.size(), fetchResult.getMimeType(), originalSone));
Bucket soneBucket = fetchResult.asBucket();
- InputStream soneInputStream = null;
- try {
- soneInputStream = soneBucket.getInputStream();
- Sone parsedSone = soneParser.parseSone(originalSone,
- soneInputStream);
+ try (InputStream soneInputStream = soneBucket.getInputStream()) {
+ Sone parsedSone = soneParser.parseSone(originalSone, soneInputStream);
if (parsedSone != null) {
+ logger.finer(() -> format("Sone %s was successfully parsed.", parsedSone));
parsedSone.setLatestEdition(requestUri.getEdition());
}
return parsedSone;
} catch (Exception e1) {
- logger.log(Level.WARNING, String.format("Could not parse Sone from %s!", requestUri), e1);
+ logger.log(Level.WARNING, e1, () -> format("Could not parse Sone from %s!", requestUri));
} finally {
- close(soneInputStream);
- close(soneBucket);
+ soneBucket.free();
}
return null;
}