From 06f1d210888120605ad968f5f5797205b0234a92 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 25 Nov 2013 22:24:32 +0100 Subject: [PATCH] Extract listener management into its own class. --- .../jsite/application/ProjectInsertListeners.java | 133 +++++++++++++++++++++ .../jsite/application/ProjectInserter.java | 107 ++--------------- 2 files changed, 144 insertions(+), 96 deletions(-) create mode 100644 src/main/java/de/todesbaum/jsite/application/ProjectInsertListeners.java diff --git a/src/main/java/de/todesbaum/jsite/application/ProjectInsertListeners.java b/src/main/java/de/todesbaum/jsite/application/ProjectInsertListeners.java new file mode 100644 index 0000000..6f5735e --- /dev/null +++ b/src/main/java/de/todesbaum/jsite/application/ProjectInsertListeners.java @@ -0,0 +1,133 @@ +/* + * jSite - ProjectInsertListeners.java - Copyright © 2013 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; + +import java.util.ArrayList; +import java.util.List; + +/** + * Manages {@link InsertListener}s for the {@link ProjectInserter}. + * + * @author David ‘Bombe’ Roden <bombe@freenetproject.org> + */ +class ProjectInsertListeners { + + /** The list of insert listeners. */ + private final List insertListeners = new ArrayList(); + + /** + * Adds a listener to the list of registered listeners. + * + * @param insertListener + * The listener to add + */ + void addInsertListener(InsertListener insertListener) { + insertListeners.add(insertListener); + } + + /** + * Removes a listener from the list of registered listeners. + * + * @param insertListener + * The listener to remove + */ + void removeInsertListener(InsertListener insertListener) { + insertListeners.remove(insertListener); + } + + /** + * Notifies all listeners that the project insert has started. + * + * @param project + * @see InsertListener#projectInsertStarted(Project) + */ + void fireProjectInsertStarted(Project project) { + for (InsertListener insertListener : insertListeners) { + insertListener.projectInsertStarted(project); + } + } + + /** + * Notifies all listeners that the insert has generated a URI. + * + * @param project + * @param uri + * The generated URI + * @see InsertListener#projectURIGenerated(Project, String) + */ + void fireProjectURIGenerated(Project project, String uri) { + for (InsertListener insertListener : insertListeners) { + insertListener.projectURIGenerated(project, uri); + } + } + + /** + * Notifies all listeners that the insert has made some progress. + * + * @param project + * @see InsertListener#projectUploadFinished(Project) + */ + void fireProjectUploadFinished(Project project) { + for (InsertListener insertListener : insertListeners) { + insertListener.projectUploadFinished(project); + } + } + + /** + * Notifies all listeners that the insert has made some progress. + * + * @param project + * @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 + * true if the total number of blocks has already been + * finalized, false otherwise + * @see InsertListener#projectInsertProgress(Project, int, int, int, int, + * boolean) + */ + void fireProjectInsertProgress(Project project, int succeeded, int failed, int fatal, int total, boolean finalized) { + for (InsertListener insertListener : insertListeners) { + insertListener.projectInsertProgress(project, succeeded, failed, fatal, total, finalized); + } + } + + /** + * Notifies all listeners the project insert has finished. + * + * @param project + * @param success + * true if the project was inserted successfully, + * false if it failed + * @param cause + * The cause of the failure, if any + * @see InsertListener#projectInsertFinished(Project, boolean, Throwable) + */ + void fireProjectInsertFinished(Project project, boolean success, Throwable cause) { + for (InsertListener insertListener : insertListeners) { + insertListener.projectInsertFinished(project, success, cause); + } + } + +} diff --git a/src/main/java/de/todesbaum/jsite/application/ProjectInserter.java b/src/main/java/de/todesbaum/jsite/application/ProjectInserter.java index 3a68ea0..df09ff0 100644 --- a/src/main/java/de/todesbaum/jsite/application/ProjectInserter.java +++ b/src/main/java/de/todesbaum/jsite/application/ProjectInserter.java @@ -68,8 +68,7 @@ public class ProjectInserter implements FileScannerListener, Runnable { /** Counter for FCP connection identifier. */ private static int counter = 0; - /** The list of insert listeners. */ - private List insertListeners = new ArrayList(); + private final ProjectInsertListeners projectInsertListeners = new ProjectInsertListeners(); /** The freenet interface. */ private Freenet7Interface freenetInterface; @@ -111,91 +110,7 @@ public class ProjectInserter implements FileScannerListener, Runnable { * 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) { - 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#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 - * 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 - * true if the total number of blocks has already - * been finalized, false otherwise - */ - protected void fireProjectInsertProgress(int succeeded, int failed, int fatal, int total, boolean finalized) { - 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 - * true if the project was inserted successfully, - * false if it failed - * @param cause - * The cause of the failure, if any - */ - protected void fireProjectInsertFinished(boolean success, Throwable cause) { - for (InsertListener insertListener : insertListeners) { - insertListener.projectInsertFinished(project, success, cause); - } + projectInsertListeners.addInsertListener(insertListener); } /** @@ -424,7 +339,7 @@ public class ProjectInserter implements FileScannerListener, Runnable { */ @Override public void run() { - fireProjectInsertStarted(); + projectInsertListeners.fireProjectInsertStarted(project); List files = fileScanner.getFiles(); /* create connection to node */ @@ -441,7 +356,7 @@ public class ProjectInserter implements FileScannerListener, Runnable { } if (!connected || cancelled) { - fireProjectInsertFinished(false, cancelled ? new AbortedException() : cause); + projectInsertListeners.fireProjectInsertFinished(project, false, cancelled ? new AbortedException() : cause); return; } @@ -465,7 +380,7 @@ public class ProjectInserter implements FileScannerListener, Runnable { try { putDir.addFileEntry(fileEntry); } catch (IOException ioe1) { - fireProjectInsertFinished(false, ioe1); + projectInsertListeners.fireProjectInsertFinished(project, false, ioe1); return; } } @@ -474,9 +389,9 @@ public class ProjectInserter implements FileScannerListener, Runnable { /* start request */ try { client.execute(putDir, progressListener); - fireProjectUploadFinished(); + projectInsertListeners.fireProjectUploadFinished(project); } catch (IOException ioe1) { - fireProjectInsertFinished(false, ioe1); + projectInsertListeners.fireProjectInsertFinished(project, false, ioe1); return; } @@ -494,7 +409,7 @@ public class ProjectInserter implements FileScannerListener, Runnable { String messageName = message.getName(); if ("URIGenerated".equals(messageName)) { finalURI = message.get("URI"); - fireProjectURIGenerated(finalURI); + projectInsertListeners.fireProjectURIGenerated(project, finalURI); } if ("SimpleProgress".equals(messageName)) { int total = Integer.parseInt(message.get("Total")); @@ -502,7 +417,7 @@ public class ProjectInserter implements FileScannerListener, Runnable { int fatal = Integer.parseInt(message.get("FatallyFailed")); int failed = Integer.parseInt(message.get("Failed")); boolean finalized = Boolean.parseBoolean(message.get("FinalizedTotal")); - fireProjectInsertProgress(succeeded, failed, fatal, total, finalized); + projectInsertListeners.fireProjectInsertProgress(project, succeeded, failed, fatal, total, finalized); } success |= "PutSuccessful".equals(messageName); finished = (success && (finalURI != null)) || "PutFailed".equals(messageName) || messageName.endsWith("Error"); @@ -518,7 +433,7 @@ public class ProjectInserter implements FileScannerListener, Runnable { project.setLastInsertionTime(System.currentTimeMillis()); project.onSuccessfulInsert(); } - fireProjectInsertFinished(success, cancelled ? new AbortedException() : (disconnected ? new IOException("Connection terminated") : null)); + projectInsertListeners.fireProjectInsertFinished(project, success, cancelled ? new AbortedException() : (disconnected ? new IOException("Connection terminated") : null)); } // @@ -533,7 +448,7 @@ public class ProjectInserter implements FileScannerListener, Runnable { if (!fileScanner.isError()) { new Thread(this).start(); } else { - fireProjectInsertFinished(false, null); + projectInsertListeners.fireProjectInsertFinished(project, false, null); } fileScanner.removeFileScannerListener(this); } -- 2.7.4