Java tutorial
/* Copyright (C) 2006 NTT DATA Corporation 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. 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. */ package com.clustercontrol.ping.util; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashSet; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.clustercontrol.platform.ping.FpingCommand; /** * ??????????? * * @version 2.0.0 * @since 2.0.0 */ public class ReachAddressFping { private static Log m_log = LogFactory.getLog(ReachAddressFping.class); /** ? */ private int m_sentCount = PingProperties.getFpingCount(); /** ? */ private int m_sentInterval = PingProperties.getFpingInterval(); /** */ private int m_timeout = PingProperties.getFpingTimeout(); /** ?(byte)*/ private int m_bytes = PingProperties.getFpingBytes(); /**??**/ private ArrayList<String> m_errMsg; /**??**/ private ArrayList<String> m_resultMsg; /** * */ public ReachAddressFping(int sentNum, int sentInterval, int timeout) { m_sentCount = sentNum; m_sentInterval = sentInterval; // sec to msec m_timeout = timeout; PingProperties.getProperties(); } /** * ???????????? * * @param info * @return PING */ public boolean isReachable(HashSet<String> hosts, int version) { Process process = null;//fping int m_exitValue = 0; //fping? String fpingPath; if (version == 6) { fpingPath = PingProperties.getFping6Path(); } else { fpingPath = PingProperties.getFpingPath(); } String cmd[] = FpingCommand.getCommand(fpingPath, hosts, m_sentCount, m_sentInterval, m_timeout, m_bytes); try { process = Runtime.getRuntime().exec(cmd); if (process != null) { //??? StreamReader errStreamReader = new StreamReader(process.getErrorStream()); errStreamReader.start(); StreamReader inStreamReader = new StreamReader(process.getInputStream()); inStreamReader.start(); // m_exitValue = process.waitFor(); //? inStreamReader.join(); m_resultMsg = inStreamReader.getResult(); m_log.debug("isReachable() STDOUT :" + inStreamReader.getResult().toString()); errStreamReader.join(); m_errMsg = errStreamReader.getResult(); m_log.debug("isReachable() STDERR :" + errStreamReader.getResult().toString()); } } catch (IOException e) { m_errMsg = new ArrayList<String>(); m_errMsg.add(e.getMessage()); m_log.warn("isReachable() : " + e.getClass().getSimpleName() + ", " + e.getMessage(), e); return false; } catch (InterruptedException e) { m_errMsg = new ArrayList<String>(); m_errMsg.add(e.getMessage()); m_log.warn("isReachable() : " + e.getClass().getSimpleName() + ", " + e.getMessage(), e); return false; } finally { if (process != null) { process.destroy(); } } if (m_exitValue == 0 || m_exitValue == 1) { m_log.debug("exit value = " + m_exitValue); //fping??0???? //fping??unreachable??1??????1??? return true; } else { String errMsg = "fping exit value is not 0 or 1. exit value = " + m_exitValue; m_log.info(errMsg); for (int j = 0; j < cmd.length; j++) { m_log.info("cmd[" + j + "] = " + cmd[j]); } m_errMsg = new ArrayList<String>(); m_errMsg.add(errMsg); //fping??!0 or !1??? return false; } } /** * ???? * ??????????? */ static class StreamReader extends Thread { private BufferedReader m_br; private ArrayList<String> m_ret; private InputStream m_ist; /** * * @param ist */ public StreamReader(InputStream ist) { super(); m_br = new BufferedReader(new InputStreamReader(ist)); m_ist = ist; m_ret = new ArrayList<String>(); } @Override public void run() { try { while (true) { String outputString = m_br.readLine(); if (outputString != null) { m_ret.add(outputString); } else { break; } } } catch (IOException e) { m_log.warn("run() : " + e.getClass().getSimpleName() + ", " + e.getMessage(), e); } try { /** * ? */ m_ist.close(); } catch (IOException e) { m_log.warn("isReachable() : " + e.getClass().getSimpleName() + ", " + e.getMessage(), e); } } /** * ????ArrayList * @return ????String??ArrayList */ public ArrayList<String> getResult() { return m_ret; } } public ArrayList<String> getM_errMsg() { return m_errMsg; } public ArrayList<String> getM_resultMsg() { return m_resultMsg; } }