com.athena.peacock.common.core.util.ScpUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.athena.peacock.common.core.util.ScpUtil.java

Source

/* 
 * Copyright 2013 The Athena-Peacock Project.
 *
 * 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.
 *
 * Revision History
 * Author         Date            Description
 * ---------------   ----------------   ------------
 * Sang-cheon Park   2013. 10. 11.      First Draft.
 */
package com.athena.peacock.common.core.util;

import java.io.File;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.optional.ssh.Scp;
import org.apache.tools.ant.types.FileSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import com.athena.peacock.common.core.action.support.TargetHost;

/**
 * <pre>
 * ? ? scp ?    ?
 * </pre>
 * 
 * @author Sang-cheon Park
 * @version 1.0
 */
public class ScpUtil {

    private static final Logger logger = LoggerFactory.getLogger(ScpUtil.class);

    /**
     * <pre>
     * source?  ?(? ) ? ? target .
     * </pre>
     * @param targetHost
     * @param source
     * @param target
     */
    public static void upload(TargetHost targetHost, String source, String target) {

        Assert.notNull(targetHost, "targetHost cannot be null.");
        Assert.notNull(source, "source cannot be null.");
        Assert.notNull(target, "target cannot be null.");

        String destination = targetHost.getUsername() + "@" + targetHost.getHost() + ":" + target;

        logger.debug("[scp upload] " + source + " - " + destination);

        Project project = new Project();

        Scp scp = new Scp();

        // Ant Project Property
        scp.setProject(project);
        scp.setVerbose(true);

        // Set Scp properties 
        scp.setPort(targetHost.getPort());
        scp.setPassword(targetHost.getPassword());
        scp.setTodir(destination);
        scp.setTrust(targetHost.isTrust());

        // ? source  ?  FileSet? scp? .
        File filesetDir = new File(source);
        if (filesetDir.isDirectory()) {
            FileSet fileSet = new FileSet();
            fileSet.setDir(filesetDir);
            fileSet.setProject(project);

            scp.addFileset(fileSet);
        } else {
            scp.setFile(source);
        }

        if (targetHost.getKeyfile() != null) {
            scp.setKeyfile(targetHost.getKeyfile());
        }

        scp.execute();
    }//end of upload()

}
//end of ScpUtil.java