Java調用bat sh腳本通用類

java調用bat、shell腳本通用類,工做記錄。java

package test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

import test.ProcessUtil.Result.Judege;

public class ProcessUtil
{

	private static final ExecutorService EXECUTORS = Executors.newFixedThreadPool(4);

	public static ProcessRunner buildProcessRunner()
	{
		return new ProcessRunner();
	}

	public static class ProcessRunner
	{
		private int maxErrorLineNumb = 100;

		private int maxInputLineNumb = 500;

		/**
		 * 調用核心的命令
		 * 
		 * [@param](https://my.oschina.net/u/2303379) cmd
		 *            命令
		 * [@param](https://my.oschina.net/u/2303379) cmdInputParas
		 *            執行命令須要輸入的參數,好比命令行登陸數據庫須要輸入密碼<br>
		 *            echo "password" | cmd
		 * [@return](https://my.oschina.net/u/556800) 返回值,包括錯誤回顯,正確回顯,整個腳本執行的返回碼
		 * 
		 */
		public Result runCMD(List<String> cmd, List<String> cmdInputParas)
		{
			Process process = null;
			BufferedWriter bw = null;
			try
			{
				ProcessBuilder processBuilder = new ProcessBuilder(cmd);
				process = processBuilder.start();
				bw = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
				for (String p : cmdInputParas)
				{
					bw.write(p);
					bw.newLine();
				}
				bw.flush();
				bw.close();
				Callable<List<String>> inputRunner = new Runner(maxErrorLineNumb, process.getInputStream());
				FutureTask<List<String>> inputTask = new FutureTask<List<String>>(inputRunner);
				EXECUTORS.execute(inputTask);
				Callable<List<String>> errorRunner = new Runner(maxInputLineNumb, process.getErrorStream());
				FutureTask<List<String>> errorTask = new FutureTask<List<String>>(errorRunner);
				EXECUTORS.execute(errorTask);
				List<String> inputResult = inputTask.get();
				List<String> errorResult = errorTask.get();
				int returnCode = process.waitFor();
				return new Result(inputResult, errorResult, returnCode);

			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
			catch (ExecutionException e)
			{
				e.printStackTrace();
			}
			finally
			{
				try
				{
					if (bw != null)
					{
						bw.close();
					}
				}
				catch (IOException e)
				{

				}
				if (process != null)
				{
					process.destroy();
				}

			}
			return new Result();
		}

		/**
		 * 調用核心的命令
		 * 
		 * [@param](https://my.oschina.net/u/2303379) cmd
		 *            命令
		 * 
		 * [@return](https://my.oschina.net/u/556800) 返回值,包括錯誤回顯,正確回顯,整個腳本執行的返回碼
		 */
		public Result runCMD(List<String> cmd)
		{
			return runCMD(cmd, Arrays.asList());
		}

	}

	/**
	 * 執行命令類
	 * 
	 */
	private static class Runner implements Callable<List<String>>
	{

		private int maxLineNumb;

		private InputStream inputStream;

		public Runner(int maxLineNumb, InputStream inputStream)
		{
			super();
			this.maxLineNumb = maxLineNumb;
			this.inputStream = inputStream;
		}

		@Override
		public List<String> call()
		{
			List<String> result = new ArrayList<String>();
			BufferedReader br = null;
			try
			{
				br = new BufferedReader(new InputStreamReader(inputStream));
				int i = 0;
				String line = null;
				while (null != (line = br.readLine()) && (i++ < maxLineNumb))
				{
					result.add(line);
				}
				return result;
			}
			catch (IOException e)
			{

				e.printStackTrace();
			}
			finally
			{
				try
				{
					if (br != null)
					{
						br.close();
					}
				}
				catch (IOException e)
				{

				}
			}
			return result;
		}

	}

	/**
	 * 結果
	 * 
	 */
	public static class Result
	{
		// 回顯
		private List<String> inputInfos;
		// 錯誤回顯
		private List<String> errorInfos;
		// 返回碼
		private int returnCode = -1;

		public Result(List<String> inputInfos, List<String> errorInfos, int returnCode)
		{
			this.inputInfos = inputInfos;
			this.errorInfos = errorInfos;
			this.returnCode = returnCode;
		}

		public Result()
		{

		}

		@Override
		public String toString()
		{
			return "Result [inputInfos=" + inputInfos + ", errorInfos=" + errorInfos + ", returnCode=" + returnCode + "]";
		}

		public boolean isSuccess(Judege j)
		{
			if (errorInfos == null || inputInfos == null || returnCode == -1)
			{
				return false;
			}
			return j.isSuccess(inputInfos, errorInfos, returnCode);
		}

		/**
		 * 判斷結果接口能夠通用化判斷,防止組內人胡編亂寫
		 */
		interface Judege
		{
			public boolean isSuccess(List<String> inputInfos, List<String> errorInfos, int returnCode);
		}
	}

	public static void main(String[] args)
	{
		ProcessRunner runner = ProcessUtil.buildProcessRunner();
		Result result = runner.runCMD(Arrays.asList("cmd", "/C", "ping 127.0.0.1 -n 1"));
		System.out.println(result);
		boolean isRight = result.isSuccess(new Judege()
		{
			@Override
			public boolean isSuccess(List<String> inputInfos, List<String> errorInfos, int returnCode)
			{
				return returnCode == 0 && errorInfos.size() == 0;
			}
		});
		System.out.println("result is " + isRight);
	}

}
相關文章
相關標籤/搜索