Coverage Report - org.mule.util.NetworkUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
NetworkUtils
0%
0/18
0%
0/4
2
 
 1  
 /*
 2  
  * $Id: NetworkUtils.java 8501 2007-09-20 03:15:10Z aguenther $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.util;
 12  
 
 13  
 import java.net.Socket;
 14  
 import java.net.URL;
 15  
 
 16  
 import org.apache.commons.logging.Log;
 17  
 import org.apache.commons.logging.LogFactory;
 18  
 
 19  
 public final class NetworkUtils
 20  
 {
 21  0
     private static final Log logger = LogFactory.getLog(NetworkUtils.class);
 22  
     
 23  
     private NetworkUtils()
 24  0
     {
 25  
         // utility class only
 26  0
     }
 27  
     
 28  
     public static boolean isServerReachable(URL url, int timeout)
 29  
     {
 30  0
         int port = url.getPort() != -1 ? url.getPort() : url.getDefaultPort();
 31  0
         return isServerReachable(url.getHost(), port, timeout);
 32  
     }
 33  
     
 34  
     public static boolean isServerReachable(String host, int port, int timeout)
 35  
     {
 36  0
         boolean isServerReachable = false;
 37  0
         Socket socket = null;
 38  
         
 39  
         try
 40  
         {
 41  0
             socket = TimedSocket.createSocket(host, port, timeout);
 42  0
             isServerReachable = true;
 43  
         }
 44  0
         catch (Exception e)
 45  
         {
 46  0
             logger.debug("Server at " + host + ":" + port + " not reachable. " + e.getMessage());
 47  
             try
 48  
             {
 49  0
                 if (socket != null)
 50  
                 {
 51  0
                     socket.close();
 52  
                 }
 53  
             }
 54  0
             catch (Exception socketNotClosed)
 55  
             {
 56  0
                 logger.debug(socketNotClosed);
 57  0
             }
 58  0
         }
 59  
 
 60  0
         return isServerReachable;
 61  
     }
 62  
 }