Java Create Directory mkdirsInRemote(String host, String path, boolean deleteIfExisted)

Here you can find the source of mkdirsInRemote(String host, String path, boolean deleteIfExisted)

Description

mkdirs In Remote

License

Apache License

Declaration

public static void mkdirsInRemote(String host, String path, boolean deleteIfExisted) throws IOException 

Method Source Code

//package com.java2s;
/**//from w  w  w .  java 2s.c  o  m
 * 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 mkdirsInRemote(String host, String path, boolean deleteIfExisted) throws IOException {
        if (deleteIfExisted)
            deleteInRemote(host, path);
        if (!runCommandInRemoteHost(host, "mkdir -p " + path, 0).isEmpty())
            throw new IOException("fail to delete " + host + ":" + path);
    }

    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. mkdirs(String s)
  2. mkdirsFor(File destination)
  3. mkdirsIfAbsent(String pathname)
  4. mkdirsIfNotExists(String pathname)
  5. mkdirsifnotExists(String s)
  6. mkdirsOrCrash(File d)
  7. mkdirsOrFail(File dirFile)
  8. mkdirsWithExistsCheck(File dir)
  9. mkdirsWithRetry(final File f)