From: David ‘Bombe’ Roden Date: Thu, 17 Jan 2013 05:17:47 +0000 (+0100) Subject: Convert image insert events to EventBus-based events. X-Git-Tag: 0.8.5^2~3^2~91 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=9f3bbe5bb4450085d23bfa39c95da13267104916;p=Sone.git Convert image insert events to EventBus-based events. --- diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 8984cdd..e278629 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -35,10 +35,7 @@ import java.util.logging.Logger; import net.pterodactylus.sone.core.Options.DefaultOption; import net.pterodactylus.sone.core.Options.Option; import net.pterodactylus.sone.core.Options.OptionWatcher; -import net.pterodactylus.sone.core.event.ImageInsertAbortedEvent; -import net.pterodactylus.sone.core.event.ImageInsertFailedEvent; import net.pterodactylus.sone.core.event.ImageInsertFinishedEvent; -import net.pterodactylus.sone.core.event.ImageInsertStartedEvent; import net.pterodactylus.sone.core.event.MarkPostKnownEvent; import net.pterodactylus.sone.core.event.MarkPostReplyKnownEvent; import net.pterodactylus.sone.core.event.MarkSoneKnownEvent; @@ -90,6 +87,7 @@ import net.pterodactylus.util.version.Version; import com.google.common.base.Predicate; import com.google.common.collect.Collections2; import com.google.common.eventbus.EventBus; +import com.google.common.eventbus.Subscribe; import com.google.inject.Inject; import freenet.keys.FreenetURI; @@ -99,7 +97,7 @@ import freenet.keys.FreenetURI; * * @author David ‘Bombe’ Roden */ -public class Core extends AbstractService implements IdentityListener, UpdateListener, SoneProvider, PostProvider, SoneInsertListener, ImageInsertListener { +public class Core extends AbstractService implements IdentityListener, UpdateListener, SoneProvider, PostProvider, SoneInsertListener { /** The logger. */ private static final Logger logger = Logging.getLogger(Core.class); @@ -223,7 +221,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis this.freenetInterface = freenetInterface; this.identityManager = identityManager; this.soneDownloader = new SoneDownloader(this, freenetInterface); - this.imageInserter = new ImageInserter(this, freenetInterface); + this.imageInserter = new ImageInserter(freenetInterface); this.updateChecker = new UpdateChecker(freenetInterface); this.webOfTrustUpdater = webOfTrustUpdater; this.eventBus = eventBus; @@ -2462,47 +2460,18 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis eventBus.post(new SoneInsertAbortedEvent(sone, cause)); } - // - // SONEINSERTLISTENER METHODS - // - /** - * {@inheritDoc} - */ - @Override - public void imageInsertStarted(Image image) { - logger.log(Level.WARNING, String.format("Image insert started for %s...", image)); - eventBus.post(new ImageInsertStartedEvent(image)); - } - - /** - * {@inheritDoc} - */ - @Override - public void imageInsertAborted(Image image) { - logger.log(Level.WARNING, String.format("Image insert aborted for %s.", image)); - eventBus.post(new ImageInsertAbortedEvent(image)); - } - - /** - * {@inheritDoc} + * Deletes the temporary image. + * + * @param imageInsertFinishedEvent + * The event */ - @Override - public void imageInsertFinished(Image image, FreenetURI key) { - logger.log(Level.WARNING, String.format("Image insert finished for %s: %s", image, key)); - image.setKey(key.toString()); - deleteTemporaryImage(image.getId()); + @Subscribe + public void imageInsertFinished(ImageInsertFinishedEvent imageInsertFinishedEvent) { + logger.log(Level.WARNING, String.format("Image insert finished for %s: %s", imageInsertFinishedEvent.image(), imageInsertFinishedEvent.resultingUri())); + imageInsertFinishedEvent.image().setKey(imageInsertFinishedEvent.resultingUri().toString()); + deleteTemporaryImage(imageInsertFinishedEvent.image().getId()); touchConfiguration(); - eventBus.post(new ImageInsertFinishedEvent(image)); - } - - /** - * {@inheritDoc} - */ - @Override - public void imageInsertFailed(Image image, Throwable cause) { - logger.log(Level.WARNING, String.format("Image insert failed for %s." + image), cause); - eventBus.post(new ImageInsertFailedEvent(image, cause)); } /** diff --git a/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java b/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java index 9515876..9394e93 100644 --- a/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java +++ b/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java @@ -18,20 +18,23 @@ package net.pterodactylus.sone.core; import java.net.MalformedURLException; -import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; -import java.util.List; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; +import net.pterodactylus.sone.core.event.ImageInsertAbortedEvent; +import net.pterodactylus.sone.core.event.ImageInsertFailedEvent; +import net.pterodactylus.sone.core.event.ImageInsertFinishedEvent; +import net.pterodactylus.sone.core.event.ImageInsertStartedEvent; import net.pterodactylus.sone.data.Image; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.data.TemporaryImage; import net.pterodactylus.util.logging.Logging; import com.db4o.ObjectContainer; +import com.google.common.eventbus.EventBus; import com.google.inject.Inject; import freenet.client.ClientMetadata; @@ -65,6 +68,9 @@ public class FreenetInterface { /** The logger. */ private static final Logger logger = Logging.getLogger(FreenetInterface.class); + /** The event bus. */ + private final EventBus eventBus; + /** The node to interact with. */ private final Node node; @@ -80,11 +86,14 @@ public class FreenetInterface { /** * Creates a new Freenet interface. * + * @param eventBus + * The event bus * @param node * The node to interact with */ @Inject - public FreenetInterface(Node node) { + public FreenetInterface(EventBus eventBus, Node node) { + this.eventBus = eventBus; this.node = node; this.client = node.clientCore.makeClient(RequestStarter.INTERACTIVE_PRIORITY_CLASS, false, true); } @@ -375,9 +384,12 @@ public class FreenetInterface { } /** - * Insert token that can be used to add {@link ImageInsertListener}s and - * cancel a running insert. + * Insert token that can cancel a running insert and sends events. * + * @see ImageInsertAbortedEvent + * @see ImageInsertStartedEvent + * @see ImageInsertFailedEvent + * @see ImageInsertFinishedEvent * @author David ‘Bombe’ Roden */ public class InsertToken implements ClientPutCallback { @@ -385,9 +397,6 @@ public class FreenetInterface { /** The image being inserted. */ private final Image image; - /** The list of registered image insert listeners. */ - private final List imageInsertListeners = Collections.synchronizedList(new ArrayList()); - /** The client putter. */ private ClientPutter clientPutter; @@ -405,30 +414,6 @@ public class FreenetInterface { } // - // LISTENER MANAGEMENT - // - - /** - * Adds the given listener to the list of registered listener. - * - * @param imageInsertListener - * The listener to add - */ - public void addImageInsertListener(ImageInsertListener imageInsertListener) { - imageInsertListeners.add(imageInsertListener); - } - - /** - * Removes the given listener from the list of registered listener. - * - * @param imageInsertListener - * The listener to remove - */ - public void removeImageInsertListener(ImageInsertListener imageInsertListener) { - imageInsertListeners.remove(imageInsertListener); - } - - // // ACCESSORS // @@ -436,15 +421,13 @@ public class FreenetInterface { * Sets the client putter that is inserting the image. This will also * signal all registered listeners that the image has started. * - * @see ImageInsertListener#imageInsertStarted(Image) * @param clientPutter * The client putter */ + @SuppressWarnings("synthetic-access") public void setClientPutter(ClientPutter clientPutter) { this.clientPutter = clientPutter; - for (ImageInsertListener imageInsertListener : imageInsertListeners) { - imageInsertListener.imageInsertStarted(image); - } + eventBus.post(new ImageInsertStartedEvent(image)); } // @@ -453,15 +436,11 @@ public class FreenetInterface { /** * Cancels the running insert. - * - * @see ImageInsertListener#imageInsertAborted(Image) */ @SuppressWarnings("synthetic-access") public void cancel() { clientPutter.cancel(null, node.clientCore.clientContext); - for (ImageInsertListener imageInsertListener : imageInsertListeners) { - imageInsertListener.imageInsertAborted(image); - } + eventBus.post(new ImageInsertAbortedEvent(image)); } // @@ -480,13 +459,12 @@ public class FreenetInterface { * {@inheritDoc} */ @Override + @SuppressWarnings("synthetic-access") public void onFailure(InsertException insertException, BaseClientPutter clientPutter, ObjectContainer objectContainer) { - for (ImageInsertListener imageInsertListener : imageInsertListeners) { - if ((insertException != null) && ("Cancelled by user".equals(insertException.getMessage()))) { - imageInsertListener.imageInsertAborted(image); - } else { - imageInsertListener.imageInsertFailed(image, insertException); - } + if ((insertException != null) && ("Cancelled by user".equals(insertException.getMessage()))) { + eventBus.post(new ImageInsertAbortedEvent(image)); + } else { + eventBus.post(new ImageInsertFailedEvent(image, insertException)); } } @@ -518,10 +496,9 @@ public class FreenetInterface { * {@inheritDoc} */ @Override + @SuppressWarnings("synthetic-access") public void onSuccess(BaseClientPutter clientPutter, ObjectContainer objectContainer) { - for (ImageInsertListener imageInsertListener : imageInsertListeners) { - imageInsertListener.imageInsertFinished(image, resultingUri); - } + eventBus.post(new ImageInsertFinishedEvent(image, resultingUri)); } } diff --git a/src/main/java/net/pterodactylus/sone/core/ImageInsertListener.java b/src/main/java/net/pterodactylus/sone/core/ImageInsertListener.java deleted file mode 100644 index ca1ab3e..0000000 --- a/src/main/java/net/pterodactylus/sone/core/ImageInsertListener.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Sone - ImageInsertListener.java - Copyright © 2011–2012 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 - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package net.pterodactylus.sone.core; - -import java.util.EventListener; - -import net.pterodactylus.sone.core.FreenetInterface.InsertToken; -import net.pterodactylus.sone.data.Image; -import freenet.keys.FreenetURI; - -/** - * Listener interface for objects that want to be notified about the status of - * an image insert. - * - * @see ImageInserter#insertImage(net.pterodactylus.sone.data.TemporaryImage, - * Image) - * @see FreenetInterface#insertImage(net.pterodactylus.sone.data.TemporaryImage, - * Image, net.pterodactylus.sone.core.FreenetInterface.InsertToken) - * @see InsertToken - * @author David ‘Bombe’ Roden - */ -public interface ImageInsertListener extends EventListener { - - /** - * Notifies a listener that the insert of the given image started. - * - * @param image - * The image that is being inserted - */ - public void imageInsertStarted(Image image); - - /** - * Notifies a listener that the insert of the given image was aborted by the - * user. - * - * @param image - * The image that is no longer being inserted - */ - public void imageInsertAborted(Image image); - - /** - * Notifies a listener that the given image was inserted successfully. - * - * @param image - * The image that was inserted - * @param key - * The final key of the image - */ - public void imageInsertFinished(Image image, FreenetURI key); - - /** - * Notifies a listener that the given image could not be inserted. - * - * @param image - * The image that could not be inserted - * @param cause - * The cause of the insertion failure - */ - public void imageInsertFailed(Image image, Throwable cause); - -} diff --git a/src/main/java/net/pterodactylus/sone/core/ImageInserter.java b/src/main/java/net/pterodactylus/sone/core/ImageInserter.java index 4fc7e89..4655c33 100644 --- a/src/main/java/net/pterodactylus/sone/core/ImageInserter.java +++ b/src/main/java/net/pterodactylus/sone/core/ImageInserter.java @@ -42,9 +42,6 @@ public class ImageInserter { /** The logger. */ private static final Logger logger = Logging.getLogger(ImageInserter.class); - /** The core. */ - private final Core core; - /** The freenet interface. */ private final FreenetInterface freenetInterface; @@ -54,19 +51,15 @@ public class ImageInserter { /** * Creates a new image inserter. * - * @param core - * The Sone core * @param freenetInterface * The freenet interface */ - public ImageInserter(Core core, FreenetInterface freenetInterface) { - this.core = core; + public ImageInserter(FreenetInterface freenetInterface) { this.freenetInterface = freenetInterface; } /** - * Inserts the given image. The {@link #core} will automatically added as - * {@link ImageInsertListener} to the created {@link InsertToken}. + * Inserts the given image. * * @param temporaryImage * The temporary image data @@ -78,7 +71,6 @@ public class ImageInserter { try { InsertToken insertToken = freenetInterface.new InsertToken(image); insertTokens.put(image.getId(), insertToken); - insertToken.addImageInsertListener(core); freenetInterface.insertImage(temporaryImage, image, insertToken); } catch (SoneException se1) { logger.log(Level.WARNING, "Could not insert image!", se1); @@ -98,7 +90,6 @@ public class ImageInserter { return; } insertToken.cancel(); - insertToken.removeImageInsertListener(core); } } diff --git a/src/main/java/net/pterodactylus/sone/core/event/ImageInsertFinishedEvent.java b/src/main/java/net/pterodactylus/sone/core/event/ImageInsertFinishedEvent.java index 0da0c82..53023b0 100644 --- a/src/main/java/net/pterodactylus/sone/core/event/ImageInsertFinishedEvent.java +++ b/src/main/java/net/pterodactylus/sone/core/event/ImageInsertFinishedEvent.java @@ -18,6 +18,7 @@ package net.pterodactylus.sone.core.event; import net.pterodactylus.sone.data.Image; +import freenet.keys.FreenetURI; /** * Event that signals that an {@link Image} insert is finished. @@ -26,14 +27,33 @@ import net.pterodactylus.sone.data.Image; */ public class ImageInsertFinishedEvent extends ImageEvent { + /** The URI of the image. */ + private final FreenetURI resultingUri; + /** * Creates a new “image insert finished” event. * * @param image * The image whose insert finished + * @param resultingUri + * The resulting URI of the image */ - public ImageInsertFinishedEvent(Image image) { + public ImageInsertFinishedEvent(Image image, FreenetURI resultingUri) { super(image); + this.resultingUri = resultingUri; + } + + // + // ACCESSORS + // + + /** + * Returns the URI of the image. + * + * @return The URI of the image + */ + public FreenetURI resultingUri() { + return resultingUri; } }