HADOOP-12474. MiniKMS should use random ports for Jetty server by default. Contributed by Mingliang Liu.

This commit is contained in:
Haohui Mai 2015-10-13 13:07:57 -07:00
parent 6716f15964
commit 7f00fcac77
2 changed files with 6 additions and 9 deletions

View File

@ -1283,6 +1283,9 @@ Release 2.8.0 - UNRELEASED
HADOOP-12441. Fixed shell-kill command behaviour to work correctly on some HADOOP-12441. Fixed shell-kill command behaviour to work correctly on some
Linux distributions after HADOOP-12317. (Wangda Tan via vinodkv) Linux distributions after HADOOP-12317. (Wangda Tan via vinodkv)
HADOOP-12474. MiniKMS should use random ports for Jetty server by default.
(Mingliang Liu via wheat9)
Release 2.7.2 - UNRELEASED Release 2.7.2 - UNRELEASED
INCOMPATIBLE CHANGES INCOMPATIBLE CHANGES

View File

@ -47,19 +47,13 @@ public class MiniKMS {
private static Server createJettyServer(String keyStore, String password, int inPort) { private static Server createJettyServer(String keyStore, String password, int inPort) {
try { try {
boolean ssl = keyStore != null; boolean ssl = keyStore != null;
InetAddress localhost = InetAddress.getByName("localhost");
String host = "localhost"; String host = "localhost";
ServerSocket ss = new ServerSocket((inPort < 0) ? 0 : inPort, 50, localhost); Server server = new Server(inPort);
int port = ss.getLocalPort();
ss.close();
Server server = new Server(0);
if (!ssl) { if (!ssl) {
server.getConnectors()[0].setHost(host); server.getConnectors()[0].setHost(host);
server.getConnectors()[0].setPort(port);
} else { } else {
SslSocketConnector c = new SslSocketConnectorSecure(); SslSocketConnector c = new SslSocketConnectorSecure();
c.setHost(host); c.setHost(host);
c.setPort(port);
c.setNeedClientAuth(false); c.setNeedClientAuth(false);
c.setKeystore(keyStore); c.setKeystore(keyStore);
c.setKeystoreType("jks"); c.setKeystoreType("jks");
@ -80,7 +74,7 @@ public class MiniKMS {
String scheme = (ssl) ? "https" : "http"; String scheme = (ssl) ? "https" : "http";
return new URL(scheme + "://" + return new URL(scheme + "://" +
server.getConnectors()[0].getHost() + ":" + server.getConnectors()[0].getHost() + ":" +
server.getConnectors()[0].getPort()); server.getConnectors()[0].getLocalPort());
} catch (MalformedURLException ex) { } catch (MalformedURLException ex) {
throw new RuntimeException("It should never happen, " + ex.getMessage(), throw new RuntimeException("It should never happen, " + ex.getMessage(),
ex); ex);
@ -92,7 +86,7 @@ public class MiniKMS {
private String log4jConfFile; private String log4jConfFile;
private File keyStoreFile; private File keyStoreFile;
private String keyStorePassword; private String keyStorePassword;
private int inPort = -1; private int inPort;
public Builder() { public Builder() {
kmsConfDir = new File("target/test-classes").getAbsoluteFile(); kmsConfDir = new File("target/test-classes").getAbsoluteFile();