Java File Path Delete deleteInRemote(String host, String path)

Here you can find the source of deleteInRemote(String host, String path)

Description

delete In Remote

License

Apache License

Declaration

public static void deleteInRemote(String host, String path) throws IOException 

Method Source Code

//package com.java2s;
/**/*  w w  w  .  j  ava 2s. com*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStreamReader;

import java.util.Map;

public class Main {
    public static void deleteInRemote(String host, String path) throws IOException {
        if (!runCommandInRemoteHost(host, "rm -rf " + path, 0).isEmpty())
            throw new IOException("fail to delete " + path + " in " + host);
    }

    public static String runCommandInRemoteHost(String host, String command, Integer expectedExitCode)
            throws IOException {
        command = command.replaceAll("\"", "\\\\\"");
        command = command.replaceAll("\\$", "\\\\\\$");
        return runCommand("ssh " + host + " \"" + command + "\"", expectedExitCode, null, null);
    }

    public static Process runCommand(String command, String extraPath, String libPath) throws IOException {
        return runCommand(new String[] { "/bin/bash", "-c", command }, extraPath, libPath);
    }

    public static Process runCommand(String[] cmdArray, String extraPath, String libPath) throws IOException {
        ProcessBuilder processBuilder = new ProcessBuilder(cmdArray);
        Map<String, String> environment = processBuilder.environment();
        if (extraPath != null) {
            if (extraPath.isEmpty())
                extraPath = ".";
            environment.put("PATH", extraPath + ":" + environment.get("PATH"));
        }
        if (libPath != null) {
            if (libPath.isEmpty())
                libPath = ".";
            environment.put("LD_LIBRARY_PATH", libPath + ":" + environment.get("LD_LIBRARY_PATH"));
        }
        return processBuilder.start();
    }

    public static String runCommand(String command, Integer expectedExitCode, String extraPath, String libPath)
            throws IOException {
        return runCommand(command, expectedExitCode, extraPath, libPath, true);
    }

    public static String runCommand(String command, Integer expectedExitCode, String extraPath, String libPath,
            boolean destory) throws IOException {
        BufferedReader stdOutputReader = null;
        StringBuilder output = new StringBuilder();
        Process process = runCommand(command, extraPath, libPath);
        ;
        try {
            stdOutputReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = stdOutputReader.readLine()) != null) {
                output.append(line).append('\n');
            }
            if (output.length() > 0)
                output.deleteCharAt(output.length() - 1);
            if (expectedExitCode != null && process.waitFor() != expectedExitCode)
                throw new IOException(
                        "exit code=" + process.exitValue() + ", expected exit code=" + expectedExitCode);
        } catch (Throwable t) {
            destory = true;
            throw new IOException("fail to exceute " + command + ", output=" + output
                    + (extraPath == null ? "" : ", extraPath=" + extraPath)
                    + (libPath == null ? "" : ", libPath=" + libPath), t);
        } finally {
            if (stdOutputReader != null)
                stdOutputReader.close();
            process.getOutputStream().close();
            process.getInputStream().close();
            process.getErrorStream().close();
            if (destory)
                process.destroy();
        }
        return output.toString();
    }
}

Related

  1. deleteFolderAndContent(String folderPath)
  2. deleteFolderRec(File path, boolean alsoDeleteGivenFolder)
  3. deleteFolderRecursively(File path, boolean includeSelf)
  4. deleteIfExist(String filePath)
  5. deleteInPathTempFiles(String workingFolder)
  6. deleteJaasFile(String jaasPath)
  7. deleteKey(String path, String key)
  8. deleteMultipleFiles(String filePath, int numberOfFiles)
  9. deleteNonEmptyDirectory(String dirPath)