X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fde%2Ftodesbaum%2Fjsite%2Fapplication%2FProjectInserter.java;h=c6d0f85698a17758c2703218cada8fd94489208e;hb=3a538c657652e408b4507d59a8c3c48bbf00caad;hp=3d2457bd872b11a3b3e8b05ab59998c6a993de13;hpb=b697c40d075280114882b149a9d1f068766294c5;p=jSite.git diff --git a/src/de/todesbaum/jsite/application/ProjectInserter.java b/src/de/todesbaum/jsite/application/ProjectInserter.java index 3d2457b..c6d0f85 100644 --- a/src/de/todesbaum/jsite/application/ProjectInserter.java +++ b/src/de/todesbaum/jsite/application/ProjectInserter.java @@ -30,6 +30,8 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -54,12 +56,15 @@ import de.todesbaum.util.io.StreamCopier; */ public class ProjectInserter implements FileScannerListener, Runnable { + /** The logger. */ + private static final Logger logger = Logger.getLogger(ProjectInserter.class.getName()); + + /** 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 insertListeners = new ArrayList(); @@ -75,6 +80,9 @@ public class ProjectInserter implements FileScannerListener, Runnable { /** Object used for synchronization. */ protected final Object lockObject = new Object(); + /** The temp directory. */ + private String tempDirectory; + /** * Adds a listener to the list of registered listeners. * @@ -122,6 +130,17 @@ public class ProjectInserter implements FileScannerListener, Runnable { /** * Notifies all listeners that the insert has made some progress. * + * @see InsertListener#projectUploadFinished(Project) + */ + protected void fireProjectUploadFinished() { + for (InsertListener insertListener : insertListeners) { + insertListener.projectUploadFinished(project); + } + } + + /** + * Notifies all listeners that the insert has made some progress. + * * @see InsertListener#projectInsertProgress(Project, int, int, int, int, * boolean) * @param succeeded @@ -159,17 +178,6 @@ public class ProjectInserter implements FileScannerListener, Runnable { } /** - * Sets the debug mode. - * - * @param debug - * true to activate debug mode, false - * to deactivate - */ - public void setDebug(boolean debug) { - this.debug = debug; - } - - /** * Sets the project to insert. * * @param project @@ -190,6 +198,17 @@ public class ProjectInserter implements FileScannerListener, Runnable { } /** + * Sets the temp directory to use. + * + * @param tempDirectory + * The temp directory to use, or {@code null} to use the system + * default + */ + public void setTempDirectory(String tempDirectory) { + this.tempDirectory = tempDirectory; + } + + /** * Starts the insert. */ public void start() { @@ -261,7 +280,7 @@ public class ProjectInserter implements FileScannerListener, Runnable { * if an I/O error occurs */ private InputStream createContainerInputStream(Map> containerFiles, String containerName, int edition, long[] containerLength) throws IOException { - File tempFile = File.createTempFile("jsite", ".zip"); + File tempFile = File.createTempFile("jsite", ".zip", (tempDirectory == null) ? null : new File(tempDirectory)); tempFile.deleteOnExit(); FileOutputStream fileOutputStream = new FileOutputStream(tempFile); ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream); @@ -366,7 +385,8 @@ public class ProjectInserter implements FileScannerListener, Runnable { List files = fileScanner.getFiles(); /* create connection to node */ - Connection connection = freenetInterface.getConnection("project-insert-" + counter++); + Connection connection = freenetInterface.getConnection("project-insert-" + random + counter++); + connection.setTempDirectory(tempDirectory); boolean connected = false; Throwable cause = null; try { @@ -390,14 +410,21 @@ public class ProjectInserter implements FileScannerListener, Runnable { /* collect files */ int edition = project.getEdition(); String dirURI = "USK@" + project.getInsertURI() + "/" + project.getPath() + "/" + edition + "/"; - ClientPutComplexDir putDir = new ClientPutComplexDir("dir-" + counter++, dirURI); - putDir.setDefaultName(project.getIndexFile()); + ClientPutComplexDir putDir = new ClientPutComplexDir("dir-" + counter++, dirURI, tempDirectory); + if ((project.getIndexFile() != null) && (project.getIndexFile().length() > 0)) { + putDir.setDefaultName(project.getIndexFile()); + } putDir.setVerbosity(Verbosity.ALL); putDir.setMaxRetries(-1); for (String filename : files) { FileEntry fileEntry = createFileEntry(filename, edition, containerFiles); if (fileEntry != null) { - putDir.addFileEntry(fileEntry); + try { + putDir.addFileEntry(fileEntry); + } catch (IOException ioe1) { + fireProjectInsertFinished(false, ioe1); + return; + } } } @@ -411,16 +438,20 @@ 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) { Message message = client.readMessage(); finished = (message == null) || (disconnected = client.isDisconnected()); - if (debug) { - System.out.println(message); + if (firstMessage) { + fireProjectUploadFinished(); + firstMessage = false; } + logger.log(Level.FINE, "Received message: " + message); if (!finished) { + @SuppressWarnings("null") String messageName = message.getName(); if ("URIGenerated".equals(messageName)) { finalURI = message.get("URI"); @@ -442,6 +473,7 @@ 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);