Treat an empty index file as no index file.
[jSite.git] / src / de / todesbaum / jsite / application / ProjectInserter.java
index 03072f8..7256bbe 100644 (file)
@@ -43,88 +43,182 @@ import de.todesbaum.util.freenet.fcp2.FileEntry;
 import de.todesbaum.util.freenet.fcp2.Message;
 import de.todesbaum.util.freenet.fcp2.RedirectFileEntry;
 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;
 
 /**
- * @author David Roden <droden@gmail.com>
- * @version $Id: ProjectInserter.java 440 2006-03-30 09:31:25Z bombe $
+ * 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;
+
+       /** Whether debug mode is set. */
        private boolean debug = false;
+
+       /** The list of insert listeners. */
        private List<InsertListener> insertListeners = new ArrayList<InsertListener>();
+
+       /** The freenet interface. */
        protected Freenet7Interface freenetInterface;
+
+       /** The project to insert. */
        protected Project project;
+
+       /** The file scanner. */
        private FileScanner fileScanner;
+
+       /** Object used for synchronization. */
        protected final Object lockObject = new Object();
-       private int maxRetries = 99999;
 
+       /**
+        * Adds a listener to the list of registered listeners.
+        *
+        * @param insertListener
+        *            The listener to add
+        */
        public void addInsertListener(InsertListener insertListener) {
                insertListeners.add(insertListener);
        }
 
+       /**
+        * Removes a listener from the list of registered listeners.
+        *
+        * @param insertListener
+        *            The listener to remove
+        */
        public void removeInsertListener(InsertListener insertListener) {
                insertListeners.remove(insertListener);
        }
 
