View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.tck;
8   
9   import java.io.IOException;
10  import java.net.ServerSocket;
11  import java.util.ArrayList;
12  import java.util.List;
13  
14  import org.apache.commons.logging.Log;
15  import org.apache.commons.logging.LogFactory;
16  
17  /**
18   * Utility class to find available ports.
19   */
20  public class PortUtils
21  {
22      final static private int MIN_PORT = 5000;
23      final static private int MAX_PORT = 6000;
24      final static Log logger = LogFactory.getLog(PortUtils.class);
25      /**
26       * Find a given number of available ports
27       * 
28       * @param numberOfPorts The number of free ports to find
29       * @return an List with the number of requested ports
30       */
31      public static List<Integer> findFreePorts(int numberOfPorts)
32      {
33          List<Integer> freePorts = new ArrayList<Integer>();
34          for (int port = MIN_PORT; freePorts.size() != numberOfPorts && port < MAX_PORT; ++port)
35          {
36              if (isPortFree(port))
37              {
38                  freePorts.add(port);
39              }
40          }
41  
42          if (freePorts.size() != numberOfPorts)
43          {
44              logger.info("requested " + numberOfPorts + " open ports, but returning " + freePorts.size());
45          }
46          return freePorts;
47      }
48      
49      /**
50       * Iterate through the ports and log whether each is available
51       * @param failIfTaken If true, fails the current test if the port is not available
52       * @throws Exception 
53       */
54      public static void checkPorts(boolean failIfTaken, String prefix, List<Integer> ports) throws Exception
55      {
56          for (Integer port : ports)
57          {
58              if (isPortFree(port))
59              {
60                  logger.info(prefix + " port is free : " + port);
61              }
62              else
63              {
64                  logger.info(prefix + " port is not free : " + port);
65                  if (failIfTaken)
66                  {
67                      throw new Exception("port is not free : " + port);
68                  }
69              }
70          }
71      }
72  
73      /**
74       * Check and log is a given port is available
75       * 
76       * @param port the port number to check
77       * @return true if the port is available, false otherwise
78       */
79      public static boolean isPortFree(int port)
80      {
81          boolean portIsFree = true;
82          
83          ServerSocket server = null;
84          try
85          {
86              server = new ServerSocket(port);
87          }
88          catch (IOException e)
89          {
90              portIsFree = false;
91          }
92          finally
93          {
94              if (server != null)
95              {
96                  try
97                  {
98                      server.close();
99                  }
100                 catch (IOException e)
101                 {
102                     // ignore
103                 }
104             }
105         }        
106         return portIsFree;
107     }
108 }