Search in sources :

Example 6 with FileNotFoundException

use of org.apache.commons.vfs2.FileNotFoundException in project artisynth_core by artisynth.

the class FileCacher method cache.

public File cache(URIx uri, File cacheFile, FileTransferMonitor monitor) throws FileSystemException {
    // For atomic operation, first download to temporary directory
    File tmpCacheFile = new File(cacheFile.getAbsolutePath() + TMP_EXTENSION);
    URIx cacheURI = new URIx(cacheFile.getAbsoluteFile());
    URIx tmpCacheURI = new URIx(tmpCacheFile.getAbsoluteFile());
    FileObject localTempFile = manager.resolveFile(tmpCacheURI.toString(true));
    FileObject localCacheFile = manager.resolveFile(cacheURI.toString(true));
    // will resolve next
    FileObject remoteFile = null;
    // loop through authenticators until we either succeed or cancel
    boolean cancel = false;
    while (remoteFile == null && cancel == false) {
        remoteFile = resolveRemote(uri);
    }
    if (remoteFile == null || !remoteFile.exists()) {
        throw new FileSystemException("Cannot find remote file <" + uri.toString() + ">", new FileNotFoundException("<" + uri.toString() + ">"));
    }
    // monitor the file transfer progress
    if (monitor != null) {
        monitor.monitor(localTempFile, remoteFile, -1, cacheFile.getName());
        monitor.start();
        monitor.fireStartEvent(localTempFile);
    }
    // transfer content
    try {
        if (remoteFile.isFile()) {
            localTempFile.copyFrom(remoteFile, Selectors.SELECT_SELF);
        } else if (remoteFile.isFolder()) {
            // final FileObject fileSystem = manager.createFileSystem(remoteFile);
            localTempFile.copyFrom(remoteFile, new AllFileSelector());
        // fileSystem.close();
        }
        if (monitor != null) {
            monitor.fireCompleteEvent(localTempFile);
        }
    } catch (Exception e) {
        // try to delete local file
        localTempFile.delete();
        throw new RuntimeException("Failed to complete transfer of " + remoteFile.getURL() + " to " + localTempFile.getURL(), e);
    } finally {
        // close files if we need to
        localTempFile.close();
        remoteFile.close();
        if (monitor != null) {
            monitor.release(localTempFile);
            monitor.stop();
        }
    }
    // now that the copy is complete, do a rename operation
    try {
        if (tmpCacheFile.isDirectory()) {
            SafeFileUtils.moveDirectory(tmpCacheFile, cacheFile);
        } else {
            SafeFileUtils.moveFile(tmpCacheFile, cacheFile);
        }
    } catch (Exception e) {
        // delete if possible
        localCacheFile.delete();
        throw new RuntimeException("Failed to atomically move " + "to " + localCacheFile.getURL(), e);
    }
    return cacheFile;
}
Also used : FileSystemException(org.apache.commons.vfs2.FileSystemException) AllFileSelector(org.apache.commons.vfs2.AllFileSelector) FileNotFoundException(java.io.FileNotFoundException) URIx(maspack.fileutil.uri.URIx) FileObject(org.apache.commons.vfs2.FileObject) File(java.io.File) FileSystemException(org.apache.commons.vfs2.FileSystemException) FileNotFoundException(java.io.FileNotFoundException) JSchException(com.jcraft.jsch.JSchException)

Aggregations

FileObject (org.apache.commons.vfs2.FileObject)6 FileSystemException (org.apache.commons.vfs2.FileSystemException)6 FileNotFoundException (java.io.FileNotFoundException)5 JSchException (com.jcraft.jsch.JSchException)4 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 AllFileSelector (org.apache.commons.vfs2.AllFileSelector)2 HazelcastInstance (com.hazelcast.core.HazelcastInstance)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 OutputStream (java.io.OutputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ParseException (javax.mail.internet.ParseException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 URIx (maspack.fileutil.uri.URIx)1 LocalizingDynamicSchemaProcessor (mondrian.i18n.LocalizingDynamicSchemaProcessor)1 MondrianException (mondrian.olap.MondrianException)1 PropertyList (mondrian.olap.Util.PropertyList)1 AxisFault (org.apache.axis2.AxisFault)1 ClusteringAgent (org.apache.axis2.clustering.ClusteringAgent)1