org.ensembl.gti.seqstore.database.cramstore.EnaCramUploader.java Source code

Java tutorial

Introduction

Here is the source code for org.ensembl.gti.seqstore.database.cramstore.EnaCramUploader.java

Source

/*
 * Copyright 2015 EMBL-European Bioinformatics Institute
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.ensembl.gti.seqstore.database.cramstore;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.commons.net.ftp.FTPClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class EnaCramUploader {

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    private final FTPClient ftpClient;
    private final String ftpUri;
    private final String user;
    private final String password;

    public EnaCramUploader(String ftpUri, String user, String password) {
        this.ftpUri = ftpUri;
        this.user = user;
        this.password = password;
        ftpClient = new FTPClient();
    }

    protected File compressFile(File file) throws IOException, InterruptedException {
        File gzipFile = new File(file.getPath() + ".gz");
        log.info("Compressing " + file.getPath() + " to " + gzipFile.getPath());
        Process exec = Runtime.getRuntime().exec("gzip " + file.getPath());
        exec.waitFor();
        log.info("Completed compressing " + file.getPath() + " to " + gzipFile.getPath());
        file.delete();
        return gzipFile;
    }

    protected void uploadFile(File file) {
        log.info("Uploading file " + file);
        try {
            ftpClient.connect(ftpUri);
            ftpClient.login(user, password);
            ftpClient.mode(FTPClient.BINARY_FILE_TYPE);
            ftpClient.enterLocalPassiveMode();
            ftpClient.storeFile(file.getName(), new FileInputStream(file));
            ftpClient.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
        log.info("Completed uploading file " + file);
    }

}