Java tutorial
/* * Copyright 2002-2012 the original author or authors. * * 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 io.manasobi.utils; import java.io.ByteArrayOutputStream; import org.apache.commons.exec.CommandLine; import org.apache.commons.exec.DefaultExecuteResultHandler; import org.apache.commons.exec.DefaultExecutor; import org.apache.commons.exec.ExecuteWatchdog; import org.apache.commons.exec.PumpStreamHandler; import org.apache.commons.lang3.StringUtils; import io.manasobi.exception.CmdUtilsException; /** * Apache Commons Exec?? Java ? ? * * @author manasobi * @since 1.0.0 */ public final class CmdUtils { private CmdUtils() { } private static final int SUCCESS_RETURN_CODE = 0; private static final int DEFAULT_TIMEOUT = 60 * 1000; /** * ? ? ? ? * * @param line * @return int ? */ public static int execute(String line) { CommandLine commandLine = CommandLine.parse(line); return execute(commandLine, null); } /** * ? ?? ? ? ? * * @param line * @param argument ? * @return int ? */ public static int execute(String line, String argument) { CommandLine commandLine = CommandLine.parse(line); return execute(commandLine, argument); } /** * ? ?? ? ? ? * * @param line * @param arguments ? * @return int ? */ public static int execute(String line, String[] arguments) { CommandLine commandLine = CommandLine.parse(line); for (String argument : arguments) { commandLine.addArgument(argument, false); } return execute(commandLine, null); } /** * (commandLine)? ? ? ? * * @param commandLine * @return int ? */ public static int execute(CommandLine commandLine) { return execute(commandLine, null, DEFAULT_TIMEOUT); } /** * (commandLine) ? ?? ? ? ? * * @param commandLine * @param argument ? * @return int ? */ public static int execute(CommandLine commandLine, String argument) { return execute(commandLine, argument, DEFAULT_TIMEOUT); } /** * (commandLine)? ? ? ? * * @param commandLine * @param timeout * @return int ? */ public static int execute(CommandLine commandLine, long timeout) { return execute(commandLine, null, timeout); } /** * (commandLine) ? ?? ? ? ? * * @param commandLine * @param argument ? * @param timeout * * @return int ? */ public static int execute(CommandLine commandLine, String argument, long timeout) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); PumpStreamHandler streamHandler = new PumpStreamHandler(baos); DefaultExecutor executor = new DefaultExecutor(); executor.setStreamHandler(streamHandler); // ? if (StringUtils.isNotEmpty(argument)) { commandLine.addArguments(argument); } // if (timeout > 0) { ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout); executor.setWatchdog(watchdog); } // ?? ? executor.setExitValue(SUCCESS_RETURN_CODE); try { DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); executor.execute(commandLine, resultHandler); resultHandler.waitFor(); return resultHandler.getExitValue(); } catch (Exception e) { throw new CmdUtilsException(e.getMessage()); } } }