X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Ftodesbaum%2Fjsite%2Fapplication%2FProjectInserter.java;h=c7dad3b11f9d77b62d7cdb9a33cb50f6e1e6bc54;hb=cd351fe39bcbc8933187c3864e79e3dc896a374c;hp=1504a9fb7891868d7af617d2e962565ec73fbc45;hpb=f936411037a6ec1afbc5efbb17dc9b0136adda76;p=jSite.git diff --git a/src/main/java/de/todesbaum/jsite/application/ProjectInserter.java b/src/main/java/de/todesbaum/jsite/application/ProjectInserter.java index 1504a9f..c7dad3b 100644 --- a/src/main/java/de/todesbaum/jsite/application/ProjectInserter.java +++ b/src/main/java/de/todesbaum/jsite/application/ProjectInserter.java @@ -25,21 +25,23 @@ import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Optional; import java.util.Set; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; import java.util.logging.Logger; import net.pterodactylus.util.io.StreamCopier.ProgressListener; -import com.google.common.base.Optional; import de.todesbaum.jsite.gui.FileScanner; -import de.todesbaum.jsite.gui.FileScanner.ScannedFile; +import de.todesbaum.jsite.gui.ScannedFile; import de.todesbaum.jsite.gui.FileScannerListener; import de.todesbaum.util.freenet.fcp2.Client; import de.todesbaum.util.freenet.fcp2.ClientPutComplexDir; @@ -65,7 +67,7 @@ public class ProjectInserter implements FileScannerListener, Runnable { private static final int random = (int) (Math.random() * Integer.MAX_VALUE); /** Counter for FCP connection identifier. */ - private static int counter = 0; + private static final AtomicInteger counter = new AtomicInteger(); private final ProjectInsertListeners projectInsertListeners = new ProjectInsertListeners(); @@ -170,9 +172,8 @@ public class ProjectInserter implements FileScannerListener, Runnable { public void start(ProgressListener progressListener) { cancelled = false; this.progressListener = progressListener; - fileScanner = new FileScanner(project); - fileScanner.addFileScannerListener(this); - new Thread(fileScanner).start(); + fileScanner = new FileScanner(project, this); + fileScanner.startInBackground(); } /** @@ -195,7 +196,7 @@ public class ProjectInserter implements FileScannerListener, Runnable { * The name and hash of the file to insert * @return A file entry for the given file */ - private FileEntry createFileEntry(ScannedFile file) { + private Optional createFileEntry(ScannedFile file) { String filename = file.getFilename(); FileOption fileOption = project.getFileOption(filename); if (fileOption.isInsert()) { @@ -204,25 +205,25 @@ public class ProjectInserter implements FileScannerListener, Runnable { if (!project.isAlwaysForceInsert() && !fileOption.isForceInsert() && file.getHash().equals(fileOption.getLastInsertHash())) { /* only insert a redirect. */ logger.log(Level.FINE, String.format("Inserting redirect to edition %d for %s.", fileOption.getLastInsertEdition(), filename)); - return new RedirectFileEntry(fileOption.getChangedName().or(filename), fileOption.getMimeType(), "SSK@" + project.getRequestURI() + "/" + project.getPath() + "-" + fileOption.getLastInsertEdition() + "/" + fileOption.getLastInsertFilename()); + return Optional.of(new RedirectFileEntry(fileOption.getChangedName().orElse(filename), fileOption.getMimeType(), "SSK@" + project.getRequestURI() + "/" + project.getPath() + "-" + fileOption.getLastInsertEdition() + "/" + fileOption.getLastInsertFilename())); } try { - return createFileEntry(filename, fileOption.getChangedName(), fileOption.getMimeType()); + return Optional.of(createFileEntry(filename, fileOption.getChangedName(), fileOption.getMimeType())); } catch (IOException ioe1) { /* ignore, null is returned. */ } } else { if (fileOption.isInsertRedirect()) { - return new RedirectFileEntry(fileOption.getChangedName().or(filename), fileOption.getMimeType(), fileOption.getCustomKey()); + return Optional.of(new RedirectFileEntry(fileOption.getChangedName().orElse(filename), fileOption.getMimeType(), fileOption.getCustomKey())); } } - return null; + return Optional.empty(); } private FileEntry createFileEntry(String filename, Optional changedName, String mimeType) throws FileNotFoundException { File physicalFile = new File(project.getLocalPath(), filename); InputStream fileEntryInputStream = new FileInputStream(physicalFile); - return new DirectFileEntry(changedName.or(filename), mimeType, fileEntryInputStream, physicalFile.length()); + return new DirectFileEntry(changedName.orElse(filename), mimeType, fileEntryInputStream, physicalFile.length()); } /** @@ -280,23 +281,16 @@ public class ProjectInserter implements FileScannerListener, Runnable { logger.log(Level.FINEST, "Ignoring {0}.", fileOptionEntry.getKey()); continue; } - String fileName = fileOption.getChangedName().or(fileOptionEntry.getKey()); + String fileName = fileOption.getChangedName().orElse(fileOptionEntry.getKey()); logger.log(Level.FINEST, "Adding “{0}” for {1}.", new Object[] { fileName, fileOptionEntry.getKey() }); if (!fileNames.add(fileName)) { checkReport.addIssue("error.duplicate-file", true, fileName); } } long totalSize = 0; - FileScanner fileScanner = new FileScanner(project); final CountDownLatch completionLatch = new CountDownLatch(1); - fileScanner.addFileScannerListener(new FileScannerListener() { - - @Override - public void fileScannerFinished(FileScanner fileScanner) { - completionLatch.countDown(); - } - }); - new Thread(fileScanner).start(); + FileScanner fileScanner = new FileScanner(project, (error, files) -> completionLatch.countDown()); + fileScanner.startInBackground(); while (completionLatch.getCount() > 0) { try { completionLatch.await(); @@ -328,7 +322,7 @@ public class ProjectInserter implements FileScannerListener, Runnable { /* create connection to node */ synchronized (lockObject) { - connection = freenetInterface.getConnection("project-insert-" + random + counter++); + connection = freenetInterface.getConnection("project-insert-" + random + counter.getAndIncrement()); } connection.setTempDirectory(tempDirectory); boolean connected = false; @@ -349,7 +343,7 @@ public class ProjectInserter implements FileScannerListener, Runnable { /* collect files */ int edition = project.getEdition(); String dirURI = "USK@" + project.getInsertURI() + "/" + project.getPath() + "/" + edition + "/"; - ClientPutComplexDir putDir = new ClientPutComplexDir("dir-" + counter++, dirURI, tempDirectory); + ClientPutComplexDir putDir = new ClientPutComplexDir("dir-" + counter.getAndIncrement(), dirURI, tempDirectory); if ((project.getIndexFile() != null) && (project.getIndexFile().length() > 0)) { FileOption indexFileOption = project.getFileOption(project.getIndexFile()); Optional changedName = indexFileOption.getChangedName(); @@ -364,10 +358,10 @@ public class ProjectInserter implements FileScannerListener, Runnable { putDir.setEarlyEncode(useEarlyEncode); putDir.setPriorityClass(priority); for (ScannedFile file : files) { - FileEntry fileEntry = createFileEntry(file); - if (fileEntry != null) { + Optional fileEntry = createFileEntry(file); + if (fileEntry.isPresent()) { try { - putDir.addFileEntry(fileEntry); + putDir.addFileEntry(fileEntry.get()); } catch (IOException ioe1) { projectInsertListeners.fireProjectInsertFinished(project, false, ioe1); return; @@ -433,13 +427,12 @@ public class ProjectInserter implements FileScannerListener, Runnable { * {@inheritDoc} */ @Override - public void fileScannerFinished(FileScanner fileScanner) { - if (!fileScanner.isError()) { + public void fileScannerFinished(boolean error, Collection files) { + if (!error) { new Thread(this).start(); } else { projectInsertListeners.fireProjectInsertFinished(project, false, null); } - fileScanner.removeFileScannerListener(this); } /**