From: David ‘Bombe’ Roden Date: Thu, 17 Jun 2010 17:47:54 +0000 (+0200) Subject: Add possibility to stop a running insert. X-Git-Tag: 0.9.2~5 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=ed54585ba9537da09379df378e51216e9ae528c1;p=jSite.git Add possibility to stop a running insert. --- diff --git a/src/de/todesbaum/jsite/application/AbortedException.java b/src/de/todesbaum/jsite/application/AbortedException.java new file mode 100644 index 0000000..bde6953 --- /dev/null +++ b/src/de/todesbaum/jsite/application/AbortedException.java @@ -0,0 +1,30 @@ +/* + * jSite - AbortedException.java - Copyright © 2010 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 + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package de.todesbaum.jsite.application; + +/** + * Marker exception that signals that the user aborted an insert. + * + * @author David ‘Bombe’ Roden <bombe@freenetproject.org> + */ +public class AbortedException extends Exception { + + /* nothing here. */ + +} diff --git a/src/de/todesbaum/jsite/application/ProjectInserter.java b/src/de/todesbaum/jsite/application/ProjectInserter.java index 32c9c5b..756e121 100644 --- a/src/de/todesbaum/jsite/application/ProjectInserter.java +++ b/src/de/todesbaum/jsite/application/ProjectInserter.java @@ -88,6 +88,12 @@ 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; + /** * Adds a listener to the list of registered listeners. * @@ -217,12 +223,25 @@ public class ProjectInserter implements FileScannerListener, Runnable { * Starts the insert. */ public void start() { + cancelled = false; 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. * @@ -462,7 +481,9 @@ public class ProjectInserter implements FileScannerListener, Runnable { List 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 +493,8 @@ public class ProjectInserter implements FileScannerListener, Runnable { cause = e1; } - if (!connected) { - fireProjectInsertFinished(false, cause); + if (!connected || cancelled) { + fireProjectInsertFinished(false, cancelled ? new AbortedException() : cause); return; } @@ -520,7 +541,7 @@ public class ProjectInserter implements FileScannerListener, Runnable { 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) { @@ -556,7 +577,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)); } //