Author: ron.sigal(a)jboss.com
Date: 2009-07-28 16:36:37 -0400 (Tue, 28 Jul 2009)
New Revision: 5311
Modified:
remoting2/branches/2.2/src/main/org/jboss/remoting/transport/PortUtil.java
Log:
JBREM-1139: Supports configuration of range from which ports may be chosen.
Modified: remoting2/branches/2.2/src/main/org/jboss/remoting/transport/PortUtil.java
===================================================================
--- remoting2/branches/2.2/src/main/org/jboss/remoting/transport/PortUtil.java 2009-07-28
20:35:27 UTC (rev 5310)
+++ remoting2/branches/2.2/src/main/org/jboss/remoting/transport/PortUtil.java 2009-07-28
20:36:37 UTC (rev 5311)
@@ -25,7 +25,7 @@
import java.net.ServerSocket;
import java.net.InetAddress;
import java.io.IOException;
-import java.security.SecureRandom;
+import java.util.Map;
import java.util.Random;
import org.jboss.logging.Logger;
@@ -39,12 +39,18 @@
*/
public class PortUtil
{
+ public static final String MIN_PORT = "minPort";
+ public static final String MAX_PORT = "maxPort";
+
private static final Logger log = Logger.getLogger(PortUtil.class);
private static final int MIN_UNPRIVILEGED_PORT = 1024;
private static final int MAX_LEGAL_PORT = 65535;
private static int portCounter = 0;
private static int retryMax = 50;
+
+ private static int minPort = MIN_UNPRIVILEGED_PORT;
+ private static int maxPort = MAX_LEGAL_PORT;
static
{
@@ -134,20 +140,111 @@
private static synchronized int getNextPort()
{
- if (portCounter < MAX_LEGAL_PORT)
+ if (portCounter < maxPort)
return portCounter++;
- portCounter = MIN_UNPRIVILEGED_PORT;
- return MAX_LEGAL_PORT;
+ portCounter = minPort;
+ return maxPort;
}
public static int getRandomStartingPort()
{
- int range = MAX_LEGAL_PORT - MIN_UNPRIVILEGED_PORT + 1;
- int port = new Random(System.currentTimeMillis()).nextInt(range) +
MIN_UNPRIVILEGED_PORT;
+ int range = maxPort - minPort + 1;
+ int port = new Random(System.currentTimeMillis()).nextInt(range) + minPort;
return port;
}
+
+ public static synchronized int getMinPort()
+ {
+ return minPort;
+ }
+ public static synchronized void setMinPort(int minPort) throws IllegalStateException
+ {
+ if (minPort > PortUtil.maxPort)
+ {
+ String msg = "trying to set minPort to value greater than maxPort: " +
minPort + " > " + PortUtil.maxPort;
+ log.debug(msg);
+ throw new IllegalStateException(msg);
+ }
+ if (minPort < PortUtil.minPort)
+ {
+ log.debug("will not set minPort to " + minPort + ": minPort is
already " + PortUtil.minPort);
+ return;
+ }
+ log.debug("setting minPort to " + minPort);
+ PortUtil.minPort = minPort;
+ }
+
+ public static synchronized int getMaxPort()
+ {
+ return maxPort;
+ }
+
+ public static synchronized void setMaxPort(int maxPort)
+ {
+ if (maxPort < PortUtil.minPort)
+ {
+ String msg = "trying to set maxPort to value less than minPort: " +
maxPort + " < " + PortUtil.minPort;
+ log.debug(msg);
+ throw new IllegalStateException(msg);
+ }
+ if (maxPort > PortUtil.maxPort)
+ {
+ log.debug("will not set maxPort to " + maxPort + ": maxPort is
already " + PortUtil.maxPort);
+ return;
+ }
+ log.debug("setting maxPort to " + maxPort);
+ PortUtil.maxPort = maxPort;
+ }
+
+ public static synchronized void updateRange(Map config)
+ {
+ if (config != null)
+ {
+ int savedMinPort = getMinPort();
+ Object o = config.get(MIN_PORT);
+ if (o instanceof String)
+ {
+ try
+ {
+ setMinPort(Integer.parseInt((String) o));
+ }
+ catch (NumberFormatException e)
+ {
+ log.error("minPort parameter has invalid format: " + o);
+ }
+ }
+ else if (o != null)
+ {
+ log.error("minPort parameter must be a string in integer format: "
+ o);
+ }
+
+ int savedMaxPort = getMaxPort();
+ o = config.get(MAX_PORT);
+ if (o instanceof String)
+ {
+ try
+ {
+ setMaxPort(Integer.parseInt((String) o));
+ }
+ catch (NumberFormatException e)
+ {
+ log.error("maxPort parameter has invalid format: " + o);
+ }
+ }
+ else if (o != null)
+ {
+ log.error("maxPort parameter must be a string in integer format: "
+ o);
+ }
+
+ if (savedMinPort != getMinPort() || savedMaxPort != getMaxPort())
+ {
+ portCounter = getRandomStartingPort();
+ }
+ }
+ }
+
public static void main(String args[])
{
try
Show replies by date