Java tutorial
/* * Copyright 2012 AMG.lab, a Bull Group Company * * 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.xlcloud.ssh; import java.io.File; import javax.inject.Inject; import org.apache.commons.io.FilenameUtils; import org.apache.log4j.Logger; import org.xlcloud.ssh.exception.SSHException; import com.jcraft.jsch.JSchException; /** * SSH command executor to copy file to destination using SCP. * * @author Micha Kamiski, AMG.net */ public class SCPExecutor { private static final Logger LOG = Logger.getLogger(SshCommandExecutor.class); @Inject private SshSessionFactory sessionFactory; @Inject SshCommandExecutor commandExecutor; /** * Copy file from to destination on server where session is configured. This * method also creates path for this file on destination server. * * @param fileFrom * @param destination * - path on destionation server * @return * @throws SSHException */ public SshExecutionOutput scp(File fileFrom, String destination) throws SSHException { final String path = "/" + FilenameUtils.getPath(destination); try { commandExecutor.execute(new SshExecutionCommand() { @Override public String printParams() { return ""; } @Override public String getUsername() { // TODO Auto-generated method stub return null; } @Override public String getScriptName() { return "mkdir -p " + path; } }); } catch (JSchException e) { LOG.error("Could not create destination path: " + destination, e); throw new SSHException("Could not SCP file to destination", e); } LOG.info("executing SCP to destination: " + destination); SCPTask scpTask = new SCPTask(fileFrom, destination); scpTask.setSessionFactory(sessionFactory); scpTask.run(); return scpTask.getExecutionOutput(); } }