log.debug("Method interrupted waiting for archive stream lock.");
} finally {
archiveStreamService.startLock.unlock();
}
StreamSession newSession = archiveStreamService.streamSession;
int numOfFiles = archiveStreamService.getFileNum();
IFile currentFile = null;
int worked = 0;
int lastWorked = 0;
int filesReceived = 0;
double increment = 0.0;
InputStream in = newSession.getInputStream(0);
ZipInputStream zin = new ZipInputStream(in);
try {
ZipEntry zipEntry = null;
monitor.beginTask(null, 100);
monitor.subTask("Receiving project files...");
if (numOfFiles >= 1) {
increment = (double) 100 / numOfFiles;
} else {
monitor.worked(100);
}
while ((zipEntry = zin.getNextEntry()) != null) {
// every zipEntry represents/contains exactly one file
String projectID = new String(zipEntry.getExtra());
IProject currentProject = localProjects.get(projectID);
if (currentProject == null) {
// this is really unlikely
log.error("currentProject is null");
throw new SarosCancellationException(
"File did not belong to a project that is supposed to be added");
} else {
log.info("everything seems to be normal");
}
currentFile = currentProject.getFile(zipEntry.getName());
monitor.setTaskName(MessageFormat.format("Receiving {0}",
zipEntry.getName()));
if (currentFile.exists()) {
log.debug(currentFile
+ " already exists on invitee. Replacing this file.");
currentFile.delete(true, null);
}
currentFile.create(new UncloseableInputStream(zin), true, null);
worked = (int) Math.round(increment * filesReceived);
if ((worked - lastWorked) > 0) {
monitor.worked((worked - lastWorked));
lastWorked = worked;
}
filesReceived++;
checkCancellation();
}
// Just to be sure we don't have any problems with rounding
monitor.worked(100);
} catch (SarosCancellationException e) {
log.debug("Invitation process was cancelled.");
localCancel("An invitee cancelled the invitation.",
CancelOption.NOTIFY_PEER);
executeCancellation();
} catch (CoreException e) {
log.error("Exception while creating file. Message: ", e);
localCancel(
"A problem occurred while the project's files were being received: \""
+ e.getMessage() + "\" The invitation was cancelled.",
CancelOption.NOTIFY_PEER);
executeCancellation();
} catch (EOFException e) {
log.error("Error while receiving files: " + e.getMessage());
localCancel(
"A problem occured when receiving the project files. It is possible that the files were corrupted in transit.\n\nPlease attempt invitation again.",
CancelOption.NOTIFY_PEER);
executeCancellation();
} catch (Exception e) {
log.error("Unknown exception: ", e);
} finally {
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(zin);
newSession.stopSession();
}
}