Add missing @Override annotations.
[jSite.git] / src / de / todesbaum / jsite / application / ProjectInserter.java
index 9471175..a3fb0da 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * jSite - ProjectInserter.java - Copyright © 2006–2011 David Roden
+ * jSite - ProjectInserter.java - Copyright © 2006–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
@@ -28,8 +28,9 @@ import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
-import java.util.Set;
 import java.util.Map.Entry;
+import java.util.Set;
+import java.util.concurrent.CountDownLatch;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -43,6 +44,7 @@ import de.todesbaum.util.freenet.fcp2.Connection;
 import de.todesbaum.util.freenet.fcp2.DirectFileEntry;
 import de.todesbaum.util.freenet.fcp2.FileEntry;
 import de.todesbaum.util.freenet.fcp2.Message;
+import de.todesbaum.util.freenet.fcp2.PriorityClass;
 import de.todesbaum.util.freenet.fcp2.RedirectFileEntry;
 import de.todesbaum.util.freenet.fcp2.Verbosity;
 import de.todesbaum.util.io.StreamCopier.ProgressListener;
@@ -90,6 +92,15 @@ public class ProjectInserter implements FileScannerListener, Runnable {
        /** Progress listener for payload transfers. */
        private ProgressListener progressListener;
 
+       /** Whether to use “early encode.” */
+       private boolean useEarlyEncode;
+
+       /** The insert priority. */
+       private PriorityClass priority;
+
+       /** The manifest putter. */
+       private ManifestPutter manifestPutter;
+
        /**
         * Adds a listener to the list of registered listeners.
         *
@@ -216,6 +227,37 @@ public class ProjectInserter implements FileScannerListener, Runnable {
        }
 
        /**
+        * Sets whether to use the “early encode“ flag for the insert.
+        *
+        * @param useEarlyEncode
+        *            {@code true} to set the “early encode” flag for the insert,
+        *            {@code false} otherwise
+        */
+       public void setUseEarlyEncode(boolean useEarlyEncode) {
+               this.useEarlyEncode = useEarlyEncode;
+       }
+
+       /**
+        * Sets the insert priority.
+        *
+        * @param priority
+        *            The insert priority
+        */
+       public void setPriority(PriorityClass priority) {
+               this.priority = priority;
+       }
+
+       /**
+        * Sets the manifest putter to use for inserts.
+        *
+        * @param manifestPutter
+        *            The manifest putter to use
+        */
+       public void setManifestPutter(ManifestPutter manifestPutter) {
+               this.manifestPutter = manifestPutter;
+       }
+
+       /**
         * Starts the insert.
         *
         * @param progressListener
@@ -280,24 +322,23 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                String filename = file.getFilename();
                FileOption fileOption = project.getFileOption(filename);
                if (fileOption.isInsert()) {
+                       fileOption.setCurrentHash(file.getHash());
                        /* check if file was modified. */
-                       if (file.getHash().equals(fileOption.getLastInsertHash())) {
+                       if (!fileOption.isForceInsert() && file.getHash().equals(fileOption.getLastInsertHash())) {
                                /* only insert a redirect. */
-                               return new RedirectFileEntry(filename, fileOption.getMimeType(), "SSK@" + project.getRequestURI() + "/" + project.getPath() + "-" + fileOption.getLastInsertEdition() + "/" + filename);
+                               logger.log(Level.FINE, String.format("Inserting redirect to edition %d for %s.", fileOption.getLastInsertEdition(), filename));
+                               return new RedirectFileEntry(fileOption.hasChangedName() ? fileOption.getChangedName() : filename, fileOption.getMimeType(), "SSK@" + project.getRequestURI() + "/" + project.getPath() + "-" + fileOption.getLastInsertEdition() + "/" + fileOption.getLastInsertFilename());
                        }
-                       fileOption.setCurrentHash(file.getHash());
                        try {
                                long[] fileLength = new long[1];
                                InputStream fileEntryInputStream = createFileInputStream(filename, fileOption, edition, fileLength);
-                               fileEntry = new DirectFileEntry(filename, fileOption.getMimeType(), fileEntryInputStream, fileLength[0]);
+                               fileEntry = new DirectFileEntry(fileOption.hasChangedName() ? fileOption.getChangedName() : filename, fileOption.getMimeType(), fileEntryInputStream, fileLength[0]);
                        } catch (IOException ioe1) {
                                /* ignore, null is returned. */
                        }
                } else {
                        if (fileOption.isInsertRedirect()) {
-                               fileEntry = new RedirectFileEntry(filename, fileOption.getMimeType(), fileOption.getCustomKey());
-                       } else {
-                               fileOption.setLastInsertHash("");
+                               fileEntry = new RedirectFileEntry(fileOption.hasChangedName() ? fileOption.getChangedName() : filename, fileOption.getMimeType(), fileOption.getCustomKey());
                        }
                }
                return fileEntry;
@@ -367,12 +408,42 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                                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();
+               while (completionLatch.getCount() > 0) {
+                       try {
+                               completionLatch.await();
+                       } catch (InterruptedException ie1) {
+                               /* TODO: logging */
+                       }
+               }
+               for (ScannedFile scannedFile : fileScanner.getFiles()) {
+                       String fileName = scannedFile.getFilename();
+                       FileOption fileOption = project.getFileOption(fileName);
+                       if ((fileOption != null) && !fileOption.isInsert()) {
+                               continue;
+                       }
+                       totalSize += new File(project.getLocalPath(), fileName).length();
+               }
+               if (totalSize > 2 * 1024 * 1024) {
+                       checkReport.addIssue("warning.site-larger-than-2-mib", false);
+               }
                return checkReport;
        }
 
        /**
         * {@inheritDoc}
         */
+       @Override
        public void run() {
                fireProjectInsertStarted();
                List<ScannedFile> files = fileScanner.getFiles();
@@ -406,8 +477,9 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                }
                putDir.setVerbosity(Verbosity.ALL);
                putDir.setMaxRetries(-1);
-               putDir.setEarlyEncode(false);
-               putDir.setManifestPutter(ManifestPutter.DEFAULT);
+               putDir.setEarlyEncode(useEarlyEncode);
+               putDir.setPriorityClass(priority);
+               putDir.setManifestPutter(manifestPutter);
                for (ScannedFile file : files) {
                        FileEntry fileEntry = createFileEntry(file, edition);
                        if (fileEntry != null) {
@@ -465,7 +537,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                        int newEdition = Integer.parseInt(editionPart);
                        project.setEdition(newEdition);
                        project.setLastInsertionTime(System.currentTimeMillis());
-                       project.copyHashes();
+                       project.onSuccessfulInsert();
                }
                fireProjectInsertFinished(success, cancelled ? new AbortedException() : (disconnected ? new IOException("Connection terminated") : null));
        }
@@ -477,6 +549,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
        /**
         * {@inheritDoc}
         */
+       @Override
        public void fileScannerFinished(FileScanner fileScanner) {
                if (!fileScanner.isError()) {
                        new Thread(this).start();
@@ -526,6 +599,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                /**
                 * {@inheritDoc}
                 */
+               @Override
                public Iterator<Issue> iterator() {
                        return issues.iterator();
                }