+       /**
+        * Notifies all listeners that the project insert has started.
+        *
+        * @see InsertListener#projectInsertStarted(Project)
+        */
        protected void fireProjectInsertStarted() {
-               for (InsertListener insertListener: insertListeners) {
+               for (InsertListener insertListener : insertListeners) {
                        insertListener.projectInsertStarted(project);
                }
        }
 
+       /**
+        * Notifies all listeners that the insert has generated a URI.
+        *
+        * @see InsertListener#projectURIGenerated(Project, String)
+        * @param uri
+        *            The generated URI
+        */
+       protected void fireProjectURIGenerated(String uri) {
+               for (InsertListener insertListener : insertListeners) {
+                       insertListener.projectURIGenerated(project, uri);
+               }
+       }
+
+       /**
+        * Notifies all listeners that the insert has made some progress.
+        *
+        * @see InsertListener#projectInsertProgress(Project, int, int, int, int,
+        *      boolean)
+        * @param succeeded
+        *            The number of succeeded blocks
+        * @param failed
+        *            The number of failed blocks
+        * @param fatal
+        *            The number of fatally failed blocks
+        * @param total
+        *            The total number of blocks
+        * @param finalized
+        *            <code>true</code> if the total number of blocks has already
+        *            been finalized, <code>false</code> otherwise
+        */
        protected void fireProjectInsertProgress(int succeeded, int failed, int fatal, int total, boolean finalized) {
-               for (InsertListener insertListener: insertListeners) {
+               for (InsertListener insertListener : insertListeners) {
                        insertListener.projectInsertProgress(project, succeeded, failed, fatal, total, finalized);
                }
        }
 
+       /**
+        * 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,
+        *            <code>false</code> if it failed
+        * @param cause
+        *            The cause of the failure, if any
+        */
        protected void fireProjectInsertFinished(boolean success, Throwable cause) {
-               for (InsertListener insertListener: insertListeners) {
+               for (InsertListener insertListener : insertListeners) {
                        insertListener.projectInsertFinished(project, success, cause);
                }
        }
 
        /**
+        * Sets the debug mode.
+        *
         * @param debug
-        *            The debug to set.
+        *            <code>true</code> to activate debug mode, <code>false</code>
+        *            to deactivate
         */
        public void setDebug(boolean debug) {
                this.debug = debug;
        }
 
        /**
+        * Sets the project to insert.
+        *
         * @param project
-        *            The project to set.
+        *            The project to insert
         */
        public void setProject(Project project) {
                this.project = project;
        }
 
        /**
+        * Sets the freenet interface to use.
+        *
         * @param freenetInterface
-        *            The freenetInterface to set.
+        *            The freenet interface to use
         */
        public void setFreenetInterface(Freenet7Interface freenetInterface) {
                this.freenetInterface = freenetInterface;
        }
 
        /**
-        * @param maxRetries
-        *            The maxRetries to set.
+        * Starts the insert.
         */
-       public void setMaxRetries(int maxRetries) {
-               this.maxRetries = maxRetries;
-       }
-
        public void start() {
                fileScanner = new FileScanner(project);
                fileScanner.addFileScannerListener(this);
                new Thread(fileScanner).start();
        }
 
+       /**
+        * 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
+        *            The file options
+        * @param edition
+        *            The current edition
+        * @param length
+        *            An array containing a single long which is used to
+        *            <em>return</em> the final length of the file, after all
+        *            replacements
+        * @return The input stream for the file
+        * @throws IOException
+        *             if an I/O error occurs
+        */
        private InputStream createFileInputStream(String filename, FileOption fileOption, int edition, long[] length) throws IOException {
                File file = new File(project.getLocalPath(), filename);
                length[0] = file.length();
@@ -134,60 +228,84 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                ByteArrayOutputStream filteredByteOutputStream = new ByteArrayOutputStream(Math.min(Integer.MAX_VALUE, (int) length[0]));
                ReplacingOutputStream outputStream = new ReplacingOutputStream(filteredByteOutputStream);
                FileInputStream fileInput = new FileInputStream(file);
-               outputStream.addReplacement("$[CONTAINER]", "/");
-               outputStream.addReplacement("$[EDITION]", String.valueOf(edition));
-               outputStream.addReplacement("$[URI]", project.getFinalURI(0));
-               for (int index = 1; index <= fileOption.getEditionRange(); index++) {
-                       outputStream.addReplacement("$[URI+" + index + "]", project.getFinalURI(index));
-                       outputStream.addReplacement("$[EDITION+" + index + "]", String.valueOf(edition + index));
+               try {
+                       outputStream.addReplacement("$[EDITION]", String.valueOf(edition));
+                       outputStream.addReplacement("$[URI]", project.getFinalRequestURI(0));
+                       for (int index = 1; index <= fileOption.getEditionRange(); index++) {
+                               outputStream.addReplacement("$[URI+" + index + "]", project.getFinalRequestURI(index));
+                               outputStream.addReplacement("$[EDITION+" + index + "]", String.valueOf(edition + index));
+                       }
+                       StreamCopier.copy(fileInput, outputStream, length[0]);
+               } finally {
+                       Closer.close(fileInput);
+                       Closer.close(outputStream);
+                       Closer.close(filteredByteOutputStream);
                }
-               StreamCopier.copy(fileInput, outputStream, length[0]);
-               outputStream.close();
-               filteredByteOutputStream.close();
                byte[] filteredBytes = filteredByteOutputStream.toByteArray();
                length[0] = filteredBytes.length;
                return new ByteArrayInputStream(filteredBytes);
        }
 
+       /**
+        * Creates an input stream for a container.
+        *
+        * @param containerFiles
+        *            All container definitions
+        * @param containerName
+        *            The name of the container to create
+        * @param edition
+        *            The current edition
+        * @param containerLength
+        *            An array containing a single long which is used to
+        *            <em>return</em> the final length of the container stream,
+        *            after all replacements
+        * @return The input stream for the container
+        * @throws IOException
+        *             if an I/O error occurs
+        */
        private InputStream createContainerInputStream(Map<String, List<String>> containerFiles, String containerName, int edition, long[] containerLength) throws IOException {
                File tempFile = File.createTempFile("jsite", ".zip");
                tempFile.deleteOnExit();
                FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
                ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
-               for (String filename: containerFiles.get(containerName)) {
-                       File dataFile = new File(project.getLocalPath(), filename);
-                       if (dataFile.exists()) {
-                               ZipEntry zipEntry = new ZipEntry(filename);
-                               long[] fileLength = new long[1];
-                               InputStream wrappedInputStream = createFileInputStream(filename, project.getFileOption(filename), edition, fileLength);
-                               zipOutputStream.putNextEntry(zipEntry);
-                               StreamCopier.copy(wrappedInputStream, zipOutputStream, fileLength[0]);
-                               zipOutputStream.closeEntry();
-                               wrappedInputStream.close();
+               try {
+                       for (String filename : containerFiles.get(containerName)) {
+                               File dataFile = new File(project.getLocalPath(), filename);
+                               if (dataFile.exists()) {
+                                       ZipEntry zipEntry = new ZipEntry(filename);
+                                       long[] fileLength = new long[1];
+                                       InputStream wrappedInputStream = createFileInputStream(filename, project.getFileOption(filename), edition, fileLength);
+                                       try {
+                                               zipOutputStream.putNextEntry(zipEntry);
+                                               StreamCopier.copy(wrappedInputStream, zipOutputStream, fileLength[0]);
+                                       } finally {
+                                               zipOutputStream.closeEntry();
+                                               wrappedInputStream.close();
+                                       }
+                               }
                        }
+               } finally {
+                       zipOutputStream.closeEntry();
+                       Closer.close(zipOutputStream);
+                       Closer.close(fileOutputStream);
                }
-               zipOutputStream.closeEntry();
-
-               /* FIXME - create metadata */
-               // ZipEntry metadataEntry = new ZipEntry("metadata");
-               // zipOutputStream.putNextEntry(metadataEntry);
-               // Metadata zipMetadata = new Metadata();
-               // for (String filename: containerFiles.get(containerName)) {
-               // if (new File(project.getLocalPath(), filename).exists()) {
-               // DocumentMetadata zipEntryMetadata = new DocumentMetadata();
-               // zipEntryMetadata.setName(filename);
-               // zipEntryMetadata.setFormat(project.getFileOption(filename).getMimeType());
-               // zipMetadata.addDocument(zipEntryMetadata);
-               // }
-               // }
-               // zipOutputStream.write(zipMetadata.toByteArray());
-               // zipOutputStream.closeEntry();
-               zipOutputStream.close();
 
                containerLength[0] = tempFile.length();
                return new FileInputStream(tempFile);
        }
 
+       /**
+        * Creates a file entry suitable for handing in to
+        * {@link ClientPutComplexDir#addFileEntry(FileEntry)}.
+        *
+        * @param filename
+        *            The name of the file to insert
+        * @param edition
+        *            The current edition
+        * @param containerFiles
+        *            The container definitions
+        * @return A file entry for the given file
+        */
        private FileEntry createFileEntry(String filename, int edition, Map<String, List<String>> containerFiles) {
                FileEntry fileEntry = null;
                FileOption fileOption = project.getFileOption(filename);
@@ -198,6 +316,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                                InputStream containerInputStream = createContainerInputStream(containerFiles, containerName, edition, containerLength);
                                fileEntry = new DirectFileEntry(containerName + ".zip", "application/zip", containerInputStream, containerLength[0]);
                        } catch (IOException ioe1) {
+                               /* ignore, null is returned. */
                        }
                } else {
                        if (fileOption.isInsert()) {
@@ -206,6 +325,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                                        InputStream fileEntryInputStream = createFileInputStream(filename, fileOption, edition, fileLength);
                                        fileEntry = new DirectFileEntry(filename, project.getFileOption(filename).getMimeType(), fileEntryInputStream, fileLength[0]);
                                } catch (IOException ioe1) {
+                                       /* ignore, null is returned. */
                                }
                        } else {
                                fileEntry = new RedirectFileEntry(filename, fileOption.getMimeType(), fileOption.getCustomKey());
@@ -214,8 +334,18 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                return fileEntry;
        }
 
+       /**
+        * Creates container definitions.
+        *
+        * @param files
+        *            The list of all files
+        * @param containers
+        *            The list of all containers
+        * @param containerFiles
+        *            Empty map that will be filled with container definitions
+        */
        private void createContainers(List<String> files, List<String> containers, Map<String, List<String>> containerFiles) {
-               for (String filename: new ArrayList<String>(files)) {
+               for (String filename : new ArrayList<String>(files)) {
                        FileOption fileOption = project.getFileOption(filename);
                        String containerName = fileOption.getContainer();
                        if (!containerName.equals("")) {
@@ -239,13 +369,20 @@ 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 {
-                       connection.connect();
+                       connected = connection.connect();
                } catch (IOException e1) {
-                       fireProjectInsertFinished(false, e1);
+                       cause = e1;
+               }
+
+               if (!connected) {
+                       fireProjectInsertFinished(false, cause);
                        return;
                }
+
                Client client = new Client(connection);
 
                /* create containers */
@@ -254,13 +391,15 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                createContainers(files, containers, containerFiles);
 
                /* collect files */
-               int edition = ((EditionProject) project).getEdition();
-               String dirURI = project.getInsertURI() + project.getPath() + "-" + edition;
+               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(maxRetries);
-               for (String filename: files) {
+               putDir.setMaxRetries(-1);
+               for (String filename : files) {
                        FileEntry fileEntry = createFileEntry(filename, edition, containerFiles);
                        if (fileEntry != null) {
                                putDir.addFileEntry(fileEntry);
@@ -276,17 +415,23 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                }
 
                /* parse progress and success messages */
-               boolean success = true;
+               String finalURI = null;
+               boolean success = false;
                boolean finished = false;
                boolean disconnected = false;
                while (!finished) {
                        Message message = client.readMessage();
-                       finished = (message == null) && (disconnected = client.isDisconnected());
+                       finished = (message == null) || (disconnected = client.isDisconnected());
                        if (debug) {
                                System.out.println(message);
                        }
                        if (!finished) {
+                               @SuppressWarnings("null")
                                String messageName = message.getName();
+                               if ("URIGenerated".equals(messageName)) {
+                                       finalURI = message.get("URI");
+                                       fireProjectURIGenerated(finalURI);
+                               }
                                if ("SimpleProgress".equals(messageName)) {
                                        int total = Integer.parseInt(message.get("Total"));
                                        int succeeded = Integer.parseInt(message.get("Succeeded"));
@@ -296,16 +441,18 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                                        fireProjectInsertProgress(succeeded, failed, fatal, total, finalized);
                                }
                                success = "PutSuccessful".equals(messageName);
-                               finished = success || "PutFailed".equals(messageName);
+                               finished = success || "PutFailed".equals(messageName) || messageName.endsWith("Error");
                        }
                }
 
                /* post-insert work */
                fireProjectInsertFinished(success, disconnected ? new IOException("Connection terminated") : null);
                if (success) {
-                       if (project instanceof EditionProject) {
-                               ((EditionProject) project).setEdition(edition + 1);
-                       }
+                       @SuppressWarnings("null")
+                       String editionPart = finalURI.substring(finalURI.lastIndexOf('/') + 1);
+                       int newEdition = Integer.parseInt(editionPart);
+                       project.setEdition(newEdition);
+                       project.setLastInsertionTime(System.currentTimeMillis());
                }
        }