Search in sources :

Example 1 with FileAttributes

use of net.schmizz.sshj.sftp.FileAttributes in project vcell by virtualcell.

the class SimDataConnection method downloadSimData.

public static File downloadSimData(File tempSimDir, String vcellClusterPassword, String userName, KeyValue simIDKey, Integer jobIndex, boolean isOld) throws Exception {
    String simDataLogFileName = SimulationData.createCanonicalSimLogFileName(simIDKey, jobIndex, isOld);
    SSHClient ssh = null;
    SFTPClient sftpClient = null;
    if (tempSimDir == null) {
        tempSimDir = File.createTempFile("SimID_" + simIDKey + "_" + jobIndex + "_", ".dir");
        if (!tempSimDir.delete()) {
            throw new Exception("Couldn't make local dir " + tempSimDir);
        }
        if (!tempSimDir.mkdir()) {
            throw new Exception("Couldn't make local dir " + tempSimDir);
        }
    }
    if (!tempSimDir.exists()) {
        throw new Exception("Directory not exist " + tempSimDir);
    }
    File localSimDir = new File(tempSimDir, userName);
    if (!localSimDir.exists()) {
        if (!localSimDir.mkdir()) {
            throw new Exception("Couldn't make local dir " + localSimDir);
        }
    }
    System.out.println("Local download dir '" + localSimDir + "'");
    final String compareStr = "SimID_" + simIDKey.toString() + (isOld ? "." : "_" + jobIndex + "_");
    System.out.println(compareStr);
    RemoteResourceFilter remoteResourceFilter = new RemoteResourceFilter() {

        @Override
        public boolean accept(RemoteResourceInfo arg0) {
            return arg0.getName().startsWith(compareStr);
        }
    };
    try {
        System.out.println("Creating ssh connection...");
        ssh = createSSHClient(dataServer, "vcell", vcellClusterPassword);
        System.out.println("Creating ftpClient...");
        sftpClient = ssh.newSFTPClient();
        sftpClient.getFileTransfer().setPreserveAttributes(false);
        String primaryFullPath = primaryPath + userName + "/" + simDataLogFileName;
        System.out.println("Seeking primary location..." + primaryFullPath);
        FileAttributes fileAttributes = sftpClient.statExistence(primaryFullPath);
        boolean bExist = (fileAttributes == null ? false : true);
        if (bExist) {
            System.out.println("Downloading primary location...");
            List<RemoteResourceInfo> remoteResourceInfos = sftpClient.ls(primaryPath + userName, remoteResourceFilter);
            download(sftpClient, localSimDir, remoteResourceInfos);
        // printRemoteResourceInfo(remoteResourceInfos);
        // sftpClient.get(primaryFullPath, logF.getAbsolutePath());
        // System.out.println("Downloaded data from "+dataServer+":"+primaryFullPath+" to "+logF);
        } else {
            String secondaryFullPath = secondaryPath + userName + "/" + simDataLogFileName;
            System.out.println("Seeking secondary location..." + secondaryFullPath);
            fileAttributes = sftpClient.statExistence(secondaryFullPath);
            bExist = (fileAttributes == null ? false : true);
            if (bExist) {
                System.out.println("Downloading secondary location...");
                List<RemoteResourceInfo> remoteResourceInfos = sftpClient.ls(secondaryPath + userName, remoteResourceFilter);
                download(sftpClient, localSimDir, remoteResourceInfos);
            // printRemoteResourceInfo(remoteResourceInfos);
            // sftpClient.get(secondaryFullPath, logF.getAbsolutePath());
            // System.out.println("Downloaded data from "+dataServer+":"+secondaryFullPath+" to "+logF);
            } else {
                System.out.println("Data Not Found for simid " + simIDKey);
            }
        }
        return localSimDir;
    } finally {
        if (sftpClient != null) {
            try {
                sftpClient.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (ssh != null) {
            try {
                ssh.disconnect();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : SSHClient(net.schmizz.sshj.SSHClient) SFTPClient(net.schmizz.sshj.sftp.SFTPClient) RemoteResourceInfo(net.schmizz.sshj.sftp.RemoteResourceInfo) File(java.io.File) RemoteResourceFilter(net.schmizz.sshj.sftp.RemoteResourceFilter) FileAttributes(net.schmizz.sshj.sftp.FileAttributes)

Aggregations

File (java.io.File)1 SSHClient (net.schmizz.sshj.SSHClient)1 FileAttributes (net.schmizz.sshj.sftp.FileAttributes)1 RemoteResourceFilter (net.schmizz.sshj.sftp.RemoteResourceFilter)1 RemoteResourceInfo (net.schmizz.sshj.sftp.RemoteResourceInfo)1 SFTPClient (net.schmizz.sshj.sftp.SFTPClient)1