Set the EarlyEncode flag on insert.
[jSite.git] / src / de / todesbaum / jsite / application / ProjectInserter.java
index 28bbcfa..cb0e830 100644 (file)
@@ -1,6 +1,5 @@
 /*
- * jSite - a tool for uploading websites into Freenet
- * Copyright (C) 2006 David Roden
+ * jSite - ProjectInserter.java - Copyright © 2006–2011 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
@@ -53,6 +52,7 @@ import de.todesbaum.util.freenet.fcp2.Verbosity;
 import de.todesbaum.util.io.Closer;
 import de.todesbaum.util.io.ReplacingOutputStream;
 import de.todesbaum.util.io.StreamCopier;
+import de.todesbaum.util.io.StreamCopier.ProgressListener;
 
 /**
  * Manages project inserts.
@@ -88,6 +88,15 @@ public class ProjectInserter implements FileScannerListener, Runnable {
        /** The temp directory. */
        private String tempDirectory;
 
+       /** The current connection. */
+       private Connection connection;
+
+       /** Whether the insert is cancelled. */
+       private volatile boolean cancelled = false;
+
+       /** Progress listener for payload transfers. */
+       private ProgressListener progressListener;
+
        /**
         * Adds a listener to the list of registered listeners.
         *
@@ -215,14 +224,31 @@ public class ProjectInserter implements FileScannerListener, Runnable {
 
        /**
         * Starts the insert.
+        *
+        * @param progressListener
+        *            Listener to notify on progress events
         */
-       public void start() {
+       public void start(ProgressListener progressListener) {
+               cancelled = false;
+               this.progressListener = progressListener;
                fileScanner = new FileScanner(project);
                fileScanner.addFileScannerListener(this);
                new Thread(fileScanner).start();
        }
 
        /**
+        * Stops the current insert.
+        */
+       public void stop() {
+               cancelled = true;
+               synchronized (lockObject) {
+                       if (connection != null) {
+                               connection.disconnect();
+                       }
+               }
+       }
+
+       /**
         * Creates an input stream that delivers the given file, replacing edition
         * tokens in the file’s content, if necessary.
         *
@@ -410,7 +436,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                        }
                }
                String indexFile = project.getIndexFile();
-               boolean hasIndexFile = (indexFile != null);
+               boolean hasIndexFile = (indexFile != null) && (indexFile.length() > 0);
                if (hasIndexFile && !project.getFileOption(indexFile).getContainer().equals("")) {
                        checkReport.addIssue("warning.container-index", false);
                }
@@ -420,7 +446,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                }
                Map<String, FileOption> fileOptions = project.getFileOptions();
                Set<Entry<String, FileOption>> fileOptionEntries = fileOptions.entrySet();
-               boolean insert = false;
+               boolean insert = fileOptionEntries.isEmpty();
                for (Entry<String, FileOption> fileOptionEntry : fileOptionEntries) {
                        String fileName = fileOptionEntry.getKey();
                        FileOption fileOption = fileOptionEntry.getValue();
@@ -462,7 +488,9 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                List<String> files = fileScanner.getFiles();
 
                /* create connection to node */
-               Connection connection = freenetInterface.getConnection("project-insert-" + random + counter++);
+               synchronized (lockObject) {
+                       connection = freenetInterface.getConnection("project-insert-" + random + counter++);
+               }
                connection.setTempDirectory(tempDirectory);
                boolean connected = false;
                Throwable cause = null;
@@ -472,8 +500,8 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                        cause = e1;
                }
 
-               if (!connected) {
-                       fireProjectInsertFinished(false, cause);
+               if (!connected || cancelled) {
+                       fireProjectInsertFinished(false, cancelled ? new AbortedException() : cause);
                        return;
                }
 
@@ -493,6 +521,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                }
                putDir.setVerbosity(Verbosity.ALL);
                putDir.setMaxRetries(-1);
+               putDir.setEarlyEncode(true);
                for (String filename : files) {
                        FileEntry fileEntry = createFileEntry(filename, edition, containerFiles);
                        if (fileEntry != null) {
@@ -507,7 +536,8 @@ public class ProjectInserter implements FileScannerListener, Runnable {
 
                /* start request */
                try {
-                       client.execute(putDir);
+                       client.execute(putDir, progressListener);
+                       fireProjectUploadFinished();
                } catch (IOException ioe1) {
                        fireProjectInsertFinished(false, ioe1);
                        return;
@@ -515,17 +545,12 @@ public class ProjectInserter implements FileScannerListener, Runnable {
 
                /* parse progress and success messages */
                String finalURI = null;
-               boolean firstMessage = true;
                boolean success = false;
                boolean finished = false;
                boolean disconnected = false;
-               while (!finished) {
+               while (!finished && !cancelled) {
                        Message message = client.readMessage();
                        finished = (message == null) || (disconnected = client.isDisconnected());
-                       if (firstMessage) {
-                               fireProjectUploadFinished();
-                               firstMessage = false;
-                       }
                        logger.log(Level.FINE, "Received message: " + message);
                        if (!finished) {
                                @SuppressWarnings("null")
@@ -555,7 +580,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                        project.setEdition(newEdition);
                        project.setLastInsertionTime(System.currentTimeMillis());
                }
-               fireProjectInsertFinished(success, disconnected ? new IOException("Connection terminated") : null);
+               fireProjectInsertFinished(success, cancelled ? new AbortedException() : (disconnected ? new IOException("Connection terminated") : null));
        }
 
        //