X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fde%2Ftodesbaum%2Fjsite%2Fapplication%2FProjectInserter.java;h=7b4d619c6323624af8b422094296a0f972a3bcca;hb=9d9bcc367fd6e0f22ec28764fe119ecca323ad22;hp=03072f82e2a12c1400d619d5433a1da616b0c746;hpb=47653a276ba2307820187ad126dfcacc147a105f;p=jSite.git diff --git a/src/de/todesbaum/jsite/application/ProjectInserter.java b/src/de/todesbaum/jsite/application/ProjectInserter.java index 03072f8..7b4d619 100644 --- a/src/de/todesbaum/jsite/application/ProjectInserter.java +++ b/src/de/todesbaum/jsite/application/ProjectInserter.java @@ -43,12 +43,13 @@ 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 $ + * @version $Id$ */ public class ProjectInserter implements FileScannerListener, Runnable { @@ -59,7 +60,6 @@ public class ProjectInserter implements FileScannerListener, Runnable { protected Project project; private FileScanner fileScanner; protected final Object lockObject = new Object(); - private int maxRetries = 99999; public void addInsertListener(InsertListener insertListener) { insertListeners.add(insertListener); @@ -74,6 +74,12 @@ public class ProjectInserter implements FileScannerListener, Runnable { insertListener.projectInsertStarted(project); } } + + protected void fireProjectURIGenerated(String uri) { + for (InsertListener insertListener: insertListeners) { + insertListener.projectURIGenerated(project, uri); + } + } protected void fireProjectInsertProgress(int succeeded, int failed, int fatal, int total, boolean finalized) { for (InsertListener insertListener: insertListeners) { @@ -111,14 +117,6 @@ public class ProjectInserter implements FileScannerListener, Runnable { this.freenetInterface = freenetInterface; } - /** - * @param maxRetries - * The maxRetries to set. - */ - public void setMaxRetries(int maxRetries) { - this.maxRetries = maxRetries; - } - public void start() { fileScanner = new FileScanner(project); fileScanner.addFileScannerListener(this); @@ -134,16 +132,19 @@ 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); @@ -154,35 +155,27 @@ public class ProjectInserter implements FileScannerListener, Runnable { 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); @@ -240,12 +233,19 @@ public class ProjectInserter implements FileScannerListener, Runnable { /* create connection to node */ Connection connection = freenetInterface.getConnection("project-insert-" + 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,12 +254,12 @@ 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 = "freenet:USK@" + project.getInsertURI() + "/" + project.getPath() + "/" + edition + "/"; ClientPutComplexDir putDir = new ClientPutComplexDir("dir-" + counter++, dirURI); putDir.setDefaultName(project.getIndexFile()); putDir.setVerbosity(Verbosity.ALL); - putDir.setMaxRetries(maxRetries); + putDir.setMaxRetries(-1); for (String filename: files) { FileEntry fileEntry = createFileEntry(filename, edition, containerFiles); if (fileEntry != null) { @@ -276,17 +276,22 @@ 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) { 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 +301,16 @@ 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); - } + String editionPart = finalURI.substring(finalURI.lastIndexOf('/') + 1); + int newEdition = Integer.parseInt(editionPart); + project.setEdition(newEdition); } }