2 * jSite - ProjectInserter.java - Copyright © 2006–2014 David Roden
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 2 of the License, or
7 * (at your option) any later version.
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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 package de.todesbaum.jsite.application;
22 import java.io.FileInputStream;
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.Collection;
27 import java.util.List;
28 import java.util.Optional;
29 import java.util.concurrent.atomic.AtomicInteger;
30 import java.util.logging.Level;
31 import java.util.logging.Logger;
33 import net.pterodactylus.util.io.StreamCopier.ProgressListener;
35 import de.todesbaum.jsite.gui.FileScanner;
36 import de.todesbaum.jsite.gui.ScannedFile;
37 import de.todesbaum.jsite.gui.FileScannerListener;
38 import de.todesbaum.util.freenet.fcp2.Client;
39 import de.todesbaum.util.freenet.fcp2.ClientPutComplexDir;
40 import de.todesbaum.util.freenet.fcp2.Connection;
41 import de.todesbaum.util.freenet.fcp2.DirectFileEntry;
42 import de.todesbaum.util.freenet.fcp2.FileEntry;
43 import de.todesbaum.util.freenet.fcp2.Message;
44 import de.todesbaum.util.freenet.fcp2.PriorityClass;
45 import de.todesbaum.util.freenet.fcp2.RedirectFileEntry;
46 import de.todesbaum.util.freenet.fcp2.Verbosity;
49 * Manages project inserts.
51 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
53 public class ProjectInserter implements FileScannerListener, Runnable {
56 private static final Logger logger = Logger.getLogger(ProjectInserter.class.getName());
58 /** Random number for FCP instances. */
59 private static final int random = (int) (Math.random() * Integer.MAX_VALUE);
61 /** Counter for FCP connection identifier. */
62 private static final AtomicInteger counter = new AtomicInteger();
64 private final ProjectInsertListeners projectInsertListeners = new ProjectInsertListeners();
66 /** The freenet interface. */
67 private Freenet7Interface freenetInterface;
69 /** The project to insert. */
70 private Project project;
72 /** The file scanner. */
73 private FileScanner fileScanner;
75 /** Object used for synchronization. */
76 private final Object lockObject = new Object();
78 /** The temp directory. */
79 private String tempDirectory;
81 /** The current connection. */
82 private Connection connection;
84 /** Whether the insert is cancelled. */
85 private volatile boolean cancelled = false;
87 /** Progress listener for payload transfers. */
88 private ProgressListener progressListener;
90 /** Whether to use “early encode.” */
91 private boolean useEarlyEncode;
93 /** The insert priority. */
94 private PriorityClass priority;
97 * Adds a listener to the list of registered listeners.
99 * @param insertListener
100 * The listener to add
102 public void addInsertListener(InsertListener insertListener) {
103 projectInsertListeners.addInsertListener(insertListener);
107 * Sets the project to insert.
110 * The project to insert
112 public void setProject(Project project) {
113 this.project = project;
117 * Sets the freenet interface to use.
119 * @param freenetInterface
120 * The freenet interface to use
122 public void setFreenetInterface(Freenet7Interface freenetInterface) {
123 this.freenetInterface = freenetInterface;
127 * Sets the temp directory to use.
129 * @param tempDirectory
130 * The temp directory to use, or {@code null} to use the system
133 public void setTempDirectory(String tempDirectory) {
134 this.tempDirectory = tempDirectory;
138 * Sets whether to use the “early encode“ flag for the insert.
140 * @param useEarlyEncode
141 * {@code true} to set the “early encode” flag for the insert,
142 * {@code false} otherwise
144 public void setUseEarlyEncode(boolean useEarlyEncode) {
145 this.useEarlyEncode = useEarlyEncode;
149 * Sets the insert priority.
152 * The insert priority
154 public void setPriority(PriorityClass priority) {
155 this.priority = priority;
161 * @param progressListener
162 * Listener to notify on progress events
164 public void start(ProgressListener progressListener) {
166 this.progressListener = progressListener;
167 fileScanner = new FileScanner(project, this);
168 fileScanner.startInBackground();
172 * Stops the current insert.
176 synchronized (lockObject) {
177 if (connection != null) {
178 connection.disconnect();
184 * Creates a file entry suitable for handing in to
185 * {@link ClientPutComplexDir#addFileEntry(FileEntry)}.
188 * The name and hash of the file to insert
189 * @return A file entry for the given file
191 private Optional<FileEntry> createFileEntry(ScannedFile file) {
192 String filename = file.getFilename();
193 FileOption fileOption = project.getFileOption(filename);
194 if (fileOption.isInsert()) {
195 fileOption.setCurrentHash(file.getHash());
196 /* check if file was modified. */
197 if (!project.isAlwaysForceInsert() && !fileOption.isForceInsert() && file.getHash().equals(fileOption.getLastInsertHash())) {
198 /* only insert a redirect. */
199 logger.log(Level.FINE, String.format("Inserting redirect to edition %d for %s.", fileOption.getLastInsertEdition(), filename));
200 return Optional.of(new RedirectFileEntry(fileOption.getChangedName().orElse(filename), fileOption.getMimeType(), "SSK@" + project.getRequestURI() + "/" + project.getPath() + "-" + fileOption.getLastInsertEdition() + "/" + fileOption.getLastInsertFilename()));
203 return Optional.of(createFileEntry(filename, fileOption.getChangedName(), fileOption.getMimeType()));
204 } catch (IOException ioe1) {
205 /* ignore, null is returned. */
208 if (fileOption.isInsertRedirect()) {
209 return Optional.of(new RedirectFileEntry(fileOption.getChangedName().orElse(filename), fileOption.getMimeType(), fileOption.getCustomKey()));
212 return Optional.empty();
215 private FileEntry createFileEntry(String filename, Optional<String> changedName, String mimeType) throws FileNotFoundException {
216 File physicalFile = new File(project.getLocalPath(), filename);
217 InputStream fileEntryInputStream = new FileInputStream(physicalFile);
218 return new DirectFileEntry(changedName.orElse(filename), mimeType, fileEntryInputStream, physicalFile.length());
226 projectInsertListeners.fireProjectInsertStarted(project);
227 List<ScannedFile> files = fileScanner.getFiles();
229 /* create connection to node */
230 synchronized (lockObject) {
231 connection = freenetInterface.getConnection("project-insert-" + random + counter.getAndIncrement());
233 connection.setTempDirectory(tempDirectory);
234 boolean connected = false;
235 Throwable cause = null;
237 connected = connection.connect();
238 } catch (IOException e1) {
242 if (!connected || cancelled) {
243 projectInsertListeners.fireProjectInsertFinished(project, false, cancelled ? new AbortedException() : cause);
247 Client client = new Client(connection);
250 int edition = project.getEdition();
251 String dirURI = "USK@" + project.getInsertURI() + "/" + project.getPath() + "/" + edition + "/";
252 ClientPutComplexDir putDir = new ClientPutComplexDir("dir-" + counter.getAndIncrement(), dirURI, tempDirectory);
253 if ((project.getIndexFile() != null) && (project.getIndexFile().length() > 0)) {
254 FileOption indexFileOption = project.getFileOption(project.getIndexFile());
255 Optional<String> changedName = indexFileOption.getChangedName();
256 if (changedName.isPresent()) {
257 putDir.setDefaultName(changedName.get());
259 putDir.setDefaultName(project.getIndexFile());
262 putDir.setVerbosity(Verbosity.ALL);
263 putDir.setMaxRetries(-1);
264 putDir.setEarlyEncode(useEarlyEncode);
265 putDir.setPriorityClass(priority);
266 for (ScannedFile file : files) {
267 Optional<FileEntry> fileEntry = createFileEntry(file);
268 if (fileEntry.isPresent()) {
270 putDir.addFileEntry(fileEntry.get());
271 } catch (IOException ioe1) {
272 projectInsertListeners.fireProjectInsertFinished(project, false, ioe1);
280 client.execute(putDir, progressListener);
281 projectInsertListeners.fireProjectUploadFinished(project);
282 } catch (IOException ioe1) {
283 projectInsertListeners.fireProjectInsertFinished(project, false, ioe1);
287 /* parse progress and success messages */
288 String finalURI = null;
289 boolean success = false;
290 boolean finished = false;
291 boolean disconnected = false;
292 while (!finished && !cancelled) {
293 Message message = client.readMessage();
294 finished = (message == null) || (disconnected = client.isDisconnected());
295 logger.log(Level.FINE, "Received message: " + message);
297 @SuppressWarnings("null")
298 String messageName = message.getName();
299 if ("URIGenerated".equals(messageName)) {
300 finalURI = message.get("URI");
301 projectInsertListeners.fireProjectURIGenerated(project, finalURI);
303 if ("SimpleProgress".equals(messageName)) {
304 int total = Integer.parseInt(message.get("Total"));
305 int succeeded = Integer.parseInt(message.get("Succeeded"));
306 int fatal = Integer.parseInt(message.get("FatallyFailed"));
307 int failed = Integer.parseInt(message.get("Failed"));
308 boolean finalized = Boolean.parseBoolean(message.get("FinalizedTotal"));
309 projectInsertListeners.fireProjectInsertProgress(project, succeeded, failed, fatal, total, finalized);
311 success |= "PutSuccessful".equals(messageName);
312 finished = (success && (finalURI != null)) || "PutFailed".equals(messageName) || messageName.endsWith("Error");
316 /* post-insert work */
318 @SuppressWarnings("null")
319 String editionPart = finalURI.substring(finalURI.lastIndexOf('/') + 1);
320 int newEdition = Integer.parseInt(editionPart);
321 project.setEdition(newEdition);
322 project.setLastInsertionTime(System.currentTimeMillis());
323 project.onSuccessfulInsert();
325 projectInsertListeners.fireProjectInsertFinished(project, success, cancelled ? new AbortedException() : (disconnected ? new IOException("Connection terminated") : null));
329 // INTERFACE FileScannerListener
336 public void fileScannerFinished(boolean error, Collection<ScannedFile> files) {
338 new Thread(this).start();
340 projectInsertListeners.fireProjectInsertFinished(project, false, null);