/**
* Manages project inserts.
- *
+ *
* @author David ‘Bombe’ Roden <bombe@freenetproject.org>
*/
public class ProjectInserter implements FileScannerListener, Runnable {
+ /** 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;
/**
* Adds a listener to the list of registered listeners.
- *
+ *
* @param insertListener
* The listener to add
*/
/**
* Removes a listener from the list of registered listeners.
- *
+ *
* @param insertListener
* The listener to remove
*/
/**
* Notifies all listeners that the project insert has started.
- *
+ *
* @see InsertListener#projectInsertStarted(Project)
*/
protected void fireProjectInsertStarted() {
/**
* Notifies all listeners that the insert has generated a URI.
- *
+ *
* @see InsertListener#projectURIGenerated(Project, String)
* @param uri
* The generated URI
/**
* Notifies all listeners that the insert has made some progress.
- *
+ *
* @see InsertListener#projectInsertProgress(Project, int, int, int, int,
* boolean)
* @param succeeded
/**
* 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,
/**
* Sets the debug mode.
- *
+ *
* @param debug
* <code>true</code> to activate debug mode, <code>false</code>
* to deactivate
/**
* Sets the project to insert.
- *
+ *
* @param project
* The project to insert
*/
/**
* Sets the freenet interface to use.
- *
+ *
* @param freenetInterface
* The freenet interface to use
*/
/**
* Creates an input stream that delivers the given file, replacing edition
* tokens in the file’s content, if necessary.
- *
+ *
* @param filename
* The name of the file
* @param fileOption
/**
* Creates an input stream for a container.
- *
+ *
* @param containerFiles
* All container definitions
* @param containerName
/**
* Creates a file entry suitable for handing in to
* {@link ClientPutComplexDir#addFileEntry(FileEntry)}.
- *
+ *
* @param filename
* The name of the file to insert
* @param edition
/**
* Creates container definitions.
- *
+ *
* @param files
* The list of all files
* @param containers
List<String> files = fileScanner.getFiles();
/* create connection to node */
- Connection connection = freenetInterface.getConnection("project-insert-" + counter++);
+ Connection connection = freenetInterface.getConnection("project-insert-" + random + counter++);
boolean connected = false;
Throwable cause = null;
try {
int edition = project.getEdition();
String dirURI = "USK@" + project.getInsertURI() + "/" + project.getPath() + "/" + edition + "/";
ClientPutComplexDir putDir = new ClientPutComplexDir("dir-" + counter++, dirURI);
- putDir.setDefaultName(project.getIndexFile());
+ if ((project.getIndexFile() != null) && (project.getIndexFile().length() > 0)) {
+ putDir.setDefaultName(project.getIndexFile());
+ }
putDir.setVerbosity(Verbosity.ALL);
putDir.setMaxRetries(-1);
for (String filename : files) {
System.out.println(message);
}
if (!finished) {
+ @SuppressWarnings("null")
String messageName = message.getName();
if ("URIGenerated".equals(messageName)) {
finalURI = message.get("URI");
/* 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);
+ project.setLastInsertionTime(System.currentTimeMillis());
}
}