Abandon logging that does not work with Freenet’s logger at all
[Sone.git] / src / main / java / net / pterodactylus / sone / core / ImageInserter.java
1 /*
2  * Sone - ImageInserter.java - Copyright © 2011–2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.core;
19
20 import static com.google.common.base.Preconditions.checkArgument;
21 import static com.google.common.base.Preconditions.checkNotNull;
22 import static java.util.logging.Logger.getLogger;
23
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29
30 import net.pterodactylus.sone.core.FreenetInterface.InsertToken;
31 import net.pterodactylus.sone.data.Image;
32 import net.pterodactylus.sone.data.TemporaryImage;
33
34 import com.google.common.base.Function;
35
36 /**
37  * The image inserter is responsible for inserting images using
38  * {@link FreenetInterface#insertImage(TemporaryImage, Image, InsertToken)} and
39  * also tracks running inserts, giving the possibility to abort a running
40  * insert.
41  *
42  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43  */
44 public class ImageInserter {
45
46         /** The logger. */
47         private static final Logger logger = getLogger(ImageInserter.class.getName());
48
49         /** The freenet interface. */
50         private final FreenetInterface freenetInterface;
51         private final Function<Image, InsertToken> insertTokenSupplier;
52
53         /** The tokens of running inserts. */
54         private final Map<String, InsertToken> insertTokens = Collections.synchronizedMap(new HashMap<String, InsertToken>());
55
56         /**
57          * Creates a new image inserter.
58          *
59          * @param freenetInterface
60          *            The freenet interface
61          * @param insertTokenSupplier
62          *            The supplier for insert tokens
63          */
64         public ImageInserter(FreenetInterface freenetInterface, Function<Image, InsertToken> insertTokenSupplier) {
65                 this.freenetInterface = freenetInterface;
66                 this.insertTokenSupplier = insertTokenSupplier;
67         }
68
69         /**
70          * Inserts the given image.
71          *
72          * @param temporaryImage
73          *            The temporary image data
74          * @param image
75          *            The image
76          */
77         public void insertImage(TemporaryImage temporaryImage, Image image) {
78                 checkNotNull(temporaryImage, "temporaryImage must not be null");
79                 checkNotNull(image, "image must not be null");
80                 checkArgument(image.getId().equals(temporaryImage.getId()), "image IDs must match");
81                 try {
82                         InsertToken insertToken = insertTokenSupplier.apply(image);
83                         insertTokens.put(image.getId(), insertToken);
84                         freenetInterface.insertImage(temporaryImage, image, insertToken);
85                 } catch (SoneException se1) {
86                         logger.log(Level.WARNING, "Could not insert image!", se1);
87                 }
88         }
89
90         /**
91          * Cancels a running image insert. If no insert is running for the given
92          * image, nothing happens.
93          *
94          * @param image
95          *            The image being inserted
96          */
97         public void cancelImageInsert(Image image) {
98                 InsertToken insertToken = insertTokens.remove(image.getId());
99                 if (insertToken == null) {
100                         return;
101                 }
102                 insertToken.cancel();
103         }
104
105 }