Author: rmannibucau
Date: Tue Aug 21 15:48:47 2012
New Revision: 1375619
URL: http://svn.apache.org/viewvc?rev=1375619&view=rev
Log:
random ports should be kept in memory in tomee arquillian adapters to avoid to
assign twice the same (possible since we don't hold the socket)
Modified:
openejb/trunk/openejb/arquillian/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/TomEEContainer.java
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/NetworkUtil.java
Modified:
openejb/trunk/openejb/arquillian/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/TomEEContainer.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb/arquillian/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/TomEEContainer.java?rev=1375619&r1=1375618&r2=1375619&view=diff
==============================================================================
---
openejb/trunk/openejb/arquillian/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/TomEEContainer.java
(original)
+++
openejb/trunk/openejb/arquillian/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/TomEEContainer.java
Tue Aug 21 15:48:47 2012
@@ -43,7 +43,8 @@ import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
-import java.util.Arrays;
+import java.util.ArrayList;
+import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
@@ -100,21 +101,38 @@ public abstract class TomEEContainer<Con
}
}
}
+
//
// Set ports if they are unspecified
//
+ final Collection<Integer> randomPorts = new ArrayList<Integer>();
+ for (int i : configuration.portsAlreadySet()) { // ensure we don't use
already initialized port (fixed ones)
+ randomPorts.add(i);
+ }
for (Map.Entry<String, Object> entry : map.entrySet()) {
if (!entry.getKey().toLowerCase().endsWith("port")) continue;
try {
Object value = entry.getValue();
int port = new Integer(value + "");
if (port <= 0) {
- port = nextPort(configuration.getPortRange(),
configuration.portsAlreadySet());
+ int retry = 0;
+ do { // nextPort can in some case returns twice the same
port since it doesn't hold the port
+ if (retry++ == Integer.MAX_VALUE) { // really too
much, just some protection over infinite loop
+ break;
+ }
+
+ // ports already set != random port if some port are
forced
+ port = nextPort(configuration.getPortRange(),
randomPorts);
+ } while (randomPorts.contains(port));
+
entry.setValue(port);
+ randomPorts.add(port);
}
} catch (NumberFormatException mustNotBeAPortConfig) {
+ // no-op
}
}
+ randomPorts.clear();
// with multiple containers we don't want it so let the user eb able
to skip it
if (configuration.getExportConfAsSystemProperty()) {
@@ -138,25 +156,17 @@ public abstract class TomEEContainer<Con
}
}
- private int nextPort(final String portRange, int[] excluded) {
+ private int nextPort(final String portRange, final Collection<Integer>
excluded) {
if (portRange == null || portRange.isEmpty()) {
int retry = 10;
while (retry > 0) {
- boolean ok = true;
int port = NetworkUtil.getNextAvailablePort();
- if (excluded != null) {
- for (int exclude : excluded) {
- if (exclude == port) {
- ok = false;
- }
- }
- }
- if (ok) {
+ if (!excluded.contains(port)) {
return port;
}
retry--;
}
- throw new IllegalArgumentException("can't find a port available
excluding " + Arrays.asList(excluded));
+ throw new IllegalArgumentException("can't find a port available
excluding " + excluded);
}
if (!portRange.contains("-")) {
Modified:
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/NetworkUtil.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/NetworkUtil.java?rev=1375619&r1=1375618&r2=1375619&view=diff
==============================================================================
---
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/NetworkUtil.java
(original)
+++
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/NetworkUtil.java
Tue Aug 21 15:48:47 2012
@@ -18,6 +18,7 @@ package org.apache.openejb.util;
import java.io.IOException;
import java.net.ServerSocket;
+import java.util.Collection;
public final class NetworkUtil {
private NetworkUtil() {
@@ -41,7 +42,7 @@ public final class NetworkUtil {
return port;
}
- public static int getNextAvailablePort(int min, int max, int... excepted) {
+ public static int getNextAvailablePort(int min, int max,
Collection<Integer> excepted) {
int port = -1;
ServerSocket s = null;
for (int i = min; i <= max; i++) {
@@ -50,17 +51,7 @@ public final class NetworkUtil {
port = s.getLocalPort();
s.close();
- boolean forbidden = false;
- if (excepted != null) {
- for (int j = 0; j < excepted.length; j++) {
- if (port == excepted[j]) {
- forbidden = true;
- break;
- }
- }
- }
-
- if (!forbidden) {
+ if (excepted == null || !excepted.contains(port)) {
break;
}
} catch (IOException ioe) {