Treat an empty index file as no index file.
[jSite.git] / src / de / todesbaum / jsite / application / ProjectInserter.java
index a881196..7256bbe 100644 (file)
@@ -49,11 +49,14 @@ import de.todesbaum.util.io.StreamCopier;
 
 /**
  * Manages project inserts.
- * 
+ *
  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
  */
 public class ProjectInserter implements FileScannerListener, Runnable {
 
+       /** Random number for FCP instances. */
+       private static final int random = (int) (Math.random() * Integer.MAX_VALUE);
+
        /** Counter for FCP connection identifier. */
        private static int counter = 0;
 
@@ -77,7 +80,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
 
        /**
         * Adds a listener to the list of registered listeners.
-        * 
+        *
         * @param insertListener
         *            The listener to add
         */
@@ -87,7 +90,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
 
        /**
         * Removes a listener from the list of registered listeners.
-        * 
+        *
         * @param insertListener
         *            The listener to remove
         */
@@ -97,7 +100,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
 
        /**
         * Notifies all listeners that the project insert has started.
-        * 
+        *
         * @see InsertListener#projectInsertStarted(Project)
         */
        protected void fireProjectInsertStarted() {
@@ -108,7 +111,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
 
        /**
         * Notifies all listeners that the insert has generated a URI.
-        * 
+        *
         * @see InsertListener#projectURIGenerated(Project, String)
         * @param uri
         *            The generated URI
@@ -121,7 +124,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
 
        /**
         * Notifies all listeners that the insert has made some progress.
-        * 
+        *
         * @see InsertListener#projectInsertProgress(Project, int, int, int, int,
         *      boolean)
         * @param succeeded
@@ -144,7 +147,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
 
        /**
         * Notifies all listeners the project insert has finished.
-        * 
+        *
         * @see InsertListener#projectInsertFinished(Project, boolean, Throwable)
         * @param success
         *            <code>true</code> if the project was inserted successfully,
@@ -160,7 +163,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
 
        /**
         * Sets the debug mode.
-        * 
+        *
         * @param debug
         *            <code>true</code> to activate debug mode, <code>false</code>
         *            to deactivate
@@ -171,7 +174,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
 
        /**
         * Sets the project to insert.
-        * 
+        *
         * @param project
         *            The project to insert
         */
@@ -181,7 +184,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
 
        /**
         * Sets the freenet interface to use.
-        * 
+        *
         * @param freenetInterface
         *            The freenet interface to use
         */
@@ -201,7 +204,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
        /**
         * Creates an input stream that delivers the given file, replacing edition
         * tokens in the file’s content, if necessary.
-        * 
+        *
         * @param filename
         *            The name of the file
         * @param fileOption
@@ -245,7 +248,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
 
        /**
         * Creates an input stream for a container.
-        * 
+        *
         * @param containerFiles
         *            All container definitions
         * @param containerName
@@ -294,7 +297,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
        /**
         * Creates a file entry suitable for handing in to
         * {@link ClientPutComplexDir#addFileEntry(FileEntry)}.
-        * 
+        *
         * @param filename
         *            The name of the file to insert
         * @param edition
@@ -333,7 +336,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
 
        /**
         * Creates container definitions.
-        * 
+        *
         * @param files
         *            The list of all files
         * @param containers
@@ -366,7 +369,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                List<String> files = fileScanner.getFiles();
 
                /* create connection to node */
-               Connection connection = freenetInterface.getConnection("project-insert-" + counter++);
+               Connection connection = freenetInterface.getConnection("project-insert-" + random + counter++);
                boolean connected = false;
                Throwable cause = null;
                try {
@@ -391,7 +394,9 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                int edition = project.getEdition();
                String dirURI = "USK@" + project.getInsertURI() + "/" + project.getPath() + "/" + edition + "/";
                ClientPutComplexDir putDir = new ClientPutComplexDir("dir-" + counter++, dirURI);
-               putDir.setDefaultName(project.getIndexFile());
+               if ((project.getIndexFile() != null) && (project.getIndexFile().length() > 0)) {
+                       putDir.setDefaultName(project.getIndexFile());
+               }
                putDir.setVerbosity(Verbosity.ALL);
                putDir.setMaxRetries(-1);
                for (String filename : files) {
@@ -421,6 +426,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                                System.out.println(message);
                        }
                        if (!finished) {
+                               @SuppressWarnings("null")
                                String messageName = message.getName();
                                if ("URIGenerated".equals(messageName)) {
                                        finalURI = message.get("URI");
@@ -442,9 +448,11 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                /* post-insert work */
                fireProjectInsertFinished(success, disconnected ? new IOException("Connection terminated") : null);
                if (success) {
+                       @SuppressWarnings("null")
                        String editionPart = finalURI.substring(finalURI.lastIndexOf('/') + 1);
                        int newEdition = Integer.parseInt(editionPart);
                        project.setEdition(newEdition);
+                       project.setLastInsertionTime(System.currentTimeMillis());
                }
        }