/*
 * jPOS Project [http://jpos.org]
 * Copyright (C) 2000-2020 jPOS Software SRL
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.jpos.ee.status;

import java.net.Socket;
import java.io.IOException;
import org.jpos.core.Configurable;
import org.jpos.core.Configuration;
import org.jpos.core.ConfigurationException;
import org.jpos.util.Log;

public class Ping extends Log implements MonitorTask, Configurable {
    String host;
    int port = 7;

    // code taken from Alireza's blog
    // http://jroller.com/page/alireza/20050601
    public String checkService () {
        boolean rc = false;
        String detail = "";
        long start = System.currentTimeMillis();
        try {
            Socket socket = new Socket(host, port);
            socket.setSoLinger (true, 0);
            socket.close();
            rc = true;
        } catch (IOException e) {
            String msg = e.getMessage().toUpperCase();
            if (msg.indexOf ("CONNECTION REFUSED") >= 0) {
                rc = true;
            }
            else {
                rc = false;
                detail = " " + msg;
            }
        }
        long elapsed = System.currentTimeMillis() - start;
        return (rc ? Status.OK : Status.WARN) + detail 
            + " time=" + elapsed + "ms";
    }
    public void setConfiguration (Configuration cfg) 
        throws ConfigurationException
    {
        host = cfg.get ("host", null);
        if (host == null)
            throw new ConfigurationException (
                "host property has not been specified"
            );
        port = cfg.getInt ("port", 7);
    }
}


@@ -779,16 +779,50 @@
 tatus;%0A%0A
+import java.net.ConnectException;%0A
 import j
@@ -1516,18 +1516,23 @@
  catch (
-IO
+Connect
 Exceptio
@@ -1554,174 +1554,108 @@
     
-String msg = e.getMessage().toUpperCase();%0A            if (msg.indexOf (%22CONNECTION REFUSED%22) %3E= 0) %7B%0A                rc = true;%0A            %7D%0A            else %7B%0A    
+rc = true;%0A        %7D catch (IOException e) %7B%0A            String msg = e.getMessage().toUpperCase();%0A
     
@@ -1686,20 +1686,16 @@
         
-    
 detail =
@@ -1710,22 +1710,8 @@
 sg;%0A
-            %7D%0A
     

make `Ping` class internationalization friendly