/** * Download an artifact from a host to the cache. * * @param host the remote host * @param artifact the artifact being retrieved * @param destination the cached destination * @return TRUE if downloaded * @exception IOException if an IO error occurs * @exception TransitException if a transit system error occurs */ private boolean download(ResourceHost host, Artifact artifact, File destination) throws IOException, TransitException { if (host == null) { return false; } CacheMonitorRouter monitor = Transit.getInstance().getCacheMonitorRouter(); File parentDir = destination.getParentFile(); File tempFile = File.createTempFile("~dpml", ".tmp", parentDir); tempFile.deleteOnExit(); // safety harness in case we abort abnormally FileOutputStream tempOut = new FileOutputStream(tempFile); try { Date lastModified = host.download(artifact, tempOut); // An atomic operation and no risk of a corrupted // artifact content. tempFile.renameTo(destination); destination.setLastModified(lastModified.getTime()); return true; } catch (Throwable e) { tempFile.delete(); if (monitor != null) { monitor.failedDownloadFromHost(host.toString(), artifact, e); } return false; } }