--- /dev/null
+/*
+ * 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<InsertListener> insertListeners = new ArrayList<InsertListener>();
+
+ /**
+ * 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
+ * <code>true</code> if the total number of blocks has already been
+ * finalized, <code>false</code> 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
+ * <code>true</code> if the project was inserted successfully,
+ * <code>false</code> 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);
+ }
+ }
+
+}
/** Counter for FCP connection identifier. */
private static int counter = 0;
- /** The list of insert listeners. */
- private List<InsertListener> insertListeners = new ArrayList<InsertListener>();
+ private final ProjectInsertListeners projectInsertListeners = new ProjectInsertListeners();
/** The freenet interface. */
private Freenet7Interface freenetInterface;
* 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
- * <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) {
- 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) {
- insertListener.projectInsertFinished(project, success, cause);
- }
+ projectInsertListeners.addInsertListener(insertListener);
}
/**
*/
@Override
public void run() {
- fireProjectInsertStarted();
+ projectInsertListeners.fireProjectInsertStarted(project);
List<ScannedFile> files = fileScanner.getFiles();
/* create connection to node */
}
if (!connected || cancelled) {
- fireProjectInsertFinished(false, cancelled ? new AbortedException() : cause);
+ projectInsertListeners.fireProjectInsertFinished(project, false, cancelled ? new AbortedException() : cause);
return;
}
try {
putDir.addFileEntry(fileEntry);
} catch (IOException ioe1) {
- fireProjectInsertFinished(false, ioe1);
+ projectInsertListeners.fireProjectInsertFinished(project, false, ioe1);
return;
}
}
/* start request */
try {
client.execute(putDir, progressListener);
- fireProjectUploadFinished();
+ projectInsertListeners.fireProjectUploadFinished(project);
} catch (IOException ioe1) {
- fireProjectInsertFinished(false, ioe1);
+ projectInsertListeners.fireProjectInsertFinished(project, false, ioe1);
return;
}
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"));
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");
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));
}
//
if (!fileScanner.isError()) {
new Thread(this).start();
} else {
- fireProjectInsertFinished(false, null);
+ projectInsertListeners.fireProjectInsertFinished(project, false, null);
}
fileScanner.removeFileScannerListener(this);
}