All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.xebialabs.overthere.ssh.SshFile Maven / Gradle / Ivy

There is a newer version: 5.6.15
Show newest version
/*
 * Copyright (c) 2008-2014, XebiaLabs B.V., All rights reserved.
 *
 *
 * Overthere is licensed under the terms of the GPLv2
 * , like most XebiaLabs Libraries.
 * There are special exceptions to the terms and conditions of the GPLv2 as it is applied to
 * this software, see the FLOSS License Exception
 * .
 *
 * This program is free software; you can redistribute it and/or modify it under the terms
 * of the GNU General Public License as published by the Free Software Foundation; version 2
 * of the License.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this
 * program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth
 * Floor, Boston, MA 02110-1301  USA
 */
package com.xebialabs.overthere.ssh;

import java.util.List;
import com.google.common.base.CharMatcher;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;

import com.xebialabs.overthere.CmdLine;
import com.xebialabs.overthere.OperatingSystemFamily;
import com.xebialabs.overthere.OverthereExecutionOutputHandler;
import com.xebialabs.overthere.OverthereFile;
import com.xebialabs.overthere.RuntimeIOException;
import com.xebialabs.overthere.spi.BaseOverthereFile;

import static com.google.common.collect.Lists.newArrayList;
import static com.xebialabs.overthere.OperatingSystemFamily.WINDOWS;

/**
 * A file on a host connected through SSH.
 */
abstract class SshFile extends BaseOverthereFile {

    protected List pathComponents;

    /**
     * Constructs an SshOverthereFile
     *
     * @param connection the connection to the host
     * @param path       the path of the file on the host
     */
    SshFile(C connection, String path) {
        this(connection, splitPath(path, connection.getHostOperatingSystem()));
    }

    SshFile(C connection, List pathComponents) {
        super(connection);
        checkWindowsPath(pathComponents, connection.getHostOperatingSystem());
        this.pathComponents = pathComponents;
    }

    @Override
    public String getPath() {
        return joinPath(pathComponents, connection.getHostOperatingSystem());
    }

    @Override
    public boolean isHidden() {
        return getName().startsWith(".");
    }

    @Override
    public String getName() {
        if (pathComponents.isEmpty()) {
            return connection.getHostOperatingSystem().getFileSeparator();
        } else {
            return pathComponents.get(pathComponents.size() - 1);
        }
    }

    @Override
    public OverthereFile getParentFile() {
        if (pathComponents.isEmpty()) {
            // The root path is its own parent.
            return this;
        }

        if (connection.getHostOperatingSystem() == WINDOWS && pathComponents.size() == 1) {
            // On Windows, the drive path is its own parent
            return this;
        }

        return connection.getFile(joinPath(pathComponents.subList(0, pathComponents.size() - 1), connection.getHostOperatingSystem()));
    }

    @Override
    public void delete() throws RuntimeIOException {
        if (exists()) {
            if (isDirectory()) {
                deleteDirectory();
            } else {
                deleteFile();
            }
        }
    }

    protected abstract void deleteFile();

    protected abstract void deleteDirectory();

    protected int executeCommand(OverthereExecutionOutputHandler outHandler, OverthereExecutionOutputHandler errHandler, CmdLine commandLine) {
        return connection.execute(outHandler, errHandler, commandLine);
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof SshFile)) {
            return false;
        }

        return getPath().equals(((SshFile) obj).getPath());
    }

    @Override
    public int hashCode() {
        return getPath().hashCode();
    }

    @Override
    public String toString() {
        String p = getPath();
        if (p.length() >= 1 && p.charAt(0) == '/') {
            return getConnection() + p;
        } else {
            return getConnection() + "/" + p;
        }
    }

    static List splitPath(String path, OperatingSystemFamily os) {
        Splitter s = os == WINDOWS ? WINDOWS_PATH_SPLITTER : UNIX_PATH_SPLITTER;
        return newArrayList(s.split(path));
    }

    static String joinPath(List pathComponents, OperatingSystemFamily os) {
        String fileSep = os.getFileSeparator();

        if (pathComponents.isEmpty()) {
            return fileSep;
        }

        if (os == WINDOWS) {
            String path = Joiner.on(fileSep).join(pathComponents);
            if (pathComponents.size() == 1) {
                path += fileSep;
            }
            return path;
        } else {
            return fileSep + Joiner.on(fileSep).join(pathComponents);
        }
    }

    static void checkWindowsPath(List pathComponents, OperatingSystemFamily os) {
        if (os == WINDOWS && pathComponents.isEmpty()) {
            throw new IllegalArgumentException("Empty path is not allowed on Windows");
        }
    }

    private static final Splitter UNIX_PATH_SPLITTER = Splitter.on('/').omitEmptyStrings();

    private static final Splitter WINDOWS_PATH_SPLITTER = Splitter.on(CharMatcher.anyOf("/\\")).omitEmptyStrings();

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy