boolean forceOverwrite = overwrite || !destination.exists();
if (!forceOverwrite) {
try {
String cachingName = determineCachingName(destination, hashFileName);
if (!destination.getName().equals(cachingName)) {
throw new TestContainerException("File " + destination + " should have name "
+ cachingName);
}
}
catch (TestContainerException ignore) {
forceOverwrite = true;
}
}
if (forceOverwrite) {
try {
LOGGER.debug("Creating new file at destination: " + destination.getAbsolutePath());
destination.getParentFile().mkdirs();
destination.createNewFile();
FileOutputStream os = null;
try {
os = new FileOutputStream(destination);
FileChannel fileChannel = os.getChannel();
StreamUtils.ProgressBar progressBar = null;
if (LOGGER.isInfoEnabled()) {
if (downloadFeeback) {
progressBar = new StreamUtils.FineGrainedProgressBar(displayName);
}
else {
progressBar = new StreamUtils.CoarseGrainedProgressBar(displayName);
}
}
// Check if this is an exploded bundle...
if (url.getPath().endsWith("/") && "file".equals(url.getProtocol())) {
StreamUtils.streamCopy(new URL("assembly:" + url.toExternalForm()),
fileChannel, progressBar);
}
else {
StreamUtils.streamCopy(url, fileChannel, progressBar);
}
fileChannel.close();
LOGGER.debug("Successfully downloaded to [" + destination + "]");
}
finally {
if (os != null) {
os.close();
}
}
}
catch (IOException e) {
throw new TestContainerException("[" + url + "] could not be downloaded", e);
}
}
if (checkAttributes) {
try {
validateBundle(url, destination);
}
catch (TestContainerException e) {
if (failOnValidation) {
throw e;
}
return null;
}
}
String cachingName = determineCachingName(destination, hashFileName);
File newDestination = new File(destination.getParentFile(), cachingName);
if (!cachingName.equals(destination.getName())) {
if (newDestination.exists()) {
if (!newDestination.delete()) {
throw new TestContainerException("Cannot delete " + newDestination);
}
}
if (!destination.renameTo(newDestination)) {
throw new TestContainerException("Cannot rename " + destination + " to "
+ newDestination);
}
fileNamesForUrls.setProperty(url.toExternalForm(), cachingName);
saveProperties(fileNamesForUrls, downloadedBundlesFile);
}