2 * jSite - a tool for uploading websites into Freenet
3 * Copyright (C) 2006 David Roden
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 package de.todesbaum.jsite.application;
22 import java.io.ByteArrayInputStream;
23 import java.io.ByteArrayOutputStream;
25 import java.io.FileInputStream;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
33 import java.util.zip.ZipEntry;
34 import java.util.zip.ZipOutputStream;
36 import de.todesbaum.jsite.gui.FileScanner;
37 import de.todesbaum.jsite.gui.FileScannerListener;
38 import de.todesbaum.util.freenet.fcp2.Client;
39 import de.todesbaum.util.freenet.fcp2.ClientPutComplexDir;
40 import de.todesbaum.util.freenet.fcp2.Connection;
41 import de.todesbaum.util.freenet.fcp2.DirectFileEntry;
42 import de.todesbaum.util.freenet.fcp2.FileEntry;
43 import de.todesbaum.util.freenet.fcp2.Message;
44 import de.todesbaum.util.freenet.fcp2.RedirectFileEntry;
45 import de.todesbaum.util.freenet.fcp2.Verbosity;
46 import de.todesbaum.util.io.Closer;
47 import de.todesbaum.util.io.ReplacingOutputStream;
48 import de.todesbaum.util.io.StreamCopier;
51 * Manages project inserts.
53 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
55 public class ProjectInserter implements FileScannerListener, Runnable {
57 /** Random number for FCP instances. */
58 private static final int random = (int) (Math.random() * Integer.MAX_VALUE);
60 /** Counter for FCP connection identifier. */
61 private static int counter = 0;
63 /** Whether debug mode is set. */
64 private boolean debug = false;
66 /** The list of insert listeners. */
67 private List<InsertListener> insertListeners = new ArrayList<InsertListener>();
69 /** The freenet interface. */
70 protected Freenet7Interface freenetInterface;
72 /** The project to insert. */
73 protected Project project;
75 /** The file scanner. */
76 private FileScanner fileScanner;
78 /** Object used for synchronization. */
79 protected final Object lockObject = new Object();
82 * Adds a listener to the list of registered listeners.
84 * @param insertListener
87 public void addInsertListener(InsertListener insertListener) {
88 insertListeners.add(insertListener);
92 * Removes a listener from the list of registered listeners.
94 * @param insertListener
95 * The listener to remove
97 public void removeInsertListener(InsertListener insertListener) {
98 insertListeners.remove(insertListener);
102 * Notifies all listeners that the project insert has started.
104 * @see InsertListener#projectInsertStarted(Project)
106 protected void fireProjectInsertStarted() {
107 for (InsertListener insertListener : insertListeners) {
108 insertListener.projectInsertStarted(project);
113 * Notifies all listeners that the insert has generated a URI.
115 * @see InsertListener#projectURIGenerated(Project, String)
119 protected void fireProjectURIGenerated(String uri) {
120 for (InsertListener insertListener : insertListeners) {
121 insertListener.projectURIGenerated(project, uri);
126 * Notifies all listeners that the insert has made some progress.
128 * @see InsertListener#projectInsertProgress(Project, int, int, int, int,
131 * The number of succeeded blocks
133 * The number of failed blocks
135 * The number of fatally failed blocks
137 * The total number of blocks
139 * <code>true</code> if the total number of blocks has already
140 * been finalized, <code>false</code> otherwise
142 protected void fireProjectInsertProgress(int succeeded, int failed, int fatal, int total, boolean finalized) {
143 for (InsertListener insertListener : insertListeners) {
144 insertListener.projectInsertProgress(project, succeeded, failed, fatal, total, finalized);
149 * Notifies all listeners the project insert has finished.
151 * @see InsertListener#projectInsertFinished(Project, boolean, Throwable)
153 * <code>true</code> if the project was inserted successfully,
154 * <code>false</code> if it failed
156 * The cause of the failure, if any
158 protected void fireProjectInsertFinished(boolean success, Throwable cause) {
159 for (InsertListener insertListener : insertListeners) {
160 insertListener.projectInsertFinished(project, success, cause);
165 * Sets the debug mode.
168 * <code>true</code> to activate debug mode, <code>false</code>
171 public void setDebug(boolean debug) {
176 * Sets the project to insert.
179 * The project to insert
181 public void setProject(Project project) {
182 this.project = project;
186 * Sets the freenet interface to use.
188 * @param freenetInterface
189 * The freenet interface to use
191 public void setFreenetInterface(Freenet7Interface freenetInterface) {
192 this.freenetInterface = freenetInterface;
198 public void start() {
199 fileScanner = new FileScanner(project);
200 fileScanner.addFileScannerListener(this);
201 new Thread(fileScanner).start();
205 * Creates an input stream that delivers the given file, replacing edition
206 * tokens in the file’s content, if necessary.
209 * The name of the file
213 * The current edition
215 * An array containing a single long which is used to
216 * <em>return</em> the final length of the file, after all
218 * @return The input stream for the file
219 * @throws IOException
220 * if an I/O error occurs
222 private InputStream createFileInputStream(String filename, FileOption fileOption, int edition, long[] length) throws IOException {
223 File file = new File(project.getLocalPath(), filename);
224 length[0] = file.length();
225 if (!fileOption.getReplaceEdition()) {
226 return new FileInputStream(file);
228 ByteArrayOutputStream filteredByteOutputStream = new ByteArrayOutputStream(Math.min(Integer.MAX_VALUE, (int) length[0]));
229 ReplacingOutputStream outputStream = new ReplacingOutputStream(filteredByteOutputStream);
230 FileInputStream fileInput = new FileInputStream(file);
232 outputStream.addReplacement("$[EDITION]", String.valueOf(edition));
233 outputStream.addReplacement("$[URI]", project.getFinalRequestURI(0));
234 for (int index = 1; index <= fileOption.getEditionRange(); index++) {
235 outputStream.addReplacement("$[URI+" + index + "]", project.getFinalRequestURI(index));
236 outputStream.addReplacement("$[EDITION+" + index + "]", String.valueOf(edition + index));
238 StreamCopier.copy(fileInput, outputStream, length[0]);
240 Closer.close(fileInput);
241 Closer.close(outputStream);
242 Closer.close(filteredByteOutputStream);
244 byte[] filteredBytes = filteredByteOutputStream.toByteArray();
245 length[0] = filteredBytes.length;
246 return new ByteArrayInputStream(filteredBytes);
250 * Creates an input stream for a container.
252 * @param containerFiles
253 * All container definitions
254 * @param containerName
255 * The name of the container to create
257 * The current edition
258 * @param containerLength
259 * An array containing a single long which is used to
260 * <em>return</em> the final length of the container stream,
261 * after all replacements
262 * @return The input stream for the container
263 * @throws IOException
264 * if an I/O error occurs
266 private InputStream createContainerInputStream(Map<String, List<String>> containerFiles, String containerName, int edition, long[] containerLength) throws IOException {
267 File tempFile = File.createTempFile("jsite", ".zip");
268 tempFile.deleteOnExit();
269 FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
270 ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
272 for (String filename : containerFiles.get(containerName)) {
273 File dataFile = new File(project.getLocalPath(), filename);
274 if (dataFile.exists()) {
275 ZipEntry zipEntry = new ZipEntry(filename);
276 long[] fileLength = new long[1];
277 InputStream wrappedInputStream = createFileInputStream(filename, project.getFileOption(filename), edition, fileLength);
279 zipOutputStream.putNextEntry(zipEntry);
280 StreamCopier.copy(wrappedInputStream, zipOutputStream, fileLength[0]);
282 zipOutputStream.closeEntry();
283 wrappedInputStream.close();
288 zipOutputStream.closeEntry();
289 Closer.close(zipOutputStream);
290 Closer.close(fileOutputStream);
293 containerLength[0] = tempFile.length();
294 return new FileInputStream(tempFile);
298 * Creates a file entry suitable for handing in to
299 * {@link ClientPutComplexDir#addFileEntry(FileEntry)}.
302 * The name of the file to insert
304 * The current edition
305 * @param containerFiles
306 * The container definitions
307 * @return A file entry for the given file
309 private FileEntry createFileEntry(String filename, int edition, Map<String, List<String>> containerFiles) {
310 FileEntry fileEntry = null;
311 FileOption fileOption = project.getFileOption(filename);
312 if (filename.startsWith("/container/:")) {
313 String containerName = filename.substring("/container/:".length());
315 long[] containerLength = new long[1];
316 InputStream containerInputStream = createContainerInputStream(containerFiles, containerName, edition, containerLength);
317 fileEntry = new DirectFileEntry(containerName + ".zip", "application/zip", containerInputStream, containerLength[0]);
318 } catch (IOException ioe1) {
319 /* ignore, null is returned. */
322 if (fileOption.isInsert()) {
324 long[] fileLength = new long[1];
325 InputStream fileEntryInputStream = createFileInputStream(filename, fileOption, edition, fileLength);
326 fileEntry = new DirectFileEntry(filename, project.getFileOption(filename).getMimeType(), fileEntryInputStream, fileLength[0]);
327 } catch (IOException ioe1) {
328 /* ignore, null is returned. */
331 fileEntry = new RedirectFileEntry(filename, fileOption.getMimeType(), fileOption.getCustomKey());
338 * Creates container definitions.
341 * The list of all files
343 * The list of all containers
344 * @param containerFiles
345 * Empty map that will be filled with container definitions
347 private void createContainers(List<String> files, List<String> containers, Map<String, List<String>> containerFiles) {
348 for (String filename : new ArrayList<String>(files)) {
349 FileOption fileOption = project.getFileOption(filename);
350 String containerName = fileOption.getContainer();
351 if (!containerName.equals("")) {
352 if (!containers.contains(containerName)) {
353 containers.add(containerName);
354 containerFiles.put(containerName, new ArrayList<String>());
355 /* hmm. looks like a hack to me. */
356 files.add("/container/:" + containerName);
358 containerFiles.get(containerName).add(filename);
359 files.remove(filename);
368 fireProjectInsertStarted();
369 List<String> files = fileScanner.getFiles();
371 /* create connection to node */
372 Connection connection = freenetInterface.getConnection("project-insert-" + random + counter++);
373 boolean connected = false;
374 Throwable cause = null;
376 connected = connection.connect();
377 } catch (IOException e1) {
382 fireProjectInsertFinished(false, cause);
386 Client client = new Client(connection);
388 /* create containers */
389 final List<String> containers = new ArrayList<String>();
390 final Map<String, List<String>> containerFiles = new HashMap<String, List<String>>();
391 createContainers(files, containers, containerFiles);
394 int edition = project.getEdition();
395 String dirURI = "USK@" + project.getInsertURI() + "/" + project.getPath() + "/" + edition + "/";
396 ClientPutComplexDir putDir = new ClientPutComplexDir("dir-" + counter++, dirURI);
397 if ((project.getIndexFile() != null) && (project.getIndexFile().length() > 0)) {
398 putDir.setDefaultName(project.getIndexFile());
400 putDir.setVerbosity(Verbosity.ALL);
401 putDir.setMaxRetries(-1);
402 for (String filename : files) {
403 FileEntry fileEntry = createFileEntry(filename, edition, containerFiles);
404 if (fileEntry != null) {
405 putDir.addFileEntry(fileEntry);
411 client.execute(putDir);
412 } catch (IOException ioe1) {
413 fireProjectInsertFinished(false, ioe1);
417 /* parse progress and success messages */
418 String finalURI = null;
419 boolean success = false;
420 boolean finished = false;
421 boolean disconnected = false;
423 Message message = client.readMessage();
424 finished = (message == null) || (disconnected = client.isDisconnected());
426 System.out.println(message);
429 @SuppressWarnings("null")
430 String messageName = message.getName();
431 if ("URIGenerated".equals(messageName)) {
432 finalURI = message.get("URI");
433 fireProjectURIGenerated(finalURI);
435 if ("SimpleProgress".equals(messageName)) {
436 int total = Integer.parseInt(message.get("Total"));
437 int succeeded = Integer.parseInt(message.get("Succeeded"));
438 int fatal = Integer.parseInt(message.get("FatallyFailed"));
439 int failed = Integer.parseInt(message.get("Failed"));
440 boolean finalized = Boolean.parseBoolean(message.get("FinalizedTotal"));
441 fireProjectInsertProgress(succeeded, failed, fatal, total, finalized);
443 success = "PutSuccessful".equals(messageName);
444 finished = success || "PutFailed".equals(messageName) || messageName.endsWith("Error");
448 /* post-insert work */
449 fireProjectInsertFinished(success, disconnected ? new IOException("Connection terminated") : null);
451 @SuppressWarnings("null")
452 String editionPart = finalURI.substring(finalURI.lastIndexOf('/') + 1);
453 int newEdition = Integer.parseInt(editionPart);
454 project.setEdition(newEdition);
455 project.setLastInsertionTime(System.currentTimeMillis());
460 // INTERFACE FileScannerListener
466 public void fileScannerFinished(FileScanner fileScanner) {
467 if (!fileScanner.isError()) {
468 new Thread(this).start();
470 fireProjectInsertFinished(false, null);
472 fileScanner.removeFileScannerListener(this);